0

I would like to invoke a method which has an enum type as parameter with reflection. How to do this.

Code:

@When("^I click on the (\\d+)st link inside the \"(.*?)\" filter$")
public void i_click_on_the_st_link_inside_the_filter(int index, FiltersEnum filter)

}

public void invokemethod() {
    Class<?>[] param = new Class[2];
    param[0] = Integer.TYPE;
    param[1] = 

    Object localObject = this.getSourceObject();
    Method method = localObject.getClass().getMethod("i_click_on_the_st_link_inside_the_filter",param);
    method.invoke(this.getSourceObject(), this.getValues());

}

The question is what should be the param[1] = in the case of FiltersEnum filter parameter?

user1511408
  • 445
  • 1
  • 4
  • 9

1 Answers1

0

Use something like this:

private enum TestEnum {
    TEST
}

private class TestClass {
    public void testMethod(String param1, TestEnum param2) {
        System.out.println(param1 + " - " + param2);
    }
}

private static TestClass createTestClassInstance() {
    //return new TestClass instance
}

public static void main(String[] args) throws Exception {
    Class<?>[] classes = new Class<?>[2];
    classes[0] = String.class;
    classes[1] = TestEnum.class;
    Method m = TestClass.class.getMethod("testMethod", classes);
    m.invoke(createTestClassInstance(), "test", TestEnum.TEST);
}

Output:

test - TEST
Arek Woźniak
  • 695
  • 3
  • 12
  • 25