use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class SpringConstructorInjectionCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
if (isClassTreeAnnotatedWith(classTree, "org.springframework.stereotype.Controller", "org.springframework.stereotype.Repository", "org.springframework.stereotype.Service")) {
List<Tree> toReport = classTree.members().stream().filter(SpringConstructorInjectionCheck::isMemberAutowired).map(SpringConstructorInjectionCheck::toReportTree).collect(Collectors.toList());
if (!toReport.isEmpty()) {
int cost = toReport.size();
// find constructor
classTree.members().stream().filter(m -> m.is(Kind.CONSTRUCTOR)).map(m -> ((MethodTree) m).simpleName()).findFirst().ifPresent(toReport::add);
reportIssue(toReport.get(0), "Remove this annotation and use constructor injection instead.", toReport.stream().skip(1).map(i -> new JavaFileScannerContext.Location("", i)).collect(Collectors.toList()), cost);
}
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaParserTest method receiver_type_should_be_parsed.
@Test
public void receiver_type_should_be_parsed() throws Exception {
try {
String code = "class Main { class Inner { Inner(Main Main.this) {}};}";
CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse(code);
Tree constructor = ((ClassTree) ((ClassTree) cut.types().get(0)).members().get(0)).members().get(0);
assertThat(constructor).isInstanceOf(MethodTree.class);
assertThat(((MethodTree) constructor).parameters().get(0).simpleName().name()).isEqualTo("this");
} catch (Exception ex) {
fail("Receiver type of inner classes should be parsed correctly", ex);
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project JavaForFun by gumartinm.
the class ParameterCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree method = (MethodTree) tree;
if (method.parameters().size() == 1) {
MethodSymbol symbol = method.symbol();
Type firstParameterType = symbol.parameterTypes().get(0);
Type returnType = symbol.returnType().type();
if (returnType.is(firstParameterType.fullyQualifiedName())) {
reportIssue(method.simpleName(), "Remove this method");
}
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class SelectorMethodArgumentCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
if (Boolean.TRUE.equals(methodTree.isOverriding())) {
return;
}
List<Symbol> booleanParameterSymbols = getBooleanParametersAsSymbol(methodTree.parameters());
BlockTree blockTree = methodTree.block();
if (isPublic(methodTree) && blockTree != null && !booleanParameterSymbols.isEmpty()) {
for (Symbol variable : booleanParameterSymbols) {
Collection<IdentifierTree> usages = variable.usages();
if (usages.size() == 1) {
blockTree.accept(new ConditionalStatementVisitor(variable.name(), Iterables.get(usages, 0), methodTree));
}
}
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class StandardFunctionalInterfaceCheck method lookupMatchingStandardInterface.
private static Optional<String> lookupMatchingStandardInterface(MethodSymbol functionalMethod) {
MethodTree declaration = functionalMethod.declaration();
if (!functionalMethod.thrownTypes().isEmpty() || (declaration != null && !declaration.typeParameters().isEmpty())) {
return Optional.empty();
}
Type returnType = declaration != null ? declaration.returnType().symbolType() : functionalMethod.returnType().type();
return STD_INTERFACE_BY_PARAMETER_COUNT.getOrDefault(functionalMethod.parameterTypes().size(), Collections.emptyList()).stream().map(standardInterface -> standardInterface.matchingSpecialization(functionalMethod, returnType)).filter(Objects::nonNull).findFirst();
}
Aggregations