IsInstanceOf()
Determines whether an object is an instance of a Lucee interface or component, or of a Java class.
IsInstanceOf( obj=any, type=string );
Returns: Boolean
| Argument | Description |
|---|---|
|
obj
any,
required
|
edit
The CFC instance or Java object that you are testing Alias: value |
|
type
string,
required
|
edit
The name of the interface, component, or Java class of which the object might be an instance. |
Usage Notes
editIsInstanceOf accepts three type-string forms for components.
For each component in the inheritance chain, the runtime checks the type string against three things — any match is enough:
- The bare class name — the last segment of the loading path, e.g.
Childfor a CFC atmypackage/SubFolder/Child.cfc. - The loading-context-relative path that was passed to
new, e.g.SubFolder.Childwhen instantiated from insidemypackage/. - The fully-qualified path from the web root, which is what
getMetaData( c ).namereturns, e.g.mypackage.SubFolder.Child.
Matches are case-insensitive. The check walks the extends chain and any interfaces declared via implements on the component or its ancestors.
var c = new mypackage.SubFolder.Child(); // extends ParentBase implements MyInterface
isInstanceOf( c, "Child" ); // true — bare class name
isInstanceOf( c, "mypackage.SubFolder.Child" ); // true — full path
isInstanceOf( c, getMetaData( c ).name ); // true — same as above
isInstanceOf( c, "ParentBase" ); // true — extends chain
isInstanceOf( c, "MyInterface" ); // true — implements (incl. from ancestors)
isInstanceOf( c, "Component" ); // true — every CFC matches
A path slice that's none of those three forms (e.g. SubFolder.Child when the loading path was different) returns false.
Java class checks accept the FQCN, plus a small shortlist of bare names.
For Java types, pass the fully-qualified Java class or interface name (e.g. java.util.Map, java.lang.String) — Lucee runs the standard instanceof check against the underlying Java object.
A few bare names also resolve, via a hardcoded shortlist in ClassUtil.checkPrimaryTypes: the Java primitives (boolean, int, long, float, double, byte, char, short), plus Object, String, Integer, Character, plus the CFML synonyms Numeric (= Double) and Null (= Object). Other bare Java names (Map, CharSequence, Comparable, etc.) do not resolve — use the FQCN.
Examples
editwriteDump(isInstanceOf({},"java.util.Map")); // true
writeDump(isInstanceOf("Lucee","java.util.Map")); // false
writeDump(isInstanceOf("Lucee","java.lang.String")); // true
See also
- Components (CFCs)
- Decision logic
- Java
- Data Types in Lucee
- Search Issue Tracker open_in_new
- Search Lucee Test Cases open_in_new (good for further, detailed examples)