Search in sources :

Example 16 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class ClassTypeResolverTest method testJavaTypeDefinitionGetSuperTypeSet.

@Test
public void testJavaTypeDefinitionGetSuperTypeSet() {
    JavaTypeDefinition originalTypeDef = forClass(List.class, forClass(Integer.class));
    Set<JavaTypeDefinition> set = originalTypeDef.getSuperTypeSet();
    assertEquals(set.size(), 4);
    assertTrue(set.contains(forClass(Object.class)));
    assertTrue(set.contains(originalTypeDef));
    assertTrue(set.contains(forClass(Collection.class, forClass(Integer.class))));
    assertTrue(set.contains(forClass(Iterable.class, forClass(Integer.class))));
}
Also used : JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) Test(org.junit.Test)

Example 17 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class ClassTypeResolverTest method testMethodParameterization.

@Test
public void testMethodParameterization() throws JaxenException, NoSuchMethodException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericMethodsImplicit.class);
    List<AbstractJavaNode> expressions = convertList(acu.findChildNodesWithXPath("//ArgumentList"), AbstractJavaNode.class);
    JavaTypeDefinition context = forClass(GenericMethodsImplicit.class, forClass(Thread.class));
    Method method = GenericMethodsImplicit.class.getMethod("bar", Object.class, Object.class, Integer.class, Object.class);
    ASTArgumentList argList = (ASTArgumentList) expressions.get(0);
    MethodType inferedMethod = MethodTypeResolution.parameterizeInvocation(context, method, argList);
    assertEquals(inferedMethod.getParameterTypes().get(0), forClass(SuperClassA2.class));
    assertEquals(inferedMethod.getParameterTypes().get(1), forClass(SuperClassA2.class));
    assertEquals(inferedMethod.getParameterTypes().get(2), forClass(Integer.class));
    assertEquals(inferedMethod.getParameterTypes().get(3), forClass(SuperClassAOther2.class));
}
Also used : MethodType(net.sourceforge.pmd.lang.java.typeresolution.MethodType) SuperClassA2(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA2) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) SuperClassAOther2(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther2) Method(java.lang.reflect.Method) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) Test(org.junit.Test)

Example 18 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class ClassTypeResolverTest method testJavaTypeDefinitionEquals.

@Test
public void testJavaTypeDefinitionEquals() {
    JavaTypeDefinition a = forClass(Integer.class);
    JavaTypeDefinition b = forClass(Integer.class);
    // test non-generic types
    assertEquals(a, b);
    assertNotEquals(a, null);
    // test generic arg equality
    b = forClass(List.class, a);
    a = forClass(List.class, a);
    assertEquals(a, b);
    a = forClass(List.class, forClass(String.class));
    assertNotEquals(a, b);
    assertNotEquals(b, a);
    // test raw vs proper, proper vs raw
    a = forClass(JavaTypeDefinitionEquals.class);
    b = forClass(JavaTypeDefinitionEquals.class, forClass(List.class, a));
    assertEquals(a, b);
    assertEquals(b, a);
}
Also used : JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) ArrayList(java.util.ArrayList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) JavaTypeDefinitionEquals(net.sourceforge.pmd.typeresolution.testdata.dummytypes.JavaTypeDefinitionEquals) Test(org.junit.Test)

Example 19 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class ClassTypeResolverTest method testMethodInitialBounds.

@Test
public void testMethodInitialBounds() throws NoSuchMethodException {
    JavaTypeDefinition context = forClass(GenericMethodsImplicit.class, forClass(Thread.class));
    List<Variable> variables = new ArrayList<>();
    List<Bound> initialBounds = new ArrayList<>();
    Method method = GenericMethodsImplicit.class.getMethod("foo");
    MethodTypeResolution.produceInitialBounds(method, context, variables, initialBounds);
    assertEquals(initialBounds.size(), 6);
    // A
    assertTrue(initialBounds.contains(new Bound(variables.get(0), forClass(Object.class), SUBTYPE)));
    // B
    assertTrue(initialBounds.contains(new Bound(variables.get(1), forClass(Number.class), SUBTYPE)));
    assertTrue(initialBounds.contains(new Bound(variables.get(1), forClass(Runnable.class), SUBTYPE)));
    // C
    assertTrue(initialBounds.contains(new Bound(variables.get(2), variables.get(3), SUBTYPE)));
    assertTrue(initialBounds.contains(new Bound(variables.get(2), forClass(Object.class), SUBTYPE)));
    // D
    assertTrue(initialBounds.contains(new Bound(variables.get(3), forClass(Thread.class), SUBTYPE)));
}
Also used : Variable(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) ArrayList(java.util.ArrayList) Bound(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Bound) AnoymousExtendingObject(net.sourceforge.pmd.typeresolution.testdata.AnoymousExtendingObject) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)19 ArrayList (java.util.ArrayList)11 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)6 Test (org.junit.Test)6 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)5 List (java.util.List)4 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)4 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)3 Method (java.lang.reflect.Method)2 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)2 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)2 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)2 ASTTypeArguments (net.sourceforge.pmd.lang.java.ast.ASTTypeArguments)2 Bound (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Bound)2 Variable (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable)2 AnoymousExtendingObject (net.sourceforge.pmd.typeresolution.testdata.AnoymousExtendingObject)2 SuperClassA2 (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA2)2 Type (java.lang.reflect.Type)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1