/*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) {
return false;
}
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。