Search in sources :

Example 26 with ASTCompilationUnit

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

the class ParserTstUtil method getNodes.

public static <E> Set<E> getNodes(LanguageVersion languageVersion, Class<E> clazz, String javaCode) {
    Collector<E> coll = new Collector<>(clazz);
    LanguageVersionHandler languageVersionHandler = languageVersion.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);
    return (Set<E>) coll.getCollection();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) StringReader(java.io.StringReader) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) JavaParserVisitor(net.sourceforge.pmd.lang.java.ast.JavaParserVisitor)

Example 27 with ASTCompilationUnit

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

the class ProjectMemoizerTest method memoizationTest.

@Test
public void memoizationTest() {
    ASTCompilationUnit acu = ParserTstUtil.parseJavaDefaultVersion(MetricsVisitorTestData.class);
    List<Integer> expected = visitWith(acu, true);
    List<Integer> real = visitWith(acu, false);
    assertEquals(expected, real);
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Test(org.junit.Test)

Example 28 with ASTCompilationUnit

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

the class SignatureTest method testGetterDetection.

@Test
public void testGetterDetection() {
    ASTCompilationUnit compilationUnit = parseJava17(GetterDetection.class);
    compilationUnit.jjtAccept(new JavaParserVisitorAdapter() {

        @Override
        public Object visit(ASTMethodDeclaration node, Object data) {
            assertEquals(Role.GETTER_OR_SETTER, Role.get(node));
            return data;
        }
    }, null);
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) JavaParserVisitorAdapter(net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter) Test(org.junit.Test)

Example 29 with ASTCompilationUnit

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

the class JavaMultifileVisitorTest method testOperationsAreThere.

@Test
public void testOperationsAreThere() {
    ASTCompilationUnit acu = parseAndVisitForClass(MultifileVisitorTestData2.class);
    final ProjectMirror toplevel = PackageStats.INSTANCE;
    final JavaOperationSigMask opMask = new JavaOperationSigMask();
    // We could parse qnames from string but probably simpler to do that
    acu.jjtAccept(new JavaParserVisitorAdapter() {

        @Override
        public Object visit(ASTMethodDeclaration node, Object data) {
            assertTrue(toplevel.hasMatchingSig(node.getQualifiedName(), opMask));
            return data;
        }
    }, null);
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) JavaOperationSigMask(net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSigMask) JavaParserVisitorAdapter(net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter) Test(org.junit.Test)

Example 30 with ASTCompilationUnit

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

the class JavaMultifileVisitorTest method parseAndVisitForClass.

static ASTCompilationUnit parseAndVisitForClass(Class<?> clazz) {
    ASTCompilationUnit acu = ParserTstUtil.parseJavaDefaultVersion(clazz);
    LanguageVersionHandler handler = ParserTstUtil.getDefaultLanguageVersionHandler();
    handler.getQualifiedNameResolutionFacade(JavaMultifileVisitorTest.class.getClassLoader()).start(acu);
    handler.getTypeResolutionFacade(JavaMultifileVisitorTest.class.getClassLoader()).start(acu);
    handler.getMultifileFacade().start(acu);
    return acu;
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler)

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