如何获取作为通用接收的类的所有字段信息?
如何获取作为通用接收的类的所有字段信息?
我给Account.class作为GenericClass的构造函数,并执行t1.getClass().getDeclaredFields();
命令来获取该类的字段信息。
但结果,declaredFields的大小为0。
调试结果显示以下结果。
result = {Field[21]@10835}
0 = {Field@10837} "private static final int java.lang.Class.ANNOTATION"
1 = {Field@10838} "private static final int java.lang.Class.ENUM"
2 = {Field@10839} "private static final int java.lang.Class.SYNTHETIC"
3 = {Field@10840} "private transient volatile java.lang.reflect.Constructor java.lang.Class.cachedConstructor"
4 = {Field@10841} "private transient java.lang.String java.lang.Class.name"
5 = {Field@10842} "private transient java.lang.Module java.lang.Class.module"
6 = {Field@10843} "private transient java.lang.String java.lang.Class.packageName"
7 = {Field@10844} "private final java.lang.Class java.lang.Class.componentType"
8 = {Field@10845} "private static java.security.ProtectionDomain java.lang.Class.allPermDomain"
9 = {Field@10846} "private transient volatile java.lang.ref.SoftReference java.lang.Class.reflectionData"
10 = {Field@10847} "private transient volatile int java.lang.Class.classRedefinedCount"
11 = {Field@10848} "private transient volatile sun.reflect.generics.repository.ClassRepository java.lang.Class.genericInfo"
12 = {Field@10849} "private static final java.lang.Class[] java.lang.Class.EMPTY_CLASS_ARRAY"
13 = {Field@10850} "private static final long java.lang.Class.serialVersionUID"
14 = {Field@10851} "private static final java.io.ObjectStreamField[] java.lang.Class.serialPersistentFields"
15 = {Field@10852} "private static jdk.internal.reflect.ReflectionFactory java.lang.Class.reflectionFactory"
16 = {Field@10853} "private transient volatile java.lang.Object[] java.lang.Class.enumConstants"
17 = {Field@10854} "private transient volatile java.util.Map java.lang.Class.enumConstantDirectory"
18 = {Field@10855} "private transient volatile java.lang.Class$AnnotationData java.lang.Class.annotationData"
19 = {Field@10856} "private transient volatile sun.reflect.annotation.AnnotationType java.lang.Class.annotationType"
20 = {Field@10857} "transient java.lang.ClassValue$ClassValueMap java.lang.Class.classValueMap"
账户信息如下。
@Entity
@Getter
@Setter
public class Account{
@Id
@GeneratedValue
@Column(name = "account_id", nullable = false)
private Long id;
// User ID
@Column(unique = true, nullable = false)
private String userid;
// username
private String username;
// user password
private String password;
// User Permissions
@Enumerated(EnumType.STRING)
private AccountRole role;
测试代码如下。
@SpringBootTest
class PlxEntityToExcelTest {
@Autowired
AccountRepository accountRepository;
@Autowired
AccountService accountService;
@Test
void basic() {
System.out.println("accountRepository = " + accountRepository);
Iterable<Account> all = accountRepository.findAll();
for (Account account: all) {
System.out.println("account = " + account);
}
}
@Test
void anyClass() {
Account account = new Account();
account.setUserid("test");
account.setUsername("username");
account.setPassword("qwer");
account.setRole(AccountRole.USER);
accountService.createAccount(account);
Iterable<Account> all = accountRepository.findAll();
GenericClass<Account> cn = new GenericClass<Account>(Account.class);
cn.entityToExcel((List<Account>) all);
}
public class GenericClass<T>{
private T t1;
public GenericClass(Class<T> specificClass) {
this.t1 = (T) specificClass;
}
private void createHeader(){
Field[] declaredFields = t1.getClass().getDeclaredFields();
}
public void entityToExcel(List<T> list) {
System.out.println("list = " + list);
createHeader();
}
}
}
How to get all field information of class received as Generic?
I gave Account.class as the constructor of GenericClass, and executed the t1.getClass().getDeclaredFields();
command to get the field information of the class.
But as a result, the size of declaredFields was 0.
The debug result shows the following result.
result = {Field[21]@10835}
0 = {Field@10837} "private static final int java.lang.Class.ANNOTATION"
1 = {Field@10838} "private static final int java.lang.Class.ENUM"
2 = {Field@10839} "private static final int java.lang.Class.SYNTHETIC"
3 = {Field@10840} "private transient volatile java.lang.reflect.Constructor java.lang.Class.cachedConstructor"
4 = {Field@10841} "private transient java.lang.String java.lang.Class.name"
5 = {Field@10842} "private transient java.lang.Module java.lang.Class.module"
6 = {Field@10843} "private transient java.lang.String java.lang.Class.packageName"
7 = {Field@10844} "private final java.lang.Class java.lang.Class.componentType"
8 = {Field@10845} "private static java.security.ProtectionDomain java.lang.Class.allPermDomain"
9 = {Field@10846} "private transient volatile java.lang.ref.SoftReference java.lang.Class.reflectionData"
10 = {Field@10847} "private transient volatile int java.lang.Class.classRedefinedCount"
11 = {Field@10848} "private transient volatile sun.reflect.generics.repository.ClassRepository java.lang.Class.genericInfo"
12 = {Field@10849} "private static final java.lang.Class[] java.lang.Class.EMPTY_CLASS_ARRAY"
13 = {Field@10850} "private static final long java.lang.Class.serialVersionUID"
14 = {Field@10851} "private static final java.io.ObjectStreamField[] java.lang.Class.serialPersistentFields"
15 = {Field@10852} "private static jdk.internal.reflect.ReflectionFactory java.lang.Class.reflectionFactory"
16 = {Field@10853} "private transient volatile java.lang.Object[] java.lang.Class.enumConstants"
17 = {Field@10854} "private transient volatile java.util.Map java.lang.Class.enumConstantDirectory"
18 = {Field@10855} "private transient volatile java.lang.Class$AnnotationData java.lang.Class.annotationData"
19 = {Field@10856} "private transient volatile sun.reflect.annotation.AnnotationType java.lang.Class.annotationType"
20 = {Field@10857} "transient java.lang.ClassValue$ClassValueMap java.lang.Class.classValueMap"
Account information is as follows.
@Entity
@Getter
@Setter
public class Account{
@Id
@GeneratedValue
@Column(name = "account_id", nullable = false)
private Long id;
// User ID
@Column(unique = true, nullable = false)
private String userid;
// username
private String username;
// user password
private String password;
// User Permissions
@Enumerated(EnumType.STRING)
private AccountRole role;
The test code is as follows.
@SpringBootTest
class PlxEntityToExcelTest {
@Autowired
AccountRepository accountRepository;
@Autowired
AccountService accountService;
@Test
void basic() {
System.out.println("accountRepository = " + accountRepository);
Iterable<Account> all = accountRepository.findAll();
for (Account account: all) {
System.out.println("account = " + account);
}
}
@Test
void anyClass() {
Account account = new Account();
account.setUserid("test");
account.setUsername("username");
account.setPassword("qwer");
account.setRole(AccountRole.USER);
accountService.createAccount(account);
Iterable<Account> all = accountRepository.findAll();
GenericClass<Account> cn = new GenericClass<Account>(Account.class);
cn.entityToExcel((List<Account>) all);
}
public class GenericClass<T>{
private T t1;
public GenericClass(Class<T> specificClass) {
this.t1 = (T) specificClass;
}
private void createHeader(){
Field[] declaredFields = t1.getClass().getDeclaredFields();
}
public void entityToExcel(List<T> list) {
System.out.println("list = " + list);
createHeader();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解你想要做什么,你正在尝试获取类 Account 的字段。在这种情况下,您不需要在 GenericClass 的构造函数中将 clazz 实例强制转换为 Account 实例,因此您只需执行
t1.getDeclaredFields();
以下是经过正确更改的代码:泛型类
if I understood correctly what you are trying to do, you are trying to get the fields of the class Account. In that case you do not need to cast the clazz instance to an Account instance in the constructor of GenericClass and therefore you only need to do
t1.getDeclaredFields();
Here is the code with the right changes for GenericClass