use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class StaticImportCountCheck method visitNode.
@Override
public void visitNode(Tree tree) {
CompilationUnitTree cut = (CompilationUnitTree) tree;
List<ImportClauseTree> staticImports = cut.imports().stream().filter(importClauseTree -> importClauseTree.is(Tree.Kind.IMPORT) && ((ImportTree) importClauseTree).isStatic()).collect(Collectors.toList());
int staticImportsCount = staticImports.size();
if (staticImportsCount > threshold) {
List<JavaFileScannerContext.Location> flow = staticImports.stream().map(importStatement -> new JavaFileScannerContext.Location("+1", importStatement)).collect(Collectors.toList());
String message = String.format("Reduce the number of \"static\" imports in this class from %d to the maximum allowed %d.", staticImportsCount, threshold);
reportIssue(staticImports.get(0), message, flow, null);
}
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class UCFGJavaVisitorTest method assertUnknownMethodCalled.
private void assertUnknownMethodCalled(String source) {
CompilationUnitTree cut = getCompilationUnitTreeWithSemantics(source);
UnknownMethodVisitor unknownMethodVisitor = new UnknownMethodVisitor();
unknownMethodVisitor.visitCompilationUnit(cut);
assertThat(unknownMethodVisitor.unknownMethodCount).isGreaterThan(0);
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class UCFGJavaVisitorTest method assertCodeToUCfg.
private UCFG assertCodeToUCfg(String source, UCFG expectedUCFG, boolean testLocations) {
CompilationUnitTree cut = getCompilationUnitTreeWithSemantics(source);
UCFGJavaVisitor UCFGJavaVisitor = new UCFGJavaVisitor(tmp.getRoot());
UCFGJavaVisitor.fileKey = FILE_KEY;
UCFGJavaVisitor.visitCompilationUnit(cut);
UCFG actualUCFG = null;
try {
File java_ucfg_dir = new File(new File(tmp.getRoot(), "ucfg"), "java");
File ucfg = new File(java_ucfg_dir, "ucfg_0.proto");
actualUCFG = UCFGtoProtobuf.fromProtobufFile(ucfg);
} catch (IOException e) {
e.printStackTrace();
}
assertThat(actualUCFG.methodId()).isEqualTo(expectedUCFG.methodId());
assertThat(actualUCFG.basicBlocks()).isEqualTo(expectedUCFG.basicBlocks());
assertThat(actualUCFG.basicBlocks().values().stream().flatMap(b -> b.calls().stream())).containsExactlyElementsOf(expectedUCFG.basicBlocks().values().stream().flatMap(b -> b.calls().stream()).collect(Collectors.toList()));
assertThat(actualUCFG.basicBlocks().values().stream().map(BasicBlock::terminator)).containsExactlyElementsOf(expectedUCFG.basicBlocks().values().stream().map(BasicBlock::terminator).collect(Collectors.toList()));
assertThat(actualUCFG.entryBlocks()).isEqualTo(expectedUCFG.entryBlocks());
assertThat(toLocationStream(actualUCFG).noneMatch(l -> l == UCFGBuilder.LOC)).isTrue();
if (testLocations) {
Stream<LocationInFile> locStream = toLocationStream(actualUCFG);
assertThat(locStream).containsExactlyElementsOf(toLocationStream(expectedUCFG).collect(Collectors.toList()));
}
return actualUCFG;
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree 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.CompilationUnitTree in project sonar-java by SonarSource.
the class SimpleClassNameCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
CompilationUnitTree cut = (CompilationUnitTree) tree;
cut.types().stream().filter(NOT_EMPTY_STATEMENT).map(t -> ((ClassTree) t).symbol()).forEach(this::checkSymbol);
List<ImportTree> imports = cut.imports().stream().filter(NOT_EMPTY_STATEMENT).map(t -> (ImportTree) t).collect(Collectors.toList());
boolean fileContainsStarImport = imports.stream().filter(it -> it.qualifiedIdentifier().is(Kind.MEMBER_SELECT)).map(it -> ((MemberSelectExpressionTree) it.qualifiedIdentifier()).identifier()).anyMatch(i -> "*".equals(i.name()));
if (!fileContainsStarImport) {
checkImports(imports);
}
}
Aggregations