关于使用反射机制得到泛型的字段名的问题

1、实体类

public class LSLSEntry {
	private String storeguid;
	private String checkguid;
	private String isstoprunning;
	public String _5801;
	public String _5802;
	public String _5803;
	public String _6001;
	public String _6002;
	public String _6003;
	public String _6101;
	public String _6201;
	public String _6301;
	public String _6401;
	public String _6501;
	public String _6502;
	public String _6601;
	public String _6602;
	public String _6701;
	public String _6702;
	public String _6703;
	public String _6704;
	public String _6801;
	public String _6802;
	public String _6803;
	public String _6804;
	public String _6805;
	public String _7201;
	public String _7401;
	public String _7402;
	public String _7403;
	public String _7404;
	public String _7601;
	public String _7701;
	public String _7702;
	public String _7703;
	public String _7704;
	public String _7705;
	public String _7706;
	public String _7707;
	public String _7708;
	public String _7709;
	public String _7801;
	public String _7802;
	public String _8001;
	public String _8101;
	public String _8102;
	public String _8103;
	public String _8104;
	public String _8105;
	public String _8106;
	public String _8107;
	public String _8108;
	public String _8109;
	public String _8110;
	public String _8111;
	public String _8112;
	public String _8201;
	public String _8301;
	public String _8401;
	public String _8402;
	public String _8403;
	public String _8404;
	public String getStoreguid() {
		return storeguid;
	}
	public void setStoreguid(String storeguid) {
		this.storeguid = storeguid;
	}
	public String getCheckguid() {
		return checkguid;
	}
	public void setCheckguid(String checkguid) {
		this.checkguid = checkguid;
	}
	public String getIsstoprunning() {
		return isstoprunning;
	}
	public void setIsstoprunning(String isstoprunning) {
		this.isstoprunning = isstoprunning;
	}
	
}

2、反射代码

import java.lang.reflect.Field;

import org.junit.Test;

public class TestReflect {
	@Test
	public void test() {
		//可以得到字段名
		LSLSEntry entry = new LSLSEntry();
		printValue(entry);
	}

	@Test
	public void test1() {
		//得不到字段名,为什么???
		printValue(LSLSEntry.class);
	}

	@Test
	public void test2() {
		//得不到字段名,为什么??
		PrintInfo2<LSLSEntry> info = new PrintInfo2<LSLSEntry>(LSLSEntry.class);
		info.printValue();
	}

	@Test
	public void test3() {
		//可以得到字段名
		LSLSEntry entry = new LSLSEntry();
		PrintInfo<LSLSEntry> info = new PrintInfo<LSLSEntry>(entry);
		info.printValue();
	}

	@Test
	public void test4() {
		//可以得到字段名
		Field[] fields = LSLSEntry.class.getFields();
		for (Field f : fields) {
			System.out.println(f.getName());
		}
	}

	private <T> void printValue(T entry) {
		Field[] fields = entry.getClass().getFields();
		for (Field f : fields) {
			System.out.println(f.getName());
		}
	}

	private class PrintInfo<T> {
		T entry;
		public PrintInfo(T entry) {
			this.entry = entry;
		}

		private void printValue() {
			Field[] fields = entry.getClass().getFields();
			for (Field f : fields) {
				System.out.println(f.getName());
			}
		}
	}
	
	private class PrintInfo2<T> {
		Class<?> entry;
		public PrintInfo2(Class<?> entry) {
			this.entry = entry;
		}

		private void printValue() {
			Field[] fields = entry.getClass().getFields();
			for (Field f : fields) {
				System.out.println(f.getName());
			}
		}
	}

}
在做项目的时候遇到这个问题,在反射泛型的字段时,只要将要反射的类实例化之后,反射其所对应的引用才能得到其字段,而直接反射类得不到。
网上查询了好长时间没有找到答案,贴出来,求答案……

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值