Search in sources :

Example 26 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.

the class ImmutableAnalysis method checkSuper.

private Violation checkSuper(ImmutableSet<String> immutableTyParams, ClassType type) {
    ClassType superType = (ClassType) state.getTypes().supertype(type);
    if (superType.getKind() == TypeKind.NONE || state.getTypes().isSameType(state.getSymtab().objectType, superType)) {
        return Violation.absent();
    }
    if (WellKnownMutability.isAnnotation(state, type)) {
        // TODO(b/25630189): add enforcement
        return Violation.absent();
    }
    AnnotationInfo superannotation = getImmutableAnnotation(superType.tsym, state);
    String message = String.format("'%s' extends '%s'", threadSafety.getPrettyName(type.tsym), threadSafety.getPrettyName(superType.tsym));
    if (superannotation != null) {
        // If the superclass does happen to be immutable, we don't need to recursively
        // inspect it. We just have to check that it's instantiated correctly:
        Violation info = threadSafety.checkSuperInstantiation(immutableTyParams, superannotation, superType);
        if (!info.isPresent()) {
            return Violation.absent();
        }
        return info.plus(message);
    }
    // Recursive case: check if the supertype is 'effectively' immutable.
    Violation info = checkForImmutability(Optional.<ClassTree>empty(), immutableTyParams, superType, new ViolationReporter() {

        @Override
        public Description.Builder describe(Tree tree, Violation info) {
            return BugChecker.buildDescriptionFromChecker(tree, bugChecker).setMessage(info.plus(info.message()).message());
        }
    });
    if (!info.isPresent()) {
        return Violation.absent();
    }
    return info.plus(message);
}
Also used : Violation(com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ClassType(com.sun.tools.javac.code.Type.ClassType)

Example 27 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.

the class ImmutableAnalysis method checkForImmutability.

/**
 * Check that an {@code @Immutable}-annotated class:
 *
 * <ul>
 *   <li>does not declare or inherit any mutable fields,
 *   <li>any immutable supertypes are instantiated with immutable type arguments as required by
 *       their containerOf spec, and
 *   <li>any enclosing instances are immutable.
 * </ul>
 *
 * requiring supertypes to be annotated immutable would be too restrictive.
 */
public Violation checkForImmutability(Optional<ClassTree> tree, ImmutableSet<String> immutableTyParams, ClassType type, ViolationReporter reporter) {
    Violation info = areFieldsImmutable(tree, immutableTyParams, type, reporter);
    if (info.isPresent()) {
        return info;
    }
    for (Type interfaceType : state.getTypes().interfaces(type)) {
        AnnotationInfo interfaceAnnotation = getImmutableAnnotation(interfaceType.tsym, state);
        if (interfaceAnnotation == null) {
            continue;
        }
        info = threadSafety.checkSuperInstantiation(immutableTyParams, interfaceAnnotation, interfaceType);
        if (info.isPresent()) {
            return info.plus(String.format("'%s' extends '%s'", threadSafety.getPrettyName(type.tsym), threadSafety.getPrettyName(interfaceType.tsym)));
        }
    }
    if (!type.asElement().isEnum()) {
        // don't check enum super types here to avoid double-reporting errors
        info = checkSuper(immutableTyParams, type);
        if (info.isPresent()) {
            return info;
        }
    }
    Type mutableEnclosing = threadSafety.mutableEnclosingInstance(tree, type);
    if (mutableEnclosing != null) {
        return info.plus(String.format("'%s' has mutable enclosing instance '%s'", threadSafety.getPrettyName(type.tsym), mutableEnclosing));
    }
    return Violation.absent();
}
Also used : Violation(com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation) ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type)

Example 28 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.

the class ImmutableAnalysis method isFieldImmutable.

/**
 * Check a single field for immutability.
 */
private Violation isFieldImmutable(Optional<Tree> tree, ImmutableSet<String> immutableTyParams, ClassSymbol classSym, ClassType classType, VarSymbol var, ViolationReporter reporter) {
    if (bugChecker.isSuppressed(var)) {
        return Violation.absent();
    }
    if (!var.getModifiers().contains(Modifier.FINAL) && !ASTHelpers.hasAnnotation(var, LazyInit.class, state)) {
        Violation info = Violation.of(String.format("'%s' has non-final field '%s'", threadSafety.getPrettyName(classSym), var.getSimpleName()));
        if (tree.isPresent()) {
            // If we have a tree to attach diagnostics to, report the error immediately instead of
            // accumulating the path to the error from the top-level class being checked
            state.reportMatch(reporter.report(tree.get(), info, SuggestedFixes.addModifiers(tree.get(), state, Modifier.FINAL)));
            return Violation.absent();
        }
        return info;
    }
    Type varType = state.getTypes().memberType(classType, var);
    Violation info = threadSafety.isThreadSafeType(/* allowContainerTypeParameters= */
    true, immutableTyParams, varType);
    if (info.isPresent()) {
        info = info.plus(String.format("'%s' has field '%s' of type '%s'", threadSafety.getPrettyName(classSym), var.getSimpleName(), varType));
        if (tree.isPresent()) {
            // If we have a tree to attach diagnostics to, report the error immediately instead of
            // accumulating the path to the error from the top-level class being checked
            state.reportMatch(reporter.report(tree.get(), info, Optional.empty()));
            return Violation.absent();
        }
        return info;
    }
    return Violation.absent();
}
Also used : Violation(com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation) ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type)

Example 29 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project glide by bumptech.

the class ProcessorUtil method findClassValuesFromAnnotationOnClassAsNames.

Set<String> findClassValuesFromAnnotationOnClassAsNames(Element clazz, Class<? extends Annotation> annotationClass) {
    String annotationClassName = annotationClass.getName();
    AnnotationValue excludedModuleAnnotationValue = null;
    for (AnnotationMirror annotationMirror : clazz.getAnnotationMirrors()) {
        // instead. This check is necessary because a given class may have multiple Annotations.
        if (!annotationClassName.equals(annotationMirror.getAnnotationType().toString())) {
            continue;
        }
        Set<? extends Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> values = annotationMirror.getElementValues().entrySet();
        // (usually value).
        if (values.size() != 1) {
            throw new IllegalArgumentException("Expected single value, but found: " + values);
        }
        excludedModuleAnnotationValue = values.iterator().next().getValue();
        if (excludedModuleAnnotationValue == null || excludedModuleAnnotationValue instanceof Attribute.UnresolvedClass) {
            throw new IllegalArgumentException("Failed to find value for: " + annotationClass + " from mirrors: " + clazz.getAnnotationMirrors());
        }
    }
    if (excludedModuleAnnotationValue == null) {
        return Collections.emptySet();
    }
    Object value = excludedModuleAnnotationValue.getValue();
    if (value instanceof List) {
        List<?> values = (List<?>) value;
        Set<String> result = new HashSet<>(values.size());
        for (Object current : values) {
            result.add(getExcludedModuleClassFromAnnotationAttribute(clazz, current));
        }
        return result;
    } else {
        ClassType classType = (ClassType) value;
        return Collections.singleton(classType.toString());
    }
}
Also used : Attribute(com.sun.tools.javac.code.Attribute) ClassType(com.sun.tools.javac.code.Type.ClassType) AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 30 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project ceylon by eclipse.

the class T6889255 method getExpectedName.

String getExpectedName(VarSymbol v, int i) {
    // synthetic method
    if (((v.owner.owner.flags() & Flags.ENUM) != 0) && v.owner.name.toString().equals("valueOf"))
        return "name";
    // -- no Code attribute for the LocalVariableTable attribute
    if ((v.owner.owner.flags() & Flags.INTERFACE) != 0)
        return "arg" + (i - 1);
    // -- no Code attribute for the LocalVariableTable attribute
    if ((v.owner.flags() & Flags.ABSTRACT) != 0)
        return "arg" + (i - 1);
    // bridge methods use xN
    if ((v.owner.flags() & Flags.BRIDGE) != 0)
        return "x" + (i - 1);
    // The rest of this method assumes the local conventions in the test program
    Type t = v.type;
    String s;
    if (t.tag == TypeTags.CLASS)
        s = ((ClassType) t).tsym.name.toString();
    else
        s = t.toString();
    return String.valueOf(Character.toLowerCase(s.charAt(0))) + i;
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type) ClassType(com.sun.tools.javac.code.Type.ClassType)

Aggregations

ClassType (com.sun.tools.javac.code.Type.ClassType)34 Type (com.sun.tools.javac.code.Type)21 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)10 ArrayType (com.sun.tools.javac.code.Type.ArrayType)10 Symbol (com.sun.tools.javac.code.Symbol)9 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)9 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)9 WildcardType (com.sun.tools.javac.code.Type.WildcardType)9 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)8 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)7 MethodType (com.sun.tools.javac.code.Type.MethodType)7 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)7 ArrayList (java.util.ArrayList)5 ClassTree (com.sun.source.tree.ClassTree)4 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)4 Violation (com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation)3 MethodTree (com.sun.source.tree.MethodTree)3 NewClassTree (com.sun.source.tree.NewClassTree)3 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)3 AttrContext (com.sun.tools.javac.comp.AttrContext)3