[java]反射的应用-动态代理

被代理类为何要依托一个接口才能生存代理类?

  • 什么时候会生成反编译结果?

  • 如何查看反编译后结果?

System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");

如何查看 Proxy 模式的 $ProxyX.class文件

System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");

System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", true);//这个不对
  • 注解的本质是什么?

Java动态代理

@Target(value = {ElementType.FIELD, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Hello {
    String value();
}

public class Test {
    @Hello("hi")
    public static void main(String[] args) throws Exception {
        System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");
        //System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        Class<Test> cls = Test.class;
        Method method = cls.getMethod("main", String[].class);
        Hello hello = method.getAnnotation(Hello.class);
    }
}

posted @ 2020-08-27 09:12  mmaotai  阅读(235)  评论(0编辑  收藏  举报