Search in sources :

Example 31 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class JavaRuleViolation method isSupressed.

/**
 * Check for suppression on this node, on parents, and on contained types
 * for ASTCompilationUnit
 *
 * @param node
 */
public static boolean isSupressed(Node node, Rule rule) {
    boolean result = suppresses(node, rule);
    if (!result && node instanceof ASTCompilationUnit) {
        for (int i = 0; !result && i < node.jjtGetNumChildren(); i++) {
            result = suppresses(node.jjtGetChild(i), rule);
        }
    }
    if (!result) {
        Node parent = node.jjtGetParent();
        while (!result && parent != null) {
            result = suppresses(parent, rule);
            parent = parent.jjtGetParent();
        }
    }
    return result;
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Node(net.sourceforge.pmd.lang.ast.Node) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Example 32 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class ClassTypeResolverJava8Test method testSuperExpression.

@Test
public void testSuperExpression() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass18(SuperExpression.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix"), AbstractJavaTypeNode.class);
    int index = 0;
    assertEquals(SuperClass.class, expressions.get(index++).getType());
    // Make sure we got them all
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 33 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class NcssCountImportsDecorator method visit.

@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    ASTCompilationUnit acu = node.getFirstParentOfType(ASTCompilationUnit.class);
    List<ASTImportDeclaration> imports = acu.findChildrenOfType(ASTImportDeclaration.class);
    int increment = imports.size();
    if (!acu.findChildrenOfType(ASTPackageDeclaration.class).isEmpty()) {
        increment++;
    }
    ((MutableInt) data).add(increment);
    return super.visit(node, data);
}
Also used : ASTImportDeclaration(net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) MutableInt(org.apache.commons.lang3.mutable.MutableInt)

Example 34 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class ParserTstUtil method parseJava.

/**
 * Parses Java code and executes the symbol table visitor.
 *
 * @param languageVersionHandler The version handler for the wanted version
 * @param code                   The source code
 *
 * @return The compilation unit
 */
public static ASTCompilationUnit parseJava(LanguageVersionHandler languageVersionHandler, String code) {
    ASTCompilationUnit rootNode = (ASTCompilationUnit) languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
    languageVersionHandler.getQualifiedNameResolutionFacade(ParserTstUtil.class.getClassLoader()).start(rootNode);
    languageVersionHandler.getSymbolFacade().start(rootNode);
    return rootNode;
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) StringReader(java.io.StringReader)

Example 35 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class ParserTstUtil method getOrderedNodes.

public static <E> List<E> getOrderedNodes(Class<E> clazz, String javaCode) {
    Collector<E> coll = new Collector<>(clazz, new ArrayList<E>());
    LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
    ASTCompilationUnit cu = (ASTCompilationUnit) languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(javaCode));
    JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(), new Class[] { JavaParserVisitor.class }, coll);
    jpv.visit(cu, null);
    new QualifiedNameResolver().initializeWith(ParserTstUtil.class.getClassLoader(), cu);
    new SymbolFacade().initializeWith(cu);
    new DataFlowFacade().initializeWith(languageVersionHandler.getDataFlowHandler(), cu);
    return (List<E>) coll.getCollection();
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) QualifiedNameResolver(net.sourceforge.pmd.lang.java.qname.QualifiedNameResolver) SymbolFacade(net.sourceforge.pmd.lang.java.symboltable.SymbolFacade) StringReader(java.io.StringReader) DataFlowFacade(net.sourceforge.pmd.lang.java.dfa.DataFlowFacade) ArrayList(java.util.ArrayList) List(java.util.List) JavaParserVisitor(net.sourceforge.pmd.lang.java.ast.JavaParserVisitor)

Aggregations

ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)79 Test (org.junit.Test)66 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)36 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)27 StringReader (java.io.StringReader)8 RuleContext (net.sourceforge.pmd.RuleContext)8 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)8 ASTImportDeclaration (net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration)7 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)6 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)5 ArrayList (java.util.ArrayList)4 ParserOptions (net.sourceforge.pmd.lang.ParserOptions)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)2 Parser (net.sourceforge.pmd.lang.Parser)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2