use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaAstScannerTest method should_swallow_log_and_report_checks_exceptions_for_symbolic_execution.
@Test
public void should_swallow_log_and_report_checks_exceptions_for_symbolic_execution() {
JavaAstScanner scanner = new JavaAstScanner(JavaParser.createParser(), null);
logTester.clear();
SonarComponents sonarComponent = new SonarComponents(null, context.fileSystem(), null, null, null, null);
context.setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
sonarComponent.setSensorContext(context);
scanner.setVisitorBridge(new VisitorsBridge(Collections.singletonList(new SECheck() {
@Override
public void init(MethodTree methodTree, CFG cfg) {
throw new NullPointerException("nobody expect the spanish inquisition !");
}
}), new ArrayList<>(), sonarComponent, SymbolicExecutionMode.ENABLED_WITHOUT_X_FILE));
scanner.scan(ImmutableList.of(new File("src/test/resources/se/MethodBehavior.java")));
assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(1);
assertThat(logTester.logs(LoggerLevel.ERROR).get(0)).startsWith("Unable to run check class org.sonar.java.se.SymbolicExecutionVisitor");
assertThat(sonarComponent.analysisErrors).hasSize(1);
assertThat(sonarComponent.analysisErrors.get(0).getKind()).isSameAs(AnalysisError.Kind.SE_ERROR);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class UCFGJavaVisitor method buildUCfg.
private UCFG buildUCfg(MethodTree methodTree, CFG cfg) {
String signature = signatureFor(methodTree.symbol());
IdentifierGenerator idGenerator = new IdentifierGenerator(methodTree);
UCFGBuilder builder = UCFGBuilder.createUCFGForMethod(signature);
methodTree.parameters().stream().map(p -> idGenerator.lookupIdFor(p.symbol())).map(UCFGBuilder::variableWithId).forEach(builder::addMethodParam);
BlockBuilder entryBlockBuilder = buildBasicBlock(cfg.entry(), methodTree, idGenerator);
if (getAnnotatedStringParameters(methodTree).count() > 0) {
builder.addStartingBlock(buildParameterAnnotationsBlock(methodTree, idGenerator, cfg));
builder.addBasicBlock(entryBlockBuilder);
} else {
builder.addStartingBlock(entryBlockBuilder);
}
cfg.blocks().stream().filter(b -> !b.equals(cfg.entry())).forEach(b -> builder.addBasicBlock(buildBasicBlock(b, methodTree, idGenerator)));
return builder.build();
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class UCFGJavaVisitor method buildBasicBlock.
private UCFGBuilder.BlockBuilder buildBasicBlock(CFG.Block javaBlock, MethodTree methodTree, IdentifierGenerator idGenerator) {
UCFGBuilder.BlockBuilder blockBuilder = UCFGBuilder.newBasicBlock(String.valueOf(javaBlock.id()), location(javaBlock));
javaBlock.elements().forEach(e -> buildCall(e, blockBuilder, idGenerator));
Tree terminator = javaBlock.terminator();
if (terminator != null && terminator.is(Tree.Kind.RETURN_STATEMENT)) {
ExpressionTree returnedExpression = ((ReturnStatementTree) terminator).expression();
Expression retExpr = constant(IdentifierGenerator.CONST);
if (methodTree.returnType() != null && isString(methodTree.returnType().symbolType())) {
retExpr = idGenerator.lookupExpressionFor(returnedExpression);
}
blockBuilder.ret(retExpr, location(terminator));
return blockBuilder;
}
Set<CFG.Block> successors = javaBlock.successors();
if (!successors.isEmpty()) {
blockBuilder.jumpTo(successors.stream().map(b -> UCFGBuilder.createLabel(Integer.toString(b.id()))).toArray(Label[]::new));
return blockBuilder;
}
Preconditions.checkState(javaBlock.id() == 0);
blockBuilder.ret(constant("implicit return"), location(methodTree.lastToken()));
return blockBuilder;
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class CompareToNotOverloadedCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
if (hasSemantic() && isCompareToMethod(methodTree) && Boolean.FALSE.equals(methodTree.isOverriding())) {
ClassJavaType ownerType = (ClassJavaType) methodTree.symbol().owner().type();
ownerType.superTypes().stream().filter(supertype -> supertype.is("java.lang.Comparable")).findFirst().ifPresent(comparableType -> {
String name = "Object";
if (comparableType.isParameterized()) {
ParametrizedTypeJavaType ptjt = (ParametrizedTypeJavaType) comparableType;
name = ptjt.substitution(ptjt.typeParameters().get(0)).symbol().name();
}
reportIssue(methodTree.parameters().get(0), "Refactor this method so that its argument is of type '" + name + "'.");
});
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class DeadStoreCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
if (methodTree.block() == null) {
return;
}
// TODO(npe) Exclude try statements with finally as CFG is incorrect for those and lead to false positive
if (hasTryFinallyWithLocalVar(methodTree.block(), methodTree.symbol())) {
return;
}
Symbol.MethodSymbol methodSymbol = methodTree.symbol();
CFG cfg = CFG.build(methodTree);
LiveVariables liveVariables = LiveVariables.analyze(cfg);
// Liveness analysis provides information only for block boundaries, so we should do analysis between elements within blocks
for (CFG.Block block : cfg.blocks()) {
checkElements(block, liveVariables.getOut(block), methodSymbol);
}
}
Aggregations