注解和反射的demo
Posted hans774882968
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了注解和反射的demo相关的知识,希望对你有一定的参考价值。
showPrivateField、callPrivateMethod分别展示如何获取私有属性和调用私有方法。
showMethods展示如何得到一个类的所有成员方法。
showAnno展示如何得到一个类的所有注解。
checkClassesThatHasAnno展示如何获取具有某个特定注解的所有类。
代码
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Retention(RUNTIME)
@interface Best
String name();
int age();
@Retention(RUNTIME)
@interface Better
String name();
int age();
@Best(name = "best", age = 18)
@Better(name = "better", age = 22)
class A
private String name;
A()
this.name = "wsw";
A(String name)
this.name = name;
public String publicMethod()
return this.name + "说:就勉强让你调用人家一次吧!说好了就这一次哦!";
private String privateMethod(int v)
return this.name + "说:球球你别调用我啊!" + v;
@Best(age = 24, name = "B也是best的!")
class B
@Better(age = 26, name = "我只是better而已。")
class C
@Best(age = 28, name = "D也是best的!")
class D
public class RetentionDemo
private static void showPrivateField(Class<?> targetClass, A a1, A a2) throws
IllegalAccessException, NoSuchFieldException
Field nameField = targetClass.getDeclaredField("name");
nameField.setAccessible(true);
System.out.println(nameField.get(a1) + " , " + nameField.get(a2));
private static void callPrivateMethod(Class<?> targetClass, A a1, A a2) throws
IllegalAccessException, NoSuchMethodException, InvocationTargetException
Method privateMethod = targetClass.getDeclaredMethod("privateMethod", int.class);
privateMethod.setAccessible(true);
System.out.println(privateMethod.invoke(a1, 1) + " , " +
privateMethod.invoke(a2, 2));
private static void showMethods(Class targetClass)
Method[] methods = targetClass.getDeclaredMethods();
for (Method m : methods)
System.out.println(m.getName() + "的参数类型表:");
Class[] ptypes = m.getParameterTypes();
for (Class p : ptypes) System.out.println(p.getName());
private static void showAnno(Class targetClass)
System.out.println(targetClass.getName() + "的所有已定义注解:");
Annotation[] anns = targetClass.getDeclaredAnnotations();
for (Annotation ann : anns) System.out.println(ann);
private static void show(Class[] classes, Class wantedAnno)
List<Class> bestClasses = Arrays.stream(classes).filter(c ->
List<Annotation> anns = Arrays.asList(c.getDeclaredAnnotations());
return anns.stream().anyMatch(ann -> ann.annotationType() == wantedAnno);
).collect(Collectors.toList());
System.out.println("所有具有" + wantedAnno.getName() + "注解的类:");
for (Class c : bestClasses)
Annotation ann = c.getAnnotation(wantedAnno);
System.out.println(c.getName() + ": " + ann);
Class typ = c.getAnnotation(wantedAnno).annotationType();
if (typ == Best.class)
Best b = (Best) ann;
System.out.println(b.name() + " ; " + b.age());
if (typ == Better.class)
Better b = (Better) ann;
System.out.println(b.name() + " ; " + b.age());
private static void checkClassesThatHasAnno()
Class[] classes = A.class, B.class, C.class, D.class;
show(classes, Best.class);
show(classes, Better.class);
public static void main(String[] args) throws
ClassNotFoundException, InstantiationException, IllegalAccessException,
NoSuchMethodException, InvocationTargetException, NoSuchFieldException
A a1 = new A("hans");
Class targetClass = Class.forName("A");
A a2 = (A) targetClass.newInstance();
showPrivateField(targetClass, a1, a2);
callPrivateMethod(targetClass, a1, a2);
showMethods(targetClass);
showAnno(targetClass);
checkClassesThatHasAnno();
输出
hans , wsw
hans说:球球你别调用我啊!1 , wsw说:球球你别调用我啊!2
publicMethod的参数类型表:
privateMethod的参数类型表:
int
A的所有已定义注解:
@Best(name=best, age=18)
@Better(name=better, age=22)
所有具有Best注解的类:
A: @Best(name=best, age=18)
best ; 18
B: @Best(age=24, name=B也是best的!)
B也是best的! ; 24
D: @Best(age=28, name=D也是best的!)
D也是best的! ; 28
所有具有Better注解的类:
A: @Better(name=better, age=22)
better ; 22
C: @Better(age=26, name=我只是better而已。)
我只是better而已。 ; 26
以上是关于注解和反射的demo的主要内容,如果未能解决你的问题,请参考以下文章