Java注解和反射
可以被其它程序(编译器)读取
内置注解:
-
@Override
-
@Deprecated
-
@Suppresswarnings
元注解:
-
@Target:作用域(ElementType.METHOD ,ElementType.TYPE)
-
@Retention:有效作用范围(runtime > class > sources)
-
@Documented : 是否将注解生在在JAVAdoc中
-
@Inherited:子类可以继承父类的注解
自定义注解:
@interface 注解名 {定义内容}
public class Test { @MyAnnotation(name = "pxw",age = 18) public void test(){ } } @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation{ //注解的参数:参数类型+参数名() String name() default ""; int age(); }
反射
获取Class类的几种方式
public class Test01 { public static void main(String[] args) throws Exception { //通过反射获取类的class对象 Class c1 = Class.forName("com.pxw.annotation.User"); System.out.println(c1); //一个类在内存中只有一个Class对象 //一个类被加载后,类的整个结构都会被封装在Class对象中 //方式一 Person p = new Student(); Class c2 = p.getClass(); System.out.println(c2); System.out.println(c2.hashCode()); //方式二 Class c3 = Class.forName("com.pxw.annotation.Student"); System.out.println(c3.hashCode()); //方式三 Class<Student> c4 = Student.class; System.out.println(c4.hashCode()); //方式四:基本内置类型的包装类都有一个Type属性 Class c5 = Integer.TYPE; System.out.println(c5);//int //获得父类类型 Class c6 = c2.getSuperclass(); System.out.println(c6);//class com.pxw.annotation.Person } } class Person{ public String name; public Person() { } public Person(String name) { this.name = name; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + '}'; } } class Student extends Person{ public Student() { this.name = "学生"; } } class Teacher extends Person{ public Teacher() { this.name = "老师"; } } class User{ private String name; private int id; private int age; public User() { } public User(String name, int id, int age) { this.name = name; this.id = id; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", id=" + id + ", age=" + age + '}'; } }
加载、链接、初始化
//获取类的信息 public class Test02 { public static void main(String[] args) throws Exception{ Class c1 = Class.forName("com.pxw.annotation.User"); //获取类的名字 System.out.println(c1.getName()); System.out.println(c1.getSimpleName()); //获取类的属性 //Field[] fields1 = c1.getFields();//找到public属性 Field[] fields = c1.getDeclaredFields();//找到全部属性 for(Field field : fields){ System.out.println(field); } //获取指定属性的值 Field name = c1.getDeclaredField("name"); System.out.println(name); //获取类的方法 Method[] methods = c1.getMethods();//获取本类及其父类的所有public方法 Method[] declaredMethods = c1.getDeclaredMethods();//获取本类的所有方法,包括私有的 //获取指定方法 Method getName = c1.getMethod("getName", null); Method setName = c1.getMethod("setName", String.class); System.out.println(getName); System.out.println(setName); //获取指定的构造器 Constructor[] constructors = c1.getConstructors(); Constructor[] declaredConstructors = c1.getDeclaredConstructors(); //获取指定的构造器 Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class); System.out.println(declaredConstructor); } }
//反射操作注解 public class Test04 { public static void main(String[] args) throws Exception{ Class c1 = Class.forName("com.pxw.annotation.Employee"); //通过反射获取注解 Annotation[] annotations = c1.getAnnotations(); for(Annotation annotation: annotations){ System.out.println(annotation); } //获取注解value值 Tablep tablep = (Tablep)c1.getAnnotation(Tablep.class); System.out.println(tablep.value()); //获得类指定的注解 Field f = c1.getDeclaredField("id"); AnnotatedType annotatedType = f.getAnnotatedType(); System.out.println(annotatedType); Fieldp fieldp = f.getAnnotation(Fieldp.class); System.out.println(fieldp.column()); System.out.println(fieldp.type()); System.out.println(fieldp.length()); } } @Tablep("db_employee") class Employee{ @Fieldp(column = "db_id",type = "int",length = 10) int id; @Fieldp(column = "db_name",type = "varchar",length = 3) String name; public Employee() { } public Employee(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + '}'; } } //类名的注解 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface Tablep{ String value(); } //属性的注解 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface Fieldp{ String column(); String type(); int length(); }

浙公网安备 33010602011771号