Search in sources :

Example 1 with SynchronizedTree

use of com.sun.source.tree.SynchronizedTree in project error-prone by google.

the class StaticGuardedByInstance method matchSynchronized.

@Override
public Description matchSynchronized(SynchronizedTree tree, VisitorState state) {
    Symbol lock = ASTHelpers.getSymbol(stripParentheses(tree.getExpression()));
    if (!(lock instanceof VarSymbol)) {
        return Description.NO_MATCH;
    }
    if (lock.isStatic()) {
        return Description.NO_MATCH;
    }
    Multimap<VarSymbol, Tree> writes = WriteVisitor.scan(tree.getBlock());
    for (Entry<VarSymbol, Tree> write : writes.entries()) {
        if (!write.getKey().isStatic()) {
            continue;
        }
        state.reportMatch(buildDescription(write.getValue()).setMessage(String.format(MESSAGE, lock)).build());
    }
    return Description.NO_MATCH;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) ExpressionTree(com.sun.source.tree.ExpressionTree) UnaryTree(com.sun.source.tree.UnaryTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewClassTree(com.sun.source.tree.NewClassTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) Tree(com.sun.source.tree.Tree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 2 with SynchronizedTree

use of com.sun.source.tree.SynchronizedTree in project error-prone by google.

the class Matchers method inSynchronized.

/**
 * Matches if this Tree is enclosed by either a synchronized block or a synchronized method.
 */
public static final <T extends Tree> Matcher<T> inSynchronized() {
    return new Matcher<T>() {

        @Override
        public boolean matches(T tree, VisitorState state) {
            SynchronizedTree synchronizedTree = ASTHelpers.findEnclosingNode(state.getPath(), SynchronizedTree.class);
            if (synchronizedTree != null) {
                return true;
            }
            MethodTree methodTree = ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class);
            return methodTree != null && methodTree.getModifiers().getFlags().contains(Modifier.SYNCHRONIZED);
        }
    };
}
Also used : SynchronizedTree(com.sun.source.tree.SynchronizedTree) InstanceMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.InstanceMethodMatcher) AnyMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.AnyMethodMatcher) StaticMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.StaticMethodMatcher) ConstructorMatcher(com.google.errorprone.matchers.method.MethodMatchers.ConstructorMatcher) VisitorState(com.google.errorprone.VisitorState) MethodTree(com.sun.source.tree.MethodTree)

Example 3 with SynchronizedTree

use of com.sun.source.tree.SynchronizedTree in project error-prone by google.

the class DoubleCheckedLocking method findDCL.

/**
 * Matches an instance of DCL. The canonical pattern is:
 *
 * <pre>{@code
 * if ($X == null) {
 *   synchronized (...) {
 *     if ($X == null) {
 *       ...
 *     }
 *     ...
 *   }
 * }
 * }</pre>
 *
 * Gaps before the synchronized or inner 'if' statement are ignored, and the operands in the
 * null-checks are accepted in either order.
 */
@Nullable
static DCLInfo findDCL(IfTree outerIf) {
    // TODO(cushon): Optional.ifPresent...
    ExpressionTree outerIfTest = getNullCheckedExpression(outerIf.getCondition());
    if (outerIfTest == null) {
        return null;
    }
    SynchronizedTree synchTree = getChild(outerIf.getThenStatement(), SynchronizedTree.class);
    if (synchTree == null) {
        return null;
    }
    IfTree innerIf = getChild(synchTree.getBlock(), IfTree.class);
    if (innerIf == null) {
        return null;
    }
    ExpressionTree innerIfTest = getNullCheckedExpression(innerIf.getCondition());
    if (innerIfTest == null) {
        return null;
    }
    Symbol outerSym = ASTHelpers.getSymbol(outerIfTest);
    if (!Objects.equals(outerSym, ASTHelpers.getSymbol(innerIfTest))) {
        return null;
    }
    if (!(outerSym instanceof VarSymbol)) {
        return null;
    }
    VarSymbol var = (VarSymbol) outerSym;
    return DCLInfo.create(outerIf, synchTree, innerIf, var);
}
Also used : SynchronizedTree(com.sun.source.tree.SynchronizedTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) ExpressionTree(com.sun.source.tree.ExpressionTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) IfTree(com.sun.source.tree.IfTree) Nullable(javax.annotation.Nullable)

Aggregations

SynchronizedTree (com.sun.source.tree.SynchronizedTree)3 ExpressionTree (com.sun.source.tree.ExpressionTree)2 Symbol (com.sun.tools.javac.code.Symbol)2 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)2 VisitorState (com.google.errorprone.VisitorState)1 AnyMethodMatcher (com.google.errorprone.matchers.method.MethodMatchers.AnyMethodMatcher)1 ConstructorMatcher (com.google.errorprone.matchers.method.MethodMatchers.ConstructorMatcher)1 InstanceMethodMatcher (com.google.errorprone.matchers.method.MethodMatchers.InstanceMethodMatcher)1 StaticMethodMatcher (com.google.errorprone.matchers.method.MethodMatchers.StaticMethodMatcher)1 AssignmentTree (com.sun.source.tree.AssignmentTree)1 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)1 IfTree (com.sun.source.tree.IfTree)1 MethodTree (com.sun.source.tree.MethodTree)1 NewClassTree (com.sun.source.tree.NewClassTree)1 Tree (com.sun.source.tree.Tree)1 UnaryTree (com.sun.source.tree.UnaryTree)1 Nullable (javax.annotation.Nullable)1