java 注解日志_Java开发日志-自定义注解

Java-反射

注意:

反射是通过对方法区中对象的信息,再重构一个一模一样的对象

无论是调用方法还是调用属性,都需要传递clazz.newInstance();的值

反射-调用方法

代码实现

public class Main {

public void method(){

System.out.println("需要调用的方法");

}

public static void main() {

Class clazz = Main.class;//clazzz中存储了所有类的信息

Main main = clazz.newInstance(); //获取从类信息提炼出来的新对象,与new 的对象一致

Method method = clazz.getMethod("method");//从对象池中提取信息

method.invoke(main);//调用方法,传递反射得到的对象main

}

}

反射-调用属性值

注意:调用私有属性需要用

clazz.getDeclaredField("string");

并且同时设置 filed.setAccessible(true);

代码

public class Main {

private String string = "test";//需要反射的目标字符串

public static void main() {

Class clazz = Main.class;//clazzz中存储了所有类的信息

Main main = clazz.newInstance(); //获取从类信息提炼出来的新对象,与new 的对象一致

Field filed = clazz.getDeclaredField("string");//一定要用getDeclaredField()

filed.setAccessible(true);//一定要设置

Object o = filed.get(main);

}

}

反射-获取所有属性

public class Main {

private String str1 = "123";

private final String str2 = "456";

public final String str3 = "123";

public static void main(String[] args) {

Class clazz = Main.class;//clazzz中存储了所有类的信息

Main main = clazz.newInstance(); //获取从类信息提炼出来的新对象,与new 的对象一致

Field[] fields = clazz.getDeclaredFields();//注意这里getDeclaredFields()

for (Field field : fields) {

field.setAccessible(true);

System.out.println(field.getName());

}

}

}

反射-获取所有方法名

public class Main {

public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {

Class clazz = Main.class;//clazzz中存储了所有类的信息

Main main = clazz.newInstance(); //获取从类信息提炼出来的新对象,与new 的对象一致

Method[] methods = clazz.getDeclaredMethods();

for (Method method : methods) {

method.setAccessible(true);

System.out.println(method.getName());//获取所有方法名

}

}

public void method(){}

}

自定义注解

作用:

在目标位置标注注解,获取用户的传值

获取注解位置的值

注意:

获取方法的注解需要用Method method = clazz.getAnnotation(Annotation.class);

获取类,属性等同理

获取字段注解

代码实现

public class Annotation {

@Test("annotation")

String text = "text";

public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {

Class clazz = Annotation.class;

Annotation annotation = clazz.newInstance();

Field field = clazz.getDeclaredField("text");

Test test = field.getAnnotation(Test.class);

String value = test.value();

System.out.println(value);

}

}

获取方法注解

public class Annotation {

public static void main(String[] args) {

Class clazz = Annotation.class;

Annotation annotation = clazz.newInstance();

Method method = clazz.getDeclaredMethod("method");

Test test = method.getAnnotation(Test.class);

boolean delete = test.isDelete();

System.out.println(delete);

}

@Test(value = "method", isDelete = true)

public void method() {}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值