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