use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class RedundantAssignmentsCheck method handleAssignment.
private void handleAssignment(CheckerContext context, AssignmentExpressionTree assignmentExpressionTree) {
SymbolicValueSymbol assignedVariable = context.getState().peekValueSymbol();
Symbol assignedSymbol = assignedVariable.symbol();
if (assignedSymbol == null || // meaning that 'stream = stream.map(...);' would be detected as redundant assignment if not explicitly excluded
STREAM_TYPES.stream().anyMatch(assignedSymbol.type()::is)) {
return;
}
ExplodedGraph.Node node = context.getNode();
ProgramState previousState = node.programState;
SymbolicValue oldValue = previousState.getValue(assignedSymbol);
SymbolicValue newValue = assignedVariable.symbolicValue();
Symbol fromSymbol = previousState.peekValueSymbol().symbol();
assignmentsByMethod.peek().put(assignmentExpressionTree, new AssignmentDataHolder(assignedSymbol, oldValue, newValue, fromSymbol, node));
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class MethodMatcherCollectionTest method should_not_call_matchers_if_symbol_is_not_method_symbol.
@Test
public void should_not_call_matchers_if_symbol_is_not_method_symbol() {
Symbol mockSymbol = mock(Symbol.class);
when(mockSymbol.isMethodSymbol()).thenReturn(false);
MethodMatcher mockMatcher = mock(MethodMatcher.class);
assertThat(MethodMatcherCollection.create(mockMatcher).anyMatch(mockSymbol)).isFalse();
verify(mockMatcher, never()).matches(mockSymbol);
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class MethodMatcherTest method does_not_match_without_enclosingClass.
@Test
public void does_not_match_without_enclosingClass() throws Exception {
MethodMatcher matcher = MethodMatcher.create().name("toString").withoutParameter();
Symbol.MethodSymbol symbol = mock(Symbol.MethodSymbol.class);
when(symbol.enclosingClass()).thenReturn(null);
MethodTree tree = mock(MethodTree.class);
when(tree.symbol()).thenReturn(symbol);
assertThat(matcher.matches(tree)).isFalse();
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class BytecodeCFGBuilderTest method getCFGForMethod.
private BytecodeCFG getCFGForMethod(String methodName) {
SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
File file = new File("src/test/java/org/sonar/java/bytecode/cfg/BytecodeCFGBuilderTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
SemanticModel.createFor(tree, squidClassLoader);
Symbol.TypeSymbol innerClass = ((Symbol.TypeSymbol) ((ClassTree) tree.types().get(0)).symbol().lookupSymbols("InnerClass").iterator().next());
Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) innerClass.lookupSymbols(methodName).iterator().next();
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 BytecodeCFGBuilderTest method test_all_instructions_are_part_of_CFG.
@Test
public void test_all_instructions_are_part_of_CFG() throws Exception {
SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
File file = new File("src/test/java/org/sonar/java/bytecode/cfg/testdata/CFGTestData.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
SemanticModel.createFor(tree, squidClassLoader);
Symbol.TypeSymbol testClazz = ((ClassTree) tree.types().get(0)).symbol();
ClassReader cr = new ClassReader(squidClassLoader.getResourceAsStream(Convert.bytecodeName(CFGTestData.class.getCanonicalName()) + ".class"));
ClassNode classNode = new ClassNode(Opcodes.ASM5);
cr.accept(classNode, 0);
for (MethodNode method : classNode.methods) {
Multiset<String> opcodes = Arrays.stream(method.instructions.toArray()).map(AbstractInsnNode::getOpcode).filter(opcode -> opcode != -1).map(opcode -> Printer.OPCODES[opcode]).collect(Collectors.toCollection(HashMultiset::create));
Symbol methodSymbol = Iterables.getOnlyElement(testClazz.lookupSymbols(method.name));
BytecodeCFG bytecodeCFG = SETestUtils.bytecodeCFG(((JavaSymbol.MethodJavaSymbol) methodSymbol).completeSignature(), squidClassLoader);
Multiset<String> cfgOpcodes = cfgOpcodes(bytecodeCFG);
assertThat(cfgOpcodes).isEqualTo(opcodes);
}
}
Aggregations