"Fossies" - the Fresh Open Source Software Archive

Member "grails-core-5.2.4/grails-docs/src/test/resources/docs/ref/Domain Classes/instanceOf.html" (14 Sep 2022, 60480 Bytes) of package /linux/www/grails-core-5.2.4.tar.gz:


Caution: In this restricted "Fossies" environment the current HTML page may not be correctly presentated and may have some non-functional links. You can here alternatively try to browse the pure source code or just view or download the uninterpreted raw source code. If the rendering is insufficient you may try to find and view the page on the grails-core-5.2.4.tar.gz project site itself.

(Quick Reference)

instanceOf

Purpose

Determines if a domain class instance is an instance of the specified class, resolving the actual class if the instance is a proxy.

Examples

Given the domain classes:

class Container {
   static hasMany = [children: Child]
}
class Child {
   String name
   static belongsTo = [container: Container]
}
class Thing extends Child {}
class Other extends Child {}

Then you can determine the type of the elements in a Container’s `children collection using

def container = Container.get(id)
for (child in container.children) {
   if (child.instanceOf(Thing)) {
      // process Thing
   }
   else if (child.instanceOf(Other)) {
      // process Other
   }
   else {
      // handle unexpected type
   }
}

Description

Parameters:

  • clazz - the type to check