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:
<code>EqualsAndHashCode.Exclude exclude = field.getAnnotation(EqualsAndHashCode.Exclude.class);
</code>
<code>EqualsAndHashCode.Exclude exclude = field.getAnnotation(EqualsAndHashCode.Exclude.class);
</code>
EqualsAndHashCode.Exclude exclude = field.getAnnotation(EqualsAndHashCode.Exclude.class);
and
<code>field.isAnnotationPresent(EqualsAndHashCode.Exclude.class);
</code>
<code>field.isAnnotationPresent(EqualsAndHashCode.Exclude.class);
</code>
field.isAnnotationPresent(EqualsAndHashCode.Exclude.class);
Both methods do not seem to work. Here is a snippet of my current code:
<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.");
}
}
}
}
</code>
<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.");
}
}
}
}
</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.");
}
}
}
}
0
EqualsAndHashCode
declared as
<code>@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface EqualsAndHashCode {
}
</code>
<code>@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface EqualsAndHashCode {
}
</code>
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface EqualsAndHashCode {
}
RetentionPolicy.SOURCE
means it is discardet by compiller, so you can’t get it at runtime.