提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
实例化注释
/*nowjava.com - 时 代 Java 提 供*/ //package com.nowjava; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class Main { public static void main(String[] argv) throws Exception { Class annotationClass = String.class; System.out.println(instantiateAnnotation(annotationClass)); } public static Annotation instantiateAnnotation( final Class<? extends Annotation> annotationClass) { // source http://stackoverflow.com/questions/2786292/is-it-possible-to-instantiate-a-java-annotation-given-a-class-extends-annotatio //来自 时 代 Java - nowjava.com return (Annotation) Proxy.newProxyInstance( annotationClass.getClassLoader(), new Class[] { Annotation.class }, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) { if (method.getName().equals("hashCode")) { return annotationClass.getName().hashCode(); } else if (method.getName().equals("toString")) { return "[AnnotationObj:" + annotationClass.getName() + "]"; } else if (method.getName().equals("equals")) { if (args[0] == null) {