aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/javac/handlers/JavacHandlerUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/lombok/javac/handlers/JavacHandlerUtil.java')
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java72
1 files changed, 70 insertions, 2 deletions
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index b1557533..e4e40095 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -27,6 +27,7 @@ import static lombok.javac.Javac.*;
import static lombok.javac.JavacAugments.JCTree_generatedNode;
import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -1039,6 +1040,57 @@ public class JavacHandlerUtil {
return (field.mods.flags & Flags.ENUM) != 0;
}
+ static class JCAnnotatedTypeReflect {
+ private static Class<?> TYPE;
+ private static Constructor<?> CONSTRUCTOR;
+ private static Field ANNOTATIONS, UNDERLYING_TYPE;
+
+ private static void init(Class<?> in) {
+ if (TYPE != null) return;
+ if (!in.getName().equals("com.sun.tools.javac.tree.JCTree$JCAnnotatedType")) return;
+ try {
+ CONSTRUCTOR = in.getDeclaredConstructor(List.class, JCExpression.class);
+ CONSTRUCTOR.setAccessible(true);
+ ANNOTATIONS = in.getDeclaredField("annotations");
+ UNDERLYING_TYPE = in.getDeclaredField("underlyingType");
+ TYPE = in;
+ } catch (Exception ignore) {}
+ }
+
+ static boolean is(JCTree obj) {
+ if (obj == null) return false;
+ init(obj.getClass());
+ return obj.getClass() == TYPE;
+ }
+
+ @SuppressWarnings("unchecked")
+ static List<JCAnnotation> getAnnotations(JCTree obj) {
+ init(obj.getClass());
+ try {
+ return (List<JCAnnotation>) ANNOTATIONS.get(obj);
+ } catch (Exception e) {
+ return List.nil();
+ }
+ }
+
+ static JCExpression getUnderlyingType(JCTree obj) {
+ init(obj.getClass());
+ try {
+ return (JCExpression) UNDERLYING_TYPE.get(obj);
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ static JCExpression create(List<JCAnnotation> annotations, JCExpression underlyingType) {
+ try {
+ return (JCExpression) CONSTRUCTOR.newInstance(annotations, underlyingType);
+ } catch (Exception e) {
+ return null;
+ }
+ }
+ }
+
// jdk9 support, types have changed, names stay the same
static class ClassSymbolMembersField {
private static final Field membersField;
@@ -1570,6 +1622,16 @@ public class JavacHandlerUtil {
return out.toList();
}
+ public static List<JCAnnotation> getTypeUseAnnotations(JCExpression from) {
+ if (!JCAnnotatedTypeReflect.is(from)) return List.nil();
+ return JCAnnotatedTypeReflect.getAnnotations(from);
+ }
+
+ public static JCExpression removeTypeUseAnnotations(JCExpression from) {
+ if (!JCAnnotatedTypeReflect.is(from)) return from;
+ return JCAnnotatedTypeReflect.getUnderlyingType(from);
+ }
+
public static JCExpression namePlusTypeParamsToTypeReference(JavacTreeMaker maker, Name typeName, List<JCTypeParameter> params) {
if (params.isEmpty()) {
return maker.Ident(typeName);
@@ -1650,7 +1712,7 @@ public class JavacHandlerUtil {
}
/**
- * Creates a full clone of a given javac AST type node. Every part is cloned (every identifier, every select, every wildcard, every type apply).
+ * Creates a full clone of a given javac AST type node. Every part is cloned (every identifier, every select, every wildcard, every type apply, every type_use annotation).
*
* If there's any node in the tree that we don't know how to clone, that part isn't cloned. However, we wouldn't know what could possibly show up that we
* can't currently clone; that's just a safeguard.
@@ -1712,6 +1774,12 @@ public class JavacHandlerUtil {
return maker.Wildcard(newKind, newInner);
}
+ if (JCAnnotatedTypeReflect.is(in)) {
+ JCExpression underlyingType = cloneType0(maker, JCAnnotatedTypeReflect.getUnderlyingType(in));
+ List<JCAnnotation> anns = copyAnnotations(JCAnnotatedTypeReflect.getAnnotations(in));
+ return JCAnnotatedTypeReflect.create(anns, underlyingType);
+ }
+
// This is somewhat unsafe, but it's better than outright throwing an exception here. Returning null will just cause an exception down the pipeline.
return (JCExpression) in;
}
@@ -1887,7 +1955,7 @@ public class JavacHandlerUtil {
public static boolean isDirectDescendantOfObject(JavacNode typeNode) {
if (!(typeNode.get() instanceof JCClassDecl)) throw new IllegalArgumentException("not a type node");
- JCTree extending = Javac.getExtendsClause((JCClassDecl)typeNode.get());
+ JCTree extending = Javac.getExtendsClause((JCClassDecl) typeNode.get());
if (extending == null) return true;
String p = extending.toString();
return p.equals("Object") || p.equals("java.lang.Object");