Search in sources :

Example 6 with LanguageVersion

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

the class SourceCodeProcessor method processSource.

private void processSource(Reader sourceCode, RuleSets ruleSets, RuleContext ctx) {
    LanguageVersion languageVersion = ctx.getLanguageVersion();
    LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
    Parser parser = PMD.parserFor(languageVersion, configuration);
    Node rootNode = parse(ctx, sourceCode, parser);
    resolveQualifiedNames(rootNode, languageVersionHandler);
    symbolFacade(rootNode, languageVersionHandler);
    Language language = languageVersion.getLanguage();
    usesDFA(languageVersion, rootNode, ruleSets, language);
    usesTypeResolution(languageVersion, rootNode, ruleSets, language);
    usesMultifile(rootNode, languageVersionHandler, ruleSets, language);
    List<Node> acus = Collections.singletonList(rootNode);
    ruleSets.apply(acus, ctx, language);
}
Also used : Language(net.sourceforge.pmd.lang.Language) Node(net.sourceforge.pmd.lang.ast.Node) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) Parser(net.sourceforge.pmd.lang.Parser)

Example 7 with LanguageVersion

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

the class PMDParameters method toConfiguration.

/**
 * Converts these parameters into a configuration.
 *
 * @return A new PMDConfiguration corresponding to these parameters
 *
 * @throws IllegalArgumentException if the parameters are inconsistent or incomplete
 */
public PMDConfiguration toConfiguration() {
    if (null == this.getSourceDir() && null == this.getUri() && null == this.getFileListPath()) {
        throw new IllegalArgumentException("Please provide a parameter for source root directory (-dir or -d), database URI (-uri or -u), or file list path (-filelist).");
    }
    PMDConfiguration configuration = new PMDConfiguration();
    configuration.setInputPaths(this.getSourceDir());
    configuration.setInputFilePath(this.getFileListPath());
    configuration.setInputUri(this.getUri());
    configuration.setReportFormat(this.getFormat());
    configuration.setBenchmark(this.isBenchmark());
    configuration.setDebug(this.isDebug());
    configuration.setMinimumPriority(this.getMinimumPriority());
    configuration.setReportFile(this.getReportfile());
    configuration.setReportProperties(this.getProperties());
    configuration.setReportShortNames(this.isShortnames());
    configuration.setRuleSets(this.getRulesets());
    configuration.setRuleSetFactoryCompatibilityEnabled(!this.noRuleSetCompatibility);
    configuration.setShowSuppressedViolations(this.isShowsuppressed());
    configuration.setSourceEncoding(this.getEncoding());
    configuration.setStressTest(this.isStress());
    configuration.setSuppressMarker(this.getSuppressmarker());
    configuration.setThreads(this.getThreads());
    configuration.setFailOnViolation(this.isFailOnViolation());
    configuration.setAnalysisCacheLocation(this.cacheLocation);
    configuration.setIgnoreIncrementalAnalysis(this.isIgnoreIncrementalAnalysis());
    LanguageVersion languageVersion = LanguageRegistry.findLanguageVersionByTerseName(this.getLanguage() + ' ' + this.getVersion());
    if (languageVersion != null) {
        configuration.getLanguageVersionDiscoverer().setDefaultLanguageVersion(languageVersion);
    }
    try {
        configuration.prependClasspath(this.getAuxclasspath());
    } catch (IOException e) {
        throw new IllegalArgumentException("Invalid auxiliary classpath: " + e.getMessage(), e);
    }
    return configuration;
}
Also used : LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) IOException(java.io.IOException) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration)

Example 8 with LanguageVersion

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

the class Benchmarker method main.

/**
 * @param args
 *            String[]
 * @throws RuleSetNotFoundException
 * @throws IOException
 * @throws PMDException
 */
public static void main(String[] args) throws RuleSetNotFoundException, IOException, PMDException {
    String targetjdk = findOptionalStringValue(args, "--targetjdk", "1.4");
    Language language = LanguageRegistry.getLanguage("Java");
    LanguageVersion languageVersion = language.getVersion(targetjdk);
    if (languageVersion == null) {
        languageVersion = language.getDefaultVersion();
    }
    String srcDir = findOptionalStringValue(args, "--source-directory", "/usr/local/java/src/java/lang/");
    List<DataSource> dataSources = FileUtil.collectFiles(srcDir, new LanguageFilenameFilter(language));
    boolean debug = findBooleanSwitch(args, "--debug");
    boolean parseOnly = findBooleanSwitch(args, "--parse-only");
    if (debug) {
        System.out.println("Using " + language.getName() + " " + languageVersion.getVersion());
    }
    if (parseOnly) {
        Parser parser = PMD.parserFor(languageVersion, null);
        parseStress(parser, dataSources, debug);
    } else {
        String ruleset = findOptionalStringValue(args, "--ruleset", "");
        if (debug) {
            System.out.println("Checking directory " + srcDir);
        }
        Set<RuleDuration> results = new TreeSet<>();
        RuleSetFactory factory = new RuleSetFactory();
        if (StringUtils.isNotBlank(ruleset)) {
            stress(languageVersion, factory.createRuleSet(ruleset), dataSources, results, debug);
        } else {
            Iterator<RuleSet> i = factory.getRegisteredRuleSets();
            while (i.hasNext()) {
                stress(languageVersion, i.next(), dataSources, results, debug);
            }
        }
        TextReport report = new TextReport();
        report.generate(results, System.err);
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) LanguageFilenameFilter(net.sourceforge.pmd.lang.LanguageFilenameFilter) DataSource(net.sourceforge.pmd.util.datasource.DataSource) Parser(net.sourceforge.pmd.lang.Parser) RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) Language(net.sourceforge.pmd.lang.Language) TreeSet(java.util.TreeSet) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion)

Example 9 with LanguageVersion

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

the class LanguageVersionDiscovererTest method testPlsql.

/**
 * Test on PLSQL file with default version
 */
@Test
public void testPlsql() {
    LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer();
    File plsqlFile = new File("/path/to/MY_PACKAGE.sql");
    LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(plsqlFile);
    assertEquals("LanguageVersion must be PLSQL!", LanguageRegistry.getLanguage(PLSQLLanguageModule.NAME).getDefaultVersion(), languageVersion);
}
Also used : LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) LanguageVersionDiscoverer(net.sourceforge.pmd.lang.LanguageVersionDiscoverer) File(java.io.File) Test(org.junit.Test)

Example 10 with LanguageVersion

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

the class LanguageVersionDiscovererTest method testJavaFileUsingDefaults.

/**
 * Test on Java file with default options.
 */
@Test
public void testJavaFileUsingDefaults() {
    LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer();
    File javaFile = new File("/path/to/MyClass.java");
    LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(javaFile);
    assertEquals("LanguageVersion must be Java 9 !", LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("9"), languageVersion);
}
Also used : LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) LanguageVersionDiscoverer(net.sourceforge.pmd.lang.LanguageVersionDiscoverer) File(java.io.File) 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