Search in sources :

Example 26 with LanguageVersion

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

the class RuleSetWriter method createRuleElement.

private Element createRuleElement(Rule rule) {
    if (rule instanceof RuleReference) {
        RuleReference ruleReference = (RuleReference) rule;
        RuleSetReference ruleSetReference = ruleReference.getRuleSetReference();
        if (ruleSetReference.isAllRules()) {
            if (!ruleSetFileNames.contains(ruleSetReference.getRuleSetFileName())) {
                ruleSetFileNames.add(ruleSetReference.getRuleSetFileName());
                return createRuleSetReferenceElement(ruleSetReference);
            } else {
                return null;
            }
        } else {
            Language language = ruleReference.getOverriddenLanguage();
            LanguageVersion minimumLanguageVersion = ruleReference.getOverriddenMinimumLanguageVersion();
            LanguageVersion maximumLanguageVersion = ruleReference.getOverriddenMaximumLanguageVersion();
            Boolean deprecated = ruleReference.isOverriddenDeprecated();
            String name = ruleReference.getOverriddenName();
            String ref = ruleReference.getRuleSetReference().getRuleSetFileName() + '/' + ruleReference.getRule().getName();
            String message = ruleReference.getOverriddenMessage();
            String externalInfoUrl = ruleReference.getOverriddenExternalInfoUrl();
            String description = ruleReference.getOverriddenDescription();
            RulePriority priority = ruleReference.getOverriddenPriority();
            List<PropertyDescriptor<?>> propertyDescriptors = ruleReference.getOverriddenPropertyDescriptors();
            Map<PropertyDescriptor<?>, Object> propertiesByPropertyDescriptor = ruleReference.getOverriddenPropertiesByPropertyDescriptor();
            List<String> examples = ruleReference.getOverriddenExamples();
            return createSingleRuleElement(language, minimumLanguageVersion, maximumLanguageVersion, deprecated, name, null, ref, message, externalInfoUrl, null, null, null, null, description, priority, propertyDescriptors, propertiesByPropertyDescriptor, examples);
        }
    } else {
        return createSingleRuleElement(rule instanceof ImmutableLanguage ? null : rule.getLanguage(), rule.getMinimumLanguageVersion(), rule.getMaximumLanguageVersion(), rule.isDeprecated(), rule.getName(), rule.getSince(), null, rule.getMessage(), rule.getExternalInfoUrl(), rule.getRuleClass(), rule.isDfa(), rule.isTypeResolution(), rule.isMultifile(), rule.getDescription(), rule.getPriority(), rule.getPropertyDescriptors(), rule.getPropertiesByPropertyDescriptor(), rule.getExamples());
    }
}
Also used : ImmutableLanguage(net.sourceforge.pmd.lang.rule.ImmutableLanguage) RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) ImmutableLanguage(net.sourceforge.pmd.lang.rule.ImmutableLanguage) Language(net.sourceforge.pmd.lang.Language) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion)

Example 27 with LanguageVersion

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

the class SourceCodeProcessor method determineLanguage.

private void determineLanguage(RuleContext ctx) {
    // determination
    if (ctx.getLanguageVersion() == null) {
        LanguageVersion languageVersion = configuration.getLanguageVersionOfFile(ctx.getSourceCodeFilename());
        ctx.setLanguageVersion(languageVersion);
    }
}
Also used : LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion)

Example 28 with LanguageVersion

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

the class PMD method getApplicableLanguages.

private static Set<Language> getApplicableLanguages(PMDConfiguration configuration, RuleSets ruleSets) {
    Set<Language> languages = new HashSet<>();
    LanguageVersionDiscoverer discoverer = configuration.getLanguageVersionDiscoverer();
    for (Rule rule : ruleSets.getAllRules()) {
        Language language = rule.getLanguage();
        if (languages.contains(language)) {
            continue;
        }
        LanguageVersion version = discoverer.getDefaultLanguageVersion(language);
        if (RuleSet.applies(rule, version)) {
            languages.add(language);
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("Using " + language.getShortName() + " version: " + version.getShortName());
            }
        }
    }
    return languages;
}
Also used : Language(net.sourceforge.pmd.lang.Language) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) LanguageVersionDiscoverer(net.sourceforge.pmd.lang.LanguageVersionDiscoverer) HashSet(java.util.HashSet)

Example 29 with LanguageVersion

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

the class MainDesignerController method loadSourceFromFile.

private void loadSourceFromFile(File file) {
    if (file != null) {
        try {
            String source = IOUtils.toString(new FileInputStream(file));
            sourceEditorController.setText(source);
            LanguageVersion guess = DesignerUtil.getLanguageVersionFromExtension(file.getName());
            if (guess != null) {
                // guess the language from the extension
                languageChoiceBox.getSelectionModel().select(guess);
                onRefreshASTClicked();
            }
            recentFiles.push(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 30 with LanguageVersion

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

the class RuleTst method parseTests.

private TestDescriptor[] parseTests(Rule rule, Document doc) {
    Element root = doc.getDocumentElement();
    NodeList testCodes = root.getElementsByTagName("test-code");
    TestDescriptor[] tests = new TestDescriptor[testCodes.getLength()];
    for (int i = 0; i < testCodes.getLength(); i++) {
        Element testCode = (Element) testCodes.item(i);
        boolean reinitializeRule = true;
        Node reinitializeRuleAttribute = testCode.getAttributes().getNamedItem("reinitializeRule");
        if (reinitializeRuleAttribute != null) {
            String reinitializeRuleValue = reinitializeRuleAttribute.getNodeValue();
            if ("false".equalsIgnoreCase(reinitializeRuleValue) || "0".equalsIgnoreCase(reinitializeRuleValue)) {
                reinitializeRule = false;
            }
        }
        boolean isRegressionTest = true;
        Node regressionTestAttribute = testCode.getAttributes().getNamedItem("regressionTest");
        if (regressionTestAttribute != null) {
            String reinitializeRuleValue = regressionTestAttribute.getNodeValue();
            if ("false".equalsIgnoreCase(reinitializeRuleValue)) {
                isRegressionTest = false;
            }
        }
        boolean isUseAuxClasspath = true;
        Node useAuxClasspathAttribute = testCode.getAttributes().getNamedItem("useAuxClasspath");
        if (useAuxClasspathAttribute != null) {
            String useAuxClasspathValue = useAuxClasspathAttribute.getNodeValue();
            if ("false".equalsIgnoreCase(useAuxClasspathValue)) {
                isUseAuxClasspath = false;
            }
        }
        NodeList ruleProperties = testCode.getElementsByTagName("rule-property");
        Properties properties = new Properties();
        for (int j = 0; j < ruleProperties.getLength(); j++) {
            Node ruleProperty = ruleProperties.item(j);
            String propertyName = ruleProperty.getAttributes().getNamedItem("name").getNodeValue();
            properties.setProperty(propertyName, parseTextNode(ruleProperty));
        }
        NodeList expectedMessagesNodes = testCode.getElementsByTagName("expected-messages");
        List<String> messages = new ArrayList<>();
        if (expectedMessagesNodes != null && expectedMessagesNodes.getLength() > 0) {
            Element item = (Element) expectedMessagesNodes.item(0);
            NodeList messagesNodes = item.getElementsByTagName("message");
            for (int j = 0; j < messagesNodes.getLength(); j++) {
                messages.add(parseTextNode(messagesNodes.item(j)));
            }
        }
        NodeList expectedLineNumbersNodes = testCode.getElementsByTagName("expected-linenumbers");
        List<Integer> expectedLineNumbers = new ArrayList<>();
        if (expectedLineNumbersNodes != null && expectedLineNumbersNodes.getLength() > 0) {
            Element item = (Element) expectedLineNumbersNodes.item(0);
            String numbers = item.getTextContent();
            for (String n : numbers.split(" *, *")) {
                expectedLineNumbers.add(Integer.valueOf(n));
            }
        }
        String code = getNodeValue(testCode, "code", false);
        if (code == null) {
            // Should have a coderef
            NodeList coderefs = testCode.getElementsByTagName("code-ref");
            if (coderefs.getLength() == 0) {
                throw new RuntimeException("Required tag is missing from the test-xml. Supply either a code or a code-ref tag");
            }
            Node coderef = coderefs.item(0);
            String referenceId = coderef.getAttributes().getNamedItem("id").getNodeValue();
            NodeList codeFragments = root.getElementsByTagName("code-fragment");
            for (int j = 0; j < codeFragments.getLength(); j++) {
                String fragmentId = codeFragments.item(j).getAttributes().getNamedItem("id").getNodeValue();
                if (referenceId.equals(fragmentId)) {
                    code = parseTextNode(codeFragments.item(j));
                }
            }
            if (code == null) {
                throw new RuntimeException("No matching code fragment found for coderef");
            }
        }
        String description = getNodeValue(testCode, "description", true);
        int expectedProblems = Integer.parseInt(getNodeValue(testCode, "expected-problems", true));
        String languageVersionString = getNodeValue(testCode, "source-type", false);
        if (languageVersionString == null) {
            tests[i] = new TestDescriptor(code, description, expectedProblems, rule);
        } else {
            LanguageVersion languageVersion = LanguageRegistry.findLanguageVersionByTerseName(languageVersionString);
            if (languageVersion != null) {
                tests[i] = new TestDescriptor(code, description, expectedProblems, rule, languageVersion);
            } else {
                throw new RuntimeException("Unknown LanguageVersion for test: " + languageVersionString);
            }
        }
        tests[i].setReinitializeRule(reinitializeRule);
        tests[i].setRegressionTest(isRegressionTest);
        tests[i].setUseAuxClasspath(isUseAuxClasspath);
        tests[i].setExpectedMessages(messages);
        tests[i].setExpectedLineNumbers(expectedLineNumbers);
        tests[i].setProperties(properties);
        tests[i].setNumberInDocument(i + 1);
    }
    return tests;
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Properties(java.util.Properties) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion)

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