5

How to get a type of the generic object? I have a method that gets as parameter some object. It can be realy everything. In my example it is a list of strings. But it can be a map or set or some other collection. I can't find a way to get a type of it. I've tried to use Type t = Type.forName(obj.class); but it doesn't work. A metadatas' getSObjectType() method can only be used with sObjects.

public void method1(){
    List<String> myList = new List<String>{'AA','BB'};
    method2(myList);
}

public void method2(Object obj){
    System.debug('object type: ' + obj ??? );
}

I realy want to avoid the endless brute force "if-else" like if(obj instanceOf ...) and wondering if there is an elegant way to do it?

PS: Can you point me to the official documentation about Object?

1
1

Not the general answer to your question, but for the case where the type of the argument is known at compile time (as in your method1) you can let Apex's support for method overloading do the dispatching to the right code:

@isTest
private class DispatchTest {

    @isTest
    static void test() {
        DispatchTest t = new DispatchTest();
        System.assertEquals('string', t.method('abc'));
        System.assertEquals('integer', t.method(123));
        System.assertEquals('string array', t.method(new String[] {'abc'}));
    }

    public String method(String o) {
        return 'string';
    }

    public String method(Integer o) {
        return 'integer';
    }

    public String method(String[] o) {
        return 'string array';
    }
}
1

My understanding is the Object class in APEX is similar to Object class in JAVA but none of the methods in JAVA object class is avaialble in APEX thou..

and to make your method generic, you can pass in a second parameter which takes in the Type..

so your method and the method call will be like this

public void method1(){
    List<String> myList = new List<String>{'AA','BB'};
    method2(myList,List<String>.class);
}

public void method2(Object obj,Type t){
    System.debug('object type: ' + t );
}

you can refer these 2 discussions as well

Is it possible to create a method that uses generics?

Can you use generics in Apex?

-2

Are you talking about General Apex Objects or rather SObjects? For SObjects, you can always look at the first 3 chars of an instance's Id. They will be the same for every object of a certain type (i.e. Account is always 001) - for standard objects this is across all orgs, for custom objects this is across your org and any sandbox.

This way you can just "switch" (though there's no such thing in apex) like this:

// make sure this object has an Id, i.e. don't use (new SObjectType__c()) to test ;-)
String prefix = <object>.Id.substring(0,3);
if(prefix == '001') {
    // This is an account
} else {
    //...
}
2
  • 3
    Welcome to SFSE, do try to read questions carefully. Your answer is specific to sObjects and pretty much what was explicitly not asked. – Samuel De Rycke Sep 8 '14 at 8:34
  • As @SamuelDeRycke mentioned it is not an sObject, it is an generic Object. – Sergej Utko Sep 8 '14 at 8:47

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.