@Inherited注解使用
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface ATable { public String name() default ""; }
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface BTable { public String name() default ""; }
@BTable public class Sub extends Super{ private int subx; public int suby; private Sub() { } public Sub(int i){ } private int subX(){ return 0; } public int subY(){ return 0; } }
@ATable public class Super { private int superx; public int supery; public Super() { } private int superX(){ return 0; } public int superY(){ return 0; } }
public class TestMain { public static void main(String[] args) { Class<Sub> clazz = Sub.class; System.out.println("============================Field==========================="); System.out.println(Arrays.toString(clazz.getFields())); System.out.println(Arrays.toString(clazz.getDeclaredFields())); //all + 自身 System.out.println("============================Method==========================="); System.out.println(Arrays.toString(clazz.getMethods())); //public + 继承 //all + 自身 System.out.println(Arrays.toString(clazz.getDeclaredMethods())); System.out.println("============================Constructor==========================="); System.out.println(Arrays.toString(clazz.getConstructors())); System.out.println(Arrays.toString(clazz.getDeclaredConstructors())); System.out.println("============================AnnotatedElement==========================="); //注解DBTable2是否存在于元素上 System.out.println(clazz.isAnnotationPresent(BTable.class)); //如果存在该元素的指定类型的注释DBTable2,则返回这些注释,否则返回 null。 System.out.println(clazz.getAnnotation(BTable.class)); //继承 System.out.println(Arrays.toString(clazz.getAnnotations())); System.out.println(Arrays.toString(clazz.getDeclaredAnnotations())); ////自身 } }
============================Field=========================== [public int com.plasticene.boot.redis.annotation.Sub.suby, public int com.plasticene.boot.redis.annotation.Super.supery] [private int com.plasticene.boot.redis.annotation.Sub.subx, public int com.plasticene.boot.redis.annotation.Sub.suby] ============================Method=========================== [public int com.plasticene.boot.redis.annotation.Sub.subY(), public int com.plasticene.boot.redis.annotation.Super.superY(), public final void java.lang.Object.wait() throws java.lang.InterruptedException, public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public boolean java.lang.Object.equals(java.lang.Object), public java.lang.String java.lang.Object.toString(), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll()] [private int com.plasticene.boot.redis.annotation.Sub.subX(), public int com.plasticene.boot.redis.annotation.Sub.subY()] ============================Constructor=========================== [public com.plasticene.boot.redis.annotation.Sub(int)] [private com.plasticene.boot.redis.annotation.Sub(), public com.plasticene.boot.redis.annotation.Sub(int)] ============================AnnotatedElement=========================== true @com.plasticene.boot.redis.annotation.BTable(name=) [@com.plasticene.boot.redis.annotation.ATable(name=), @com.plasticene.boot.redis.annotation.BTable(name=)] [@com.plasticene.boot.redis.annotation.BTable(name=)]