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());
}
}
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);
}
}
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;
}
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();
}
}
}
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;
}
Aggregations