Grails(19)instanceOf in Grails

Grails(19)instanceOf in Grails
This function will only work in integration test case. This method is based on the GORM.

@Test
publicvoid testInstanceOf(){
     DatabaseObject databaseObject = new DatabaseObject()
     assertTrue !(databaseObjectinstanceof DatabaseObject2 )
     assertTrue "asdfasdf"instanceof String
     TenantUtils.doWithTenant(brand.idas Integer) {
          assertTrue !(event.instanceOf(DatabaseObject2.class))
     }
     assertTrue true
}

What is the advantage of instanceOf than instanceof.

class Container {
     static hasMany = [children: Child]
}

class Child {
     String name
     static belongsTo = [container: Container]
}

class Thing extends Child {}

class Other extends Child {}

def container = Container.get(id)
for (child in container.children) {
     if(child.instanceOf(Thing)){
          // thing
     }
     else if(child.instanceOf(Other)){
          // other
     }
}

It is good to learn that in groovy world.

References:
http://www.grails.org/doc/1.3.7/ref/Domain%20Classes/instanceOf.html


你可能感兴趣的:(instanceof)