例子一
org.reflections
reflections
0.10.2
package com.chinaunicom.cnaps.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface pay {
int garid();
}
package com.chinaunicom.cnaps.test;
import org.reflections.Reflections;
import java.util.Set;
@pay(garid = -1)
public class TestMain {
static {
System.out.println("什么时候执行");
Reflections reflections = new Reflections("com.chinaunicom.cnaps.test");
Set> classSet = reflections.getTypesAnnotatedWith(pay.class);
for (Class clazzSet: classSet){
pay pay = (pay) clazzSet.getAnnotation(pay.class);
System.out.println(pay.garid());
//类名
System.out.println(clazzSet.getCanonicalName());
try {
Class cls = Class.forName(clazzSet.getCanonicalName());
TestMain testMain = (TestMain) cls.newInstance();
testMain.test();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
System.out.println("1`1111");
}
public void test(){
System.out.println("到此一游");
}
}
例子二
package com.shiro.test.inter;
import java.lang.annotation.*;
@documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TypeAnnotation {
String value() default "Is-TypeAnnotation";
String name() default "我不是田哥";
}
package com.shiro.test.inter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class TestMain {
public static void main(String[] args) throws Exception {
// Class cls = Class.forName("com.shiro.test.inter.Worker");
// Method[] method = cls.getMethods();
//
// boolean flag = cls.isAnnotationPresent(TypeAnnotation.class);
//
// if (flag) {
// TypeAnnotation typeAnno = (TypeAnnotation) cls.getAnnotation(TypeAnnotation.class);
// System.out.println("@TypeAnnotation值:" + typeAnno.value());
// }
//
//
// List list = new ArrayList();
// for (int i = 0; i < method.length; i++) {
// list.add(method[i]);
// }
//
// for (Method m : list) {
// MethodAnnotation methodAnno = m.getAnnotation(MethodAnnotation.class);
// if (methodAnno == null)
// continue;
// System.out.println( "方法名称:" + m.getName());
// System.out.println("方法上注解name = " + methodAnno.name());
// System.out.println("方法上注解url = " + methodAnno.url());
// }
//
// List fieldList = new ArrayList();
// for (Field f : cls.getDeclaredFields()) {// 访问所有字段
// FiledAnnotation filedAno = f.getAnnotation(FiledAnnotation.class);
// System.out.println( "属性名称:" + f.getName());
// System.out.println("属性注解值FiledAnnotation = " + filedAno.value());
// }
TypeAnnotation typeAnno = Worker.class.getAnnotation(TypeAnnotation.class);
System.out.println("@TypeAnnotation值:" + typeAnno.name());
FiledAnnotation filedAno = Worker.class.getDeclaredField("myfield").getAnnotation(FiledAnnotation.class);
System.out.println( "属性名称:" + Worker.class.getDeclaredField("myfield").getName());
System.out.println("属性注解值FiledAnnotation = " + filedAno.value());
MethodAnnotation methodAnno = Worker.class.getMethod("getDefineInfo").getAnnotation(MethodAnnotation.class);;
System.out.println( "方法名称:getDefineInfo");
System.out.println("方法上注解name = " + methodAnno.name());
System.out.println("方法上注解url = " + methodAnno.url());
}
}