diff options
author | David Ostrovsky <david@ostrovsky.org> | 2017-01-19 08:07:15 +0100 |
---|---|---|
committer | Damien Martin-Guillerez <dmarting@google.com> | 2017-01-20 14:39:02 +0100 |
commit | 9d99bc45df4a6fcfb929cf6e641459f8b33077e3 (patch) | |
tree | 9226baa9157b225578c3266b3472907e2d086f94 /third_party/checker_framework_javacutil/java/org | |
parent | b46631bcc83c02988d1993c671b67dc8e6b86fe7 (diff) |
Downgrade checker framework to 1.8.10
Fixes #2254.
Change-Id: Ia411fa321fdac9d6acdaac60a8463b92ee1ae407
Diffstat (limited to 'third_party/checker_framework_javacutil/java/org')
16 files changed, 226 insertions, 691 deletions
diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AbstractTypeProcessor.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AbstractTypeProcessor.java index 8db8da333c..04389039e8 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AbstractTypeProcessor.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AbstractTypeProcessor.java @@ -145,8 +145,8 @@ public abstract class AbstractTypeProcessor extends AbstractProcessor { public void typeProcessingStart() {} /** - * Processes a fully-analyzed class that contains a supported annotation - * (see {@link #getSupportedAnnotationTypes()}). + * Processes a fully analyzed class that contains a supported annotation + * (look {@link #getSupportedAnnotationTypes()}). * * <p>The passed class is always valid type-checked Java code. * @@ -176,9 +176,8 @@ public abstract class AbstractTypeProcessor extends AbstractProcessor { @Override public void finished(TaskEvent e) { - if (e.getKind() != TaskEvent.Kind.ANALYZE) { + if (e.getKind() != TaskEvent.Kind.ANALYZE) return; - } if (!hasInvokedTypeProcessingStart) { typeProcessingStart(); @@ -192,16 +191,13 @@ public abstract class AbstractTypeProcessor extends AbstractProcessor { hasInvokedTypeProcessingOver = true; } - if (e.getTypeElement() == null) { + if (e.getTypeElement() == null) throw new AssertionError("event task without a type element"); - } - if (e.getCompilationUnit() == null) { + if (e.getCompilationUnit() == null) throw new AssertionError("event task without compilation unit"); - } - if (!elements.remove(e.getTypeElement().getQualifiedName())) { + if (!elements.remove(e.getTypeElement().getQualifiedName())) return; - } TypeElement elem = e.getTypeElement(); TreePath p = Trees.instance(processingEnv).getPath(elem); diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AnnotationProvider.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AnnotationProvider.java index 580be40ef4..8ed857a9b2 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AnnotationProvider.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AnnotationProvider.java @@ -22,12 +22,12 @@ public interface AnnotationProvider { */ public AnnotationMirror getDeclAnnotation(Element elt, Class<? extends Annotation> anno); - + /** - * Return the annotation on {@code tree} that has the class - * {@code target}. If no annotation for the given target class exists, - * the result is {@code null} - * + * Return the annotation on <code>tree</code> that has the class + * <code>target</code>. If no annotation for the given target class exists, + * the result is <code>null</code> + * * @param tree * The tree of which the annotation is returned * @param target diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AnnotationUtils.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AnnotationUtils.java index 19eb74df49..54eb54d2fb 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AnnotationUtils.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/AnnotationUtils.java @@ -33,9 +33,6 @@ import javax.lang.model.type.TypeMirror; import javax.lang.model.util.ElementFilter; import javax.lang.model.util.Elements; -import com.sun.source.tree.AnnotationTree; -import com.sun.source.tree.MethodTree; -import com.sun.source.tree.ModifiersTree; import com.sun.tools.javac.code.Symbol.VarSymbol; import com.sun.tools.javac.code.Type; import com.sun.tools.javac.model.JavacElements; @@ -53,7 +50,6 @@ public class AnnotationUtils { public static void clear() { annotationsFromNames.clear(); annotationMirrorNames.clear(); - annotationMirrorSimpleNames.clear(); annotationClassNames.clear(); } @@ -65,22 +61,12 @@ public class AnnotationUtils { private static final Map<CharSequence, AnnotationMirror> annotationsFromNames = new HashMap<CharSequence, AnnotationMirror>(); - - private static final int ANNOTATION_CACHE_SIZE = 500; - /** * Cache names of AnnotationMirrors for faster access. Values in * the map are interned Strings, so they can be compared with ==. */ private static final Map<AnnotationMirror, /*@Interned*/ String> annotationMirrorNames - = CollectionUtils.createLRUCache(ANNOTATION_CACHE_SIZE); - - /** - * Cache simple names of AnnotationMirrors for faster access. Values in - * the map are interned Strings, so they can be compared with ==. - */ - private static final Map<AnnotationMirror, /*@Interned*/ String> annotationMirrorSimpleNames - = CollectionUtils.createLRUCache(ANNOTATION_CACHE_SIZE); + = new HashMap<AnnotationMirror, /*@Interned*/ String>(); /** * Cache names of classes representing AnnotationMirrors for @@ -100,13 +86,11 @@ public class AnnotationUtils { * @return an {@link AnnotationMirror} of type {@code} name */ public static AnnotationMirror fromName(Elements elements, CharSequence name) { - if (annotationsFromNames.containsKey(name)) { + if (annotationsFromNames.containsKey(name)) return annotationsFromNames.get(name); - } final DeclaredType annoType = typeFromName(elements, name); - if (annoType == null) { + if (annoType == null) return null; - } if (annoType.asElement().getKind() != ElementKind.ANNOTATION_TYPE) { ErrorReporter.errorAbort(annoType + " is not an annotation"); return null; // dead code @@ -155,9 +139,8 @@ public class AnnotationUtils { */ private static DeclaredType typeFromName(Elements elements, CharSequence name) { /*@Nullable*/ TypeElement typeElt = elements.getTypeElement(name); - if (typeElt == null) { + if (typeElt == null) return null; - } return (DeclaredType) typeElt.asType(); } @@ -170,12 +153,11 @@ public class AnnotationUtils { // ********************************************************************** /** - * @return the fully-qualified name of an annotation as a String + * @return the fully-qualified name of an annotation as a Name */ public static final /*@Interned*/ String annotationName(AnnotationMirror annotation) { - if (annotationMirrorNames.containsKey(annotation)) { + if (annotationMirrorNames.containsKey(annotation)) return annotationMirrorNames.get(annotation); - } final DeclaredType annoType = annotation.getAnnotationType(); final TypeElement elm = (TypeElement) annoType.asElement(); @@ -185,21 +167,6 @@ public class AnnotationUtils { } /** - * @return the simple name of an annotation as a String - */ - public static String annotationSimpleName(AnnotationMirror annotation) { - if (annotationMirrorSimpleNames.containsKey(annotation)) { - return annotationMirrorSimpleNames.get(annotation); - } - - final DeclaredType annoType = annotation.getAnnotationType(); - final TypeElement elm = (TypeElement) annoType.asElement(); - /*@Interned*/ String name = elm.getSimpleName().toString().intern(); - annotationMirrorSimpleNames.put(annotation, name); - return name; - } - - /** * Checks if both annotations are the same. * * Returns true iff both annotations are of the same type and have the @@ -231,9 +198,8 @@ public class AnnotationUtils { * @return true iff a1 and a2 have the same annotation type */ public static boolean areSameIgnoringValues(AnnotationMirror a1, AnnotationMirror a2) { - if (a1 != null && a2 != null) { + if (a1 != null && a2 != null) return annotationName(a1) == annotationName(a2); - } return a1 == a2; } @@ -254,7 +220,7 @@ public class AnnotationUtils { Class<? extends Annotation> anno) { /*@Interned*/ String canonicalName; if (annotationClassNames.containsKey(anno)) { - canonicalName = annotationClassNames.get(anno); + canonicalName = annotationClassNames.get(anno).intern(); } else { canonicalName = anno.getCanonicalName().intern(); annotationClassNames.put(anno, canonicalName); @@ -268,12 +234,10 @@ public class AnnotationUtils { * @return true iff c1 and c2 contain the same annotations */ public static boolean areSame(Collection<? extends AnnotationMirror> c1, Collection<? extends AnnotationMirror> c2) { - if (c1.size() != c2.size()) { + if (c1.size() != c2.size()) return false; - } - if (c1.size() == 1) { + if (c1.size() == 1) return areSame(c1.iterator().next(), c2.iterator().next()); - } Set<AnnotationMirror> s1 = createAnnotationSet(); Set<AnnotationMirror> s2 = createAnnotationSet(); @@ -287,9 +251,8 @@ public class AnnotationUtils { while (iter1.hasNext()) { AnnotationMirror anno1 = iter1.next(); AnnotationMirror anno2 = iter2.next(); - if (!areSame(anno1, anno2)) { + if (!areSame(anno1, anno2)) return false; - } } return true; } @@ -299,11 +262,11 @@ public class AnnotationUtils { * Using Collection.contains does not always work, because it * does not use areSame for comparison. * - * @return true iff c contains anno, according to areSame + * @return true iff c contains anno, according to areSame. */ public static boolean containsSame(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) { - for (AnnotationMirror an : c) { - if (AnnotationUtils.areSame(an, anno)) { + for(AnnotationMirror an : c) { + if(AnnotationUtils.areSame(an, anno)) { return true; } } @@ -315,11 +278,11 @@ public class AnnotationUtils { * Using Collection.contains does not always work, because it * does not use areSame for comparison. * - * @return true iff c contains anno, according to areSameByClass + * @return true iff c contains anno, according to areSameByClass. */ public static boolean containsSameByClass(Collection<? extends AnnotationMirror> c, Class<? extends Annotation> anno) { - for (AnnotationMirror an : c) { - if (AnnotationUtils.areSameByClass(an, anno)) { + for(AnnotationMirror an : c) { + if(AnnotationUtils.areSameByClass(an, anno)) { return true; } } @@ -331,11 +294,11 @@ public class AnnotationUtils { * Using Collection.contains does not always work, because it * does not use areSameIgnoringValues for comparison. * - * @return true iff c contains anno, according to areSameIgnoringValues + * @return true iff c contains anno, according to areSameIgnoringValues. */ public static boolean containsSameIgnoringValues(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) { - for (AnnotationMirror an : c) { - if (AnnotationUtils.areSameIgnoringValues(an, anno)) { + for(AnnotationMirror an : c) { + if(AnnotationUtils.areSameIgnoringValues(an, anno)) { return true; } } @@ -422,32 +385,12 @@ public class AnnotationUtils { for (ExecutableElement meth : ElementFilter.methodsIn(ad.getAnnotationType().asElement().getEnclosedElements())) { AnnotationValue defaultValue = meth.getDefaultValue(); - if (defaultValue != null && !valMap.containsKey(meth)) { + if (defaultValue != null && !valMap.containsKey(meth)) valMap.put(meth, defaultValue); - } } return valMap; } - - /** - * Verify whether the attribute with the name {@code name} exists in - * the annotation {@code anno}. - * - * @param anno the annotation to examine - * @param name the name of the attribute - * @return whether the attribute exists in anno - */ - public static <T> boolean hasElementValue(AnnotationMirror anno, CharSequence name) { - Map<? extends ExecutableElement, ? extends AnnotationValue> valmap = anno.getElementValues(); - for (ExecutableElement elem : valmap.keySet()) { - if (elem.getSimpleName().contentEquals(name)) { - return true; - } - } - return false; - } - /** * Get the attribute with the name {@code name} of the annotation * {@code anno}. The result is expected to have type {@code expectedType}. @@ -466,7 +409,7 @@ public class AnnotationUtils { * @param anno the annotation to disassemble * @param name the name of the attribute to access * @param expectedType the expected type used to cast the return type - * @param useDefaults whether to apply default values to the attribute + * @param useDefaults whether to apply default values to the attribute. * @return the value of the attribute with the given name */ public static <T> T getElementValue(AnnotationMirror anno, CharSequence name, @@ -510,7 +453,7 @@ public class AnnotationUtils { * @param anno the annotation to disassemble * @param name the name of the attribute to access * @param expectedType the expected type used to cast the return type - * @param useDefaults whether to apply default values to the attribute + * @param useDefaults whether to apply default values to the attribute. * @return the value of the attribute with the given name */ public static <T> List<T> getElementValueArray(AnnotationMirror anno, @@ -591,22 +534,4 @@ public class AnnotationUtils { } map.put(key, Collections.unmodifiableSet(result)); } - - /** - * Returns the annotations explicitly written on a constructor result. - * Callers should check that {@code constructorDeclaration} is in fact a declaration - * of a constructor. - * - * @param constructorDeclaration declaration tree of constructor - * @return set of annotations explicit on the resulting type of the constructor - */ - public static Set<AnnotationMirror> getExplicitAnnotationsOnConstructorResult(MethodTree constructorDeclaration) { - Set<AnnotationMirror> annotationSet = AnnotationUtils.createAnnotationSet(); - ModifiersTree modifiersTree = constructorDeclaration.getModifiers(); - if (modifiersTree != null) { - List<? extends AnnotationTree> annotationTrees = modifiersTree.getAnnotations(); - annotationSet.addAll(InternalUtils.annotationsFromTypeAnnotationTrees(annotationTrees)); - } - return annotationSet; - } } diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/BasicTypeProcessor.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/BasicTypeProcessor.java index 1fa1e6cb82..c83a00021c 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/BasicTypeProcessor.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/BasicTypeProcessor.java @@ -40,3 +40,4 @@ public abstract class BasicTypeProcessor extends AbstractTypeProcessor { } } + diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/CollectionUtils.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/CollectionUtils.java deleted file mode 100644 index eaaa69115d..0000000000 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/CollectionUtils.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.checkerframework.javacutil; - -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * Utility methods related to Java Collections - */ -public class CollectionUtils { - - /** - * A Utility method for creating LRU cache - * @param size size of the cache - * @return a new cache with the provided size - */ - public static <K, V> Map<K, V> createLRUCache(final int size) { - return new LinkedHashMap<K, V>() { - - private static final long serialVersionUID = 5261489276168775084L; - @Override - protected boolean removeEldestEntry(Map.Entry<K, V> entry) { - return size() > size; - } - }; - } -} diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ElementUtils.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ElementUtils.java index 19f0500585..160a9c2eea 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ElementUtils.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ElementUtils.java @@ -90,9 +90,6 @@ public class ElementUtils { * */ public static PackageElement parentPackage(final Elements e, final PackageElement elem) { - // The following might do the same thing: - // ((Symbol) elt).owner; - // TODO: verify and see whether the change is worth it. String fqnstart = elem.getQualifiedName().toString(); String fqn = fqnstart; if (fqn != null && !fqn.isEmpty() && fqn.contains(".")) { @@ -106,6 +103,7 @@ public class ElementUtils { * Returns true if the element is a static element: whether it is a static * field, static method, or static class * + * @param element * @return true if element is static */ public static boolean isStatic(Element element) { @@ -116,6 +114,7 @@ public class ElementUtils { * Returns true if the element is a final element: a final field, final * method, or final class * + * @param element * @return true if the element is final */ public static boolean isFinal(Element element) { @@ -125,6 +124,7 @@ public class ElementUtils { /** * Returns true if the element is a effectively final element. * + * @param element * @return true if the element is effectively final */ public static boolean isEffectivelyFinal(Element element) { @@ -141,16 +141,16 @@ public class ElementUtils { * returns the return type of a method element, the class type of a * constructor, or simply the type mirror of the element itself. * + * @param element * @return the type for the element used as a value */ public static TypeMirror getType(Element element) { - if (element.getKind() == ElementKind.METHOD) { + if (element.getKind() == ElementKind.METHOD) return ((ExecutableElement)element).getReturnType(); - } else if (element.getKind() == ElementKind.CONSTRUCTOR) { + else if (element.getKind() == ElementKind.CONSTRUCTOR) return enclosingClass(element).asType(); - } else { + else return element.asType(); - } } /** @@ -160,7 +160,7 @@ public class ElementUtils { * @param element * an element enclosed by a class, or a * {@code TypeElement} - * @return the qualified {@code Name} of the innermost class + * @return The qualified {@code Name} of the innermost class * enclosing the element */ public static /*@Nullable*/ Name getQualifiedClassName(Element element) { @@ -170,9 +170,8 @@ public class ElementUtils { } TypeElement elem = enclosingClass(element); - if (elem == null) { + if (elem == null) return null; - } return elem.getQualifiedName(); } @@ -182,8 +181,7 @@ public class ElementUtils { */ public static String getVerboseName(Element elt) { if (elt.getKind() == ElementKind.PACKAGE || - elt.getKind().isClass() || - elt.getKind().isInterface()) { + elt.getKind().isClass()) { return getQualifiedClassName(elt).toString(); } else { return getQualifiedClassName(elt) + "." + elt.toString(); @@ -205,7 +203,7 @@ public class ElementUtils { */ public static boolean isCompileTimeConstant(Element elt) { return elt != null - && (elt.getKind() == ElementKind.FIELD || elt.getKind() == ElementKind.LOCAL_VARIABLE) + && elt.getKind() == ElementKind.FIELD && ((VariableElement)elt).getConstantValue() != null; } @@ -213,15 +211,14 @@ public class ElementUtils { * Returns true if the element is declared in ByteCode. * Always return false if elt is a package. */ - public static boolean isElementFromByteCode(Element elt) { - if (elt == null) { + public static boolean isElementFromByteCode(Element elt){ + if (elt == null) return false; - } - if (elt instanceof Symbol.ClassSymbol) { + if (elt instanceof Symbol.ClassSymbol){ Symbol.ClassSymbol clss = (Symbol.ClassSymbol) elt; - if (null != clss.classfile) { - // The class file could be a .java file + if (null != clss.classfile){ + //The class file could be a .java file return clss.classfile.getName().endsWith(".class"); } else { return false; @@ -234,13 +231,12 @@ public class ElementUtils { * Returns true if the element is declared in ByteCode. * Always return false if elt is a package. */ - private static boolean isElementFromByteCode(Element elt, Element orig) { - if (elt == null) { + private static boolean isElementFromByteCode(Element elt, Element orig){ + if (elt == null) return false; - } - if (elt instanceof Symbol.ClassSymbol) { + if (elt instanceof Symbol.ClassSymbol){ Symbol.ClassSymbol clss = (Symbol.ClassSymbol) elt; - if (null != clss.classfile) { + if (null != clss.classfile){ // The class file could be a .java file return (clss.classfile.getName().endsWith(".class") || clss.classfile.getName().endsWith(".class)") || @@ -282,13 +278,13 @@ public class ElementUtils { * Does the given element need a receiver for accesses? * For example, an access to a local variable does not require a receiver. * - * @param element the element to test - * @return whether the element requires a receiver for accesses + * @param element The element to test. + * @return whether the element requires a receiver for accesses. */ public static boolean hasReceiver(Element element) { - return (element.getKind().isField() || - element.getKind() == ElementKind.METHOD || - element.getKind() == ElementKind.CONSTRUCTOR) + return element.getKind() != ElementKind.LOCAL_VARIABLE + && element.getKind() != ElementKind.PARAMETER + && element.getKind() != ElementKind.PACKAGE && !ElementUtils.isStatic(element); } @@ -298,12 +294,11 @@ public class ElementUtils { * TODO: can we learn from the implementation of * com.sun.tools.javac.model.JavacElements.getAllMembers(TypeElement)? */ - public static List<TypeElement> getSuperTypes(Elements elements, TypeElement type) { + public static List<TypeElement> getSuperTypes(TypeElement type) { List<TypeElement> superelems = new ArrayList<TypeElement>(); - if (type == null) { + if (type == null) return superelems; - } // Set up a stack containing type, which is our starting point. Deque<TypeElement> stack = new ArrayDeque<TypeElement>(); @@ -332,12 +327,6 @@ public class ElementUtils { } } - // Include java.lang.Object as implicit superclass for all classes and interfaces. - TypeElement jlobject = elements.getTypeElement("java.lang.Object"); - if (!superelems.contains(jlobject)) { - superelems.add(jlobject); - } - return Collections.<TypeElement>unmodifiableList(superelems); } @@ -346,10 +335,10 @@ public class ElementUtils { * TODO: should this use javax.lang.model.util.Elements.getAllMembers(TypeElement) * instead of our own getSuperTypes? */ - public static List<VariableElement> getAllFieldsIn(Elements elements, TypeElement type) { + public static List<VariableElement> getAllFieldsIn(TypeElement type) { List<VariableElement> fields = new ArrayList<VariableElement>(); fields.addAll(ElementFilter.fieldsIn(type.getEnclosedElements())); - List<TypeElement> alltypes = getSuperTypes(elements, type); + List<TypeElement> alltypes = getSuperTypes(type); for (TypeElement atype : alltypes) { fields.addAll(ElementFilter.fieldsIn(atype.getEnclosedElements())); } @@ -362,11 +351,11 @@ public class ElementUtils { * TODO: should this use javax.lang.model.util.Elements.getAllMembers(TypeElement) * instead of our own getSuperTypes? */ - public static List<ExecutableElement> getAllMethodsIn(Elements elements, TypeElement type) { + public static List<ExecutableElement> getAllMethodsIn(TypeElement type) { List<ExecutableElement> meths = new ArrayList<ExecutableElement>(); meths.addAll(ElementFilter.methodsIn(type.getEnclosedElements())); - List<TypeElement> alltypes = getSuperTypes(elements, type); + List<TypeElement> alltypes = getSuperTypes(type); for (TypeElement atype : alltypes) { meths.addAll(ElementFilter.methodsIn(atype.getEnclosedElements())); } diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ErrorHandler.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ErrorHandler.java index 9e3c27a7b9..3fd0faa475 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ErrorHandler.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ErrorHandler.java @@ -10,7 +10,7 @@ public interface ErrorHandler { /** * Log an error message and abort processing. * - * @param msg the error message to log + * @param msg The error message to log. */ public void errorAbort(String msg); diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ErrorReporter.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ErrorReporter.java index 309f1bb4ee..072e1a1031 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ErrorReporter.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/ErrorReporter.java @@ -21,7 +21,7 @@ public class ErrorReporter { * Log an error message and abort processing. * Call this method instead of raising an exception. * - * @param msg the error message to log + * @param msg The error message to log. */ public static void errorAbort(String msg) { if (handler != null) { diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/InternalUtils.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/InternalUtils.java index dbff45169b..40cb4747f5 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/InternalUtils.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/InternalUtils.java @@ -29,7 +29,6 @@ import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol.TypeSymbol; import com.sun.tools.javac.code.Type; -import com.sun.tools.javac.code.Type.AnnotatedType; import com.sun.tools.javac.code.Types; import com.sun.tools.javac.processing.JavacProcessingEnvironment; import com.sun.tools.javac.tree.JCTree; @@ -132,13 +131,11 @@ public class InternalUtils { */ public static boolean isAnonymousConstructor(final MethodTree method) { /*@Nullable*/ Element e = InternalUtils.symbol(method); - if (e == null || !(e instanceof Symbol)) { + if (e == null || !(e instanceof Symbol)) return false; - } - if ((((/*@NonNull*/ Symbol)e).flags() & Flags.ANONCONSTR) != 0) { + if ((((/*@NonNull*/ Symbol)e).flags() & Flags.ANONCONSTR) != 0) return true; - } return false; } @@ -198,9 +195,8 @@ public class InternalUtils { public final static List<AnnotationMirror> annotationsFromTypeAnnotationTrees(List<? extends AnnotationTree> annos) { List<AnnotationMirror> annotations = new ArrayList<AnnotationMirror>(annos.size()); - for (AnnotationTree anno : annos) { + for (AnnotationTree anno : annos) annotations.add(((JCAnnotation)anno).attribute); - } return annotations; } @@ -237,9 +233,6 @@ public class InternalUtils { * Returns whether a TypeVariable represents a captured type. */ public static boolean isCaptured(TypeVariable typeVar) { - if (typeVar instanceof AnnotatedType) { - return ((Type.TypeVar) ((Type.AnnotatedType) typeVar).unannotatedType()).isCaptured(); - } return ((Type.TypeVar) typeVar).isCaptured(); } @@ -251,21 +244,17 @@ public class InternalUtils { } /** - * Returns the least upper bound of two {@link TypeMirror}s, - * ignoring any annotations on the types. + * Returns the least upper bound of two {@link TypeMirror}s. * - * Wrapper around Types.lub to add special handling for - * null types, primitives, and wildcards. - * - * @param processingEnv the {@link ProcessingEnvironment} to use. - * @param tm1 a {@link TypeMirror}. - * @param tm2 a {@link TypeMirror}. - * @return the least upper bound of {@code tm1} and {@code tm2}. + * @param processingEnv The {@link ProcessingEnvironment} to use. + * @param tm1 A {@link TypeMirror}. + * @param tm2 A {@link TypeMirror}. + * @return The least upper bound of {@code tm1} and {@code tm2}. */ public static TypeMirror leastUpperBound( ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) { - Type t1 = ((Type) tm1).unannotatedType(); - Type t2 = ((Type) tm2).unannotatedType(); + Type t1 = (Type) tm1; + Type t2 = (Type) tm2; JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv; Types types = Types.instance(javacEnv.getContext()); if (types.isSameType(t1, t2)) { @@ -313,22 +302,17 @@ public class InternalUtils { } /** - * Returns the greatest lower bound of two {@link TypeMirror}s, - * ignoring any annotations on the types. - * - * Wrapper around Types.glb to add special handling for - * null types, primitives, and wildcards. + * Returns the greatest lower bound of two {@link TypeMirror}s. * - * - * @param processingEnv the {@link ProcessingEnvironment} to use. - * @param tm1 a {@link TypeMirror}. - * @param tm2 a {@link TypeMirror}. - * @return the greatest lower bound of {@code tm1} and {@code tm2}. + * @param processingEnv The {@link ProcessingEnvironment} to use. + * @param tm1 A {@link TypeMirror}. + * @param tm2 A {@link TypeMirror}. + * @return The greatest lower bound of {@code tm1} and {@code tm2}. */ public static TypeMirror greatestLowerBound( ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) { - Type t1 = ((Type) tm1).unannotatedType(); - Type t2 = ((Type) tm2).unannotatedType(); + Type t1 = (Type) tm1; + Type t2 = (Type) tm2; JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv; Types types = Types.instance(javacEnv.getContext()); if (types.isSameType(t1, t2)) { @@ -361,9 +345,6 @@ public class InternalUtils { if (t2.getKind() == TypeKind.WILDCARD) { return t1; } - - // If neither type is a primitive type, null type, or wildcard - // and if the types are not the same, use javac types.glb return types.glb(t1, t2); } diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Pair.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Pair.java index 0d8a862f08..779c9b34ec 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Pair.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Pair.java @@ -36,21 +36,18 @@ public class Pair<V1, V2> { public int hashCode() { if (hashCode == -1) { hashCode = 31; - if (first != null) { + if (first != null) hashCode += 17 * first.hashCode(); - } - if (second != null) { + if (second != null) hashCode += 17 * second.hashCode(); - } } return hashCode; } @Override public boolean equals(Object o) { - if (!(o instanceof Pair)) { + if (!(o instanceof Pair)) return false; - } @SuppressWarnings("unchecked") Pair<V1, V2> other = (Pair<V1, V2>) o; if (this.first == null) { diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Resolver.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Resolver.java index 45d959fe02..c47566bfa2 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Resolver.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Resolver.java @@ -1,7 +1,5 @@ package org.checkerframework.javacutil; -import static com.sun.tools.javac.code.Kinds.PCK; -import static com.sun.tools.javac.code.Kinds.TYP; import static com.sun.tools.javac.code.Kinds.VAR; import java.lang.reflect.Constructor; @@ -19,8 +17,6 @@ import com.sun.source.util.TreePath; import com.sun.source.util.Trees; import com.sun.tools.javac.api.JavacScope; import com.sun.tools.javac.code.Symbol; -import com.sun.tools.javac.code.Symbol.ClassSymbol; -import com.sun.tools.javac.code.Symbol.PackageSymbol; import com.sun.tools.javac.code.Symbol.TypeSymbol; import com.sun.tools.javac.code.Type; import com.sun.tools.javac.comp.AttrContext; @@ -43,32 +39,24 @@ public class Resolver { private final Trees trees; private final Log log; - private static final Method FIND_METHOD; - private static final Method FIND_VAR; - private static final Method FIND_IDENT; - private static final Method FIND_IDENT_IN_TYPE; - private static final Method FIND_IDENT_IN_PACKAGE; - private static final Method FIND_TYPE; + private final Method FIND_METHOD; + private final Method FIND_IDENT_IN_TYPE; + private final Method FIND_IDENT_IN_PACKAGE; + private final Method FIND_TYPE; - private static final Class<?> ACCESSERROR; - // Note that currently access(...) is defined in InvalidSymbolError, a superclass of AccessError - private static final Method ACCESSERROR_ACCESS; + public Resolver(ProcessingEnvironment env) { + Context context = ((JavacProcessingEnvironment) env).getContext(); + this.resolve = Resolve.instance(context); + this.names = Names.instance(context); + this.trees = Trees.instance(env); + this.log = Log.instance(context); - static { try { FIND_METHOD = Resolve.class.getDeclaredMethod("findMethod", Env.class, Type.class, Name.class, List.class, List.class, boolean.class, boolean.class, boolean.class); FIND_METHOD.setAccessible(true); - FIND_VAR = Resolve.class.getDeclaredMethod("findVar", - Env.class, Name.class); - FIND_VAR.setAccessible(true); - - FIND_IDENT = Resolve.class.getDeclaredMethod( - "findIdent", Env.class, Name.class, int.class); - FIND_IDENT.setAccessible(true); - FIND_IDENT_IN_TYPE = Resolve.class.getDeclaredMethod( "findIdentInType", Env.class, Type.class, Name.class, int.class); @@ -88,60 +76,6 @@ public class Resolver { err.initCause(e); throw err; } - - try { - ACCESSERROR = Class.forName("com.sun.tools.javac.comp.Resolve$AccessError"); - ACCESSERROR_ACCESS = ACCESSERROR.getMethod("access", Name.class, TypeSymbol.class); - ACCESSERROR_ACCESS.setAccessible(true); - } catch (ClassNotFoundException e) { - ErrorReporter.errorAbort("Compiler 'Resolve$AccessError' class could not be retrieved.", e); - // Unreachable code - needed so the compiler does not warn about a possibly uninitialized final field. - throw new AssertionError(); - } catch (NoSuchMethodException e) { - ErrorReporter.errorAbort("Compiler 'Resolve$AccessError' class doesn't contain required 'access' method", e); - // Unreachable code - needed so the compiler does not warn about a possibly uninitialized final field. - throw new AssertionError(); - } - } - - public Resolver(ProcessingEnvironment env) { - Context context = ((JavacProcessingEnvironment) env).getContext(); - this.resolve = Resolve.instance(context); - this.names = Names.instance(context); - this.trees = Trees.instance(env); - this.log = Log.instance(context); - } - - /** - * Finds the package with name {@code name}. - * - * @param name - * The name of the package. - * @param path - * The tree path to the local scope. - * @return the {@code PackageSymbol} for the package if it is found, - * {@code null} otherwise - */ - public PackageSymbol findPackage(String name, TreePath path) { - Log.DiagnosticHandler discardDiagnosticHandler = - new Log.DiscardDiagnosticHandler(log); - try { - JavacScope scope = (JavacScope) trees.getScope(path); - Env<AttrContext> env = scope.getEnv(); - Element res = wrapInvocationOnResolveInstance(FIND_IDENT, env, - names.fromString(name), PCK); - // findIdent will return a PackageSymbol even for a symbol that is not a package, - // such as a.b.c.MyClass.myStaticField. "exists()" must be called on it to ensure - // that it exists. - if (res.getKind() == ElementKind.PACKAGE) { - PackageSymbol ps = (PackageSymbol) res; - return ps.exists() ? ps : null; - } else { - return null; - } - } finally { - log.popDiagnosticHandler(discardDiagnosticHandler); - } } /** @@ -158,7 +92,7 @@ public class Resolver { * the field). * @param path * The tree path to the local scope. - * @return the element for the field + * @return The element for the field. */ public VariableElement findField(String name, TypeMirror type, TreePath path) { Log.DiagnosticHandler discardDiagnosticHandler = @@ -166,13 +100,10 @@ public class Resolver { try { JavacScope scope = (JavacScope) trees.getScope(path); Env<AttrContext> env = scope.getEnv(); - Element res = wrapInvocationOnResolveInstance(FIND_IDENT_IN_TYPE, env, type, + Element res = wrapInvocation(FIND_IDENT_IN_TYPE, env, type, names.fromString(name), VAR); if (res.getKind() == ElementKind.FIELD) { return (VariableElement) res; - } else if (res.getKind() == ElementKind.OTHER && ACCESSERROR.isInstance(res)) { - // Return the inaccessible field that was found - return (VariableElement) wrapInvocation(res, ACCESSERROR_ACCESS, null, null); } else { // Most likely didn't find the field and the Element is a SymbolNotFoundError return null; @@ -183,35 +114,6 @@ public class Resolver { } /** - * Finds the local variable with name {@code name} in the given scope. - * - * @param name - * The name of the local variable. - * @param path - * The tree path to the local scope. - * @return the element for the local variable - */ - public VariableElement findLocalVariableOrParameter(String name, TreePath path) { - Log.DiagnosticHandler discardDiagnosticHandler = - new Log.DiscardDiagnosticHandler(log); - try { - JavacScope scope = (JavacScope) trees.getScope(path); - Env<AttrContext> env = scope.getEnv(); - Element res = wrapInvocationOnResolveInstance(FIND_VAR, env, - names.fromString(name)); - if (res.getKind() == ElementKind.LOCAL_VARIABLE - || res.getKind() == ElementKind.PARAMETER) { - return (VariableElement) res; - } else { - // Most likely didn't find the variable and the Element is a SymbolNotFoundError - return null; - } - } finally { - log.popDiagnosticHandler(discardDiagnosticHandler); - } - } - - /** * Finds the class literal with name {@code name}. * * <p> @@ -222,7 +124,7 @@ public class Resolver { * The name of the class. * @param path * The tree path to the local scope. - * @return the element for the class + * @return The element for the class. */ public Element findClass(String name, TreePath path) { Log.DiagnosticHandler discardDiagnosticHandler = @@ -230,37 +132,7 @@ public class Resolver { try { JavacScope scope = (JavacScope) trees.getScope(path); Env<AttrContext> env = scope.getEnv(); - return wrapInvocationOnResolveInstance(FIND_TYPE, env, names.fromString(name)); - } finally { - log.popDiagnosticHandler(discardDiagnosticHandler); - } - } - - /** - * Finds the class with name {@code name} in a given package. - * - * @param name - * The name of the class. - * @param pck - * The PackageSymbol for the package. - * @param path - * The tree path to the local scope. - * @return the {@code ClassSymbol} for the class if it is found, - * {@code null} otherwise - */ - public ClassSymbol findClassInPackage(String name, PackageSymbol pck, TreePath path) { - Log.DiagnosticHandler discardDiagnosticHandler = - new Log.DiscardDiagnosticHandler(log); - try { - JavacScope scope = (JavacScope) trees.getScope(path); - Env<AttrContext> env = scope.getEnv(); - Element res = wrapInvocationOnResolveInstance(FIND_IDENT_IN_PACKAGE, env, pck, - names.fromString(name), TYP); - if (res.getKind() == ElementKind.CLASS) { - return (ClassSymbol) res; - } else { - return null; - } + return wrapInvocation(FIND_TYPE, env, names.fromString(name)); } finally { log.popDiagnosticHandler(discardDiagnosticHandler); } @@ -280,7 +152,7 @@ public class Resolver { * Type of the receiver of the method * @param path * Tree path. - * @return the method element (if found) + * @return The method element (if found). */ public Element findMethod(String methodName, TypeMirror receiverType, TreePath path, java.util.List<TypeMirror> argumentTypes) { @@ -307,7 +179,7 @@ public class Resolver { Object methodContext = buildMethodContext(); Object oldContext = getField(resolve, "currentResolutionContext"); setField(resolve, "currentResolutionContext", methodContext); - Element result = wrapInvocationOnResolveInstance(FIND_METHOD, env, site, name, argtypes, + Element result = wrapInvocation(FIND_METHOD, env, site, name, argtypes, typeargtypes, allowBoxing, useVarargs, operator); setField(resolve, "currentResolutionContext", oldContext); return result; @@ -357,13 +229,9 @@ public class Resolver { return f.get(receiver); } - private Symbol wrapInvocationOnResolveInstance(Method method, Object... args) { - return wrapInvocation(resolve, method, args); - } - - private Symbol wrapInvocation(Object receiver, Method method, Object... args) { + private Symbol wrapInvocation(Method method, Object... args) { try { - return (Symbol) method.invoke(receiver, args); + return (Symbol) method.invoke(resolve, args); } catch (IllegalAccessException e) { Error err = new AssertionError("Unexpected Reflection error"); err.initCause(e); diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TreeUtils.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TreeUtils.java index 5ceb5450dd..a3ae88fa31 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TreeUtils.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TreeUtils.java @@ -18,6 +18,7 @@ import javax.lang.model.util.ElementFilter; import com.sun.source.tree.AnnotatedTypeTree; import com.sun.source.tree.ArrayAccessTree; +import com.sun.source.tree.AssignmentTree; import com.sun.source.tree.BinaryTree; import com.sun.source.tree.BlockTree; import com.sun.source.tree.ClassTree; @@ -29,10 +30,12 @@ import com.sun.source.tree.LiteralTree; import com.sun.source.tree.MemberSelectTree; import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodTree; +import com.sun.source.tree.NewArrayTree; import com.sun.source.tree.NewClassTree; import com.sun.source.tree.ParameterizedTypeTree; import com.sun.source.tree.ParenthesizedTree; import com.sun.source.tree.PrimitiveTypeTree; +import com.sun.source.tree.ReturnTree; import com.sun.source.tree.StatementTree; import com.sun.source.tree.Tree; import com.sun.source.tree.TypeCastTree; @@ -72,28 +75,11 @@ public final class TreeUtils { * @return true iff tree describes a call to super */ public static boolean isSuperCall(MethodInvocationTree tree) { - return isNamedMethodCall("super", tree); - } - - /** - * Checks if the method invocation is a call to this. - * - * @param tree - * a tree defining a method invocation - * - * @return true iff tree describes a call to this - */ - public static boolean isThisCall(MethodInvocationTree tree) { - return isNamedMethodCall("this", tree); - - } - - protected static boolean isNamedMethodCall(String name, MethodInvocationTree tree) { /*@Nullable*/ ExpressionTree mst = tree.getMethodSelect(); assert mst != null; /*nninvariant*/ if (mst.getKind() == Tree.Kind.IDENTIFIER ) { - return ((IdentifierTree)mst).getName().contentEquals(name); + return ((IdentifierTree)mst).getName().contentEquals("super"); } if (mst.getKind() == Tree.Kind.MEMBER_SELECT) { @@ -104,7 +90,7 @@ public final class TreeUtils { } return ((IdentifierTree) selectTree.getExpression()).getName() - .contentEquals(name); + .contentEquals("super"); } return false; @@ -133,22 +119,19 @@ public final class TreeUtils { public static boolean isSelfAccess(final ExpressionTree tree) { ExpressionTree tr = TreeUtils.skipParens(tree); // If method invocation check the method select - if (tr.getKind() == Tree.Kind.ARRAY_ACCESS) { + if (tr.getKind() == Tree.Kind.ARRAY_ACCESS) return false; - } if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) { tr = ((MethodInvocationTree)tree).getMethodSelect(); } tr = TreeUtils.skipParens(tr); - if (tr.getKind() == Tree.Kind.TYPE_CAST) { + if (tr.getKind() == Tree.Kind.TYPE_CAST) tr = ((TypeCastTree)tr).getExpression(); - } tr = TreeUtils.skipParens(tr); - if (tr.getKind() == Tree.Kind.IDENTIFIER) { + if (tr.getKind() == Tree.Kind.IDENTIFIER) return true; - } if (tr.getKind() == Tree.Kind.MEMBER_SELECT) { tr = ((MemberSelectTree)tr).getExpression(); @@ -186,9 +169,8 @@ public final class TreeUtils { while (p != null) { Tree leaf = p.getLeaf(); assert leaf != null; /*nninvariant*/ - if (kinds.contains(leaf.getKind())) { + if (kinds.contains(leaf.getKind())) return leaf; - } p = p.getParentPath(); } @@ -196,7 +178,7 @@ public final class TreeUtils { } /** - * Gets path to the first enclosing class tree, where class is + * Gets path to the the first enclosing class tree, where class is * defined by the classTreeKinds method. * * @param path the path defining the tree node @@ -207,7 +189,7 @@ public final class TreeUtils { } /** - * Gets path to the first enclosing tree of the specified kind. + * Gets path to the the first enclosing tree of the specified kind. * * @param path the path defining the tree node * @param kind the kind of the desired tree @@ -218,7 +200,7 @@ public final class TreeUtils { } /** - * Gets path to the first enclosing tree with any one of the specified kinds. + * Gets path to the the first enclosing tree with any one of the specified kinds. * * @param path the path defining the tree node * @param kinds the set of kinds of the desired tree @@ -230,9 +212,8 @@ public final class TreeUtils { while (p != null) { Tree leaf = p.getLeaf(); assert leaf != null; /*nninvariant*/ - if (kinds.contains(leaf.getKind())) { + if (kinds.contains(leaf.getKind())) return p; - } p = p.getParentPath(); } @@ -251,9 +232,8 @@ public final class TreeUtils { while (p != null) { Tree leaf = p.getLeaf(); - if (treeClass.isInstance(leaf)) { + if (treeClass.isInstance(leaf)) return treeClass.cast(leaf); - } p = p.getParentPath(); } @@ -268,7 +248,7 @@ public final class TreeUtils { * * @param path the path defining the tree node * @return the enclosing class (or interface) as given by the path, or null - * if one does not exist + * if one does not exist. */ public static /*@Nullable*/ ClassTree enclosingClass(final /*@Nullable*/ TreePath path) { return (ClassTree) enclosingOfKind(path, classTreeKinds()); @@ -328,11 +308,10 @@ public final class TreeUtils { /** * Returns the tree with the assignment context for the treePath - * leaf node. (Does not handle pseudo-assignment of an argument to - * a parameter or a receiver expression to a receiver.) + * leaf node. * - * The assignment context for the {@code treePath} is the leaf of its parent, - * if the leaf is one of the following trees: + * The assignment context for the treepath is the most enclosing + * tree of type: * <ul> * <li>AssignmentTree </li> * <li>CompoundAssignmentTree </li> @@ -343,44 +322,30 @@ public final class TreeUtils { * <li>VariableTree</li> * </ul> * - * If the leaf is a ConditionalExpressionTree or ParenthesizedTree, then recur on the leaf. - * - * Otherwise, null is returned. - * - * @return the assignment context as described + * @param treePath + * @return the assignment context as described. */ public static Tree getAssignmentContext(final TreePath treePath) { - TreePath parentPath = treePath.getParentPath(); - - if (parentPath == null) { - return null; - } + TreePath path = treePath.getParentPath(); - Tree parent = parentPath.getLeaf(); - switch (parent.getKind()) { - case PARENTHESIZED: - case CONDITIONAL_EXPRESSION: - return getAssignmentContext(parentPath); - case ASSIGNMENT: - case METHOD_INVOCATION: - case NEW_ARRAY: - case NEW_CLASS: - case RETURN: - case VARIABLE: - return parent; - default: - // 11 Tree.Kinds are CompoundAssignmentTrees, - // so use instanceof rather than listing all 11. - if (parent instanceof CompoundAssignmentTree) { - return parent; - } + if (path == null) return null; - } + Tree node = path.getLeaf(); + if ((node instanceof AssignmentTree) || + (node instanceof CompoundAssignmentTree) || + (node instanceof MethodInvocationTree) || + (node instanceof NewArrayTree) || + (node instanceof NewClassTree) || + (node instanceof ReturnTree) || + (node instanceof VariableTree)) + return node; + return null; } /** * Gets the element for a class corresponding to a declaration. * + * @param node * @return the element for the given class */ public static final TypeElement elementFromDeclaration(ClassTree node) { @@ -391,6 +356,7 @@ public final class TreeUtils { /** * Gets the element for a method corresponding to a declaration. * + * @param node * @return the element for the given method */ public static final ExecutableElement elementFromDeclaration(MethodTree node) { @@ -401,6 +367,7 @@ public final class TreeUtils { /** * Gets the element for a variable corresponding to its declaration. * + * @param node * @return the element for the given variable */ public static final VariableElement elementFromDeclaration(VariableTree node) { @@ -437,7 +404,7 @@ public final class TreeUtils { * Determine whether the given ExpressionTree has an underlying element. * * @param node the ExpressionTree to test - * @return whether the tree refers to an identifier, member select, or method invocation + * @return whether the tree refers to an identifier, member select, or method invocation. */ public static final boolean isUseOfElement(ExpressionTree node) { node = TreeUtils.skipParens(node); @@ -457,11 +424,10 @@ public final class TreeUtils { */ public static final Name methodName(MethodInvocationTree node) { ExpressionTree expr = node.getMethodSelect(); - if (expr.getKind() == Tree.Kind.IDENTIFIER) { + if (expr.getKind() == Tree.Kind.IDENTIFIER) return ((IdentifierTree)expr).getName(); - } else if (expr.getKind() == Tree.Kind.MEMBER_SELECT) { + else if (expr.getKind() == Tree.Kind.MEMBER_SELECT) return ((MemberSelectTree)expr).getIdentifier(); - } ErrorReporter.errorAbort("TreeUtils.methodName: cannot be here: " + node); return null; // dead code } @@ -490,11 +456,10 @@ public final class TreeUtils { Tree first; if (tree.getKind() == Tree.Kind.BLOCK) { BlockTree block = (BlockTree)tree; - if (block.getStatements().isEmpty()) { + if (block.getStatements().isEmpty()) first = block; - } else { + else first = block.getStatements().iterator().next(); - } } else { first = tree; } @@ -504,8 +469,8 @@ public final class TreeUtils { /** * Determine whether the given class contains an explicit constructor. * - * @param node a class tree - * @return true, iff there is an explicit constructor + * @param node A class tree. + * @return True, iff there is an explicit constructor. */ public static boolean hasExplicitConstructor(ClassTree node) { TypeElement elem = TreeUtils.elementFromDeclaration(node); @@ -567,9 +532,8 @@ public final class TreeUtils { */ public static boolean isCompileTimeString(ExpressionTree node) { ExpressionTree tree = TreeUtils.skipParens(node); - if (tree instanceof LiteralTree) { + if (tree instanceof LiteralTree) return true; - } if (TreeUtils.isUseOfElement(tree)) { Element elt = TreeUtils.elementFromUse(tree); @@ -687,9 +651,8 @@ public final class TreeUtils { * of any method that overrides that one. */ public static boolean isMethodInvocation(Tree tree, ExecutableElement method, ProcessingEnvironment env) { - if (!(tree instanceof MethodInvocationTree)) { + if (!(tree instanceof MethodInvocationTree)) return false; - } MethodInvocationTree methInvok = (MethodInvocationTree)tree; ExecutableElement invoked = TreeUtils.elementFromUse(methInvok); return isMethod(invoked, method, env); @@ -724,8 +687,10 @@ public final class TreeUtils { * Determine whether the given expression is either "this" or an outer * "C.this". * - * <p> * TODO: Should this also handle "super"? + * + * @param tree + * @return */ public static final boolean isExplicitThisDereference(ExpressionTree tree) { if (tree.getKind() == Tree.Kind.IDENTIFIER @@ -747,24 +712,7 @@ public final class TreeUtils { } /** - * Determine whether {@code tree} is a class literal, such - * as - * - * <pre> - * <em>Object</em> . <em>class</em> - * </pre> - * - * @return true iff if tree is a class literal - */ - public static boolean isClassLiteral(Tree tree) { - if (tree.getKind() != Tree.Kind.MEMBER_SELECT) { - return false; - } - return "class".equals(((MemberSelectTree) tree).getIdentifier().toString()); - } - - /** - * Determine whether {@code tree} is a field access expressions, such + * Determine whether <code>tree</code> is a field access expressions, such * as * * <pre> @@ -773,7 +721,7 @@ public final class TreeUtils { * </pre> * * @return true iff if tree is a field access expression (implicit or - * explicit) + * explicit). */ public static boolean isFieldAccess(Tree tree) { if (tree.getKind().equals(Tree.Kind.MEMBER_SELECT)) { @@ -792,11 +740,11 @@ public final class TreeUtils { } /** - * Compute the name of the field that the field access {@code tree} - * accesses. Requires {@code tree} to be a field access, as determined - * by {@code isFieldAccess}. + * Compute the name of the field that the field access <code>tree</code> + * accesses. Requires <code>tree</code> to be a field access, as determined + * by <code>isFieldAccess</code>. * - * @return the name of the field accessed by {@code tree}. + * @return The name of the field accessed by <code>tree</code>. */ public static String getFieldName(Tree tree) { assert isFieldAccess(tree); @@ -810,7 +758,7 @@ public final class TreeUtils { } /** - * Determine whether {@code tree} refers to a method element, such + * Determine whether <code>tree</code> refers to a method element, such * as * * <pre> @@ -819,7 +767,7 @@ public final class TreeUtils { * </pre> * * @return true iff if tree is a method access expression (implicit or - * explicit) + * explicit). */ public static boolean isMethodAccess(Tree tree) { if (tree.getKind().equals(Tree.Kind.MEMBER_SELECT)) { @@ -844,11 +792,11 @@ public final class TreeUtils { } /** - * Compute the name of the method that the method access {@code tree} - * accesses. Requires {@code tree} to be a method access, as determined - * by {@code isMethodAccess}. + * Compute the name of the method that the method access <code>tree</code> + * accesses. Requires <code>tree</code> to be a method access, as determined + * by <code>isMethodAccess</code>. * - * @return the name of the method accessed by {@code tree}. + * @return The name of the method accessed by <code>tree</code>. */ public static String getMethodName(Tree tree) { assert isMethodAccess(tree); @@ -893,9 +841,9 @@ public final class TreeUtils { /** * Returns the VariableElement for a field declaration. * - * @param typeName the class where the field is declared - * @param fieldName the name of the field - * @param env the processing environment + * @param typeName the class where the field is declared. + * @param fieldName the name of the field. + * @param env the processing environment. * @return the VariableElement for typeName.fieldName */ public static VariableElement getField(String typeName, String fieldName, ProcessingEnvironment env) { @@ -913,7 +861,7 @@ public final class TreeUtils { * * TODO: is there a nicer way than an instanceof? * - * @param tree the Tree to test + * @param tree the Tree to test. * @return whether the tree is an ExpressionTree */ public static boolean isExpressionTree(Tree tree) { @@ -953,15 +901,4 @@ public final class TreeUtils { return false; } } - - /** - * @see Object#getClass() - * @return true iff invocationTree is an instance of getClass() - */ - public static boolean isGetClassInvocation(MethodInvocationTree invocationTree) { - final Element declarationElement = elementFromUse(invocationTree); - String ownerName = ElementUtils.getQualifiedClassName(declarationElement.getEnclosingElement()).toString(); - return ownerName.equals("java.lang.Object") - && declarationElement.getSimpleName().toString().equals("getClass"); - } } diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TypeAnnotationUtils.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TypeAnnotationUtils.java index c7faceb53a..c166e43f47 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TypeAnnotationUtils.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TypeAnnotationUtils.java @@ -1,7 +1,6 @@ package org.checkerframework.javacutil; import java.lang.reflect.InvocationTargetException; -import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Map; @@ -29,7 +28,6 @@ import com.sun.tools.javac.processing.JavacProcessingEnvironment; import com.sun.tools.javac.tree.JCTree.JCLambda; import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.List; -import com.sun.tools.javac.util.Name; import com.sun.tools.javac.util.Pair; /** @@ -45,60 +43,51 @@ public class TypeAnnotationUtils { /** * Check whether a TypeCompound is contained in a list of TypeCompounds. * - * @param list the input list of TypeCompounds - * @param tc the TypeCompound to find - * @return true, iff a TypeCompound equal to tc is contained in list + * @param list The input list of TypeCompounds. + * @param tc The TypeCompound to find. + * @return true, iff a TypeCompound equal to tc is contained in list. */ public static boolean isTypeCompoundContained(Types types, List<TypeCompound> list, TypeCompound tc) { for (Attribute.TypeCompound rawat : list) { - if (contentEquals(rawat.type.tsym.name, tc.type.tsym.name) && + if (rawat.type.tsym.name.contentEquals(tc.type.tsym.name) && // TODO: in previous line, it would be nicer to use reference equality: // rawat.type == tc.type && // or at least "isSameType": // types.isSameType(rawat.type, tc.type) && // but each fails in some cases. rawat.values.equals(tc.values) && - isSameTAPositionExceptTreePos(rawat.position, tc.position)) { + isSameTAPosition(rawat.position, tc.position)) { return true; } } return false; } - private static boolean contentEquals(Name n1, Name n2) { - // Views of underlying bytes, not copies as with Name#contentEquals - ByteBuffer b1 = ByteBuffer.wrap(n1.getByteArray(), n1.getByteOffset(), n1.getByteLength()); - ByteBuffer b2 = ByteBuffer.wrap(n2.getByteArray(), n2.getByteOffset(), n2.getByteLength()); - - return b1.equals(b2); - } - /** * Compare two TypeAnnotationPositions for equality. * - * @param p1 the first position - * @param p2 the second position - * @return true, iff the two positions are equal + * @param p1 The first position. + * @param p2 The second position. + * @return true, iff the two positions are equal. */ public static boolean isSameTAPosition(TypeAnnotationPosition p1, TypeAnnotationPosition p2) { - return isSameTAPositionExceptTreePos(p1, p2) && p1.pos == p2.pos; - } - - public static boolean isSameTAPositionExceptTreePos(TypeAnnotationPosition p1, - TypeAnnotationPosition p2) { - return p1.isValidOffset == p2.isValidOffset && - p1.bound_index == p2.bound_index && - p1.exception_index == p2.exception_index && - p1.location.equals(p2.location) && - Arrays.equals(p1.lvarIndex, p2.lvarIndex) && - Arrays.equals(p1.lvarLength, p2.lvarLength) && - Arrays.equals(p1.lvarOffset, p2.lvarOffset) && - p1.offset == p2.offset && - p1.onLambda == p2.onLambda && - p1.parameter_index == p2.parameter_index && - p1.type == p2.type && - p1.type_index == p2.type_index; + if (p1.isValidOffset == p2.isValidOffset && + p1.bound_index == p2.bound_index && + p1.exception_index == p2.exception_index && + p1.location.equals(p2.location) && + Arrays.equals(p1.lvarIndex, p2.lvarIndex) && + Arrays.equals(p1.lvarLength, p2.lvarLength) && + Arrays.equals(p1.lvarOffset, p2.lvarOffset) && + p1.offset == p2.offset && + p1.onLambda == p2.onLambda && + p1.parameter_index == p2.parameter_index && + p1.pos == p2.pos && + p1.type == p2.type && + p1.type_index == p2.type_index) { + return true; + } + return false; } /** @@ -106,7 +95,7 @@ public class TypeAnnotationUtils { * argument AnnotationMirror. * * @param am an AnnotationMirror, which may be part of an AST or an internally - * created subclass + * created subclass. * @return a new Attribute.Compound corresponding to the AnnotationMirror */ public static Attribute.Compound createCompoundFromAnnotationMirror(ProcessingEnvironment env, @@ -127,8 +116,8 @@ public class TypeAnnotationUtils { * argument AnnotationMirror. * * @param am an AnnotationMirror, which may be part of an AST or an internally - * created subclass - * @param tapos the type annotation position to use + * created subclass. + * @param tapos the type annotation position to use. * @return a new Attribute.TypeCompound corresponding to the AnnotationMirror */ public static Attribute.TypeCompound createTypeCompoundFromAnnotationMirror(ProcessingEnvironment env, @@ -148,9 +137,9 @@ public class TypeAnnotationUtils { * Returns a newly created Attribute corresponding to an argument * AnnotationValue. * - * @param meth the ExecutableElement that is assigned the value, needed for empty arrays + * @param meth the ExecutableElement that is assigned the value, needed for empty arrays. * @param av an AnnotationValue, which may be part of an AST or an internally - * created subclass + * created subclass. * @return a new Attribute corresponding to the AnnotationValue */ public static Attribute attributeFromAnnotationValue(ProcessingEnvironment env, ExecutableElement meth, AnnotationValue av) { @@ -306,15 +295,16 @@ public class TypeAnnotationUtils { * Catch all exceptions and abort if one occurs - the reflection code should * never break once fully debugged. * - * @param tc the TAPCall abstraction to encapsulate two methods - * @return the created TypeAnnotationPosition + * @param ver The SourceVersion to decide what API to use. + * @param tc The TAPCall abstraction to encapsulate two methods. + * @return The created TypeAnnotationPosition. */ private static <RET> RET call8or9(Call8or9<RET> tc) { try { boolean hasNine; try { hasNine = SourceVersion.valueOf("RELEASE_9") != null; - } catch (IllegalArgumentException iae) { + } catch(IllegalArgumentException iae) { hasNine = false; } if (hasNine) { @@ -323,7 +313,7 @@ public class TypeAnnotationUtils { boolean hasEight; try { hasEight = SourceVersion.valueOf("RELEASE_8") != null; - } catch (IllegalArgumentException iae) { + } catch(IllegalArgumentException iae) { hasEight = false; } if (hasEight) { @@ -589,15 +579,12 @@ public class TypeAnnotationUtils { TypeAnnotationPosition.class.getField("bound_index").set(res, tapos.bound_index); res.exception_index = tapos.exception_index; res.location = List.from(tapos.location); - if (tapos.lvarIndex != null) { + if (tapos.lvarIndex != null) res.lvarIndex = Arrays.copyOf(tapos.lvarIndex, tapos.lvarIndex.length); - } - if (tapos.lvarLength != null) { + if (tapos.lvarLength != null) res.lvarLength = Arrays.copyOf(tapos.lvarLength, tapos.lvarLength.length); - } - if (tapos.lvarOffset != null) { + if (tapos.lvarOffset != null) res.lvarOffset = Arrays.copyOf(tapos.lvarOffset, tapos.lvarOffset.length); - } res.offset = tapos.offset; TypeAnnotationPosition.class.getField("onLambda").set(res, tapos.onLambda); TypeAnnotationPosition.class.getField("parameter_index").set(res, tapos.parameter_index); @@ -608,7 +595,7 @@ public class TypeAnnotationUtils { } public static Type unannotatedType(final Type in) { - return call8or9( + return call8or9( new Call8or9<Type>() { @Override public Type call8() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { @@ -624,4 +611,4 @@ public class TypeAnnotationUtils { ); } -} +}
\ No newline at end of file diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TypesUtils.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TypesUtils.java index 949bf1362f..f024a60feb 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TypesUtils.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TypesUtils.java @@ -1,18 +1,8 @@ package org.checkerframework.javacutil; -import com.sun.tools.javac.code.Symtab; -import com.sun.tools.javac.code.Type; -import com.sun.tools.javac.code.TypeTag; -import com.sun.tools.javac.model.JavacTypes; -import com.sun.tools.javac.processing.JavacProcessingEnvironment; -import com.sun.tools.javac.util.Context; - import javax.annotation.processing.ProcessingEnvironment; -import javax.lang.model.element.Element; import javax.lang.model.element.Name; -import javax.lang.model.element.NestingKind; import javax.lang.model.element.TypeElement; -import javax.lang.model.element.TypeParameterElement; import javax.lang.model.type.ArrayType; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind; @@ -22,7 +12,12 @@ import javax.lang.model.type.WildcardType; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; -import static com.sun.tools.javac.code.TypeTag.WILDCARD; +import com.sun.tools.javac.code.Symtab; +import com.sun.tools.javac.code.Type; +import com.sun.tools.javac.code.TypeTag; +import com.sun.tools.javac.model.JavacTypes; +import com.sun.tools.javac.processing.JavacProcessingEnvironment; +import com.sun.tools.javac.util.Context; /** * A utility class that helps with {@link TypeMirror}s. @@ -98,13 +93,12 @@ public final class TypesUtils { */ public static boolean isDeclaredOfName(TypeMirror type, CharSequence qualifiedName) { return type.getKind() == TypeKind.DECLARED - && getQualifiedName((DeclaredType) type).contentEquals(qualifiedName); + && getQualifiedName((DeclaredType)type).contentEquals(qualifiedName); } public static boolean isBoxedPrimitive(TypeMirror type) { - if (type.getKind() != TypeKind.DECLARED) { + if (type.getKind() != TypeKind.DECLARED) return false; - } String qualifiedName = getQualifiedName((DeclaredType)type).toString(); @@ -124,29 +118,17 @@ public final class TypesUtils { DeclaredType dt = (DeclaredType) type; TypeElement elem = (TypeElement) dt.asElement(); Name name = elem.getQualifiedName(); - if ("java.lang.Throwable".contentEquals(name)) { + if ("java.lang.Throwable".contentEquals(name)) return true; - } type = elem.getSuperclass(); } return false; } /** - * Returns true iff the argument is an anonymous type. - * - * @return whether the argument is an anonymous type - */ - public static boolean isAnonymous(TypeMirror type) { - return (type instanceof DeclaredType) - && (((TypeElement) ((DeclaredType) type).asElement()).getNestingKind() - .equals(NestingKind.ANONYMOUS)); - } - - /** * Returns true iff the argument is a primitive type. * - * @return whether the argument is a primitive type + * @return whether the argument is a primitive type */ public static boolean isPrimitive(TypeMirror type) { switch (type.getKind()) { @@ -295,27 +277,8 @@ public final class TypesUtils { return type; } - /** - * Get the type parameter for this wildcard from the underlying type's bound field - * This field is sometimes null, in that case this method will return null - * @return the TypeParameterElement the wildcard is an argument to - */ - public static TypeParameterElement wildcardToTypeParam(final Type.WildcardType wildcard) { - - final Element typeParamElement; - if (wildcard.bound != null) { - typeParamElement = wildcard.bound.asElement(); - } else { - typeParamElement = null; - } - - return (TypeParameterElement) typeParamElement; - } - - /** - * Version of com.sun.tools.javac.code.Types.wildUpperBound(Type) - * that works with both jdk8 (called upperBound there) and jdk8u. - */ + // Version of com.sun.tools.javac.code.Types.wildUpperBound(Type) + // that works with both jdk8 (called upperBound there) and jdk8u. // TODO: contrast to upperBound. public static Type wildUpperBound(ProcessingEnvironment env, TypeMirror tm) { Type t = (Type) tm; @@ -328,27 +291,13 @@ public final class TypesUtils { } else { return wildUpperBound(env, w.type); } - } else { + } + else { return TypeAnnotationUtils.unannotatedType(t); } } /** - * Version of com.sun.tools.javac.code.Types.wildLowerBound(Type) - * that works with both jdk8 (called upperBound there) and jdk8u. - */ - public static Type wildLowerBound(ProcessingEnvironment env, TypeMirror tm) { - Type t = (Type) tm; - if (t.hasTag(WILDCARD)) { - Context context = ((JavacProcessingEnvironment) env).getContext(); - Symtab syms = Symtab.instance(context); - Type.WildcardType w = (Type.WildcardType) TypeAnnotationUtils.unannotatedType(t); - return w.isExtendsBound() ? syms.botType : wildLowerBound(env, w.type); - } else { - return t.unannotatedType(); - } - } - /** * Returns the {@link TypeMirror} for a given {@link Class}. */ public static TypeMirror typeFromClass(Types types, Elements elements, Class<?> clazz) { @@ -378,69 +327,4 @@ public final class TypesUtils { JavacTypes t = (JavacTypes) types; return t.getArrayType(componentType); } - - /** - * Returns true if declaredType is a Class that is used to box primitive type - * (e.g. declaredType=java.lang.Double and primitiveType=22.5d ) - */ - public static boolean isBoxOf(TypeMirror declaredType, TypeMirror primitiveType) { - if (declaredType.getKind() != TypeKind.DECLARED) { - return false; - } - - final String qualifiedName = getQualifiedName((DeclaredType) declaredType).toString(); - switch (primitiveType.getKind()) { - case BOOLEAN: return qualifiedName.equals("java.lang.Boolean"); - case BYTE: return qualifiedName.equals("java.lang.Byte"); - case CHAR: return qualifiedName.equals("java.lang.Character"); - case DOUBLE: return qualifiedName.equals("java.lang.Double"); - case FLOAT: return qualifiedName.equals("java.lang.Float"); - case INT: return qualifiedName.equals("java.lang.Integer"); - case LONG: return qualifiedName.equals("java.lang.Long"); - case SHORT: return qualifiedName.equals("java.lang.Short"); - - default: - return false; - } - } - - /** - * Given a bounded type (wildcard or typevar) get the concrete type of its upper bound. If - * the bounded type extends other bounded types, this method will iterate through their bounds - * until a class, interface, or intersection is found. - * @return a type that is not a wildcard or typevar, or null if this type is an unbounded wildcard - */ - public static TypeMirror findConcreteUpperBound(final TypeMirror boundedType) { - TypeMirror effectiveUpper = boundedType; - outerLoop : while (true) { - switch (effectiveUpper.getKind()) { - case WILDCARD: - effectiveUpper = ((javax.lang.model.type.WildcardType) effectiveUpper).getExtendsBound(); - if (effectiveUpper == null) { - return null; - } - break; - - case TYPEVAR: - effectiveUpper = ((TypeVariable) effectiveUpper).getUpperBound(); - break; - - default: - break outerLoop; - } - } - return effectiveUpper; - } - - /** - * Returns true if the erased type of subtype is a subtype of the erased type of supertype. - * - * @param types Types - * @param subtype possible subtype - * @param supertype possible supertype - * @return true if the erased type of subtype is a subtype of the erased type of supertype - */ - public static boolean isErasedSubtype(Types types, TypeMirror subtype, TypeMirror supertype) { - return types.isSubtype(types.erasure(subtype), types.erasure(supertype)); - } } diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/dist/ManualTaglet.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/dist/ManualTaglet.java index e7df75ff54..ade653e13b 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/dist/ManualTaglet.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/dist/ManualTaglet.java @@ -103,15 +103,13 @@ public class ManualTaglet implements Taglet { @Override public String toString(Tag[] tags) { - if (tags.length == 0) { + if (tags.length == 0) return ""; - } StringBuilder sb = new StringBuilder(); for (Tag t : tags) { String[] split = t.text().split(" ", 2); - if (t != tags[0]) { + if (t != tags[0]) sb.append(", "); - } sb.append(formatLink(split)); } return formatHeader(sb.toString()); diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/trees/TreeParser.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/trees/TreeParser.java index 1ac9d09309..ea7f67f69e 100644 --- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/trees/TreeParser.java +++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/trees/TreeParser.java @@ -84,9 +84,8 @@ public class TreeParser { return maker.Literal(false); } - if (Character.isLetter(token.charAt(0))) { + if (Character.isLetter(token.charAt(0))) return maker.Ident(names.fromString(token)); - } Object value = null; try { @@ -113,9 +112,8 @@ public class TreeParser { while (!")".equals(token)) { JCExpression arg = parseExpression(); args.append(arg); - if (",".equals(token)) { + if (",".equals(token)) nextToken(); - } } // For now, handle empty args only assert ")".equals(token); |