tomvda
tomvda

Reputation: 447

Detect object types in java issue

I'm in the process of creating an application but i've stumbled upon a problem.

I'm creating a program that will generate a .java file depending on the user input. In the program you'll be able to select custom api's (can't provide them to you though). Once you've selected an API call you'll have to specify the input for that method. You can also specify another API call as the input for the current parameter. I only want to show the api calls that provide the correct return value as an input for the selected api call. Here's the problem. I can detect the input type for the parameters of the selected api calls, but i can't seem to detect the type for the classValue parameter provided to listAPICallsWithReturnValue(...). the call.getMethod() function returns a java.lang.reflect.Method object.

I hope you all kinda understand what i mean... :)

public void displayParameterDialogs(APICall call) {
    JDialogMethodParameters dialog = new JDialogMethodParameters(mainframe, true);
    for (int i = 0; i < call.getMethod().getParameterTypes().length; i++) {
        dialog.init(i, call.getMethod().getParameterTypes()[i]);
        dialog.setVisible(true);
    }
}
//dialog class
public void init(int parameterIndex, Class parameterType) {

    this.jLabelInfo.setText("Data for input parameter: " + parameterIndex);

    DefaultComboBoxModel cmodel = new DefaultComboBoxModel();
    for (APICall call : TestFactory.getInstance().listAPICallsWithReturnValue(parameterType)) {
        cmodel.addElement(call);
    }

    this.jComboBox1.setModel(cmodel);
}
public APICall[] listAPICallsWithReturnValue(Class<?> classValue) {
    APICall[] calls;
    Vector<APICall> temp = new Vector<APICall>();
    Method[] methods = TestSuite.class.getMethods();

    for (Method method : methods) {
        System.out.println(method.getReturnType().getName());
        System.out.println(classValue.getClass().getName());
        System.out.println(classValue.toString());
        if (method.getReturnType().getCanonicalName().equals(classValue.toString())) {
            temp.add(new APICall(method));
        }
    }

    calls = new APICall[temp.size()];
    return temp.toArray(calls);

}

Upvotes: 0

Views: 143

Answers (3)

happymeal
happymeal

Reputation: 1413

try using getClass() in conjunction with isAssignableFrom()

example:

public class Main {

static class A {}
static class B extends A {}

public static void main(String[] args) {
    Object a = new A();
    Object b = new B();

    boolean aAssignableFromB = a.getClass().isAssignableFrom(b.getClass()); // true
    boolean bAssignableFromA = b.getClass().isAssignableFrom(a.getClass()); // false

    System.out.println(aAssignableFromB);
    System.out.println(bAssignableFromA);
}

}

Upvotes: 0

msj121
msj121

Reputation: 2842

I suggest simply printing out getCanonicalName() and you will see it won't match toString(). In fact, getName will match if not very close, maybe it wont add the word "Class" etc....

Perhaps use class.getName for both comparisons or double check what else is available to you....

Upvotes: 1

pap
pap

Reputation: 27614

perhaps:

classValue.getName()

classValue.getClass().getName() will return "Class" (since classValue is of type Class).

Upvotes: 2

Related Questions