Search in sources :

Example 1 with SourceCodeProcessor

use of net.sourceforge.pmd.SourceCodeProcessor in project Gargoyle by callakrsos.

the class PMDGargoyleThreadProcessor method processFiles.

public void processFiles(RuleSetFactory ruleSetFactory, List<DataSource> files, RuleContext ctx, List<Renderer> renderers) {
    // single threaded execution
    RuleSets rs = createRuleSets(ruleSetFactory);
    SourceCodeProcessor processor = new SourceCodeProcessor(configuration);
    for (DataSource dataSource : files) {
        String niceFileName = filenameFrom(dataSource);
        Report report = PMD.setupReport(rs, ctx, niceFileName);
        // overtake the listener
        //bug fix 2016-10-05 by kyj. 결과가 중복되서 출력됨.
        //			report.addSynchronizedListeners(ctx.getReport().getSynchronizedListeners());
        //	        ctx.setReport(report);
        //	        ctx.setSourceCodeFilename(niceFileName);
        //			if (LOG.isLoggable(Level.FINE)) {
        //				LOG.fine("Processing " + ctx.getSourceCodeFilename());
        //			}
        rs.start(ctx);
        for (Renderer r : renderers) {
            r.startFileAnalysis(dataSource);
        }
        try {
            InputStream stream = new BufferedInputStream(dataSource.getInputStream());
            //				ctx.setLanguageVersion(null);
            processor.processSourceCode(stream, rs, ctx);
        } catch (PMDException pmde) {
            //				LOGGER.error(ValueUtil.toString(pmde));
            //				if (LOG.isLoggable(Level.FINE)) {
            //					LOG.log(Level.FINE, "Error while processing file: " + niceFileName, pmde.getCause());
            //				}
            report.addError(new Report.ProcessingError(pmde.getMessage(), niceFileName));
        } catch (IOException ioe) {
            //				LOGGER.error(ValueUtil.toString(ioe));
            // unexpected exception: log and stop executor service
            addError(report, "Unable to read source file", ioe, niceFileName);
        } catch (RuntimeException re) {
            //				LOGGER.error(ValueUtil.toString(re));
            // unexpected exception: log and stop executor service
            addError(report, "RuntimeException while processing file", re, niceFileName);
        } catch (Exception e) {
            LOGGER.error(ValueUtil.toString(e));
        }
        rs.end(ctx);
        super.renderReports(renderers, ctx.getReport());
    }
}
Also used : Report(net.sourceforge.pmd.Report) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) IOException(java.io.IOException) PMDException(net.sourceforge.pmd.PMDException) IOException(java.io.IOException) DataSource(net.sourceforge.pmd.util.datasource.DataSource) BufferedInputStream(java.io.BufferedInputStream) RuleSets(net.sourceforge.pmd.RuleSets) Renderer(net.sourceforge.pmd.renderers.Renderer) PMDException(net.sourceforge.pmd.PMDException)

Example 2 with SourceCodeProcessor

use of net.sourceforge.pmd.SourceCodeProcessor in project pmd-eclipse-plugin by pmd.

the class ReviewResourceForRuleCommand method execute.

/*
     * (non-Javadoc)
     * 
     * @see
     * net.sourceforge.pmd.eclipse.runtime.cmd.AbstractDefaultCommand#execute()
     */
@Override
public void execute() throws CommandException {
    // IProject project = resource.getProject();
    IFile file = (IFile) resource.getAdapter(IFile.class);
    beginTask("PMD checking for rule: " + rule.getName(), 1);
    if (file != null) {
        RuleSet ruleSet = RuleSetUtil.newSingle(rule);
        // final PMDEngine pmdEngine = getPmdEngineForProject(project);
        File sourceCodeFile = file.getFullPath().toFile();
        if (ruleSet.applies(sourceCodeFile)) {
            try {
                context = PMD.newRuleContext(file.getName(), sourceCodeFile);
                // Reader input = new InputStreamReader(file.getContents(),
                // file.getCharset());
                RuleSets rSets = new RuleSets(ruleSet);
                new SourceCodeProcessor(new PMDConfiguration()).processSourceCode(file.getContents(), rSets, context);
            // input.close();
            // } catch (CoreException e) {
            // throw new CommandException(e);
            } catch (PMDException e) {
                throw new CommandException(e);
            } catch (CoreException e) {
                throw new CommandException(e);
            }
            // trigger event propertyChanged for all listeners
            Display.getDefault().asyncExec(new Runnable() {

                public void run() {
                    for (IPropertyListener listener : listenerList) {
                        listener.propertyChanged(context.getReport().iterator(), PMDRuntimeConstants.PROPERTY_REVIEW);
                    }
                }
            });
        }
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) RuleSets(net.sourceforge.pmd.RuleSets) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) PMDException(net.sourceforge.pmd.PMDException) CommandException(name.herlin.command.CommandException) File(java.io.File) IFile(org.eclipse.core.resources.IFile) IPropertyListener(org.eclipse.ui.IPropertyListener) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration)

Example 3 with SourceCodeProcessor

use of net.sourceforge.pmd.SourceCodeProcessor in project pmd-eclipse-plugin by pmd.

the class BasicPMDTest method testRunPmdJdk15.

/**
 * Let see with Java 1.5
 */
@Test
public void testRunPmdJdk15() {
    try {
        PMDConfiguration configuration = new PMDConfiguration();
        configuration.setDefaultLanguageVersion(LanguageRegistry.findLanguageVersionByTerseName("java 1.5"));
        final String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}";
        final RuleContext context = new RuleContext();
        context.setSourceCodeFilename("foo.java");
        context.setReport(new Report());
        final RuleSet codeStyleRuleSet = new RuleSetFactory().createRuleSet("category/java/codestyle.xml/UnnecessaryReturn");
        RuleSets rSets = new RuleSets(codeStyleRuleSet);
        new SourceCodeProcessor(configuration).processSourceCode(new StringDataSource(sourceCode).getInputStream(), rSets, context);
        final Iterator<RuleViolation> iter = context.getReport().iterator();
        Assert.assertTrue("There should be at least one violation", iter.hasNext());
        final RuleViolation violation = iter.next();
        Assert.assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
        Assert.assertEquals(3, violation.getBeginLine());
    } catch (final RuleSetNotFoundException e) {
        e.printStackTrace();
        Assert.fail();
    } catch (final PMDException e) {
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) RuleViolation(net.sourceforge.pmd.RuleViolation) RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSets(net.sourceforge.pmd.RuleSets) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) PMDException(net.sourceforge.pmd.PMDException) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration) Test(org.junit.Test)

Example 4 with SourceCodeProcessor

use of net.sourceforge.pmd.SourceCodeProcessor in project pmd-eclipse-plugin by pmd.

the class BasicPMDTest method testRunPmdJdk13.

/**
 * One first thing the plugin must be able to do is to run PMD
 */
@Test
public void testRunPmdJdk13() {
    try {
        PMDConfiguration configuration = new PMDConfiguration();
        configuration.setDefaultLanguageVersion(LanguageRegistry.findLanguageVersionByTerseName("java 1.3"));
        final String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}";
        final RuleContext context = new RuleContext();
        context.setSourceCodeFilename("foo.java");
        context.setReport(new Report());
        final RuleSet codeStyleRuleSet = new RuleSetFactory().createRuleSet("category/java/codestyle.xml/UnnecessaryReturn");
        RuleSets rSets = new RuleSets(codeStyleRuleSet);
        new SourceCodeProcessor(configuration).processSourceCode(new StringDataSource(sourceCode).getInputStream(), rSets, context);
        final Iterator<RuleViolation> iter = context.getReport().iterator();
        Assert.assertTrue("There should be at least one violation", iter.hasNext());
        final RuleViolation violation = iter.next();
        Assert.assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
        Assert.assertEquals(3, violation.getBeginLine());
    } catch (final RuleSetNotFoundException e) {
        e.printStackTrace();
        Assert.fail();
    } catch (final PMDException e) {
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) RuleViolation(net.sourceforge.pmd.RuleViolation) RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSets(net.sourceforge.pmd.RuleSets) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) PMDException(net.sourceforge.pmd.PMDException) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration) Test(org.junit.Test)

Example 5 with SourceCodeProcessor

use of net.sourceforge.pmd.SourceCodeProcessor in project eclipse-pmd by acanda.

the class Analyzer method runPMD.

private Iterable<RuleViolation> runPMD(final IFile file, final RuleSets ruleSets) {
    try {
        if (isValidFile(file, ruleSets)) {
            final Language language = LANGUAGES.get(file.getFileExtension().toLowerCase());
            if (isValidLanguage(language)) {
                final PMDConfiguration configuration = new PMDConfiguration();
                final InputStreamReader reader = new InputStreamReader(file.getContents(), file.getCharset());
                final RuleContext context = PMD.newRuleContext(file.getName(), file.getRawLocation().toFile());
                context.setLanguageVersion(language.getDefaultVersion());
                context.setIgnoreExceptions(false);
                new SourceCodeProcessor(configuration).processSourceCode(reader, ruleSets, context);
                return ImmutableList.copyOf(context.getReport().iterator());
            }
        }
    } catch (CoreException | IOException e) {
        PMDPlugin.getDefault().error("Could not run PMD on file " + file.getRawLocation(), e);
    } catch (final PMDException e) {
        if (isIncorrectSyntaxCause(e)) {
            PMDPlugin.getDefault().info("Could not run PMD because of incorrect syntax of file " + file.getRawLocation(), e);
        } else {
            PMDPlugin.getDefault().warn("Could not run PMD on file " + file.getRawLocation(), e);
        }
    }
    return ImmutableList.<RuleViolation>of();
}
Also used : Language(net.sourceforge.pmd.lang.Language) InputStreamReader(java.io.InputStreamReader) RuleContext(net.sourceforge.pmd.RuleContext) CoreException(org.eclipse.core.runtime.CoreException) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) PMDException(net.sourceforge.pmd.PMDException) IOException(java.io.IOException) RuleViolation(net.sourceforge.pmd.RuleViolation) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration)

Aggregations

SourceCodeProcessor (net.sourceforge.pmd.SourceCodeProcessor)10 RuleSets (net.sourceforge.pmd.RuleSets)9 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)8 PMDException (net.sourceforge.pmd.PMDException)7 RuleContext (net.sourceforge.pmd.RuleContext)7 RuleSet (net.sourceforge.pmd.RuleSet)6 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)5 RuleViolation (net.sourceforge.pmd.RuleViolation)5 Report (net.sourceforge.pmd.Report)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)3 DataSource (net.sourceforge.pmd.util.datasource.DataSource)3 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 IOException (java.io.IOException)2 Language (net.sourceforge.pmd.lang.Language)2 CoreException (org.eclipse.core.runtime.CoreException)2 Range (ch.acanda.eclipse.pmd.marker.MarkerUtil.Range)1 PMDMarker (ch.acanda.eclipse.pmd.marker.PMDMarker)1