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
}
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);
}
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);
}
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;
}
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)));
}
Aggregations