Search in sources :

Example 66 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testMethodInitialConstraints.

@Test
public void testMethodInitialConstraints() throws NoSuchMethodException, JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericMethodsImplicit.class);
    List<AbstractJavaNode> expressions = convertList(acu.findChildNodesWithXPath("//ArgumentList"), AbstractJavaNode.class);
    List<Variable> variables = new ArrayList<>();
    for (int i = 0; i < 2; ++i) {
        variables.add(new Variable());
    }
    Method method = GenericMethodsImplicit.class.getMethod("bar", Object.class, Object.class, Integer.class, Object.class);
    ASTArgumentList argList = (ASTArgumentList) expressions.get(0);
    List<Constraint> constraints = MethodTypeResolution.produceInitialConstraints(method, argList, variables);
    assertEquals(constraints.size(), 3);
    // A
    assertTrue(constraints.contains(new Constraint(forClass(SuperClassA.class), variables.get(0), LOOSE_INVOCATION)));
    assertTrue(constraints.contains(new Constraint(forClass(SuperClassAOther.class), variables.get(0), LOOSE_INVOCATION)));
    // B
    assertTrue(constraints.contains(new Constraint(forClass(SuperClassAOther2.class), variables.get(1), LOOSE_INVOCATION)));
}
Also used : Variable(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) SuperClassAOther(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther) SuperClassAOther2(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther2) SuperClassA(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA) Test(org.junit.Test)

Example 67 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testLiterals.

@Test
public void testLiterals() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Literals.class);
    List<ASTLiteral> literals = convertList(acu.findChildNodesWithXPath("//Literal"), ASTLiteral.class);
    int index = 0;
    // String s = "s";
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(String.class, literals.get(index++).getType());
    // boolean boolean1 = false;
    assertEquals(Boolean.TYPE, literals.get(index).getFirstDescendantOfType(ASTBooleanLiteral.class).getType());
    assertEquals(Boolean.TYPE, literals.get(index++).getType());
    // boolean boolean2 = true;
    assertEquals(Boolean.TYPE, literals.get(index).getFirstDescendantOfType(ASTBooleanLiteral.class).getType());
    assertEquals(Boolean.TYPE, literals.get(index++).getType());
    // Object obj = null;
    assertNull(literals.get(index).getFirstDescendantOfType(ASTNullLiteral.class).getType());
    assertNull(literals.get(index++).getType());
    // byte byte1 = 0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // byte byte2 = 0x0F;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // byte byte3 = -007;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // short short1 = 0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // short short2 = 0x0F;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // short short3 = -007;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // char char1 = 0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // char char2 = 0x0F;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // char char3 = 007;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // char char4 = 'a';
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Character.TYPE, literals.get(index++).getType());
    // int int1 = 0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // int int2 = 0x0F;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // int int3 = -007;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // int int4 = 'a';
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Character.TYPE, literals.get(index++).getType());
    // long long1 = 0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // long long2 = 0x0F;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // long long3 = -007;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // long long4 = 0L;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Long.TYPE, literals.get(index++).getType());
    // long long5 = 0x0Fl;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Long.TYPE, literals.get(index++).getType());
    // long long6 = -007L;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Long.TYPE, literals.get(index++).getType());
    // long long7 = 'a';
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Character.TYPE, literals.get(index++).getType());
    // float float1 = 0.0f;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Float.TYPE, literals.get(index++).getType());
    // float float2 = -10e+01f;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Float.TYPE, literals.get(index++).getType());
    // float float3 = 0x08.08p3f;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Float.TYPE, literals.get(index++).getType());
    // float float4 = 0xFF;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // float float5 = 'a';
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Character.TYPE, literals.get(index++).getType());
    // double double1 = 0.0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Double.TYPE, literals.get(index++).getType());
    // double double2 = -10e+01;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Double.TYPE, literals.get(index++).getType());
    // double double3 = 0x08.08p3;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Double.TYPE, literals.get(index++).getType());
    // double double4 = 0xFF;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // double double5 = 'a';
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Character.TYPE, literals.get(index++).getType());
    // Make sure we got them all.
    assertEquals("All literals not tested", index, literals.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) Test(org.junit.Test)

Example 68 with ASTCompilationUnit

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

the class StatementAndBraceFinderTest method testOnlyWorksForMethodsAndConstructors.

@Test(expected = RuntimeException.class)
public void testOnlyWorksForMethodsAndConstructors() {
    StatementAndBraceFinder sbf = new StatementAndBraceFinder(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler().getDataFlowHandler());
    sbf.buildDataFlowFor(new ASTMethodDeclaration(1));
    sbf.buildDataFlowFor(new ASTConstructorDeclaration(1));
    sbf.buildDataFlowFor(new ASTCompilationUnit(1));
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Test(org.junit.Test)

Example 69 with ASTCompilationUnit

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

the class ProjectMemoizerTest method forceMemoizationTest.

@Test
public void forceMemoizationTest() {
    ASTCompilationUnit acu = ParserTstUtil.parseJavaDefaultVersion(MetricsVisitorTestData.class);
    List<Integer> reference = visitWith(acu, true);
    List<Integer> real = visitWith(acu, true);
    assertEquals(reference.size(), real.size());
    // we force recomputation so each result should be different
    for (int i = 0; i < reference.size(); i++) {
        assertNotEquals(reference.get(i), real.get(i));
    }
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Test(org.junit.Test)

Example 70 with ASTCompilationUnit

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

the class SignatureTest method testSetterDetection.

@Test
public void testSetterDetection() {
    ASTCompilationUnit compilationUnit = parseJava17(SetterDetection.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)

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