use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class ExpressionUtilsTest method test_skip_parenthesis.
@Test
public void test_skip_parenthesis() 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(0);
ExpressionTree parenthesis = ((ReturnStatementTree) methodTree.block().body().get(0)).expression();
assertThat(parenthesis.is(Tree.Kind.PARENTHESIZED_EXPRESSION)).isTrue();
ExpressionTree skipped = ExpressionUtils.skipParentheses(parenthesis);
assertThat(skipped.is(Tree.Kind.CONDITIONAL_AND)).isTrue();
assertThat(ExpressionUtils.skipParentheses(((BinaryExpressionTree) skipped).leftOperand()).is(Tree.Kind.IDENTIFIER)).isTrue();
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class ExpressionUtilsTest method method_name.
@Test
public void method_name() throws Exception {
File file = new File("src/test/files/model/ExpressionUtilsMethodNameTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(0);
MethodInvocationTree firstMIT = (MethodInvocationTree) ((ExpressionStatementTree) methodTree.block().body().get(0)).expression();
MethodInvocationTree secondMIT = (MethodInvocationTree) ((ExpressionStatementTree) methodTree.block().body().get(1)).expression();
assertThat(ExpressionUtils.methodName(firstMIT).name()).isEqualTo("foo");
assertThat(ExpressionUtils.methodName(secondMIT).name()).isEqualTo("foo");
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class TypeSubstitutionSolverTest method type_hierarchy_visit_should_be_limited.
@Test
public void type_hierarchy_visit_should_be_limited() {
ParametrizedTypeCache parametrizedTypeCache = new ParametrizedTypeCache();
BytecodeCompleter bytecodeCompleter = new BytecodeCompleter(new SquidClassLoader(new ArrayList<>()), parametrizedTypeCache);
Symbols symbols = new Symbols(bytecodeCompleter);
ActionParser<Tree> parser = JavaParser.createParser();
SemanticModel semanticModel = new SemanticModel(bytecodeCompleter);
Resolve resolve = new Resolve(symbols, bytecodeCompleter, parametrizedTypeCache);
TypeAndReferenceSolver typeAndReferenceSolver = new TypeAndReferenceSolver(semanticModel, symbols, resolve, parametrizedTypeCache);
CompilationUnitTree tree = (CompilationUnitTree) parser.parse(new File("src/test/files/sym/ComplexHierarchy.java"));
new FirstPass(semanticModel, symbols, resolve, parametrizedTypeCache, typeAndReferenceSolver).visitCompilationUnit(tree);
typeAndReferenceSolver.visitCompilationUnit(tree);
ClassTree classTree = (ClassTree) tree.types().get(tree.types().size() - 1);
JavaType site = (JavaType) classTree.symbol().type();
MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) ((MethodTree) classTree.members().get(0)).block().body().get(0)).expression();
TypeSubstitutionSolver typeSubstitutionSolver = Mockito.spy(new TypeSubstitutionSolver(parametrizedTypeCache, symbols));
// call with empty formals should return.
typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(new ArrayList<>(), site);
verify(typeSubstitutionSolver, times(0)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
JavaSymbol.MethodJavaSymbol methodJavaSymbol = (JavaSymbol.MethodJavaSymbol) mit.symbol();
typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(((MethodJavaType) methodJavaSymbol.type).argTypes, site);
verify(typeSubstitutionSolver, times(11)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class IterableIteratorCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ReturnThis returnThis = new ReturnThis();
ClassTree classTree = (ClassTree) tree;
Type classType = classTree.symbol().type();
if (!(classType.isSubtypeOf("java.util.Iterator") && classType.isSubtypeOf("java.lang.Iterable"))) {
return;
}
classTree.members().stream().filter(m -> m.is(Tree.Kind.METHOD)).filter(m -> ITERATOR.matches(((MethodTree) m))).forEach(t -> t.accept(returnThis));
if (!returnThis.issueLocations.isEmpty()) {
reportIssue(returnThis.issueLocations.get(0), "Refactor this code so that the Iterator supports multiple traversal", returnThis.issueLocations.stream().skip(1).map(t -> new JavaFileScannerContext.Location("", t)).collect(Collectors.toList()), null);
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class IncorrectOrderOfMembersCheck method visitClass.
@Override
public void visitClass(ClassTree tree) {
int prev = 0;
for (int i = 0; i < tree.members().size(); i++) {
final Tree member = tree.members().get(i);
final int priority;
IdentifierTree identifier;
if (member.is(Tree.Kind.VARIABLE)) {
priority = 0;
identifier = ((VariableTree) member).simpleName();
} else if (member.is(Tree.Kind.CONSTRUCTOR)) {
priority = 1;
identifier = ((MethodTree) member).simpleName();
} else if (member.is(Tree.Kind.METHOD)) {
priority = 2;
identifier = ((MethodTree) member).simpleName();
} else {
continue;
}
if (priority < prev) {
context.reportIssue(this, identifier, "Move this " + NAMES[priority] + " to comply with Java Code Conventions.");
} else {
prev = priority;
}
}
super.visitClass(tree);
}
Aggregations