English 中文(简体)
如何在运行时检查一字段是否由 Lombok 注释附加? [重复]
原标题:How to Check if a Field is Annotated by a Lombok Annotation at Runtime? [duplicate]
  • 时间:2024-07-17 14:50:37
  •  标签:
  • java
  • lombok
This question already has answers here: Lombok @Getter and @Setter annotations are not being recognised when getAnnoations() method is used (1 answer) lombok @NonNull on Field not readable using getAnnotations (1 answer) Closed 6 days ago. I need to check if a field in a class is annotated with @EqualsAndHashCode.Exclude from Lombok. However, it seems that due to the Lombok annotations being processed at compile-time, I cannot check them at runtime. I have tried the following approaches: EqualsAndHashCode.Exclude exclude = field.getAnnotation(EqualsAndHashCode.Exclude.class); and field.isAnnotationPresent(EqualsAndHashCode.Exclude.class); Both methods do not seem to work. Here is a snippet of my current code: import lombok.EqualsAndHashCode; import java.lang.reflect.Field; public class ReflectionUtils { public static void checkLombokAnnotation(Object target) { Field[] fields = target.getClass().getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(EqualsAndHashCode.Exclude.class)) { System.out.println(field.getName() + " is excluded."); } else { System.out.println(field.getName() + " is not excluded."); } } } }
问题回答
EqualsAndHashCode is declared as @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) public @interface EqualsAndHashCode { } RetentionPolicy.SOURCE means it is discarded by the compiller, so you can t get it at runtime.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签