Search in sources :

Example 1 with InferenceVariable

use of com.github.javaparser.symbolsolver.resolution.typeinference.InferenceVariable in project javaparser by javaparser.

the class ConstraintFormulaTest method testExpressionCompatibleWithTypeReduce1.

/**
 * 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 = mock(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()));
}
Also used : InferenceVariable(com.github.javaparser.symbolsolver.resolution.typeinference.InferenceVariable) Expression(com.github.javaparser.ast.expr.Expression) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ConstraintFormula(com.github.javaparser.symbolsolver.resolution.typeinference.ConstraintFormula) Test(org.junit.Test)

Example 2 with InferenceVariable

use of com.github.javaparser.symbolsolver.resolution.typeinference.InferenceVariable in project javaparser by javaparser.

the class SameAsBoundTest method recognizeInstantiation.

@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());
}
Also used : InferenceVariable(com.github.javaparser.symbolsolver.resolution.typeinference.InferenceVariable) Bound(com.github.javaparser.symbolsolver.resolution.typeinference.Bound) Instantiation(com.github.javaparser.symbolsolver.resolution.typeinference.Instantiation) Test(org.junit.Test)

Aggregations

InferenceVariable (com.github.javaparser.symbolsolver.resolution.typeinference.InferenceVariable)2 Test (org.junit.Test)2 Expression (com.github.javaparser.ast.expr.Expression)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)1 Bound (com.github.javaparser.symbolsolver.resolution.typeinference.Bound)1 ConstraintFormula (com.github.javaparser.symbolsolver.resolution.typeinference.ConstraintFormula)1 Instantiation (com.github.javaparser.symbolsolver.resolution.typeinference.Instantiation)1