0

Is it possible to pass a custom type into a static method? I have two custom types and I'd like to pass in a custom type as the method parameter and then identify which type was passed in and run the appropriate class methods based on the type that was passed into the method:

public static void parseJSONResponse(string json, Type obj) {
    if(obj.getName() == 'MyClass1') {
        Type t = Type.forName(obj.getName());
        MmClass1 = (MyClass1)t.newInstance();
        parser = MyClass1.parse(json);
        //...handle results
    }
    else if(obj.getName() == 'MyClass2') {
        Type t = Type.forName(obj.getName());
        MyClass2 parser = (MyClass2)t.newInstance();
        parser = MyClass2.parse(json);
        //...handle results
    }
}

public static void run() {
    String POSTResponse = makeCallout();
    JSONParser parser = JSON.createParser(POSTResponse);
    MyClass1 postParser = new MyClass1(parser);
    parseJSONResponse(POSTResponse, postParser);
}

error:

Method does not exist or incorrect signature: void parseJSONResponse(String, myClass1) from the type MyService

I'm limiting the code included in this post due to the amount of lines. If I cannot pass a custom type as a method parameter, is there another option?

4
  • I think a bit more information would be helpful. What is the overarching problem you're trying to solve here? When you put "Type" in the signature, did you literally mean an instance of the Type class, or is that just a placeholder for some indeterminate type? You might want to look into Method Overloading or Interfaces (and Program to an interface, not an implementation)
    – Derek F
    Dec 6, 2019 at 19:51
  • @DerekF It is just a placeholder for a custom type that I would pass in, not an instance of the Type class. If there is a particular link for reference, please let me know. The issue I'm currently hitting is it the method signature is not recognized, even though I'm passing in the "string" and a custom type to the method that I have defined.
    – Dman100
    Dec 6, 2019 at 20:03
  • Paraphrasing code and errors doesn't usually work so well. Please edit your question to include the actual signature of your method, the full text of the error message, and a snippet of code that actually calls this method (showing us the declaration/instantiation of the second parameter is going to be important).
    – Derek F
    Dec 6, 2019 at 20:07
  • @DerekF I updated the original post to include an example of what I was trying. Hopefully, this helps.
    – Dman100
    Dec 6, 2019 at 20:27

1 Answer 1

0

I think you are trying to use dynamic method dispatch. In this scenario, you don't have to check what's the type of instance created, you can call the parent's method without having to worry about which instance passed.

In order to achieve this, you have to create an interface and ensure all of the other parser classes implements it.

public interface IJSONParser{

  public Object parse(String json);
}


public class MyClass1 implements IJSONParser
{
    private JSONParser parser;

    public MyClass1(JSONParser parser){
        this.parser = parser;
    }

    public Object parse(String json){       
        return yourParsedObject;
    }

}

public class MyClass2 implements IJSONParser
{
    private JSONParser parser;

    public MyClass2(JSONParser parser){
        this.parser = parser;
    }
    public Object parse(String json){       
        return yourParsedObject;
    }


}

Once this setup is done you can use it as?

public static void parseJSONResponse(string json, IJSONParser obj) {
     parser = obj.parse(json);
     //...handle results
}

public static void run() {
    String POSTResponse = makeCallout();
    JSONParser parser = JSON.createParser(POSTResponse);
    MyClass1 postParser = new MyClass1(parser);
    parseJSONResponse(POSTResponse, postParser);
}
1
  • Thanks Pranay Jaiswal
    – Dman100
    Dec 8, 2019 at 0:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .