use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class Result method createForJavaFile.
public static Result createForJavaFile(String filePath) {
File file = new File(filePath + ".java");
CompilationUnitTree compilationUnitTree = (CompilationUnitTree) parser.parse(file);
SemanticModel semanticModel = SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes"))));
UsageVisitor usageVisitor = new UsageVisitor();
compilationUnitTree.accept(usageVisitor);
return new Result(semanticModel, usageVisitor.symbolsUsed);
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class ModifiersUtilsTest method test_find_modifier.
@Test
public void test_find_modifier() {
File file = new File("src/test/files/model/ModifiersUtilsTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
ClassTree classTree = (ClassTree) tree.types().get(0);
assertThat(ModifiersUtils.findModifier(classTree.modifiers(), Modifier.PUBLIC).isPresent()).isTrue();
assertThat(ModifiersUtils.findModifier(classTree.modifiers(), Modifier.ABSTRACT).isPresent()).isFalse();
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class ModifiersUtilsTest method test_int_and_long_value.
@Test
public void test_int_and_long_value() throws Exception {
File file = new File("src/test/files/model/ModifiersUtilsTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
ClassTree classTree = (ClassTree) tree.types().get(0);
assertThat(ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.PUBLIC)).isTrue();
assertThat(ModifiersUtils.getModifier(classTree.modifiers(), Modifier.PUBLIC).keyword().text()).isEqualTo("public");
assertThat(ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.ABSTRACT)).isFalse();
assertThat(ModifiersUtils.getModifier(classTree.modifiers(), Modifier.ABSTRACT)).isNull();
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class TestDefaultJavaFileScannerContextWithSensorContextTester method setup.
@Before
public void setup() throws IOException {
sensorContext = SensorContextTester.create(Paths.get(""));
sensorContext.fileSystem().add(new TestInputFileBuilder("myProjectKey", JAVA_FILE.getPath()).setLanguage("java").initMetadata(new String(Files.readAllBytes(JAVA_FILE.toPath()), StandardCharsets.UTF_8)).build());
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, sensorContext.fileSystem(), javaClasspath, javaTestClasspath, checkFactory);
sonarComponents.setSensorContext(sensorContext);
// spy getRuleKey call, to avoid mocking CheckFactory and Checks
sonarComponents = spy(sonarComponents);
when(sonarComponents.getRuleKey(any())).thenReturn(RuleKey.of("repository", "rule"));
CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse(JAVA_FILE);
tree = cut.types().get(0);
scannerContext = new DefaultJavaFileScannerContext(cut, JAVA_FILE, null, sonarComponents, null, true);
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class ClassTreeImplTest method createTree.
private CompilationUnitTree createTree(String code) {
CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse(code);
SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
return compilationUnitTree;
}
Aggregations