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);
}
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()));
}
}
}
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.");
}
}
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()));
}
}
Aggregations