在 Java 中获取 class 字段名称
Get the class field name in Java
public class Product{
private String id;
private String name;
private String description;
// Getters and setters..
}
从上面class,我想在运行时获取描述的字段名:
Query query = new Query();
if (productSearchCriteria.getDescription().isPresent()) {
query.addCriteria(Criteria.where("description").is(productSearchCriteria.getDescription().get()));
}
目前,我已经硬编码了名称,但它应该是动态的,因为如果有人重命名或更改 属性 名称,这将不起作用。
环顾四周,我发现了一个 reflection-util 图书馆。
String numberProperty = PropertyUtils.getPropertyName(Product.class, Product::getDescription);
这给了我一个例外:
Caused by: java.lang.ClassCastException: class domain.Product$ByteBuddy$QIcFk1aK cannot be cast to class domain.Product (domain.Product$ByteBuddy$QIcFk1aK is in unnamed module of loader net.bytebuddy.dynamic.loading.ByteArrayClassLoader @33943b71; domain.Product is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @5be4b0e6)
at de.cronn.reflection.util.PropertyUtils.findMethodByGetter(PropertyUtils.java:322) ~[reflection-util-2.6.0.jar:na]
at de.cronn.reflection.util.PropertyDescriptorCache.lambda$getMethod(PropertyDescriptorCache.java:178) ~[reflection-util-2.6.0.jar:na]
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) ~[na:na]
at de.cronn.reflection.util.PropertyDescriptorCache.getMethod(PropertyDescriptorCache.java:178) ~[reflection-util-2.6.0.jar:na]
at de.cronn.reflection.util.PropertyUtils.getMethod(PropertyUtils.java:315) ~[reflection-util-2.6.0.jar:na]
at de.cronn.reflection.util.PropertyUtils.getPropertyDescriptor(PropertyUtils.java:282) ~[reflection-util-2.6.0.jar:na]
at de.cronn.reflection.util.PropertyUtils.getPropertyName(PropertyUtils.java:290) ~[reflection-util-2.6.0.jar:na]
at fete.bird.fetebirdproduct.common.querybuilder.QueryBuilder.BuildQuery(QueryBuilder.java:24) ~[main/:na]
at fete.bird.fetebirdproduct.service.consumer.ProductListener.GetProducts(ProductListener.java:56) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at ... 9 common frames omitted
我正在使用JDK14。我知道有Java反射API。我无法对字段名称进行硬编码。
有什么解决办法吗?
您想使用 运行时间反射方法来检索实际上是常量的内容,这有点奇怪 - 您知道它实际上是字符串“描述”,并且始终用于给定的编译 运行.
我能想象你想要这个的唯一原因是你害怕你要重命名字段然后你会忘记重命名字符串常量。
一堆 IDE-based 重命名字段的实用程序将发现与字段同名的字符串常量并提及它们 ('hey, buddy? Maybe you want me to rename this string constant here too?')。
如果这对您来说仍然不够好,您可以创建一个字符串常量并将其紧跟在后面。例如:
public class Product{
private String id;
private String name;
private String description;
private static final String DESCRIPTION_PROPERTY_NAME = "description";
// Getter and setter
}
然后始终使用 DESCRIPTION_PROPERTY_NAME
而不是 "description"
。
肯定没有人会在编辑一件东西的时候忘记编辑下一行正确的东西!
但是如果您仍然担心或者这已经变成了一些疯狂的学术练习 - 那么,它基本上是一个编译时问题,所以使用作为编译时概念的注解处理器!也许试试 lombok's FieldNameConstants.
即使这样也不好,也没有实际的方法 - 你在 getDescription
方面所做的充其量只是给你 getter.不是字段的名称。 annotation-processor-less vanilla java 在没有任何其他工具的情况下无法使用如果重命名字段将无法编译的语言结构。有引用方法的语法 (x::y
),但没有这样的构造来引用字段。
您可以尝试使用lombok
将字段名称生成为常量变量。
https://projectlombok.org/features/experimental/FieldNameConstants
@Data
@FieldNameConstants
public class Product{
private String id;
private String name;
private String description;
}
这将在编译时生成:
public class Product {
private String id;
private String name;
private String description;
public static final class Fields {
public static final String id = "id";
public static final String name = "name";
public static final String description = "description";
}
// getters and setters
}
引用将如下所示:
Query query = new Query();
if (productSearchCriteria.getDescription().isPresent()) {
query.addCriteria(Criteria.where(Product.Fields.description).is(productSearchCriteria.getDescription().get()));
}
如果您出于任何原因决定更改字段名称,这会导致编译时错误,并在运行前尽早捕获它。
不需要运行时反射恶作剧。
public class Product{
private String id;
private String name;
private String description;
// Getters and setters..
}
从上面class,我想在运行时获取描述的字段名:
Query query = new Query();
if (productSearchCriteria.getDescription().isPresent()) {
query.addCriteria(Criteria.where("description").is(productSearchCriteria.getDescription().get()));
}
目前,我已经硬编码了名称,但它应该是动态的,因为如果有人重命名或更改 属性 名称,这将不起作用。
环顾四周,我发现了一个 reflection-util 图书馆。
String numberProperty = PropertyUtils.getPropertyName(Product.class, Product::getDescription);
这给了我一个例外:
Caused by: java.lang.ClassCastException: class domain.Product$ByteBuddy$QIcFk1aK cannot be cast to class domain.Product (domain.Product$ByteBuddy$QIcFk1aK is in unnamed module of loader net.bytebuddy.dynamic.loading.ByteArrayClassLoader @33943b71; domain.Product is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @5be4b0e6)
at de.cronn.reflection.util.PropertyUtils.findMethodByGetter(PropertyUtils.java:322) ~[reflection-util-2.6.0.jar:na]
at de.cronn.reflection.util.PropertyDescriptorCache.lambda$getMethod(PropertyDescriptorCache.java:178) ~[reflection-util-2.6.0.jar:na]
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) ~[na:na]
at de.cronn.reflection.util.PropertyDescriptorCache.getMethod(PropertyDescriptorCache.java:178) ~[reflection-util-2.6.0.jar:na]
at de.cronn.reflection.util.PropertyUtils.getMethod(PropertyUtils.java:315) ~[reflection-util-2.6.0.jar:na]
at de.cronn.reflection.util.PropertyUtils.getPropertyDescriptor(PropertyUtils.java:282) ~[reflection-util-2.6.0.jar:na]
at de.cronn.reflection.util.PropertyUtils.getPropertyName(PropertyUtils.java:290) ~[reflection-util-2.6.0.jar:na]
at fete.bird.fetebirdproduct.common.querybuilder.QueryBuilder.BuildQuery(QueryBuilder.java:24) ~[main/:na]
at fete.bird.fetebirdproduct.service.consumer.ProductListener.GetProducts(ProductListener.java:56) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at ... 9 common frames omitted
我正在使用JDK14。我知道有Java反射API。我无法对字段名称进行硬编码。
有什么解决办法吗?
您想使用 运行时间反射方法来检索实际上是常量的内容,这有点奇怪 - 您知道它实际上是字符串“描述”,并且始终用于给定的编译 运行.
我能想象你想要这个的唯一原因是你害怕你要重命名字段然后你会忘记重命名字符串常量。
一堆 IDE-based 重命名字段的实用程序将发现与字段同名的字符串常量并提及它们 ('hey, buddy? Maybe you want me to rename this string constant here too?')。
如果这对您来说仍然不够好,您可以创建一个字符串常量并将其紧跟在后面。例如:
public class Product{
private String id;
private String name;
private String description;
private static final String DESCRIPTION_PROPERTY_NAME = "description";
// Getter and setter
}
然后始终使用 DESCRIPTION_PROPERTY_NAME
而不是 "description"
。
肯定没有人会在编辑一件东西的时候忘记编辑下一行正确的东西!
但是如果您仍然担心或者这已经变成了一些疯狂的学术练习 - 那么,它基本上是一个编译时问题,所以使用作为编译时概念的注解处理器!也许试试 lombok's FieldNameConstants.
即使这样也不好,也没有实际的方法 - 你在 getDescription
方面所做的充其量只是给你 getter.不是字段的名称。 annotation-processor-less vanilla java 在没有任何其他工具的情况下无法使用如果重命名字段将无法编译的语言结构。有引用方法的语法 (x::y
),但没有这样的构造来引用字段。
您可以尝试使用lombok
将字段名称生成为常量变量。
https://projectlombok.org/features/experimental/FieldNameConstants
@Data
@FieldNameConstants
public class Product{
private String id;
private String name;
private String description;
}
这将在编译时生成:
public class Product {
private String id;
private String name;
private String description;
public static final class Fields {
public static final String id = "id";
public static final String name = "name";
public static final String description = "description";
}
// getters and setters
}
引用将如下所示:
Query query = new Query();
if (productSearchCriteria.getDescription().isPresent()) {
query.addCriteria(Criteria.where(Product.Fields.description).is(productSearchCriteria.getDescription().get()));
}
如果您出于任何原因决定更改字段名称,这会导致编译时错误,并在运行前尽早捕获它。
不需要运行时反射恶作剧。