use of net.sourceforge.pmd.lang.LanguageVersion in project pmd by pmd.
the class RuleSet method applies.
/**
* Does the given Rule apply to the given LanguageVersion? If so, the
* Language must be the same and be between the minimum and maximums
* versions on the Rule.
*
* @param rule
* The rule.
* @param languageVersion
* The language version.
*
* @return <code>true</code> if the given rule matches the given language,
* which means, that the rule would be executed.
*/
public static boolean applies(Rule rule, LanguageVersion languageVersion) {
final LanguageVersion min = rule.getMinimumLanguageVersion();
final LanguageVersion max = rule.getMaximumLanguageVersion();
return rule.getLanguage().equals(languageVersion.getLanguage()) && (min == null || min.compareTo(languageVersion) <= 0) && (max == null || max.compareTo(languageVersion) >= 0);
}
use of net.sourceforge.pmd.lang.LanguageVersion in project pmd by pmd.
the class StdCyclomaticComplexityRuleTest method entryStackMustBeEmpty.
/**
* Make sure the entry stack is empty, if show classes complexity is
* disabled.
*
* @see <a href="https://sourceforge.net/p/pmd/bugs/1501/">bug #1501</a>
*/
@Test
public void entryStackMustBeEmpty() {
StdCyclomaticComplexityRule rule = new StdCyclomaticComplexityRule();
rule.setProperty(StdCyclomaticComplexityRule.SHOW_CLASSES_COMPLEXITY_DESCRIPTOR, Boolean.FALSE);
RuleContext ctx = new RuleContext();
LanguageVersion javaLanguageVersion = LanguageRegistry.getLanguage(ApexLanguageModule.NAME).getDefaultVersion();
ParserOptions parserOptions = javaLanguageVersion.getLanguageVersionHandler().getDefaultParserOptions();
Parser parser = javaLanguageVersion.getLanguageVersionHandler().getParser(parserOptions);
Node node = parser.parse("test", new StringReader("public class SampleClass {}"));
rule.apply(Arrays.asList(node), ctx);
Assert.assertTrue(rule.entryStack.isEmpty());
}
use of net.sourceforge.pmd.lang.LanguageVersion in project maven-plugins by apache.
the class PmdReport method getPMDConfiguration.
/**
* Constructs the PMD configuration class, passing it an argument that configures the target JDK.
*
* @return the resulting PMD
* @throws org.apache.maven.reporting.MavenReportException if targetJdk is not supported
*/
public PMDConfiguration getPMDConfiguration() throws MavenReportException {
PMDConfiguration configuration = new PMDConfiguration();
LanguageVersion languageVersion = null;
if (("java".equals(language) || null == language) && null != targetJdk) {
languageVersion = LanguageRegistry.findLanguageVersionByTerseName("java " + targetJdk);
if (languageVersion == null) {
throw new MavenReportException("Unsupported targetJdk value '" + targetJdk + "'.");
}
} else if ("javascript".equals(language) || "ecmascript".equals(language)) {
languageVersion = LanguageRegistry.findLanguageVersionByTerseName("ecmascript");
} else if ("jsp".equals(language)) {
languageVersion = LanguageRegistry.findLanguageVersionByTerseName("jsp");
}
if (languageVersion != null) {
getLog().debug("Using language " + languageVersion);
configuration.setDefaultLanguageVersion(languageVersion);
}
if (typeResolution) {
try {
@SuppressWarnings("unchecked") List<String> classpath = includeTests ? project.getTestClasspathElements() : project.getCompileClasspathElements();
getLog().debug("Using aux classpath: " + classpath);
configuration.prependClasspath(StringUtils.join(classpath.iterator(), File.pathSeparator));
} catch (Exception e) {
throw new MavenReportException(e.getMessage(), e);
}
}
if (null != suppressMarker) {
configuration.setSuppressMarker(suppressMarker);
}
configuration.setBenchmark(benchmark);
if (analysisCache) {
configuration.setAnalysisCacheLocation(analysisCacheLocation);
getLog().debug("Using analysis cache location: " + analysisCacheLocation);
}
return configuration;
}
use of net.sourceforge.pmd.lang.LanguageVersion in project Gargoyle by callakrsos.
the class PMDUtil method getSupportedLanguageVersions.
/**
* 지원가능한 PMD Language를 리턴.
*
* @작성자 : KYJ
* @작성일 : 2016. 10. 4.
* @return
*/
public static LanguageVersion[] getSupportedLanguageVersions() {
List<LanguageVersion> languageVersions = new ArrayList<>();
for (LanguageVersion languageVersion : LanguageRegistry.findAllVersions()) {
LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
if (languageVersionHandler != null) {
Parser parser = languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions());
if (parser != null && parser.canParse()) {
languageVersions.add(languageVersion);
LOGGER.debug("support parser: {}", parser.toString());
} else {
LOGGER.debug("not support parser: {}", parser.toString());
}
} else {
LOGGER.debug("not support parser (handler is null): {}", languageVersion.toString());
}
}
return languageVersions.toArray(new LanguageVersion[languageVersions.size()]);
}
use of net.sourceforge.pmd.lang.LanguageVersion in project Gargoyle by callakrsos.
the class DoPMD method getApplicableLanguages.
private Set<Language> getApplicableLanguages(GargoylePMDConfiguration 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);
}
}
return languages;
}
Aggregations