反射练习之越过泛型检查

public class ReflectTest {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ArrayList<Integer> arrayList = new ArrayList<>();

        Class<? extends ArrayList> aClass = arrayList.getClass();
        Method aClassMethod = aClass.getMethod("add", Object.class);
        aClassMethod.invoke(arrayList,"hello");
        aClassMethod.invoke(arrayList,"world");
        aClassMethod.invoke(arrayList,"java");
        System.out.println(arrayList);
    }
}

posted @ 2020-06-21 21:02  硬盘红了  阅读(139)  评论(0编辑  收藏  举报