use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class BytecodeCFGBuilderTest method getBytecodeCFG.
public static BytecodeCFG getBytecodeCFG(String methodName, String filename) {
SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
File file = new File(filename);
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
SemanticModel.createFor(tree, squidClassLoader);
List<Tree> classMembers = ((ClassTree) tree.types().get(0)).members();
Symbol.MethodSymbol symbol = classMembers.stream().filter(m -> m instanceof MethodTree).map(m -> ((MethodTree) m).symbol()).filter(s -> methodName.equals(s.name())).findFirst().orElseThrow(IllegalStateException::new);
return SETestUtils.bytecodeCFG(((JavaSymbol.MethodJavaSymbol) symbol).completeSignature(), squidClassLoader);
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class MethodYieldTest method test_creation_of_states.
@Test
public void test_creation_of_states() throws Exception {
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/XProcYields.java");
MethodBehavior mb = getMethodBehavior(sev, "foo");
ProgramState ps = ProgramState.EMPTY_STATE;
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
SymbolicValue sv3 = new SymbolicValue();
Symbol sym = new JavaSymbol.VariableJavaSymbol(0, "myVar", new JavaSymbol.MethodJavaSymbol(0, "dummy", null));
ps = ps.put(sym, sv1);
MethodYield methodYield = mb.happyPathYields().findFirst().get();
Stream<ProgramState> generatedStatesFromFirstYield = methodYield.statesAfterInvocation(Lists.newArrayList(sv1, sv2), Lists.newArrayList(), ps, () -> sv3);
assertThat(generatedStatesFromFirstYield).hasSize(1);
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class AbstractInjectionChecker method isIdentifierDynamicString.
protected boolean isIdentifierDynamicString(Tree methodTree, IdentifierTree arg, @Nullable Symbol currentlyChecking, boolean firstLevel) {
Symbol symbol = arg.symbol();
if (isExcluded(currentlyChecking, symbol)) {
return false;
}
boolean isLocalVar = symbol.owner().isMethodSymbol() && !((JavaSymbol.MethodJavaSymbol) symbol.owner()).getParameters().scopeSymbols().contains(symbol);
if (isLocalVar) {
// symbol is a local variable, check it is not a dynamic string.
// Check declaration
VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration();
ExpressionTree initializer = declaration.initializer();
if (initializer != null && isDynamicString(methodTree, initializer, currentlyChecking)) {
return true;
}
// check usages by revisiting the enclosing tree.
Collection<IdentifierTree> usages = symbol.usages();
LocalVariableDynamicStringVisitor visitor = new LocalVariableDynamicStringVisitor(symbol, usages, methodTree);
Tree argEnclosingDeclarationTree = semanticModel.getTree(semanticModel.getEnv(symbol));
argEnclosingDeclarationTree.accept(visitor);
return visitor.dynamicString;
}
// arg is not a local variable nor a constant, so it is a parameter or a field.
parameterName = "\"" + arg.name() + "\"";
return symbol.owner().isMethodSymbol() && !firstLevel;
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class CatchUsesExceptionWithContextCheck method visitCatch.
@Override
public void visitCatch(CatchTree tree) {
if (!isExcludedType(tree.parameter().type()) && !excludedCatchTrees.contains(tree)) {
Symbol exception = tree.parameter().symbol();
validUsagesStack.addFirst(Lists.newArrayList(exception.usages()));
super.visitCatch(tree);
Collection<IdentifierTree> usages = validUsagesStack.pop();
if (usages.isEmpty()) {
context.reportIssue(this, tree.parameter(), "Either log or rethrow this exception.");
}
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class DataStoredInSessionCheck method checkArgument.
private void checkArgument(ExpressionTree argument, ExpressionTree startPoint, MethodInvocationTree reportTree) {
ExpressionTree expressionToEvaluate = argument;
if (expressionToEvaluate.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifier = (IdentifierTree) expressionToEvaluate;
identifiersUsedToSetAttribute.add(identifier);
Symbol variable = identifier.symbol();
ExpressionTree lastAssignmentOrDeclaration = ReassignmentFinder.getClosestReassignmentOrDeclarationExpression(startPoint, variable);
if (lastAssignmentOrDeclaration != null && !usedBetween(variable, lastAssignmentOrDeclaration, startPoint)) {
expressionToEvaluate = lastAssignmentOrDeclaration;
}
}
if (isRequestOrCookieDataRetrieval(expressionToEvaluate)) {
reportIssue(reportTree.methodSelect(), "Make sure the user is authenticated before this data is stored in the session.");
} else if (expressionToEvaluate.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) expressionToEvaluate;
if (NO_EFFECT_OPERATION.anyMatch(mit)) {
checkArgument(mit.arguments().get(0), mit, reportTree);
}
}
}
Aggregations