Search in sources :

Example 51 with RuleContext

use of net.sourceforge.pmd.RuleContext in project pmd-eclipse-plugin by pmd.

the class AbstractStructureInspectorPage method getPMDMethods.

/**
 * Gets a List of all PMD-Methods.
 *
 * @return an List of ASTMethodDeclarations
 */
private List<ASTMethodDeclaration> getPMDMethods() {
    List<ASTMethodDeclaration> methodList = new ArrayList<ASTMethodDeclaration>();
    // PMD needs this Resource as a String
    try {
        DFAGraphRule dfaGraphRule = new JavaDFAGraphRule();
        RuleSet rs = RuleSetUtil.newSingle(dfaGraphRule);
        RuleContext ctx = new RuleContext();
        ctx.setSourceCodeFilename("[scratchpad]");
        // StringReader reader = new StringReader(getDocument().get());
        // run PMD using the DFAGraphRule and the Text of the Resource
        // new PMDEngine().processFile(reader, rs, ctx);
        byte[] bytes = getDocument().get().getBytes();
        InputStream input = new ByteArrayInputStream(bytes);
        RuleSets rSets = new RuleSets(rs);
        new SourceCodeProcessor(new PMDConfiguration()).processSourceCode(input, rSets, ctx);
        // the Rule then can give us the Methods
        for (DFAGraphMethod m : dfaGraphRule.getMethods()) {
            if (m instanceof ASTMethodDeclaration) {
                methodList.add((ASTMethodDeclaration) m);
            }
        }
        Collections.sort(methodList, ASTUtil.METHOD_COMPARATOR);
    } catch (PMDException pmde) {
        logError(StringKeys.ERROR_PMD_EXCEPTION + toString(), pmde);
    }
    return methodList;
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) RuleContext(net.sourceforge.pmd.RuleContext) JavaDFAGraphRule(net.sourceforge.pmd.lang.java.dfa.JavaDFAGraphRule) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) DFAGraphMethod(net.sourceforge.pmd.lang.dfa.DFAGraphMethod) JavaDFAGraphRule(net.sourceforge.pmd.lang.java.dfa.JavaDFAGraphRule) DFAGraphRule(net.sourceforge.pmd.lang.dfa.DFAGraphRule) ByteArrayInputStream(java.io.ByteArrayInputStream) RuleSets(net.sourceforge.pmd.RuleSets) PMDException(net.sourceforge.pmd.PMDException) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration)

Aggregations

RuleContext (net.sourceforge.pmd.RuleContext)51 Test (org.junit.Test)19 RuleSets (net.sourceforge.pmd.RuleSets)17 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)15 Report (net.sourceforge.pmd.Report)14 Node (net.sourceforge.pmd.lang.ast.Node)13 RuleSet (net.sourceforge.pmd.RuleSet)11 StringReader (java.io.StringReader)9 RuleViolation (net.sourceforge.pmd.RuleViolation)9 IOException (java.io.IOException)8 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)8 PMDException (net.sourceforge.pmd.PMDException)8 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)8 ArrayList (java.util.ArrayList)7 SourceCodeProcessor (net.sourceforge.pmd.SourceCodeProcessor)7 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)6 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)6 DummyNode (net.sourceforge.pmd.lang.ast.DummyNode)5 FooRule (net.sourceforge.pmd.FooRule)4 Parser (net.sourceforge.pmd.lang.Parser)4