public static void main(String[] args) { CommandLineOptions opts = new CommandLineOptions(args); List files; if (opts.containsCommaSeparatedFileList()) { files = collectFromCommaDelimitedString(opts.getInputFileName()); } else { files = collectFilesFromOneName(opts.getInputFileName()); } PMD pmd; if (opts.jdk13()) { pmd = new PMD(new TargetJDK1_3()); } else { pmd = new PMD(); } RuleContext ctx = new RuleContext(); ctx.setReport(new Report()); try { RuleSetFactory ruleSetFactory = new RuleSetFactory(); RuleSet rules = ruleSetFactory.createRuleSet(opts.getRulesets()); for (Iterator i = files.iterator(); i.hasNext(); ) { File file = (File) i.next(); ctx.setSourceCodeFilename( glomName(opts.shortNamesEnabled(), opts.getInputFileName(), file)); try { pmd.processFile(new FileInputStream(file), opts.getEncoding(), rules, ctx); } catch (PMDException pmde) { if (opts.debugEnabled()) { pmde.getReason().printStackTrace(); } ctx.getReport() .addError( new Report.ProcessingError( pmde.getMessage(), glomName(opts.shortNamesEnabled(), opts.getInputFileName(), file))); } } } catch (FileNotFoundException fnfe) { System.out.println(opts.usage()); fnfe.printStackTrace(); } catch (RuleSetNotFoundException rsnfe) { System.out.println(opts.usage()); rsnfe.printStackTrace(); } try { Renderer r = opts.createRenderer(); System.out.println(r.render(ctx.getReport())); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println(opts.usage()); if (opts.debugEnabled()) { e.printStackTrace(); } } }
public void testPluginname() throws Throwable { Rule rule = new XPathRule(); rule.addProperty("xpath", "//VariableDeclaratorId[string-length(@Image) < 3]"); rule.setMessage("{0}"); rule.addProperty("pluginname", "true"); PMD p = new PMD(); RuleContext ctx = new RuleContext(); Report report = new Report(); ctx.setReport(report); ctx.setSourceCodeFilename("n/a"); RuleSet rules = new RuleSet(); rules.addRule(rule); p.processFile(new StringReader(TEST1), rules, ctx); RuleViolation rv = (RuleViolation) report.iterator().next(); assertEquals("a", rv.getDescription()); }
/** * Performs validation of one file. * * @param source Input source file */ private void validateOne(final DataSource source) { final RuleSetFactory factory = new RuleSetFactory(); // @checkstyle MagicNumber (1 line) factory.setMinimumPriority(RulePriority.valueOf(5)); PMD.processFiles( this.config, factory, new LinkedList<DataSource>(Collections.singleton(source)), this.context, Collections.<Renderer>emptyList()); }
/** * @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 = Language.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<RuleDuration>(); RuleSetFactory factory = new RuleSetFactory(); if (StringUtil.isNotEmpty(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); } }
/** * Run PMD against a resource * * @param resource the resource to process */ protected final void reviewResource(IResource resource) { IFile file = (IFile) resource.getAdapter(IFile.class); if (file == null || file.getFileExtension() == null) return; Reader input = null; try { boolean included = isIncluded(file); log.debug("Derived files included: " + projectProperties.isIncludeDerivedFiles()); log.debug("file " + file.getName() + " is derived: " + file.isDerived()); log.debug("file checked: " + included); prepareMarkerAccumulator(file); LanguageVersionDiscoverer languageDiscoverer = new LanguageVersionDiscoverer(); LanguageVersion languageVersion = languageDiscoverer.getDefaultLanguageVersionForFile(file.getName()); // in case it is java, select the correct java version if (languageVersion != null && languageVersion.getLanguage() == Language.JAVA) { languageVersion = PMDPlugin.javaVersionFor(file.getProject()); } if (languageVersion != null) { configuration().setDefaultLanguageVersion(languageVersion); } log.debug("discovered language: " + languageVersion); final File sourceCodeFile = file.getRawLocation().toFile(); if (included && getRuleSet().applies(sourceCodeFile) && isFileInWorkingSet(file) && languageVersion != null) { subTask("PMD checking: " + file.getName()); Timer timer = new Timer(); RuleContext context = PMD.newRuleContext(file.getName(), sourceCodeFile); context.setLanguageVersion(languageVersion); input = new InputStreamReader(file.getContents(), file.getCharset()); // getPmdEngine().processFile(input, getRuleSet(), context); // getPmdEngine().processFile(sourceCodeFile, getRuleSet(), context); RuleSets rSets = new RuleSets(getRuleSet()); new SourceCodeProcessor(configuration()).processSourceCode(input, rSets, context); timer.stop(); pmdDuration += timer.getDuration(); updateMarkers(file, context, isUseTaskMarker()); worked(1); fileCount++; } else { log.debug("The file " + file.getName() + " is not in the working set"); } } catch (CoreException e) { log.error("Core exception visiting " + file.getName(), e); // TODO: // complete message } catch (PMDException e) { log.error("PMD exception visiting " + file.getName(), e); // TODO: // complete message } catch (IOException e) { log.error("IO exception visiting " + file.getName(), e); // TODO: // complete message } catch (PropertiesException e) { log.error("Properties exception visiting " + file.getName(), e); // TODO: // complete message } finally { IOUtil.closeQuietly(input); } }