Search in sources :

Example 31 with ASTMethodDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration 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

ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)31 Test (org.junit.Test)15 Node (net.sourceforge.pmd.lang.ast.Node)8 List (java.util.List)6 Map (java.util.Map)5 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)5 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)5 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)5 ArrayList (java.util.ArrayList)4 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)4 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)3 JavaParserVisitorAdapter (net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter)3 RuleContext (net.sourceforge.pmd.RuleContext)2 Structure (net.sourceforge.pmd.lang.dfa.Structure)2 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)2 ASTResultType (net.sourceforge.pmd.lang.java.ast.ASTResultType)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)2