amoss
amoss copied to clipboard
Implement 'instanceOf' as a parameter check
Implement the ability to check if a parameter is an instance of a given object.
For example, something along the lines of:
Amoss_Instance objectController = new Amoss_Instance( SomeClass.class );
objectController
.expects( 'thisMethod' )
.withParameterNamed( 'parameter1' ).instanceOf( AnotherClass.class )
OR
Amoss_Instance objectController = new Amoss_Instance( SomeClass.class );
objectController
.expects( 'thisMethod' )
.withParameterNamed( 'parameter1' ).instanceOf( Contact.class )
Can't see how this would work, since can't see how to check the instance of an object against a dynamic (variable driven) type.
For example, the following does not work:
Sobject con = new Contact();
Type conType = Contact.class;
System.debug( con instanceOf conType );
Have asked Stack for help, just in case:
- https://salesforce.stackexchange.com/questions/337824/can-you-check-the-instanceof-an-object-dynamically
Without this capability it doesn't seem possible.
Potentially, it could use a JSON hack to do it. I.E. check if the object can be serialised and deserialised via that type, but it feels fragile. E.g.
- https://salesforce.stackexchange.com/questions/151140/check-if-object-instance-is-of-type-x
I have a similar need and haven't got much past this kind of thing
public static string getType(Object o) {
if(o==null) {return '';} // we can't say much about null with our current techniques
if(o instanceof SObject) {return ((SObject)o).getSObjectType().getDescribe().getName()+''; }
if(o instanceof Boolean) {return 'Boolean';}
if(o instanceof Id) {return 'Id';}
if(o instanceof String) {return 'String';}
if(o instanceof Blob) {return 'Blob';}
if(o instanceof Date) {return 'Date';}
if(o instanceof Datetime) {return 'Datetime';}
if(o instanceof Time) {return 'Time';}
if(o instanceof Integer) {return 'Integer';}
if(o instanceof Long) {return 'Long';}
if(o instanceof Decimal) {return 'Decimal';}
if(o instanceof Double) {return 'Double';}
if(o instanceof List<object>) {return 'List';}
return 'Object'; // actually we can't detect maps and sets and maps
}