注解(Annotation)
JAVA的内置注解
元注解(Target,Retention,Document,Inherited)
import java.lang.annotation.*;
public class testanno {
@test(name = "xiaowang")
public void test(){
System.out.println();
}
}
//表示注解可以用在方法和类上
@Target(value = {ElementType.METHOD,ElementType.TYPE})
//表示注解的作用域,在源码时期、class时期、执行的时候
@Retention(value = RetentionPolicy.RUNTIME)
//是否加入JAVADOC
@Documented
//子类是否可以继承注解
@Inherited
@interface test{
//注解的参数: 参数类型 + 参数名 +()
String name();
int num() default 1;
}
反射(Reflection)
获取Class对象
package reflect;
//反射
//一个类被加载后,类的整个结构都会被封装在Class对象中
public class reflection {
public static void main(String[] args) throws ClassNotFoundException {
//方式一:通过反射获取类的Class对象,一个类在内存中只有一个Class对象
Class c1 = Class.forName("reflect.User");
System.out.println("c1 = " + c1.hashCode());
//方式二:通过实体对象获得Class对象
User user = new User();
Class c2 = user.getClass();
System.out.println("c2 = " + c2.hashCode());
//方式三:通过类名获得Class对象
Class c3 = User.class;
System.out.println("c3 = " + c3.hashCode());
}
}
@Data
class User{
private String name;
private int id;
private int age;
}
通过Class对象获取类的全部信息
package reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
//反射
//一个类被加载后,类的整个结构都会被封装在Class对象中
public class reflection {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
//通过反射获取类的Class对象,一个类在内存中只有一个Class对象
Class c1 = Class.forName("reflect.User");
System.out.println("c1 = " + c1.hashCode());
//获得包+类的名字
System.out.println("包+类的名字"+c1.getName());
//获得简单名字
System.out.println("类的名字"+c1.getSimpleName());
System.out.println("----------------------------");
//获得类的属性
Field[] fields = c1.getFields(); //只能获取public的属性
for (Field field : fields) {
System.out.print("类的public属性 = " + field);
}
fields = c1.getDeclaredFields(); //可以获取全部的属性
for (Field field : fields) {
System.out.println("类的全部属性 = " + field);
}
//获得指定属性
Field fields1 = c1.getDeclaredField("name");
System.out.println("类的指定获得的属性 = " + fields1);
System.out.println("----------------------------");
//获得方法
Method[] methods = c1.getMethods();
System.out.println("类以及父类的全部public方法 = ");
for (Method method : methods) {
System.out.println(method);
}
methods = c1.getDeclaredMethods();
System.out.println("类的全部方法 = ");
for (Method method : methods) {
System.out.println(method);
}
Method test = c1.getDeclaredMethod("test",null);
System.out.println("类的指定获得的方法 = " + test);
test = c1.getDeclaredMethod("setName", String.class);
System.out.println("类的指定获得的方法 = " + test);
System.out.println("----------------------------");
//获得构造器
Constructor[] constructors = c1.getConstructors();
System.out.println("获得全部public构造方法 = ");
for (Constructor constructor : constructors) {
System.out.println("constructor = " + constructor);
}
constructors = c1.getDeclaredConstructors();
System.out.println("获得类全部构造方法 = ");
for (Constructor constructor : constructors) {
System.out.println("constructor = " + constructor);
}
Constructor declaredConstructor = c1.getDeclaredConstructor();
System.out.println("获得无参的构造器 = " + declaredConstructor);
Constructor declaredConstructor1 = c1.getDeclaredConstructor(String.class, int.class, int.class);
System.out.println("获得有参的构造器 = " + declaredConstructor1);
}
}
@Data
@All.....
@NO...
class User{
private String name;
private int id;
private int age;
public int sss;
private void test(){
System.out.println("我是来测试的");
}
}
通过反射来创建对象,操作方法和属性
//获取Class对象
Class c1 = Class.forName("reflect.User");
System.out.println("c1 = " + c1.hashCode());
//通过反射构造对象
User u = (User)c1.newInstance(); //调用了类的无参构造器
System.out.println("u = " + u);
//通过反射获得构造器构造对象
Constructor declaredConstructor2 = c1.getDeclaredConstructor(String.class, int.class, int.class);
User u2 = (User) declaredConstructor2.newInstance("zhangsan", 1, 1); //调用了类的有参构造器
System.out.println("u2 = " + u2);
//通过反射调用方法
Method setName = c1.getDeclaredMethod("setName", String.class);
setName.invoke(u2,"lisi"); //invoke 激活 参数:(对象,参数)
System.out.println(u2.getName());
//通过反射操作属性
Field age = c1.getDeclaredField("age");
age.setAccessible(true); //打开权限,直接操作private属性
age.set(u2,66);
System.out.println(u2.getAge());
注:通过对象调用方法最快,反射调用最慢,关闭安全检查速度第二
反射获得泛型
反射获取注解
package reflect;
import java.lang.annotation.*;
//反射操作注解
public class ORM {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("reflect.Student");
//反射获得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println("annotation = " + annotation);
}
//获得注解的value值
Table table = (Table)c1.getAnnotation(Table.class);
String value = table.value();
System.out.println("value = " + value);
//获得类指定的注解
java.lang.reflect.Field name = c1.getDeclaredField("name");
Field annotation = name.getAnnotation(Field.class);
System.out.println(annotation.cloumnName());
System.out.println(annotation.type());
System.out.println(annotation.length());
}
}
@Table("db_student")
class Student{
@Field(cloumnName = "db_id",type = "int",length = 10)
private int id;
@Field(cloumnName = "db_age",type = "int",length = 10)
private int age;
@Field(cloumnName = "db_name",type = "varchar",length = 3)
private String name;
public Student() {
}
public Student(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 "Student{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Field{
String cloumnName();
String type();
int length();
}