private boolean isNotJavaLangOrSerializable() { String[] path = getContext().peekSourceCode().getName().split(Pattern.quote(File.separator)); boolean isJavaLang = path.length > 3 && "java".equals(path[path.length - 3]) && "lang".equals(path[path.length - 2]); boolean isJavaLangAnnotation = path.length > 4 && "Annotation.java".equals(path[path.length - 1]) && "java".equals(path[path.length - 4]) && "lang".equals(path[path.length - 3]) && "annotation".equals(path[path.length - 2]); boolean isSerializable = path.length > 3 && "Serializable.java".equals(path[path.length - 1]) && "java".equals(path[path.length - 3]) && "io".equals(path[path.length - 2]); return !(isJavaLang || isJavaLangAnnotation || isSerializable); }
/** * Collects actual issues detected when running a {@link JavaCheck} against a test {@link File}. * * @param testFile test {@link File} against which the {@link JavaCheck} has to be run. * @param check {@link JavaCheck} to run against the test {@link File}. * @return a {@link TreeMultimap} object containing detected actual issues. */ static TreeMultimap<Integer, String> collect(final File testFile, final JavaFileScanner check) { final Collection<File> classpath = Lists.newArrayList(); final File testJarDirectory = new File("target/test-jars"); if (testJarDirectory.exists() && testJarDirectory.isDirectory()) { classpath.addAll(FileUtils.listFiles(testJarDirectory, new String[] { "jar", "zip" }, true)); } classpath.add(new File("target/test-classes")); final VisitorsBridge visitorsBridge = new VisitorsBridge(Lists.newArrayList(check), Lists.newArrayList(classpath), null); JavaAstScanner.scanSingleFile(testFile, visitorsBridge); final VisitorContext context = visitorsBridge.getContext(); final SourceCode sourceCode = context.peekSourceCode(); return collectIssues(sourceCode); }
private void createSonarSymbolTable(CompilationUnitTree tree) { if (sonarComponents != null) { SonarSymbolTableVisitor symVisitor = new SonarSymbolTableVisitor(sonarComponents.symbolizableFor(getContext().getFile()), semanticModel); symVisitor.visitCompilationUnit(tree); } }
@Override public void visitFile(@Nullable AstNode astNode) { semanticModel = null; if (astNode != null) { CompilationUnitTree tree = (CompilationUnitTree) astNode; if (isNotJavaLangOrSerializable()) { try { semanticModel = SemanticModel.createFor(tree, getProjectClasspath()); } catch (Exception e) { LOG.error("Unable to create symbol table for : " + getContext().getFile().getAbsolutePath(), e); return; } createSonarSymbolTable(tree); } else { SemanticModel.handleMissingTypes(tree); } JavaFileScannerContext context = new DefaultJavaFileScannerContext(tree, (SourceFile) getContext().peekSourceCode(), getContext().getFile(), semanticModel, analyseAccessors); for (JavaFileScanner scanner : scanners) { scanner.scanFile(context); } if (semanticModel != null) { // Close class loader after all the checks. semanticModel.done(); } } }