Java反射-操作Field、Method、Constructor
1,Field、Method、Constructor中的反射数据
1)Field类2,打印所有成员变量、方法、构造器
文章图片
image.png
2)Method类
文章图片
image.png
3)Constructor类
文章图片
image.png
4)TheAccessibleObject
class is the base class for Field, Method and
Constructor objects.
文章图片
image.png
public static void main(String[] args) {
Field[] fields = String.class.getDeclaredFields();
for(Field field : fields){
field.setAccessible(true);
System.out.println(Modifier.toString(field.getModifiers()) + " " + field.getType().getSimpleName() + " " + field.getName() + ";
");
}System.out.println("----------------------------");
Method[] methods = String.class.getDeclaredMethods();
for(Method method : methods){
method.setAccessible(true);
String temp = Modifier.toString(method.getModifiers()) + " " + method.getReturnType() + " "
+ method.getName() + "(";
for(Class clazz : method.getParameterTypes()){
temp += clazz.getSimpleName() + ",";
}
temp = temp.substring(0, (temp.lastIndexOf(",") == -1)?temp.length():temp.lastIndexOf(","));
temp += ")";
System.out.println(temp);
}System.out.println("----------------------------");
Constructor[] constructors = String.class.getDeclaredConstructors();
for(Constructor constructor : constructors){
constructor.setAccessible(true);
String temp = Modifier.toString(constructor.getModifiers()) + " " + constructor.getName() + "(";
for(Class clazz : constructor.getParameterTypes()){
temp += clazz.getSimpleName() + ", ";
}
temp = temp.substring(0, (temp.lastIndexOf(",") == -1)?temp.length():temp.lastIndexOf(","));
temp += ")";
System.out.println(temp);
}
}
3,Field、Method、Constructor上
【Java反射-操作Field、Method、Constructor】1)是否有指定注解(自定义注解的处理)
2)modifier判断
public static void main(String[] args) throws Exception {
Method method = Class.class.getDeclaredMethod("newInstance", null);
if(method.isAnnotationPresent(CallerSensitive.class)){
System.out.println(method.getDeclaringClass());
System.out.println(Modifier.isStatic(method.getModifiers()));
System.out.println(Modifier.isPublic(method.getModifiers()));
System.out.println(Modifier.isPrivate(method.getModifiers()));
}Field field = Class.class.getDeclaredField("name");
if(!field.isAnnotationPresent(CallerSensitive.class)){
System.out.println(field.getName());
System.out.println(Modifier.isStatic(field.getModifiers()));
System.out.println(Modifier.isPublic(field.getModifiers()));
System.out.println(Modifier.isPrivate(field.getModifiers()));
}Constructor constructor = Class.class.getDeclaredConstructor(ClassLoader.class);
if(!constructor.isAnnotationPresent(CallerSensitive.class)){
System.out.println(constructor.getDeclaringClass());
System.out.println(Modifier.isStatic(constructor.getModifiers()));
System.out.println(Modifier.isPublic(constructor.getModifiers()));
System.out.println(Modifier.isPrivate(constructor.getModifiers()));
}
}
推荐阅读
- 让 Bug 无处藏身,Java 线上问题排查思路常用工具
- 玩转Java8的 Stream 之Collectors收集器
- ES2018 最新 理解Javascript中的执行上下文和执行栈
- Java 中的对象池实现
- 反编译|手把手教你写JAVA反序列化的POC
- 开发者|torchvision 中 deform_conv2d 操作的经验性解析
- Java 8 新特性总结
- Java开发学习(十一)----基于注解开发bean作用范围与生命周期管理
- java|java IP归属地功能实现详解
- SpringCloud|SpringCloud 分布式微服务架构操作步骤