use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class ExpressionUtilsTest method test_simple_assignments.
@Test
public void test_simple_assignments() throws Exception {
File file = new File("src/test/java/org/sonar/java/model/ExpressionUtilsTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(1);
List<AssignmentExpressionTree> assignments = findAssignmentExpressionTrees(methodTree);
assertThat(assignments).hasSize(4);
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(0))).isTrue();
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(1))).isTrue();
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(2))).isFalse();
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(3))).isFalse();
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class UtilityClassWithPublicConstructorCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
if (!hasSemantic() || !isUtilityClass(classTree) || isPrivateInnerClass(classTree)) {
return;
}
boolean hasImplicitPublicConstructor = true;
for (MethodTree explicitConstructor : getExplicitConstructors(classTree)) {
hasImplicitPublicConstructor = false;
if (isPublicConstructor(explicitConstructor)) {
reportIssue(explicitConstructor.simpleName(), "Hide this public constructor.");
}
}
if (hasImplicitPublicConstructor) {
reportIssue(classTree.simpleName(), "Add a private constructor to hide the implicit public one.");
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class RequestMappingMethodPublicCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
Symbol.MethodSymbol methodSymbol = methodTree.symbol();
if (isClassController(methodSymbol) && isRequestMappingAnnotated(methodSymbol) && !methodSymbol.isPublic()) {
reportIssue(methodTree.simpleName(), "Make this method \"public\".");
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class TwoLocksWaitCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
if (tree.is(METHOD, CONSTRUCTOR)) {
MethodTree methodTree = (MethodTree) tree;
int initialCounter = findModifier(methodTree.modifiers(), SYNCHRONIZED).map(m -> 1).orElse(0);
synchronizedStack.push(new Counter(initialCounter));
findWaitInvocation(methodTree);
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class WriteObjectTheOnlySynchronizedMethodCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
Symbol.TypeSymbol enclosingClass = methodTree.symbol().enclosingClass();
String className = enclosingClass.type().fullyQualifiedName();
MethodMatcher writeObjectMatcher = SerializableContract.writeObjectMatcher(className);
if (writeObjectMatcher.matches(methodTree) && hasModifier(methodTree.modifiers(), SYNCHRONIZED)) {
SynchronizationVisitor visitor = new SynchronizationVisitor(methodTree);
enclosingClass.declaration().accept(visitor);
if (!visitor.moreThanSingleLock) {
reportIssue(ModifiersUtils.getModifier(methodTree.modifiers(), SYNCHRONIZED), "Remove this \"synchronized\" keyword.");
}
}
}
Aggregations