您的(主要)错误是
AW在
getMethod()参数中传递了不必要的泛型类型。我试图编写一个与您的代码相似但可以正常工作的简单代码。希望它可以某种方式回答您的问题:
import java.util.ArrayList;import java.lang.reflect.Method;public class ReflectionTest { public static void main(String[] args) { try { Method onLoaded = SomeClass.class.getMethod("someMethod", ArrayList.class ); Method onLoaded2 = SomeClass.class.getMethod("someMethod", new Class[]{ArrayList.class} ); SomeClass someClass = new SomeClass(); ArrayList<AW> list = new ArrayList<AW>(); list.add(new AW()); list.add(new AW()); onLoaded.invoke(someClass, list); // List size : 2 list.add(new AW()); onLoaded2.invoke(someClass, list); // List size : 3 } catch (Exception ex) { ex.printStackTrace(); } }}class AW{}class SomeClass{ public void someMethod(ArrayList<AW> list) { int size = (list != null) ? list.size() : 0; System.out.println("List size : " + size); }}


