Search in sources :

Example 16 with LanguageVersion

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);
}
Also used : LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion)

Example 17 with LanguageVersion

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());
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) RuleContext(net.sourceforge.pmd.RuleContext) Node(net.sourceforge.pmd.lang.ast.Node) StringReader(java.io.StringReader) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) Parser(net.sourceforge.pmd.lang.Parser) Test(org.junit.Test)

Example 18 with LanguageVersion

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;
}
Also used : LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) ResourceNotFoundException(org.codehaus.plexus.resource.loader.ResourceNotFoundException) MavenReportException(org.apache.maven.reporting.MavenReportException) IOException(java.io.IOException) FileResourceCreationException(org.codehaus.plexus.resource.loader.FileResourceCreationException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 19 with LanguageVersion

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()]);
}
Also used : ArrayList(java.util.ArrayList) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) Parser(net.sourceforge.pmd.lang.Parser)

Example 20 with LanguageVersion

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;
}
Also used : Language(net.sourceforge.pmd.lang.Language) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) Rule(net.sourceforge.pmd.Rule) LanguageVersionDiscoverer(net.sourceforge.pmd.lang.LanguageVersionDiscoverer) HashSet(java.util.HashSet)

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