Remove record component annotations when using Zulu JDK

This commit is contained in:
Rawi01 committed 2023-04-30 11:36:22 +02:00
1 parent 4014013fee
commit 1599bc1ce4
2 files changed
+16 -5

No files matched your search

@@ -586,29 +586,40 @@ public class JavacHandlerUtil {
static class RecordComponentReflect {
private static final Field astField;
private static final Field originalAnnos;
private static final Method findRecordComponentToRemove;
static {
Field a = null;
Field o = null;
Method m = null;
try {
Class<?> forName = Class.forName("com.sun.tools.javac.code.Symbol$RecordComponent");
a = Permit.permissiveGetField(forName, "ast");
o = Permit.permissiveGetField(forName, "originalAnnos");
m = Permit.permissiveGetMethod(ClassSymbol.class, "findRecordComponentToRemove", JCVariableDecl.class);
} catch (Throwable e) {
// Ignore
}
astField = a;
originalAnnos = o;
findRecordComponentToRemove = m;
}
static void deleteAnnotation(JCClassDecl record, JCVariableDecl component, JCTree annotation) {
if (astField == null || findRecordComponentToRemove == null) return;
if ((astField == null && originalAnnos == null) || findRecordComponentToRemove == null) return;
try {
Object toRemove = Permit.invokeSneaky(findRecordComponentToRemove, record.sym, component);
JCVariableDecl variable = (JCVariableDecl) Permit.get(astField, toRemove);
variable.mods.annotations = filterListByPos(variable.mods.annotations, annotation);
if (astField != null) {
// OpenJDK
JCVariableDecl variable = Permit.get(astField, toRemove);
variable.mods.annotations = filterListByPos(variable.mods.annotations, annotation);
} else {
// Zulu JDK 17
List<JCAnnotation> annotations = Permit.get(originalAnnos, toRemove);
Permit.set(originalAnnos, toRemove, filterListByPos(annotations, annotation));
}
} catch (Throwable e) {
// Ignore
}
+2 -2
View File
@@ -297,9 +297,9 @@ public class Permit {
}
}
public static Object get(Field f, Object receiver) throws IllegalAccessException {
public static <T> T get(Field f, Object receiver) throws IllegalAccessException {
try {
return f.get(receiver);
return (T) f.get(receiver);
} catch (IllegalAccessException e) {
handleReflectionDebug(e, null);
throw e;