Search in sources :

Example 6 with ResolvedTypeParameterDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.

the class JavaParserTypeParameterResolutionTest method declaredOnMethodPositiveCase.

@Test
public void declaredOnMethodPositiveCase() {
    CompilationUnit cu = parseSample("MethodTypeParameter");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ClassOrInterfaceDeclaration classDecl = Navigator.demandClass(cu, "Foo");
    MethodDeclaration methodDecl = Navigator.demandMethod(classDecl, "usage");
    MethodCallExpr callToFoo = (MethodCallExpr) Navigator.findReturnStmt(methodDecl).getExpression().get();
    ResolvedMethodDeclaration methodDeclaration = javaParserFacade.solve(callToFoo).getCorrespondingDeclaration();
    for (ResolvedTypeParameterDeclaration tp : methodDeclaration.getTypeParameters()) {
        assertTrue(tp instanceof JavaParserTypeParameter);
        assertEquals("C", tp.getName());
        assertEquals(true, tp.declaredOnMethod());
        assertEquals(false, tp.declaredOnType());
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 7 with ResolvedTypeParameterDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration 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 8 with ResolvedTypeParameterDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.

the class SubtypeOfBoundTest method recognizeProperUpperBound2.

@Test
public void recognizeProperUpperBound2() {
    ResolvedTypeParameterDeclaration typeParameterDeclaration1 = mock(ResolvedTypeParameterDeclaration.class);
    ResolvedTypeParameterDeclaration typeParameterDeclaration2 = mock(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());
}
Also used : ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 9 with ResolvedTypeParameterDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.

the class MethodUsage method replaceTypeParameter.

public MethodUsage replaceTypeParameter(ResolvedTypeParameterDeclaration typeParameter, ResolvedType type) {
    if (type == null) {
        throw new IllegalArgumentException();
    }
    // TODO if the method declaration has a type param with that name ignore this call
    MethodUsage res = new MethodUsage(declaration, paramTypes, returnType, exceptionTypes, typeParametersMap.toBuilder().setValue(typeParameter, type).build());
    Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>();
    for (int i = 0; i < paramTypes.size(); i++) {
        ResolvedType originalParamType = paramTypes.get(i);
        ResolvedType newParamType = originalParamType.replaceTypeVariables(typeParameter, type, inferredTypes);
        res = res.replaceParamType(i, newParamType);
    }
    for (int i = 0; i < exceptionTypes.size(); i++) {
        ResolvedType originalType = exceptionTypes.get(i);
        ResolvedType newType = originalType.replaceTypeVariables(typeParameter, type, inferredTypes);
        res = res.replaceExceptionType(i, newType);
    }
    ResolvedType oldReturnType = res.returnType;
    ResolvedType newReturnType = oldReturnType.replaceTypeVariables(typeParameter, type, inferredTypes);
    res = res.replaceReturnType(newReturnType);
    return res;
}
Also used : ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 10 with ResolvedTypeParameterDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.

the class ReflectionMethodResolutionLogic method replaceParams.

private static MethodUsage replaceParams(List<ResolvedType> typeParameterValues, ResolvedReferenceTypeDeclaration typeParametrizable, ResolvedMethodDeclaration methodDeclaration) {
    MethodUsage methodUsage = new MethodUsage(methodDeclaration);
    int i = 0;
    // Only replace if we have enough values provided
    if (typeParameterValues.size() == typeParametrizable.getTypeParameters().size()) {
        for (ResolvedTypeParameterDeclaration tp : typeParametrizable.getTypeParameters()) {
            methodUsage = methodUsage.replaceTypeParameter(tp, typeParameterValues.get(i));
            i++;
        }
    }
    for (ResolvedTypeParameterDeclaration methodTypeParameter : methodDeclaration.getTypeParameters()) {
        methodUsage = methodUsage.replaceTypeParameter(methodTypeParameter, new ResolvedTypeVariable(methodTypeParameter));
    }
    return methodUsage;
}
Also used : ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) MethodUsage(com.github.javaparser.resolution.MethodUsage) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)

Aggregations

ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)18 Test (org.junit.Test)10 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)6 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)4 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)4 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)3 MethodUsage (com.github.javaparser.resolution.MethodUsage)2 ResolvedInterfaceDeclaration (com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration)2 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)2 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)2 ResolvedTypeVariable (com.github.javaparser.resolution.types.ResolvedTypeVariable)2 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)2 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)2 ReflectionInterfaceDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration)2 ArrayList (java.util.ArrayList)2 AccessSpecifier (com.github.javaparser.ast.AccessSpecifier)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 Expression (com.github.javaparser.ast.expr.Expression)1