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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章