内置注解
元注解
自定义注解
使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口
分析:
- @interface用来声明一个注解,格式:public @ interface注解名{定义内容}
- 其中的每一个方法实际上是声明了一个配置参数
- 方法的名称就是参数的名称反射
java Reflection
反射得类方法:
public class Test03 {
public static void main(String[] args) throws Exception{
//通过反射获取类的class对象
Class c1 = Class.forName("com.dong.annotation.User");
System.out.println(c1);
//一个类在内存中只有一个class对象
//一个类被加载后,类的整个结构都会被封装在class对象中
}
}
class User{
private String name;
private int id;
private int age;
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 Test04 {
public static void main(String[] args) {
Person person=new Student();
System.out.println("这个人是"+person.name);
//方式一:通过对象获得
Class c1 = person.getClass();
System.out.println(c1);
System.out.println(c1.hashCode());
//方拾二:forName获得
try {
Class c2 = Class.forName("com.dong.annotation.Student");
System.out.println(c2);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//方式三:通过类名.class获得
Class c3 = Student.class;
System.out.println(c3);
//方式四:基本内置类型里的包装类有一个type属性
Class c4 = Integer.TYPE;
System.out.println(c4);
//方式五:获得父类类型
Class c5 = c1.getSuperclass();
System.out.println(c5);
}
}
class Person{
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() {
}
public Student(String name) {
this.name="赵东";
}
}
class Teacher extends Person{
public Teacher(String name) {
this.name=name;
}
}
JVM
public static void main(String[] args) throws Exception {
//通过反射获取类的class对象
Class c1 = Class.forName("com.dong.annotation.User");
//获取构造方法
Constructor[] constructors = c1.getConstructors();//非私有外所有构造方法
Constructor constructor = c1.getConstructor(String.class,Integer.class,Integer.class);//指定构造方法
Constructor[] declaredConstructors = c1.getDeclaredConstructors();//所有构造方法
//获取普通方法
Method[] methods = c1.getMethods();//非私有普通方法
Method[] declaredMethods = c1.getDeclaredMethods();//所有普通方法
Method declaredMethod = c1.getDeclaredMethod("study",String.class);//指定普通方法
//获取属性Field
Field[] fields = c1.getDeclaredFields();//全部属性
c1.getFields();//非私有属性
c1.getField("");//指定属性
c1.getField("").get("");//属性名 --属性类型String.class --获取属性值
}
}
//动态的创建对象,通过反射
public class Test08 {
public static void main(String[] args) throws Exception {
//获得class对象
Class c1 = Class.forName("com.dong.annotation.User");
//构造一个对象
User user=(User)c1.newInstance();//调用了类的无参构造器
//通过构造器创建对象
Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
User user1 =(User) declaredConstructor.newInstance("赵东", 001, 18);
//通过反射调用普通方法
Method setName = c1.getDeclaredMethod("setName", String.class);
setName.invoke(user,"赵东");//设置name参数
//invoke 激活的意思
//通过反射操作属性
User user2=(User)c1.newInstance();
Field name = c1.getDeclaredField("name");
name.setAccessible(true);//设置是否取消私有检测
name.set(user2,"赵东2");//设置name属性
}
}
反射操作泛型
通过反射获取注解信息
public class Test10 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("com.dong.annotation.Student2");
//通过反射获得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获得注解的value值
TableNote tableNote = (TableNote) c1.getAnnotation(TableNote.class);
System.out.println(tableNote.value());//获得并打印注解的值
//获得类指定的注解
Field name = c1.getDeclaredField("name");//要获得的类型
FieldNote annotation = name.getAnnotation(FieldNote.class);//获取值的方法
System.out.println(annotation.columnName()+annotation.type()+annotation.length());
}
}
@TableNote("db_student")
class Student2{
@FieldNote(columnName = "db_id",type = "int",length = 10)
private int id;
@FieldNote(columnName = "db_age",type = "int",length = 10)
private int age;
@FieldNote(columnName = "db_name",type = "String",length = 3)
private String name;
public Student2() {
}
public Student2(int id, int age, String name) {
this.id = id;
this.age = age;
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student2{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableNote{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldNote{
String columnName();
String type();
int length();
}