diff options
Diffstat (limited to 'java/com/google/turbine/processing')
7 files changed, 81 insertions, 23 deletions
diff --git a/java/com/google/turbine/processing/TurbineAnnotationMirror.java b/java/com/google/turbine/processing/TurbineAnnotationMirror.java index 5ea3de1..df3bd19 100644 --- a/java/com/google/turbine/processing/TurbineAnnotationMirror.java +++ b/java/com/google/turbine/processing/TurbineAnnotationMirror.java @@ -18,6 +18,7 @@ package com.google.turbine.processing; import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.Iterables.getLast; +import static java.util.Objects.requireNonNull; import com.google.common.base.Joiner; import com.google.common.base.Supplier; @@ -115,8 +116,13 @@ class TurbineAnnotationMirror implements TurbineAnnotationValueMirror, Annotatio ImmutableMap.Builder<ExecutableElement, AnnotationValue> result = ImmutableMap.builder(); for (Map.Entry<String, Const> value : anno.values().entrySet()) { + // requireNonNull is safe because `elements` contains an entry for every method. + // Any element values pairs without a corresponding method in the annotation + // definition are weeded out in ConstEvaluator.evaluateAnnotation, and don't + // appear in the AnnoInfo. + MethodInfo methodInfo = requireNonNull(elements.get().get(value.getKey())); result.put( - factory.executableElement(elements.get().get(value.getKey()).sym()), + factory.executableElement(methodInfo.sym()), annotationValue(factory, value.getValue())); } return result.build(); diff --git a/java/com/google/turbine/processing/TurbineAnnotationProxy.java b/java/com/google/turbine/processing/TurbineAnnotationProxy.java index c39f310..967ead9 100644 --- a/java/com/google/turbine/processing/TurbineAnnotationProxy.java +++ b/java/com/google/turbine/processing/TurbineAnnotationProxy.java @@ -17,6 +17,7 @@ package com.google.turbine.processing; import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Objects.requireNonNull; import com.google.turbine.binder.bound.EnumConstantValue; import com.google.turbine.binder.bound.TurbineAnnotationValue; @@ -131,14 +132,15 @@ class TurbineAnnotationProxy implements InvocationHandler { private static Object constArrayValue( Class<?> returnType, ModelFactory factory, ClassLoader loader, ArrayInitValue value) { - if (returnType.getComponentType().equals(Class.class)) { + Class<?> componentType = requireNonNull(returnType.getComponentType()); + if (componentType.equals(Class.class)) { List<TypeMirror> result = new ArrayList<>(); for (Const element : value.elements()) { result.add(factory.asTypeMirror(((TurbineClassValue) element).type())); } throw new MirroredTypesException(result); } - Object result = Array.newInstance(returnType.getComponentType(), value.elements().size()); + Object result = Array.newInstance(componentType, value.elements().size()); int idx = 0; for (Const element : value.elements()) { Object v = constValue(returnType, factory, loader, element); diff --git a/java/com/google/turbine/processing/TurbineElement.java b/java/com/google/turbine/processing/TurbineElement.java index c22a442..f4f1675 100644 --- a/java/com/google/turbine/processing/TurbineElement.java +++ b/java/com/google/turbine/processing/TurbineElement.java @@ -46,7 +46,7 @@ import com.google.turbine.diag.TurbineError.ErrorKind; import com.google.turbine.model.Const; import com.google.turbine.model.Const.ArrayInitValue; import com.google.turbine.model.TurbineFlag; -import com.google.turbine.model.TurbineTyKind; +import com.google.turbine.tree.Tree; import com.google.turbine.tree.Tree.MethDecl; import com.google.turbine.tree.Tree.TyDecl; import com.google.turbine.tree.Tree.VarDecl; @@ -158,7 +158,8 @@ public abstract class TurbineElement implements Element { continue; } if (anno.sym().equals(metadata.repeatable())) { - ArrayInitValue arrayValue = (ArrayInitValue) anno.values().get("value"); + // requireNonNull is safe because java.lang.annotation.Repeatable declares `value`. + ArrayInitValue arrayValue = (ArrayInitValue) requireNonNull(anno.values().get("value")); for (Const element : arrayValue.elements()) { result.add( TurbineAnnotationProxy.create( @@ -262,11 +263,16 @@ public abstract class TurbineElement implements Element { return factory.asTypeMirror(info.superClassType()); } if (info instanceof SourceTypeBoundClass) { - // support simple name for stuff that doesn't exist + // support simple names for stuff that doesn't exist TyDecl decl = ((SourceTypeBoundClass) info).decl(); if (decl.xtnds().isPresent()) { - return factory.asTypeMirror( - ErrorTy.create(decl.xtnds().get().name().value())); + ArrayDeque<Tree.Ident> flat = new ArrayDeque<>(); + for (Tree.ClassTy curr = decl.xtnds().get(); + curr != null; + curr = curr.base().orElse(null)) { + flat.addFirst(curr.name()); + } + return factory.asTypeMirror(ErrorTy.create(flat)); } } return factory.noType(); @@ -785,18 +791,12 @@ public abstract class TurbineElement implements Element { @Override public ElementKind getKind() { - return info().name().equals("<init>") ? ElementKind.CONSTRUCTOR : ElementKind.METHOD; + return sym.name().equals("<init>") ? ElementKind.CONSTRUCTOR : ElementKind.METHOD; } @Override public Set<Modifier> getModifiers() { - int access = info().access(); - if (factory.getSymbol(info().sym().owner()).kind() == TurbineTyKind.INTERFACE) { - if ((access & (TurbineFlag.ACC_ABSTRACT | TurbineFlag.ACC_STATIC)) == 0) { - access |= TurbineFlag.ACC_DEFAULT; - } - } - return asModifierSet(ModifierOwner.METHOD, access); + return asModifierSet(ModifierOwner.METHOD, info().access()); } @Override diff --git a/java/com/google/turbine/processing/TurbineElements.java b/java/com/google/turbine/processing/TurbineElements.java index 9da210e..7ede6e3 100644 --- a/java/com/google/turbine/processing/TurbineElements.java +++ b/java/com/google/turbine/processing/TurbineElements.java @@ -131,7 +131,7 @@ public class TurbineElements implements Elements { if (!(element instanceof TurbineElement)) { throw new IllegalArgumentException(element.toString()); } - for (AnnoInfo a : ((TurbineTypeElement) element).annos()) { + for (AnnoInfo a : ((TurbineElement) element).annos()) { if (a.sym().equals(ClassSymbol.DEPRECATED)) { return true; } @@ -265,8 +265,8 @@ public class TurbineElements implements Elements { } /** - * Returns true if an element with the given {@code visibility} and located in package {@from} is - * visible to elements in package {@code to}. + * Returns true if an element with the given {@code visibility} and located in package {@code + * from} is visible to elements in package {@code to}. */ private static boolean isVisible( PackageSymbol from, PackageSymbol to, TurbineVisibility visibility) { diff --git a/java/com/google/turbine/processing/TurbineFiler.java b/java/com/google/turbine/processing/TurbineFiler.java index 186eb7f..45cdc22 100644 --- a/java/com/google/turbine/processing/TurbineFiler.java +++ b/java/com/google/turbine/processing/TurbineFiler.java @@ -18,6 +18,7 @@ package com.google.turbine.processing; import static com.google.common.base.Preconditions.checkArgument; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Objects.requireNonNull; import com.google.common.base.Function; import com.google.common.base.Supplier; @@ -361,7 +362,7 @@ public class TurbineFiler implements Filer { @Override public URI toUri() { try { - return loader.getResource(path).toURI(); + return requireNonNull(loader.getResource(path)).toURI(); } catch (URISyntaxException e) { throw new AssertionError(e); } diff --git a/java/com/google/turbine/processing/TurbineProcessingEnvironment.java b/java/com/google/turbine/processing/TurbineProcessingEnvironment.java index 726d075..8b44e75 100644 --- a/java/com/google/turbine/processing/TurbineProcessingEnvironment.java +++ b/java/com/google/turbine/processing/TurbineProcessingEnvironment.java @@ -26,7 +26,7 @@ import javax.lang.model.util.Elements; import javax.lang.model.util.Types; import org.checkerframework.checker.nullness.qual.Nullable; -/** Turbine's {@link ProcessingEnvironment). */ +/** Turbine's {@link ProcessingEnvironment}. */ public class TurbineProcessingEnvironment implements ProcessingEnvironment { private final Filer filer; diff --git a/java/com/google/turbine/processing/TurbineTypes.java b/java/com/google/turbine/processing/TurbineTypes.java index f65f921..7d2e6c0 100644 --- a/java/com/google/turbine/processing/TurbineTypes.java +++ b/java/com/google/turbine/processing/TurbineTypes.java @@ -825,7 +825,7 @@ public class TurbineTypes implements Types { @Override public List<? extends TypeMirror> directSupertypes(TypeMirror m) { - return factory.asTypeMirrors(directSupertypes(asTurbineType(m))); + return factory.asTypeMirrors(deannotate(directSupertypes(asTurbineType(m)))); } public ImmutableList<Type> directSupertypes(Type t) { @@ -882,7 +882,12 @@ public class TurbineTypes implements Types { @Override public TypeMirror erasure(TypeMirror typeMirror) { - return factory.asTypeMirror(erasure(asTurbineType(typeMirror))); + Type t = erasure(asTurbineType(typeMirror)); + if (t.tyKind() == TyKind.CLASS_TY) { + // bug-parity with javac + t = deannotate(t); + } + return factory.asTypeMirror(t); } private Type erasure(Type type) { @@ -896,6 +901,50 @@ public class TurbineTypes implements Types { }); } + /** + * Remove some type annotation metadata for bug-compatibility with javac, which does this + * inconsistently (see https://bugs.openjdk.java.net/browse/JDK-8042981). + */ + private static Type deannotate(Type ty) { + switch (ty.tyKind()) { + case CLASS_TY: + return deannotateClassTy((Type.ClassTy) ty); + case ARRAY_TY: + return deannotateArrayTy((Type.ArrayTy) ty); + case TY_VAR: + case INTERSECTION_TY: + case WILD_TY: + case METHOD_TY: + case PRIM_TY: + case VOID_TY: + case ERROR_TY: + case NONE_TY: + return ty; + } + throw new AssertionError(ty.tyKind()); + } + + private static ImmutableList<Type> deannotate(ImmutableList<Type> types) { + ImmutableList.Builder<Type> result = ImmutableList.builder(); + for (Type type : types) { + result.add(deannotate(type)); + } + return result.build(); + } + + private static Type.ArrayTy deannotateArrayTy(Type.ArrayTy ty) { + return ArrayTy.create(deannotate(ty.elementType()), /* annos= */ ImmutableList.of()); + } + + public static Type.ClassTy deannotateClassTy(Type.ClassTy ty) { + ImmutableList.Builder<Type.ClassTy.SimpleClassTy> classes = ImmutableList.builder(); + for (Type.ClassTy.SimpleClassTy c : ty.classes()) { + classes.add( + SimpleClassTy.create(c.sym(), deannotate(c.targs()), /* annos= */ ImmutableList.of())); + } + return ClassTy.create(classes.build()); + } + @Override public TypeElement boxedClass(PrimitiveType p) { return factory.typeElement(boxedClass(((PrimTy) asTurbineType(p)).primkind())); |