Search in sources :

Example 1 with Variable

use of net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable in project pmd by pmd.

the class TypeInferenceTest method testResolution.

@Test
public void testResolution() {
    List<Bound> bounds = new ArrayList<>();
    bounds.add(new Bound(JavaTypeDefinition.forClass(SuperClassA.class), alpha, SUBTYPE));
    bounds.add(new Bound(JavaTypeDefinition.forClass(SuperClassAOther.class), alpha, SUBTYPE));
    Map<Variable, JavaTypeDefinition> result = TypeInferenceResolver.resolveVariables(bounds);
    assertEquals(1, result.size());
    assertEquals(JavaTypeDefinition.forClass(SuperClassA2.class), result.get(alpha));
}
Also used : SuperClassA2(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA2) Variable(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) Bound(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Bound) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with Variable

use of net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable in project pmd by pmd.

the class MethodTypeResolution method produceInitialBounds.

public static void produceInitialBounds(Method method, JavaTypeDefinition context, List<Variable> variables, List<Bound> initialBounds) {
    // https://docs.oracle.com/javase/specs/jls/se8/html/jls-18.html#jls-18.1.3
    // When inference begins, a bound set is typically generated from a list of type parameter declarations P1,
    // ..., Pp and associated inference variables α1, ..., αp. Such a bound set is constructed as follows. For
    // each l (1 ≤ l ≤ p):
    TypeVariable<Method>[] typeVariables = method.getTypeParameters();
    variables.clear();
    for (int i = 0; i < typeVariables.length; ++i) {
        variables.add(new Variable());
    }
    for (int currVarIndex = 0; currVarIndex < typeVariables.length; ++currVarIndex) {
        Type[] bounds = typeVariables[currVarIndex].getBounds();
        boolean currVarHasNoProperUpperBound = true;
        for (Type bound : bounds) {
            // Otherwise, for each type T delimited by & in the TypeBound, the bound αl <: T[P1:=α1, ..., Pp:=αp]
            // appears in the set; if this results in no proper upper bounds for αl (only dependencies), then the
            // bound α <: Object also appears in the set.
            int boundVarIndex = -1;
            if (bound instanceof TypeVariable) {
                boundVarIndex = JavaTypeDefinition.getGenericTypeIndex(typeVariables, ((TypeVariable<?>) bound).getName());
            }
            if (boundVarIndex != -1) {
                initialBounds.add(new Bound(variables.get(currVarIndex), variables.get(boundVarIndex), SUBTYPE));
            } else {
                currVarHasNoProperUpperBound = false;
                initialBounds.add(new Bound(variables.get(currVarIndex), context.resolveTypeDefinition(bound), SUBTYPE));
            }
        }
        // If Pl has no TypeBound, the bound αl <: Object appears in the set.
        if (currVarHasNoProperUpperBound) {
            initialBounds.add(new Bound(variables.get(currVarIndex), JavaTypeDefinition.forClass(Object.class), SUBTYPE));
        }
    }
}
Also used : Type(java.lang.reflect.Type) Variable(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable) TypeVariable(java.lang.reflect.TypeVariable) TypeVariable(java.lang.reflect.TypeVariable) Bound(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Bound) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)

Example 3 with Variable

use of net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable 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)

Example 4 with Variable

use of net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable 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)

Aggregations

Variable (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable)4 ArrayList (java.util.ArrayList)3 Bound (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Bound)3 Test (org.junit.Test)3 Method (java.lang.reflect.Method)2 JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)2 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)2 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)1 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)1 AnoymousExtendingObject (net.sourceforge.pmd.typeresolution.testdata.AnoymousExtendingObject)1 SuperClassA (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA)1 SuperClassA2 (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA2)1 SuperClassAOther (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther)1 SuperClassAOther2 (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther2)1