Search in sources :

Example 1 with SynchronizedStatementTree

use of org.sonar.plugins.java.api.tree.SynchronizedStatementTree in project sonar-java by SonarSource.

the class JavaTreeModelTest method synchronized_statement.

/**
 * 14.19. The synchronized Statement
 */
@Test
public void synchronized_statement() {
    SynchronizedStatementTree tree = (SynchronizedStatementTree) firstMethodFirstStatement("class T { void m() { synchronized(e) { } } }");
    assertThat(tree.is(Tree.Kind.SYNCHRONIZED_STATEMENT)).isTrue();
    assertThat(tree.synchronizedKeyword().text()).isEqualTo("synchronized");
    assertThat(tree.openParenToken().text()).isEqualTo("(");
    assertThat(tree.expression()).isNotNull();
    assertThat(tree.closeParenToken().text()).isEqualTo(")");
    assertThat(tree.block()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 5);
}
Also used : SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree) Test(org.junit.Test)

Example 2 with SynchronizedStatementTree

use of org.sonar.plugins.java.api.tree.SynchronizedStatementTree in project sonar-java by SonarSource.

the class SynchronizedFieldAssignmentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    SynchronizedStatementTree sst = (SynchronizedStatementTree) tree;
    if (sst.expression().is(Kind.NEW_CLASS)) {
        reportIssue(tree, "Synchronizing on a new instance is a no-op.");
        return;
    }
    Symbol field = getField(sst.expression());
    if (field != null) {
        sst.block().accept(new AssignmentVisitor(field, sst.expression()));
    } else {
        Symbol parameter = getParam(sst.expression());
        if (parameter != null) {
            reportIssue(tree, String.format("\"%s\" is a method parameter, and should not be used for synchronization.", parameter.name()));
        }
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree)

Example 3 with SynchronizedStatementTree

use of org.sonar.plugins.java.api.tree.SynchronizedStatementTree in project sonar-java by SonarSource.

the class SynchronizationOnStringOrBoxedCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    SynchronizedStatementTree syncStatement = (SynchronizedStatementTree) tree;
    Type expressionType = syncStatement.expression().symbolType();
    if (expressionType.isPrimitive() || isForbiddenType(expressionType)) {
        reportIssue(syncStatement.expression(), "Synchronize on a new \"Object\" instead.");
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree)

Example 4 with SynchronizedStatementTree

use of org.sonar.plugins.java.api.tree.SynchronizedStatementTree in project sonar-java by SonarSource.

the class ValueBasedObjectUsedForLockCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ExpressionTree expression = ((SynchronizedStatementTree) tree).expression();
    Type expressionType = expression.symbolType();
    if (isValueBasedType(expressionType)) {
        reportIssue(expression, String.format("Synchronize on a non-value-based object; synchronizing on a \"%s\" could lead to contention.%s", expressionType.name(), context.getJavaVersion().java8CompatibilityMessage()));
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree)

Aggregations

SynchronizedStatementTree (org.sonar.plugins.java.api.tree.SynchronizedStatementTree)4 Type (org.sonar.plugins.java.api.semantic.Type)2 Test (org.junit.Test)1 JavaSymbol (org.sonar.java.resolve.JavaSymbol)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1