Search in sources :

Example 1 with Constraint

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

the class TypeInferenceTest method testContainmentReduceTypeVsType.

@Test
public void testContainmentReduceTypeVsType() {
    // A constraint formula of the form ‹S <= T›, where S and T are type arguments (§4.5.1), is reduced as
    // follows:
    // If T is a type: // If S is a type, the constraint reduces to ‹S = T›.
    List<BoundOrConstraint> result = new Constraint(number, integer, CONTAINS).reduce();
    assertEquals(1, result.size());
    testBoundOrConstraint(result.get(0), number, integer, EQUALITY, Constraint.class);
// If T is a type: // If S is a wildcard, the constraint reduces to false. TODO
// If T is a wildcard of the form ?, the constraint reduces to true. TODO
// If T is a wildcard of the form ? extends T': TODO
// If T is a wildcard of the form ? super T': TODO
}
Also used : BoundOrConstraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.BoundOrConstraint) BoundOrConstraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.BoundOrConstraint) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) Test(org.junit.Test)

Example 2 with Constraint

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

the class TypeInferenceTest method testSubtypeReduceAnyVsVariable.

@Test
public void testSubtypeReduceAnyVsVariable() {
    // Otherwise, if T is an inference variable, α, the constraint reduces to the bound S <: α.
    List<BoundOrConstraint> result = new Constraint(integer, alpha, SUBTYPE).reduce();
    assertEquals(1, result.size());
    testBoundOrConstraint(result.get(0), integer, alpha, SUBTYPE, Bound.class);
    result = new Constraint(alpha, beta, SUBTYPE).reduce();
    assertEquals(1, result.size());
    testBoundOrConstraint(result.get(0), alpha, beta, SUBTYPE, Bound.class);
}
Also used : BoundOrConstraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.BoundOrConstraint) BoundOrConstraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.BoundOrConstraint) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) Test(org.junit.Test)

Example 3 with Constraint

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

the class TypeInferenceTest method testLooseInvocationAnythingElse.

@Test
public void testLooseInvocationAnythingElse() {
    // Otherwise, the constraint reduces to ‹S<:T›.
    List<BoundOrConstraint> result = new Constraint(number, alpha, LOOSE_INVOCATION).reduce();
    assertEquals(1, result.size());
    testBoundOrConstraint(result.get(0), number, alpha, SUBTYPE, Constraint.class);
    result = new Constraint(alpha, number, LOOSE_INVOCATION).reduce();
    assertEquals(1, result.size());
    testBoundOrConstraint(result.get(0), alpha, number, SUBTYPE, Constraint.class);
}
Also used : BoundOrConstraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.BoundOrConstraint) BoundOrConstraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.BoundOrConstraint) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) Test(org.junit.Test)

Example 4 with Constraint

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

the class MethodTypeResolution method produceInitialConstraints.

public static List<Constraint> produceInitialConstraints(Method method, ASTArgumentList argList, List<Variable> variables) {
    List<Constraint> result = new ArrayList<>();
    Type[] methodParameters = method.getGenericParameterTypes();
    TypeVariable<Method>[] methodTypeParameters = method.getTypeParameters();
    // TODO: add support for variable arity methods
    for (int i = 0; i < methodParameters.length; i++) {
        int typeParamIndex = -1;
        if (methodParameters[i] instanceof TypeVariable) {
            typeParamIndex = JavaTypeDefinition.getGenericTypeIndex(methodTypeParameters, ((TypeVariable<?>) methodParameters[i]).getName());
        }
        if (typeParamIndex != -1) {
            // TODO: we are cheating here, it should be a contraint of the form 'var -> expression' not 'var->type'
            result.add(new Constraint(((TypeNode) argList.jjtGetChild(i)).getTypeDefinition(), variables.get(typeParamIndex), LOOSE_INVOCATION));
        }
    }
    return result;
}
Also used : Type(java.lang.reflect.Type) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) TypeVariable(java.lang.reflect.TypeVariable) ArrayList(java.util.ArrayList) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)

Example 5 with Constraint

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

Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)15 Test (org.junit.Test)14 BoundOrConstraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.BoundOrConstraint)13 ArrayList (java.util.ArrayList)2 Method (java.lang.reflect.Method)1 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 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)1 Variable (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable)1 SuperClassA (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA)1 SuperClassAOther (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther)1 SuperClassAOther2 (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther2)1