如何在n enum类型作为参数时调用方法

时间:2014-08-26 07:01:17

标签: java reflection enums invoke

我想调用一个enum类型作为参数和反射的方法。 怎么做。

代码:

@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());

}

问题是什么应该是参数[1] =在FiltersEnum过滤器参数的情况下?

1 个答案:

答案 0 :(得分:0)

使用类似的东西:

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);
}

输出:

test - TEST