Search in sources :

Example 21 with LanguageVersion

use of net.sourceforge.pmd.lang.LanguageVersion in project Gargoyle by callakrsos.

the class PMDCheckComposite method transformParametersIntoConfiguration.

public GargoylePMDConfiguration transformParametersIntoConfiguration(GargoylePMDParameters params) {
    //		if (null == params.getSourceDir() && null == params.getUri() && null == params.getFileListPath()
    //				&& null == params.getSourceText()) {
    //			throw new IllegalArgumentException(
    //					"Please provide a parameter for source root directory (-dir or -d), database URI (-uri or -u), or file list path (-filelist).");
    //		}
    GargoylePMDConfiguration configuration = new GargoylePMDConfiguration();
    configuration.setInputPaths(params.getSourceDir());
    //		configuration.setInputFilePath(params.getFileListPath());
    //		configuration.setInputUri(params.getUri());
    configuration.setReportFormat(REPORT_FILE_FORMAT);
    configuration.setSourceFileName(params.getSourceFileName());
    configuration.setSourceText(params.getSourceText());
    //		configuration.setBenchmark(params.isBenchmark());
    configuration.setDebug(params.isDebug());
    //		configuration.setMinimumPriority(params.getMinimumPriority());
    configuration.setReportFile(params.getReportfile());
    configuration.setReportProperties(params.getProperties());
    //		configuration.setReportShortNames(params.isShortnames());
    configuration.setRuleSets(RESULTSET);
    configuration.setRuleSetFactoryCompatibilityEnabled(true);
    configuration.setShowSuppressedViolations(params.isShowsuppressed());
    configuration.setSourceEncoding("UTF-8");
    //	        configuration.setStressTest(params.isStress());
    configuration.setSuppressMarker(params.getSuppressmarker());
    //		configuration.setThreads(1);
    configuration.setFailOnViolation(params.isFailOnViolation());
    LanguageVersion languageVersion = LanguageRegistry.findLanguageVersionByTerseName(params.getLanguage() + " " + params.getVersion());
    if (languageVersion != null) {
        configuration.getLanguageVersionDiscoverer().setDefaultLanguageVersion(languageVersion);
    }
    try {
        configuration.prependClasspath(params.getAuxclasspath());
    } catch (IOException e) {
        throw new IllegalArgumentException("Invalid auxiliary classpath: " + e.getMessage(), e);
    }
    return configuration;
}
Also used : GargoylePMDConfiguration(com.kyj.fx.voeditor.visual.framework.pmd.GargoylePMDConfiguration) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) IOException(java.io.IOException)

Example 22 with LanguageVersion

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

the class PMDIntegrationTest method violationRangeAndRuleId.

@Test
public void violationRangeAndRuleId() throws IOException, RuleSetNotFoundException, PMDException {
    checkState(params.language.isPresent(), "%s: language is missing", testDataXml);
    checkState(params.pmdReferenceId.isPresent(), "%s: pmdReferenceId is missing", testDataXml);
    final Matcher languageMatcher = Pattern.compile("(.*)\\s+(\\d+[\\.\\d]+)").matcher(params.language.get());
    checkState(languageMatcher.matches(), "%s: language must be formated '<terse-name> <version>'", testDataXml);
    final Language language = LanguageRegistry.findLanguageByTerseName(languageMatcher.group(1));
    if (language == null) {
        failDueToInvalidTerseName(languageMatcher.group(1));
    }
    final LanguageVersion languageVersion = language.getVersion(languageMatcher.group(2));
    final String fileExtension = "." + languageVersion.getLanguage().getExtensions().get(0);
    final File sourceFile = File.createTempFile(getFilePrefix(params), fileExtension);
    try {
        final PMDConfiguration configuration = new PMDConfiguration();
        final Reader reader = new StringReader(params.source);
        final RuleContext context = PMD.newRuleContext(sourceFile.getName(), sourceFile);
        context.setLanguageVersion(languageVersion);
        final RuleSets ruleSets = RulesetsFactoryUtils.defaultFactory().createRuleSets(params.pmdReferenceId.get());
        new SourceCodeProcessor(configuration).processSourceCode(reader, ruleSets, context);
        // handle violations that PMD does not (yet) find.
        if (!context.getReport().isEmpty()) {
            // PMD might find more than one violation. If there is one with a matching range then the test passes.
            final Optional<RuleViolation> violation = Iterators.tryFind(context.getReport().iterator(), new Predicate<RuleViolation>() {

                @Override
                public boolean apply(final RuleViolation violation) {
                    final Range range = MarkerUtil.getAbsoluteRange(params.source, violation);
                    return params.offset == range.getStart() && params.length == range.getEnd() - range.getStart();
                }
            });
            assertTrue(testDataXml + " > " + params.name + ": couldn't find violation with expected range (offset = " + params.offset + ", length = " + params.length + ")", violation.isPresent());
            final JavaQuickFixGenerator generator = new JavaQuickFixGenerator();
            final PMDMarker marker = mock(PMDMarker.class);
            final String ruleId = MarkerUtil.createRuleId(violation.get().getRule());
            when(marker.getRuleId()).thenReturn(ruleId);
            when(marker.getMarkerText()).thenReturn("");
            final JavaQuickFixContext quickFixContext = new JavaQuickFixContext(new Version(languageVersion.getVersion()));
            assertTrue("The Java quick fix generator should have quick fixes for " + ruleId, generator.hasQuickFixes(marker, quickFixContext));
            assertTrue("The Java quick fix generator should have at least one quick fix besides the SuppressWarningsQuickFix for " + ruleId, generator.getQuickFixes(marker, quickFixContext).size() > 1);
        }
    } finally {
        sourceFile.delete();
    }
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) Matcher(java.util.regex.Matcher) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) Reader(java.io.Reader) StringReader(java.io.StringReader) RuleViolation(net.sourceforge.pmd.RuleViolation) Range(ch.acanda.eclipse.pmd.marker.MarkerUtil.Range) Language(net.sourceforge.pmd.lang.Language) Version(org.osgi.framework.Version) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) RuleSets(net.sourceforge.pmd.RuleSets) PMDMarker(ch.acanda.eclipse.pmd.marker.PMDMarker) StringReader(java.io.StringReader) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) File(java.io.File) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration) Test(org.junit.Test)

Example 23 with LanguageVersion

use of net.sourceforge.pmd.lang.LanguageVersion in project pmd by pmd.

the class LanguageVersionDiscovererTest method testJavaFileUsing14.

/**
 * Test on Java file with Java version set to 1.4.
 */
@Test
public void testJavaFileUsing14() {
    LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer();
    discoverer.setDefaultLanguageVersion(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.4"));
    File javaFile = new File("/path/to/MyClass.java");
    LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(javaFile);
    assertEquals("LanguageVersion must be Java 1.4!", LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.4"), languageVersion);
}
Also used : LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) LanguageVersionDiscoverer(net.sourceforge.pmd.lang.LanguageVersionDiscoverer) File(java.io.File) Test(org.junit.Test)

Example 24 with LanguageVersion

use of net.sourceforge.pmd.lang.LanguageVersion in project pmd by pmd.

the class XPathRuleTest method testFollowingSibling.

/**
 * Following sibling check: See https://sourceforge.net/p/pmd/bugs/1209/
 *
 * @throws Exception
 *             any error
 */
@Test
public void testFollowingSibling() throws Exception {
    final String SOURCE = "public class dummy {\n" + "  public String toString() {\n" + "    String test = \"bad example\";\n" + "    test = \"a\";\n" + "    return test;\n" + "  }\n" + "}";
    LanguageVersion language = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion();
    ParserOptions parserOptions = language.getLanguageVersionHandler().getDefaultParserOptions();
    Parser parser = language.getLanguageVersionHandler().getParser(parserOptions);
    ASTCompilationUnit cu = (ASTCompilationUnit) parser.parse("test", new StringReader(SOURCE));
    RuleContext ruleContext = new RuleContext();
    ruleContext.setLanguageVersion(language);
    String xpath = "//Block/BlockStatement/following-sibling::BlockStatement";
    // XPATH version 1.0
    XPathRuleQuery xpathRuleQuery = new JaxenXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0);
    List<Node> nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(2, nodes.size());
    assertEquals(4, nodes.get(0).getBeginLine());
    assertEquals(5, nodes.get(1).getBeginLine());
    // XPATH version 2.0
    xpathRuleQuery = new SaxonXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_2_0);
    nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(2, nodes.size());
    assertEquals(4, nodes.get(0).getBeginLine());
    assertEquals(5, nodes.get(1).getBeginLine());
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) RuleContext(net.sourceforge.pmd.RuleContext) PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Node(net.sourceforge.pmd.lang.ast.Node) Parser(net.sourceforge.pmd.lang.Parser) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) StringReader(java.io.StringReader) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) XPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Test(org.junit.Test)

Example 25 with LanguageVersion

use of net.sourceforge.pmd.lang.LanguageVersion in project pmd by pmd.

the class XPathRuleTest method testImageOfPrimarySuffix.

/**
 * Test for problem reported in bug #1219 PrimarySuffix/@Image does not work
 * in some cases in xpath 2.0
 *
 * @throws Exception
 *             any error
 */
@Test
public void testImageOfPrimarySuffix() throws Exception {
    final String SUFFIX = "import java.io.File;\n" + "\n" + "public class TestSuffix {\n" + "    public static void main(String args[]) {\n" + "        new File(\"subdirectory\").list();\n" + "    }\n" + "}";
    LanguageVersion language = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion();
    ParserOptions parserOptions = language.getLanguageVersionHandler().getDefaultParserOptions();
    Parser parser = language.getLanguageVersionHandler().getParser(parserOptions);
    ASTCompilationUnit cu = (ASTCompilationUnit) parser.parse("test", new StringReader(SUFFIX));
    RuleContext ruleContext = new RuleContext();
    ruleContext.setLanguageVersion(language);
    String xpath = "//PrimarySuffix[@Image='list']";
    // XPATH version 1.0
    XPathRuleQuery xpathRuleQuery = new JaxenXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0);
    List<Node> nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(1, nodes.size());
    // XPATH version 1.0 Compatibility
    xpathRuleQuery = new SaxonXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0_COMPATIBILITY);
    nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(1, nodes.size());
    // XPATH version 2.0
    xpathRuleQuery = new SaxonXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_2_0);
    nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(1, nodes.size());
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) RuleContext(net.sourceforge.pmd.RuleContext) PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Node(net.sourceforge.pmd.lang.ast.Node) Parser(net.sourceforge.pmd.lang.Parser) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) StringReader(java.io.StringReader) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) XPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Test(org.junit.Test)

Aggregations

LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)35 Test (org.junit.Test)14 LanguageVersionDiscoverer (net.sourceforge.pmd.lang.LanguageVersionDiscoverer)10 File (java.io.File)9 IOException (java.io.IOException)8 RuleContext (net.sourceforge.pmd.RuleContext)7 Language (net.sourceforge.pmd.lang.Language)7 Parser (net.sourceforge.pmd.lang.Parser)7 StringReader (java.io.StringReader)5 Node (net.sourceforge.pmd.lang.ast.Node)5 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)4 ParserOptions (net.sourceforge.pmd.lang.ParserOptions)4 ArrayList (java.util.ArrayList)3 Rule (net.sourceforge.pmd.Rule)3 PropertyDescriptor (net.sourceforge.pmd.properties.PropertyDescriptor)3 GargoylePMDConfiguration (com.kyj.fx.voeditor.visual.framework.pmd.GargoylePMDConfiguration)2 FileNotFoundException (java.io.FileNotFoundException)2 Reader (java.io.Reader)2 HashSet (java.util.HashSet)2 JMenuBar (javax.swing.JMenuBar)2