Search in sources :

Example 6 with JavaFileScannerContext

use of org.sonar.plugins.java.api.JavaFileScannerContext in project sonar-java by SonarSource.

the class CommonsCollectionsTest method initAndScan.

private void initAndScan() {
    File prjDir = new File("target/test-projects/commons-collections-3.2.1");
    File srcDir = new File(prjDir, "src");
    context = SensorContextTester.create(srcDir);
    DefaultFileSystem fs = context.fileSystem();
    Collection<File> files = FileUtils.listFiles(srcDir, new String[] { "java" }, true);
    for (File file : files) {
        fs.add(new TestInputFileBuilder("", file.getPath()).build());
    }
    Measurer measurer = new Measurer(fs, context, mock(NoSonarFilter.class));
    JavaResourceLocator javaResourceLocator = new JavaResourceLocator() {

        public Map<String, String> sourceFileCache = Maps.newHashMap();

        @Override
        public InputFile findResourceByClassName(String className) {
            return null;
        }

        @Override
        public String findSourceFileKeyByClassName(String className) {
            String name = className.replace('.', '/');
            return sourceFileCache.get(name);
        }

        @Override
        public Collection<File> classFilesToAnalyze() {
            return Collections.emptyList();
        }

        @Override
        public Collection<File> classpath() {
            return null;
        }

        @Override
        public void scanFile(JavaFileScannerContext context) {
            JavaFilesCache javaFilesCache = new JavaFilesCache();
            javaFilesCache.scanFile(context);
            for (String key : javaFilesCache.resourcesCache.keySet()) {
                sourceFileCache.put(key, context.getFileKey());
            }
        }
    };
    squid = new JavaSquid(new JavaVersionImpl(), null, measurer, javaResourceLocator, null, new JavaCheck[0]);
    squid.scan(files, Collections.<File>emptyList());
}
Also used : NoSonarFilter(org.sonar.api.issue.NoSonarFilter) JavaCheck(org.sonar.plugins.java.api.JavaCheck) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem)

Example 7 with JavaFileScannerContext

use of org.sonar.plugins.java.api.JavaFileScannerContext in project sonar-java by SonarSource.

the class DebugCheckTestUtils method verifyIssuesWithMaxSteps.

static void verifyIssuesWithMaxSteps(String sourcefile, SECheck check, int maxSteps) {
    BehaviorCache behaviorCache = new BehaviorCache(new SquidClassLoader(new ArrayList<>()));
    SymbolicExecutionVisitor sev = new SymbolicExecutionVisitor(Collections.singletonList(check), behaviorCache) {

        @Override
        protected ExplodedGraphWalker getWalker() {
            return new ExplodedGraphWalker(Collections.singletonList(check), behaviorCache, (SemanticModel) context.getSemanticModel()) {

                @Override
                protected int maxSteps() {
                    return maxSteps;
                }
            };
        }

        @Override
        public void scanFile(JavaFileScannerContext context) {
            super.scanFile(context);
            // the check has been executed, but we still need to call the scan manually to report the issues
            check.scanFile(context);
        }
    };
    JavaCheckVerifier.verify(sourcefile, sev);
}
Also used : ExplodedGraphWalker(org.sonar.java.se.ExplodedGraphWalker) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ArrayList(java.util.ArrayList) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Aggregations

JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)7 File (java.io.File)3 SymbolicExecutionVisitor (org.sonar.java.se.SymbolicExecutionVisitor)3 Tree (org.sonar.plugins.java.api.tree.Tree)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Test (org.junit.Test)2 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)2 JavaFileScanner (org.sonar.plugins.java.api.JavaFileScanner)2 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)2 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ImmutableList (com.google.common.collect.ImmutableList)1 Multimap (com.google.common.collect.Multimap)1 RecognitionException (com.sonar.sslr.api.RecognitionException)1 InterruptedIOException (java.io.InterruptedIOException)1