How do I present a variable class in APEX?

I would like to know the variable / property class at runtime. For instance:

Integer i = 5; //pseudo-code if (i.className == 'Integer') { System.debug('This is an integer.'); } else { System.debug('This is not an integer, but a ' + i.className); } 

I cannot find a method / property that returns the type of a class in the documentation (assuming it is there). Did I miss it?

+4
source share
7 answers

From page 122 of the Apex Developer's Guide :

If at runtime you need to check if the object is really an instance of a particular class, use the instanceof keyword ...

But you cannot use the instanceof keyword in an instance of the class of its subclasses, otherwise you will get a compilation error. For instance:

 Integer i = 0; System.debug(i instanceof Integer); >> COMPILE ERROR: Operation instanceof is always true since an instance of Integer is always an instance of Integer. 

You need to use the instanceof keyword only for superclasses. For instance:

 System.debug((Object)i instanceof Integer); >> true 

If you need information about the type of the class itself, check out the System.Type methods ( pg 396 of the current Apex developer guide . Here are a few examples:

 Type integerType; integerType = Type.forName('Integer'); integerType = Integer.class; 
+9
source

Someone asked me about it today. Here is a more dynamic solution.

 MyClass mine = new MyClass(); String myStr = String.valueOf(mine); String className = myStr.split(':')[0]; System.assertEquals('MyClass', className); 
+8
source

General Salesforce Apex code for explanation:

 public class Animal {} public class Bird extends Animal {} Animal a = new Bird(); // Success System.debug(a); // Bird:[] System.debug((Bird)a); // Bird:[] System.debug((Animal)a); // Bird:[] System.debug(a instanceof Bird); // true System.debug((Animal)a instanceof Bird); // true System.debug((Bird)a instanceof Bird); // Operation instanceof is always true since an instance of Bird is always an instance of Bird System.debug((Bird)a instanceof Animal); // Operation instanceof is always true since an instance of Bird is always an instance of Animal 
+2
source

Just add more info on toString() and String.valueOf() .

String.valueOf() cannot be relied upon to get the name of the Apex class at runtime. I don't think this is documented, but if you add the toString() method to your custom class with an override keyword, then String.valueOf() will use this to determine the string value of your object.

 class MyClassDefaultToString { } class MyClassOverrideToString { public override String toString() { return 'override toString'; } } MyClassDefaultToString c1 = new MyClassDefaultToString(); System.assertEquals('MyClassDefaultToString:[]', String.valueOf(c1)); MyClassOverrideToString c2 = new MyClassOverrideToString(); System.assertNotEquals('MyClassOverrideToString:[]', String.valueOf(c2)); System.assertEquals('override toString', String.valueOf(c2)); 
+2
source

Not perfect, but here is a way to do it:

 public String getClassType(Object o) { String retVal = 'Unknown'; if(o instanceof Klass1) retVal = 'Klass1'; else if(o instanceof Klass2) retVal = 'Klass2'; else if(o instanceof Klass3) retVal = 'Klass3'; return retVal; } 
0
source

It works for me

 Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(JSON.serialize(myObjectInstance)); Map<String, Object> mComplexProperty = (Map<String, Object>)m.get('complexProperty'); String mSimpleProperty = (String)m.get('simpleProperty'); 

I'm not sure if JSON serialization and deserialization will be overhead, but ... while it works

0
source

What if you need FQCN instead of a class name (for example, what do you get from Java getName() )? Here is what you can do.

 public interface Animal {} public virtual class Bird implements Animal {} public class Robin extends Bird {} System.assertEquals( Robin.class, Type.forName(ObjUtil.getName(new Robin())) ); System.assertEquals( Robin.class, Type.forName(ObjUtil.getName((Animal)new Robin()))) ); System.assertEquals( Robin.class, Type.forName(ObjUtil.getName((Bird)new Robin()))) ); public class ObjUtil { public static String getName(Object obj, Boolean isQualifiedName) { // Qualified name if (isQualifiedName) { try { if (0 == (Long) obj) { } } catch (Exception ex) { typeName = ex.getMessage().remove('Invalid conversion from runtime type ').remove(' to Long'); System.debug('typeName = ' + typeName); return typeName; } } else { // Class name typeName = String.valueOf(obj); if (typeName.indexOf(':[') != -1) { typeName = typeName.split(':')[0]; System.debug('typeName = ' + typeName); return typeName; } } } } 
0
source

Source: https://habr.com/ru/post/1399556/


All Articles