5

How can I see if an instance of a class is of a particular type? See example code below:

IMyInterface m = new MyClass();
Type t = MyClass.class;
// how can I know if m is of type t?

Based on other threads I can see that it is not possible to get the type of an object in Apex, so I am not sure how to proceed here. My hope is to avoid string conversion as I am worried about namespace issues. For example this won't work if my org has a namespace:

// assertion fails
System.assertEquals(t.getName(), String.valueOf(m).split(':')[0]);

UPDATE

Here's the gist of what I want:

public static Boolean isInstance(Object instance, Type typeToCheck)
{
    // ???
}

As mentioned in the question I have the Type of the class, not the class itself. The pseudo code of m.getType() == t solves my problem (and obviously that's syntactically not correct since getType() is not a valid method) but I don't know the correct way of writing it.

5
  • Your update isn't "very clear", you're still checking against a concrete type. – Adrian Larson Dec 5 '16 at 21:47
  • Exactly. Hence "Check if object instance is of type X". The "m" in my example is the object instance and type X is the concrete type. I would love to make it more clear if you tell me what's unclear. Because I am ultimately seeking a solution here! Appreciate the response by the way. – Mossi Dec 5 '16 at 22:19
  • I am disappointed by how such valid question is being flagged for closure. If you don't understand it maybe you should just move right on? The Type that is confusing everyone is the System.Type class. I have even given a semi-working solution (using string conversion) which should demonstrate exactly what I am looking for. – Mossi Dec 6 '16 at 19:37
  • Instead of getting defensive, maybe take a look at what you have written and think about how you can make it more clear. I took a stab at what I think you're trying to ask, but I have no way of knowing for certain if that interpretation is correct. – Adrian Larson Dec 6 '16 at 19:44
  • I really don't understand how more clear I can be; I have a class instance, and I have a System.Type object, and I want to see if they are the same or one is an instance of the other. It seems I have to put my question in the exact wording sequence you need for it to work. But this is the art of problem solving; one person asks using words, while others do it using code or hand gestures or drawings (and I even used a combination of those). – Mossi Dec 6 '16 at 20:08
6

You can only check against concrete types. Dynamic type checking is not possible afaik. For concrete type checking, you are looking for the instanceof keyword:

public Interface IWidget { void doSomething(); }
public class Widget implements IWidget
{
    public void doSomething() { }
}
public class SpecificWidget extends Widget implements IWidget { }

Object s = new SpecificWidget();
system.assertEquals(true, s instanceof Widget);
system.assertEquals(true, s instanceof IWidget);
system.assertEquals(true, s instanceOf SpecificWidget);

Object w = new Widget();
system.assertEquals(true, w instanceOf IWidget);
system.assertEquals(true, w instanceOf Widget);
system.assertEquals(false, w instanceOf SpecificWidget);
8
  • As far as I know instanceof won't work, at least not directly because I only have the Type of the object (e.g. Widget.class). – Mossi Dec 4 '16 at 6:36
  • instanceOf would check for an instance to be an object of a Class, its super class or an instance of an interface. You seem to be after the simplest form.{IMyInterface m = new MyClass(); } System.assertEquals(true, m instanceOf IMyInterface); System.assertEquals(true, m instanceOf MyClass); -assuming MyClass implements the interface MyInterface. – SF Learner Dec 4 '16 at 22:13
  • @ShaliniMendu no that's not at all what I'm looking for. Again, I have the Type of the class (t = MyClass.class) and an instance of that class (m = new MyClass()), and I want to see if m is of type t. – Mossi Dec 5 '16 at 19:39
  • That's exactly what it does... system.debug(m instanceof Myclass). – Adrian Larson Dec 5 '16 at 19:40
  • 1
    You can't do a dynamic check. It's impossible. – Adrian Larson Dec 5 '16 at 21:46
1

I have a need for something like this, so I thought I'd take a stab at it. This feels pretty hacky, but what about using the JSON class?

public static Boolean isInstance(Object instance, Type typeToCheck) {
    try {
        JSON.deserialize(JSON.serialize(instance), typeToCheck);
    }
    catch (System.JSONException je) {
        return false;
    }
    return true;
}

Not a perfect solution since the JSON.deserialize method can fail for reasons other than a type mismatch.

Regarding getting the type of the child class, I'm just including an abstract method Type getType() for children to implement.

2
  • So far this seems like a promising approach! To make it definitive you can search the exception message for the particular string that pertains to a type mismatch, and only in that case return false. – Mossi May 17 '17 at 5:49
  • It looks like JSON.deserialize will just return an object with nulls assigned. What we want is an error, so JSON.deserializeStrict is the way to go. What's nice is that it gives you what fields were missing in the message. I wrote the following: public class AFoo { public Integer bar; public class ABar { Datetime d = Datetime.now(); } } then ran the following in Execute Anonymous System.debug(JSON.deserializeStrict(JSON.serialize(new AFoo.ABar()), AFoo.class)); resulting in a nice message System.JSONException: Unknown field: HubCtr.AFoo.d. – BMcD1990 May 22 '17 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.