Search in sources :

Example 36 with ClassSymbol

use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.

the class LiteralClassName method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (!CLASS_NAME.matches(tree, state)) {
        return NO_MATCH;
    }
    String className = constValue(getOnlyElement(tree.getArguments()), String.class);
    if (className == null) {
        return NO_MATCH;
    }
    if (className.startsWith("[")) {
        // TODO(cushon): consider handling arrays
        return NO_MATCH;
    }
    Type type = state.getTypeFromString(className);
    if (type == null) {
        return NO_MATCH;
    }
    ClassSymbol owner = getSymbol(state.findEnclosing(ClassTree.class));
    Enter enter = Enter.instance(state.context);
    if (!Resolve.instance(state.context).isAccessible(enter.getEnv(owner), type.tsym)) {
        return NO_MATCH;
    }
    SuggestedFix.Builder fix = SuggestedFix.builder();
    String replaceWith = String.format("%s.class", qualifyType(state, fix, state.getTypes().erasure(type)));
    if (state.getPath().getParentPath().getLeaf().getKind() == Tree.Kind.EXPRESSION_STATEMENT) {
        fix.addStaticImport("java.util.Objects.requireNonNull");
        replaceWith = String.format("requireNonNull(%s)", replaceWith);
    }
    fix.replace(tree, replaceWith);
    return describeMatch(tree, fix.build());
}
Also used : SuggestedFixes.qualifyType(com.google.errorprone.fixes.SuggestedFixes.qualifyType) Type(com.sun.tools.javac.code.Type) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassTree(com.sun.source.tree.ClassTree) Enter(com.sun.tools.javac.comp.Enter)

Example 37 with ClassSymbol

use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.

the class HeldLockAnalyzer method handleMonitorGuards.

private static HeldLockSet handleMonitorGuards(VisitorState state, HeldLockSet locks) {
    JCNewClass newClassTree = ASTHelpers.findEnclosingNode(state.getPath(), JCNewClass.class);
    if (newClassTree == null) {
        return locks;
    }
    Symbol clazzSym = ASTHelpers.getSymbol(newClassTree.clazz);
    if (!(clazzSym instanceof ClassSymbol)) {
        return locks;
    }
    if (!((ClassSymbol) clazzSym).fullname.contentEquals(MONITOR_GUARD_CLASS)) {
        return locks;
    }
    Optional<GuardedByExpression> lockExpression = GuardedByBinder.bindExpression(Iterables.getOnlyElement(newClassTree.getArguments()), state);
    if (!lockExpression.isPresent()) {
        return locks;
    }
    return locks.plus(lockExpression.get());
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass)

Example 38 with ClassSymbol

use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.

the class ImmutableAnnotationChecker method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    ClassSymbol symbol = getSymbol(tree);
    if (symbol == null || symbol.isAnnotationType() || !WellKnownMutability.isAnnotation(state, symbol.type)) {
        return NO_MATCH;
    }
    if (ASTHelpers.hasAnnotation(symbol, Immutable.class, state)) {
        AnnotationTree annotation = ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Immutable");
        if (annotation != null) {
            state.reportMatch(buildDescription(annotation).setMessage(ANNOTATED_ANNOTATION_MESSAGE).addFix(SuggestedFix.delete(annotation)).build());
        } else {
            state.reportMatch(buildDescription(tree).setMessage(ANNOTATED_ANNOTATION_MESSAGE).build());
        }
    }
    Violation info = new ImmutableAnalysis(this, state, "annotations should be immutable, and cannot have non-final fields", "annotations should be immutable").checkForImmutability(Optional.of(tree), ImmutableSet.of(), getType(tree));
    if (!info.isPresent()) {
        return NO_MATCH;
    }
    String message = "annotations should be immutable: " + info.message();
    return buildDescription(tree).setMessage(message).build();
}
Also used : Violation(com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 39 with ClassSymbol

use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.

the class ImmutableChecker method checkSubtype.

// Strong behavioural subtyping
/** Check for classes without {@code @Immutable} that have immutable supertypes. */
private Description checkSubtype(ClassTree tree, VisitorState state) {
    ClassSymbol sym = ASTHelpers.getSymbol(tree);
    if (sym == null) {
        return Description.NO_MATCH;
    }
    Type superType = immutableSupertype(sym, state);
    if (superType == null) {
        return Description.NO_MATCH;
    }
    String message = String.format("Class extends @Immutable type %s, but is not annotated as immutable", superType);
    Fix fix = SuggestedFix.builder().prefixWith(tree, "@Immutable ").addImport(Immutable.class.getName()).build();
    return buildDescription(tree).setMessage(message).addFix(fix).build();
}
Also used : Type(com.sun.tools.javac.code.Type) Fix(com.google.errorprone.fixes.Fix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

Example 40 with ClassSymbol

use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.

the class UTemplater method visitIdentifier.

@Override
public UExpression visitIdentifier(IdentifierTree tree, Void v) {
    Symbol sym = ASTHelpers.getSymbol(tree);
    if (sym instanceof ClassSymbol) {
        return UClassIdent.create((ClassSymbol) sym);
    } else if (sym != null && sym.isStatic()) {
        return staticMember(sym);
    } else if (freeVariables.containsKey(tree.getName().toString())) {
        VarSymbol symbol = freeVariables.get(tree.getName().toString());
        checkState(symbol == sym);
        UExpression ident = UFreeIdent.create(tree.getName());
        Matches matches = ASTHelpers.getAnnotation(symbol, Matches.class);
        if (matches != null) {
            ident = UMatches.create(getValue(matches), true, ident);
        }
        NotMatches notMatches = ASTHelpers.getAnnotation(symbol, NotMatches.class);
        if (notMatches != null) {
            ident = UMatches.create(getValue(notMatches), false, ident);
        }
        OfKind hasKind = ASTHelpers.getAnnotation(symbol, OfKind.class);
        if (hasKind != null) {
            EnumSet<Kind> allowed = EnumSet.copyOf(Arrays.asList(hasKind.value()));
            ident = UOfKind.create(ident, ImmutableSet.copyOf(allowed));
        }
        // @Repeated annotations need to be checked last.
        Repeated repeated = ASTHelpers.getAnnotation(symbol, Repeated.class);
        if (repeated != null) {
            ident = URepeated.create(tree.getName(), ident);
        }
        return ident;
    }
    if (sym == null) {
        return UTypeVarIdent.create(tree.getName());
    }
    switch(sym.getKind()) {
        case TYPE_PARAMETER:
            return UTypeVarIdent.create(tree.getName());
        default:
            return ULocalVarIdent.create(tree.getName());
    }
}
Also used : Matches(com.google.errorprone.refaster.annotation.Matches) NotMatches(com.google.errorprone.refaster.annotation.NotMatches) OfKind(com.google.errorprone.refaster.annotation.OfKind) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Kind(com.sun.source.tree.Tree.Kind) OfKind(com.google.errorprone.refaster.annotation.OfKind) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Repeated(com.google.errorprone.refaster.annotation.Repeated) NotMatches(com.google.errorprone.refaster.annotation.NotMatches)

Aggregations

ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)49 Symbol (com.sun.tools.javac.code.Symbol)20 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)20 Type (com.sun.tools.javac.code.Type)17 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)10 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)9 ClassType (com.sun.tools.javac.code.Type.ClassType)9 ClassTree (com.sun.source.tree.ClassTree)8 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)8 MethodTree (com.sun.source.tree.MethodTree)6 Tree (com.sun.source.tree.Tree)6 ArrayType (com.sun.tools.javac.code.Type.ArrayType)5 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)4 VariableTree (com.sun.source.tree.VariableTree)4 Name (com.sun.tools.javac.util.Name)4 ArrayList (java.util.ArrayList)4 Violation (com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation)3 BlockTree (com.sun.source.tree.BlockTree)3 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)3 ForLoopTree (com.sun.source.tree.ForLoopTree)3