use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class MethodNameSameAsClassCheck method visitClass.
@Override
public void visitClass(ClassTree tree) {
super.visitClass(tree);
IdentifierTree classSimpleName = tree.simpleName();
if (classSimpleName == null) {
return;
}
String className = classSimpleName.name();
for (Tree member : tree.members()) {
if (member.is(Tree.Kind.METHOD)) {
IdentifierTree simpleName = ((MethodTree) member).simpleName();
if (className.equals(simpleName.name())) {
context.reportIssue(this, simpleName, "Rename this method to prevent any misunderstanding or make it a constructor.");
}
}
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class ExplodedGraphWalker method execute.
private void execute(MethodTree tree) {
CFG cfg = CFG.build(tree);
exitBlock = cfg.exitBlock();
checkerDispatcher.init(tree, cfg);
liveVariables = LiveVariables.analyze(cfg);
explodedGraph = new ExplodedGraph();
methodTree = tree;
constraintManager = new ConstraintManager();
workList = new LinkedList<>();
// Linked hashSet is required to guarantee order of yields to be generated
endOfExecutionPath = new LinkedHashSet<>();
if (DEBUG_MODE_ACTIVATED) {
LOG.debug("Exploring Exploded Graph for method " + tree.simpleName().name() + " at line " + ((JavaTree) tree).getLine());
}
programState = ProgramState.EMPTY_STATE;
steps = 0;
for (ProgramState startingState : startingStates(tree, programState)) {
enqueue(new ProgramPoint(cfg.entry()), startingState);
}
while (!workList.isEmpty()) {
steps++;
if (steps > maxSteps()) {
throwMaxSteps(tree);
}
// LIFO:
setNode(workList.removeFirst());
CFG.Block block = (CFG.Block) programPosition.block;
if (block.successors().isEmpty()) {
endOfExecutionPath.add(node);
continue;
}
try {
Tree terminator = block.terminator();
if (programPosition.i < block.elements().size()) {
// process block element
visit(block.elements().get(programPosition.i), terminator);
} else if (terminator == null) {
// process block exit, which is unconditional jump such as goto-statement or return-statement
handleBlockExit(programPosition);
} else if (programPosition.i == block.elements().size()) {
// process block exist, which is conditional jump such as if-statement
checkerDispatcher.executeCheckPostStatement(terminator);
} else {
// process branch
// process block exist, which is conditional jump such as if-statement
checkerDispatcher.executeCheckPreStatement(terminator);
handleBlockExit(programPosition);
}
} catch (TooManyNestedBooleanStatesException e) {
throwTooManyBooleanStates(tree, e);
} catch (RelationalSymbolicValue.TransitiveRelationExceededException e) {
throwTooManyTransitiveRelationsException(tree, e);
}
}
handleEndOfExecutionPath(false);
checkerDispatcher.executeCheckEndOfExecution();
// Cleanup:
workList = null;
node = null;
programState = null;
constraintManager = null;
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class SecondPass method complete.
private void complete(JavaSymbol.MethodJavaSymbol symbol) {
MethodTree methodTree = symbol.declaration;
Resolve.Env env = semanticModel.getEnv(symbol);
completeTypeParameters(methodTree.typeParameters(), env);
ImmutableList.Builder<JavaType> thrownTypes = ImmutableList.builder();
for (TypeTree throwClause : methodTree.throwsClauses()) {
JavaType thrownType = resolveType(env, throwClause);
if (thrownType != null) {
thrownTypes.add(thrownType);
}
}
JavaType returnType = null;
List<JavaType> argTypes = Lists.newArrayList();
// no return type for constructor
if (!CONSTRUCTOR_NAME.equals(symbol.name)) {
returnType = resolveType(env, methodTree.returnType());
if (returnType != null) {
symbol.returnType = returnType.symbol;
}
} else if (!symbol.enclosingClass().isStatic()) {
JavaSymbol owner = symbol.enclosingClass().owner();
if (!owner.isPackageSymbol()) {
// JLS8 - 8.8.1 & 8.8.9 : constructors of inner class have an implicit first arg of its directly enclosing class type
argTypes.add(owner.enclosingClass().type);
}
}
List<VariableTree> parametersTree = methodTree.parameters();
List<JavaSymbol> scopeSymbols = symbol.parameters.scopeSymbols();
for (int i = 0; i < parametersTree.size(); i += 1) {
VariableTree variableTree = parametersTree.get(i);
JavaSymbol param = scopeSymbols.get(i);
if (variableTree.simpleName().name().equals(param.getName())) {
param.complete();
argTypes.add(param.getType());
}
if (((VariableTreeImpl) variableTree).isVararg()) {
symbol.flags |= Flags.VARARGS;
}
}
MethodJavaType methodType = new MethodJavaType(argTypes, returnType, thrownTypes.build(), (JavaSymbol.TypeJavaSymbol) symbol.owner);
symbol.setMethodType(methodType);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class ParameterNullnessCheck method reportIssue.
private void reportIssue(Tree syntaxNode, ExpressionTree argument, JavaSymbol.MethodJavaSymbol methodSymbol) {
String declarationMessage = "constructor declaration";
if (!methodSymbol.isConstructor()) {
declarationMessage = "method '" + methodSymbol.getName() + "' declaration";
}
String message = String.format("Annotate the parameter with @javax.annotation.Nullable in %s, or make sure that null can not be passed as argument.", declarationMessage);
Tree reportTree;
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
reportTree = ExpressionUtils.methodName((MethodInvocationTree) syntaxNode);
} else {
reportTree = ((NewClassTree) syntaxNode).identifier();
}
Flow.Builder secondaryBuilder = Flow.builder();
MethodTree declarationTree = methodSymbol.declaration();
if (declarationTree != null) {
secondaryBuilder.add(new JavaFileScannerContext.Location(StringUtils.capitalize(declarationMessage) + ".", declarationTree.simpleName()));
}
secondaryBuilder.add(new JavaFileScannerContext.Location("Argument can be null.", argument));
reportIssue(reportTree, message, Collections.singleton(secondaryBuilder.build()));
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaPropertiesHelperTest method firstExpression.
private ExpressionTree firstExpression(String code) {
CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse("class A { " + code + "}");
SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
ClassTree firstType = (ClassTree) compilationUnitTree.types().get(0);
StatementTree firstStatement = ((MethodTree) firstType.members().get(0)).block().body().get(0);
return ((ExpressionStatementTree) firstStatement).expression();
}
Aggregations