目录
Class类上有4个方法可以获取类的构造函数Constructor,如下表所示:
Class类上有4个方法可以获取类的字段Field,如下表所示:
一、注解
1.注解
注解:Annotation,Java标注,JDK5引入的一种机制。
Java中类,方法,变量,参数,包都可以被标注
元注解:专门给注解加的注解
2. 我们发现注解中可以有方法,
1、定义方法的格式:String name();
2、可以有默认值,也可以没有,如果没有默认值在使用的时候必须填写对应的值
如果需要有默认值,使用default指定默认值。
3、如果想在使用的时候不指定具体的名字,
如果不学习反射,注解没啥用!!!
3.在java的整个的注解体系中,有3个非常重要的主干类,
1、Annotation 接口,定义一些常用的方法
2、ElementType 枚举
它用来指定注解的类型。说人话,我的注解要用在哪里???
3、RetentionPolicy 枚举
它用来指定注解的策略。不同类型的策略指定的注解的作用域不同。
(1)SOURCE,注解仅存在于编译器处理期间,编译期处理完之后,这个注解就没用了
(2)CLASS,注解在.class文件中依然有效。
(3)RUNTIME,编译期是不起作用的,只有在运行期才由JVM读取。
Java自带的注解,10个。4个注解java.lang.annotation
6个注解在java.lang
4. 作用在代码上的注解
1、@Override,检查该方法是否是重写方法。如果返现其父类,或者是引用的接口中没有该方法,会报错
2、@Deprecated,标记的方法,过时的方法。
3、@SuppressWarnings编辑器去忽略警告
4、@SafeVarargs,JDK7支持忽略任何使用参数为泛型变量的方法或构造器产生的警告
5、@FunctionalInterface,JDK8开始支持,表示一个接口为函数式接口
6、@Repeatable,JDK8开始支持,标识某个注解可以在同一个声明上使用多次
all:忽略所有警告
boxing:忽略装箱、拆箱警告
rawtypes:使用生成时没有指定数据类型
unchecked:忽略没有进行类型检查操作的警告
unused:忽略没有使用的警告
5.元注解:
1、@Retention:标识这个注解作用域
2、@Documented:标记这个注解是否包含在用户文档中
3、@Target:这个注解可以修饰哪些信息
4、@Inherited:如果一个类用上了@Inherited注解,那么其子类也会继承这个注解
6.自定义 注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface MyAnnotation {
String name() default "tom";
int age();
int value();
}
二、反射
Class类
获取类对象
1.直接使用类名.class
Class<Dog> clazz = Dog.class;
对类对象操作
获取类的名字
System.out.println(clazz.getName());
获取类的加载器
ClassLoader classLoader = clazz.getClassLoader();
System.out.println(classLoader);
获取资源
URL resource = clazz.getResource("");
获取父类
System.out.println(clazz.getSuperclass());
判断一个类是不是接口,数组
System.out.println(clazz.isArray());
System.out.println(clazz.isInterface());
重点,使用class类对象实例化一个对象
@SuppressWarnings("all")
Dog dog = clazz.newInstance();
clazz.getDeclaredConstructor().newInstance();
Class类上有4个方法可以获取类的构造函数Constructor,如下表所示:
方法说明 | |
getConstructor | 根据参数列表获取该类上public的构造函数,如果没有找到则抛出NoSuchMethodException。 |
getDeclaredConstructor | 根据参数列表获取该类上该类上定义的构造函数,如果没有找到则抛出NoSuchMethodException。 |
getConstructors | 获取该类上定义的所有的public的构造函数。 |
getDeclaredConstructors | 获取该类上定义的所有的构造函数。 |
2.使用全类名
Class aClass = Class.forName("com.test02.Dog");
3.使用对象
Dog dog1 = new Dog();
Class aClass1 = dog1.getClass();
对成员变量的操作
Class<Dog> clazz = Dog.class;
只能获取到public的属性
Field type = clazz.getField("type");
System.out.println(type);
Field[] fields = clazz.getFields();
System.out.println(Arrays.toString(fields));
可以获取到private属性
Field name = clazz.getDeclaredField("name");
System.out.println(name);
System.out.println(name.getType());
Field[] declaredFields = clazz.getDeclaredFields();
System.out.println(Arrays.toString(declaredFields));
暴力注入 (不推荐)
color.setAccessible(true);
color.set(dog,"black");
Class类上有4个方法可以获取类的字段Field,如下表所示:
方法说明 | |
getField | 根据字段名称获取该类及其父类上定义的public字段,如果没有找到则抛出NoSuchFieldException。 |
getDeclaredField | 根据字段名称获取该类上定义的字段,如果没有找到则抛出NoSuchFieldException。 |
getFields | 获取该类及其父类上定义的所有的public的字段。 |
getDeclaredFields | 获取该类上定义的所有的字段。 |
案例:
获取对象的属性
public class Ch04 {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Dog dog = new Dog();
dog.setType("金毛");
Dog dog1 = new Dog();
dog1.setType("泰迪");
Class<Dog> clazz = Dog.class;
//获取属性
Field type = clazz.getDeclaredField("type");
// 想要获取哪一个对象的color
String str = (String) type.get(dog1);
System.out.println(str);
}
}
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Dog dog = new Dog();
dog.setType("萨摩耶");
Class clazz = Dog.class;
Field type = clazz.getDeclaredField("type");
type.set(dog,"拉布拉多");
System.out.println(dog.getType());
Field color = clazz.getDeclaredField("color");
// 暴力注入 (不推荐)
color.setAccessible(true);
color.set(dog,"black");
System.out.println(dog.getColor());
}
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException, InstantiationException {
Class dogClass = Class.forName("com.hh.Dog");
Dog dog = (Dog) dogClass.newInstance();
Class<Dog> clazz = Dog.class;
Method show = clazz.getMethod("show", String.class);
Class<?>[] parameterTypes = show.getParameterTypes();
// 执行方法,使用反射调用方法
show.invoke(dog,"hello");
Method[] methods = clazz.getMethods();
Method fun = clazz.getDeclaredMethod("fun");
fun.setAccessible(true);
fun.invoke(dog);
Method[] declaredMethods = clazz.getDeclaredMethods();
Method info = clazz.getDeclaredMethod("info");
String o = (String) info.invoke(dog);
System.out.println(o);
}
}
通过反射调用构造器
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<Dog> clazz = Dog.class;
Constructor<?>[] constructors = clazz.getConstructors();
Constructor<Dog> declaredConstructor = clazz.getDeclaredConstructor();
// 通过构造器创建对象
Dog dog = declaredConstructor.newInstance();
// 单例模式
// 1.构造器私有化
Constructor<Dog> declaredConstructor1 = clazz.getDeclaredConstructor(String.class);
declaredConstructor1.setAccessible(true);
Dog dog1 = declaredConstructor1.newInstance("小强");
System.out.println(dog1.getName());
}
}
案例:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface UserMapping {
String username();
String password();
}
public class UserController {
{
Class<UserController> clazz = UserController.class;
try {
Method addUser = clazz.getDeclaredMethod("addUser", User.class);
UserMapping annotation = addUser.getAnnotation(UserMapping.class);
String username = annotation.username();
String password = annotation.password();
User user = new User(username,password);
addUser.invoke(this,user);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@UserMapping(username = "admin",password = "123456")
public void addUser(User user){
System.out.println(user);
}
public static void main(String[] args) {
UserController userController = new UserController();
}
}
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}