Search in sources :

Example 1 with Filter

use of com.sun.tools.javac.util.Filter in project error-prone by google.

the class ImmutableAnalysis method areFieldsImmutable.

/**
   * Check a single class' fields for immutability.
   *
   * @param immutableTyParams the in-scope immutable type parameters
   * @param classType the type to check the fields of
   */
Violation areFieldsImmutable(Optional<ClassTree> tree, ImmutableSet<String> immutableTyParams, ClassType classType) {
    ClassSymbol classSym = (ClassSymbol) classType.tsym;
    if (classSym.members() == null) {
        return Violation.absent();
    }
    Filter<Symbol> instanceFieldFilter = new Filter<Symbol>() {

        @Override
        public boolean accepts(Symbol symbol) {
            return symbol.getKind() == ElementKind.FIELD && !symbol.isStatic();
        }
    };
    Map<Symbol, Tree> declarations = new HashMap<>();
    if (tree.isPresent()) {
        for (Tree member : tree.get().getMembers()) {
            Symbol sym = ASTHelpers.getSymbol(member);
            if (sym != null) {
                declarations.put(sym, member);
            }
        }
    }
    // javac gives us members in reverse declaration order
    // handling them in declaration order leads to marginally better diagnostics
    List<Symbol> members = ImmutableList.copyOf(classSym.members().getSymbols(instanceFieldFilter)).reverse();
    for (Symbol member : members) {
        Optional<Tree> memberTree = Optional.fromNullable(declarations.get(member));
        Violation info = isFieldImmutable(memberTree, immutableTyParams, classSym, classType, (VarSymbol) member);
        if (info.isPresent()) {
            return info;
        }
    }
    return Violation.absent();
}
Also used : Filter(com.sun.tools.javac.util.Filter) HashMap(java.util.HashMap) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 2 with Filter

use of com.sun.tools.javac.util.Filter in project error-prone by google.

the class ASTHelpers method findMatchingMethods.

/**
 * Finds all methods in any superclass of {@code startClass} with a certain {@code name} that
 * match the given {@code predicate}.
 *
 * @return The (possibly empty) set of methods in any superclass that match {@code predicate} and
 *     have the given {@code name}.
 */
public static Set<MethodSymbol> findMatchingMethods(Name name, final Predicate<MethodSymbol> predicate, Type startClass, Types types) {
    Filter<Symbol> matchesMethodPredicate = sym -> sym instanceof MethodSymbol && predicate.apply((MethodSymbol) sym);
    Set<MethodSymbol> matchingMethods = new HashSet<>();
    // Iterate over all classes and interfaces that startClass inherits from.
    for (Type superClass : types.closure(startClass)) {
        // Iterate over all the methods declared in superClass.
        TypeSymbol superClassSymbol = superClass.tsym;
        Scope superClassSymbols = superClassSymbol.members();
        if (superClassSymbols != null) {
            // Can be null if superClass is a type variable
            for (Symbol symbol : superClassSymbols.getSymbolsByName(name, matchesMethodPredicate, NON_RECURSIVE)) {
                // By definition of the filter, we know that the symbol is a MethodSymbol.
                matchingMethods.add((MethodSymbol) symbol);
            }
        }
    }
    return matchingMethods;
}
Also used : CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) Arrays(java.util.Arrays) ClassType(com.sun.tools.javac.code.Type.ClassType) Nullness(com.google.errorprone.dataflow.nullnesspropagation.Nullness) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) Log(com.sun.tools.javac.util.Log) Matcher(com.google.errorprone.matchers.Matcher) JarURLConnection(java.net.JarURLConnection) ForLoopTree(com.sun.source.tree.ForLoopTree) EnumSet(java.util.EnumSet) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Filter(com.sun.tools.javac.util.Filter) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) TreePath(com.sun.source.util.TreePath) JCLiteral(com.sun.tools.javac.tree.JCTree.JCLiteral) Compound(com.sun.tools.javac.code.Attribute.Compound) Symbol(com.sun.tools.javac.code.Symbol) Set(java.util.Set) MemberSelectTree(com.sun.source.tree.MemberSelectTree) TreeScanner(com.sun.source.util.TreeScanner) Stream(java.util.stream.Stream) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) Predicate(com.google.common.base.Predicate) Attribute(com.sun.tools.javac.code.Attribute) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) AutoValue(com.google.auto.value.AutoValue) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) AnnotationValue(javax.lang.model.element.AnnotationValue) Flags(com.sun.tools.javac.code.Flags) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) Scope(com.sun.tools.javac.code.Scope) ReturnTree(com.sun.source.tree.ReturnTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) ArrayList(java.util.ArrayList) VisitorState(com.google.errorprone.VisitorState) Kind(com.sun.source.tree.Tree.Kind) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) Tree(com.sun.source.tree.Tree) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) SimpleAnnotationValueVisitor8(javax.lang.model.util.SimpleAnnotationValueVisitor8) Enter(com.sun.tools.javac.comp.Enter) ExpressionTree(com.sun.source.tree.ExpressionTree) CharMatcher(com.google.common.base.CharMatcher) IOException(java.io.IOException) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Types(com.sun.tools.javac.code.Types) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) ArrayDeque(java.util.ArrayDeque) NON_RECURSIVE(com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) PackageTree(com.sun.source.tree.PackageTree) JCAnnotatedType(com.sun.tools.javac.tree.JCTree.JCAnnotatedType) TypeVar(com.sun.tools.javac.code.Type.TypeVar) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TypeTag(com.sun.tools.javac.code.TypeTag) JCMemberReference(com.sun.tools.javac.tree.JCTree.JCMemberReference) IdentifierTree(com.sun.source.tree.IdentifierTree) URI(java.net.URI) ImmutableSet(com.google.common.collect.ImmutableSet) DeferredDiagnosticHandler(com.sun.tools.javac.util.Log.DeferredDiagnosticHandler) SwitchTree(com.sun.source.tree.SwitchTree) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) JavacTrees(com.sun.tools.javac.api.JavacTrees) List(java.util.List) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) Suppliers(com.google.errorprone.suppliers.Suppliers) Annotation(java.lang.annotation.Annotation) ModifiersTree(com.sun.source.tree.ModifiersTree) Optional(java.util.Optional) WhileLoopTree(com.sun.source.tree.WhileLoopTree) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Name(com.sun.tools.javac.util.Name) Type(com.sun.tools.javac.code.Type) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) Deque(java.util.Deque) HashSet(java.util.HashSet) JCPackageDecl(com.sun.tools.javac.tree.JCTree.JCPackageDecl) JUNIT4_RUN_WITH_ANNOTATION(com.google.errorprone.matchers.JUnitMatchers.JUNIT4_RUN_WITH_ANNOTATION) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) ElementKind(javax.lang.model.element.ElementKind) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) JCTree(com.sun.tools.javac.tree.JCTree) NullnessAnalysis(com.google.errorprone.dataflow.nullnesspropagation.NullnessAnalysis) JUnitMatchers(com.google.errorprone.matchers.JUnitMatchers) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) Resolve(com.sun.tools.javac.comp.Resolve) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) ClassType(com.sun.tools.javac.code.Type.ClassType) JCAnnotatedType(com.sun.tools.javac.tree.JCTree.JCAnnotatedType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Scope(com.sun.tools.javac.code.Scope) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 3 with Filter

use of com.sun.tools.javac.util.Filter in project error-prone by google.

the class ImmutableAnalysis method areFieldsImmutable.

/**
 * Check a single class' fields for immutability.
 *
 * @param immutableTyParams the in-scope immutable type parameters
 * @param classType the type to check the fields of
 */
Violation areFieldsImmutable(Optional<ClassTree> tree, ImmutableSet<String> immutableTyParams, ClassType classType, ViolationReporter reporter) {
    ClassSymbol classSym = (ClassSymbol) classType.tsym;
    if (classSym.members() == null) {
        return Violation.absent();
    }
    Filter<Symbol> instanceFieldFilter = new Filter<Symbol>() {

        @Override
        public boolean accepts(Symbol symbol) {
            return symbol.getKind() == ElementKind.FIELD && !symbol.isStatic();
        }
    };
    Map<Symbol, Tree> declarations = new HashMap<>();
    if (tree.isPresent()) {
        for (Tree member : tree.get().getMembers()) {
            Symbol sym = ASTHelpers.getSymbol(member);
            if (sym != null) {
                declarations.put(sym, member);
            }
        }
    }
    // javac gives us members in reverse declaration order
    // handling them in declaration order leads to marginally better diagnostics
    List<Symbol> members = ImmutableList.copyOf(classSym.members().getSymbols(instanceFieldFilter)).reverse();
    for (Symbol member : members) {
        Optional<Tree> memberTree = Optional.ofNullable(declarations.get(member));
        Violation info = isFieldImmutable(memberTree, immutableTyParams, classSym, classType, (VarSymbol) member, reporter);
        if (info.isPresent()) {
            return info;
        }
    }
    return Violation.absent();
}
Also used : Violation(com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation) Filter(com.sun.tools.javac.util.Filter) HashMap(java.util.HashMap) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Aggregations

ClassTree (com.sun.source.tree.ClassTree)3 Tree (com.sun.source.tree.Tree)3 Symbol (com.sun.tools.javac.code.Symbol)3 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)3 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)3 Filter (com.sun.tools.javac.util.Filter)3 AutoValue (com.google.auto.value.AutoValue)1 CharMatcher (com.google.common.base.CharMatcher)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Predicate (com.google.common.base.Predicate)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 VisitorState (com.google.errorprone.VisitorState)1 Violation (com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation)1 Nullness (com.google.errorprone.dataflow.nullnesspropagation.Nullness)1 NullnessAnalysis (com.google.errorprone.dataflow.nullnesspropagation.NullnessAnalysis)1 JUnitMatchers (com.google.errorprone.matchers.JUnitMatchers)1 JUNIT4_RUN_WITH_ANNOTATION (com.google.errorprone.matchers.JUnitMatchers.JUNIT4_RUN_WITH_ANNOTATION)1 Matcher (com.google.errorprone.matchers.Matcher)1