Java中通过注解+反射拿到对象的属性和方法

本文深入介绍了Java反射机制的基本概念及其实现方式,并通过一个具体的示例演示了如何使用反射来获取类的属性和方法,包括如何读取注解以及如何调用类的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1什么是反射?

java通常是先有类再有对象,有对象我就可以调用方法或者属性。反射其实是通过Class对象来调用类里面的方法

下面就是测试代码,里面有详细的注释

package com.shandain.demo;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//添加在属性上面
@Target(ElementType.FIELD)
//运行时可见
@Retention(RetentionPolicy.RUNTIME)

public @interface NoteId {
	
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoteName {

}


 
package com.shandain.demo;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//声明在方法上
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NodeShow {

}
 
package com.shandain.demo;

public class Node {
	@NoteId
	private int id;
	@NoteName
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@NodeShow
	public void show(){
		System.out.println(name);
	}
	
}

 
package com.shandain.demo;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public class Texst {
	public static void main(String[] args) {

		Node node = new Node();
		node.setId(2);
		node.setName("哈哈");
		show(node);

	}

	public static void show(Node node) {
		
		Class c = Node.class;
		// 获取改类的所有属性
		Field[] fields = c.getDeclaredFields();
		for (Field field : fields) {
			int id = -1;
			Object name = "";
			// 在该属性上的注释
			if (field.getAnnotation(NoteId.class) != null) {
				// 设置这个时是可以访问私有权限的
				field.setAccessible(true);
				try {
					id = field.getInt(node);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(id);
			}
			if (field.getAnnotation(NoteName.class) != null) {
				field.setAccessible(true);
				try {
					name = field.get(node);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println((String) name);
			}

		}
		// 取出此类的方法
		Method[] methods = c.getDeclaredMethods();
		for (Method method : methods) {
			if (method.getAnnotation(NodeShow.class) != null) {
				method.setAccessible(true);// 增加访问权限级别
				String name = method.getName();//获取方法名
				try {
					//执行反射出来的方法,第一个参数的值是你要执行那个类,第二个参数是该方法中的参数object[]
					method.invoke(node, null);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值