Search in sources :

Example 1 with PMDException

use of net.sourceforge.pmd.PMDException 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 PMDException

use of net.sourceforge.pmd.PMDException 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 PMDException

use of net.sourceforge.pmd.PMDException 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 PMDException

use of net.sourceforge.pmd.PMDException 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 PMDException

use of net.sourceforge.pmd.PMDException in project pmd by pmd.

the class XPathMetricFunctionTest method testWithExpectedException.

private void testWithExpectedException(String xpath, String code, Class<? extends Exception> expectedThrowable, String expectedMessage) throws Exception {
    Rule rule = makeXpathRuleFromXPath(xpath);
    expected.expect(expectedThrowable);
    expected.expectMessage(expectedMessage);
    try {
        getViolations(rule, code);
    } catch (PMDException pmdE) {
        throw (Exception) pmdE.getCause();
    }
}
Also used : PMDException(net.sourceforge.pmd.PMDException) Rule(net.sourceforge.pmd.Rule) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule)

Aggregations

PMDException (net.sourceforge.pmd.PMDException)10 RuleSets (net.sourceforge.pmd.RuleSets)8 RuleContext (net.sourceforge.pmd.RuleContext)7 SourceCodeProcessor (net.sourceforge.pmd.SourceCodeProcessor)7 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)6 Report (net.sourceforge.pmd.Report)6 RuleSet (net.sourceforge.pmd.RuleSet)5 RuleViolation (net.sourceforge.pmd.RuleViolation)5 IOException (java.io.IOException)4 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)4 InputStream (java.io.InputStream)3 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)3 Renderer (net.sourceforge.pmd.renderers.Renderer)3 CoreException (org.eclipse.core.runtime.CoreException)3 Test (org.junit.Test)3 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 DataSource (net.sourceforge.pmd.util.datasource.DataSource)2 IFile (org.eclipse.core.resources.IFile)2