public static InferenceVariable unnamed(ResolvedTypeParameterDeclaration typeParameterDeclaration) { return new InferenceVariable("__unnamed__" + (unnamedInstantiated++), typeParameterDeclaration); }
public static InferenceVariable unnamed(ResolvedTypeParameterDeclaration typeParameterDeclaration) { return new InferenceVariable("__unnamed__" + (unnamedInstantiated++), typeParameterDeclaration); }
public static InferenceVariable unnamed(ResolvedTypeParameterDeclaration typeParameterDeclaration) { return new InferenceVariable("__unnamed__" + (unnamedInstantiated++), typeParameterDeclaration); }
@Test public void recognizeProperUpperBound1() { ResolvedTypeParameterDeclaration typeParameterDeclaration = EasyMock.createMock(ResolvedTypeParameterDeclaration.class); // { Integer <: α, Double <: α, α <: Object } describes two proper lower bounds and one proper upper bound for α. InferenceVariable inferenceVariable = new InferenceVariable("α", typeParameterDeclaration); Bound bound = new SubtypeOfBound(inferenceVariable, objectType); assertEquals(Optional.of(new ProperUpperBound(inferenceVariable, objectType)), bound.isProperUpperBound()); }
@Test public void recognizeProperLowerBound1() { ResolvedTypeParameterDeclaration typeParameterDeclaration = EasyMock.createMock(ResolvedTypeParameterDeclaration.class); // { Integer <: α, Double <: α, α <: Object } describes two proper lower bounds and one proper upper bound for α. InferenceVariable inferenceVariable = new InferenceVariable("α", typeParameterDeclaration); Bound bound = new SubtypeOfBound(integerType, inferenceVariable); assertEquals(Optional.of(new ProperLowerBound(inferenceVariable, integerType)), bound.isProperLowerBound()); }
@Test public void recognizeProperLowerBound2() { ResolvedTypeParameterDeclaration typeParameterDeclaration = EasyMock.createMock(ResolvedTypeParameterDeclaration.class); // { Integer <: α, Double <: α, α <: Object } describes two proper lower bounds and one proper upper bound for α. InferenceVariable inferenceVariable = new InferenceVariable("α", typeParameterDeclaration); Bound bound = new SubtypeOfBound(doubleType, inferenceVariable); assertEquals(Optional.of(new ProperLowerBound(inferenceVariable, doubleType)), bound.isProperLowerBound()); }
@Test public void recognizeInstantiation() { // { α = String } contains a single bound, instantiating α as String. InferenceVariable inferenceVariable = new InferenceVariable("α", null); Bound bound1 = new SameAsBound(inferenceVariable, stringType); Bound bound2 = new SameAsBound(stringType, inferenceVariable); assertEquals(Optional.of(new Instantiation(inferenceVariable, stringType)), bound1.isAnInstantiation()); assertEquals(Optional.of(new Instantiation(inferenceVariable, stringType)), bound2.isAnInstantiation()); }
@Test public void recognizeProperUpperBound2() { ResolvedTypeParameterDeclaration typeParameterDeclaration1 = EasyMock.createMock(ResolvedTypeParameterDeclaration.class); ResolvedTypeParameterDeclaration typeParameterDeclaration2 = EasyMock.createMock(ResolvedTypeParameterDeclaration.class); // { α <: Iterable<?>, β <: Object, α <: List<β> } describes a proper upper bound for each of α and β, along with a dependency between them. InferenceVariable alpha = new InferenceVariable("α", typeParameterDeclaration1); InferenceVariable beta = new InferenceVariable("β", typeParameterDeclaration2); ResolvedType iterableOfWildcard = new ReferenceTypeImpl(iterableType.getTypeDeclaration(), Arrays.asList(ResolvedWildcard.UNBOUNDED), typeSolver); ResolvedType listOfBeta = new ReferenceTypeImpl(listType.getTypeDeclaration(), Arrays.asList(beta), typeSolver); Bound bound1 = new SubtypeOfBound(alpha, iterableOfWildcard); Bound bound2 = new SubtypeOfBound(beta, objectType); Bound bound3 = new SubtypeOfBound(alpha, listOfBeta); assertEquals(false, isProperType(listOfBeta)); assertEquals(Optional.of(new ProperUpperBound(alpha, iterableOfWildcard)), bound1.isProperUpperBound()); assertEquals(Optional.of(new ProperUpperBound(beta, objectType)), bound2.isProperUpperBound()); assertEquals(true, bound3.isADependency()); } }
/** * From JLS 18.1.2 * * From Collections.singleton("hi"), we have the constraint formula ‹"hi" → α›. * Through reduction, this will become the constraint formula: ‹String <: α›. */ @Test public void testExpressionCompatibleWithTypeReduce1() { ResolvedTypeParameterDeclaration tp = EasyMock.createMock(ResolvedTypeParameterDeclaration.class); Expression e = new StringLiteralExpr("hi"); InferenceVariable inferenceVariable = new InferenceVariable("α", tp); ExpressionCompatibleWithType formula = new ExpressionCompatibleWithType(typeSolver, e, inferenceVariable); ConstraintFormula.ReductionResult res1 = formula.reduce(BoundSet.empty()); assertEquals( ConstraintFormula.ReductionResult.empty().withConstraint(new TypeCompatibleWithType(typeSolver, stringType, inferenceVariable)), res1); assertEquals( ConstraintFormula.ReductionResult.empty().withConstraint(new TypeSubtypeOfType(typeSolver, stringType, inferenceVariable)), res1.getConstraint(0).reduce(BoundSet.empty())); }