Search in sources :

Example 1 with ResolvedTypeParameterDeclaration

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

the class ReferenceTypeTest method testReplaceParam.

@Test
public void testReplaceParam() {
    ResolvedTypeParameterDeclaration tpA = ResolvedTypeParameterDeclaration.onType("A", "foo.Bar", Collections.emptyList());
    assertTrue(object == object.replaceTypeVariables(tpA, object));
    assertTrue(string == string.replaceTypeVariables(tpA, object));
    assertEquals(listOfStrings, listOfStrings.replaceTypeVariables(tpA, object));
    assertEquals(listOfStrings, listOfA.replaceTypeVariables(tpA, string));
}
Also used : ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) Test(org.junit.Test)

Example 2 with ResolvedTypeParameterDeclaration

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

the class ReferenceTypeTest method testReplaceTypeVariables.

@Test
public void testReplaceTypeVariables() {
    TypeSolver typeResolver = new ReflectionTypeSolver();
    ResolvedInterfaceDeclaration streamInterface = new ReflectionInterfaceDeclaration(Stream.class, typeResolver);
    ResolvedReferenceType stream = new ReferenceTypeImpl(streamInterface, typeResolver);
    ResolvedMethodDeclaration streamMap = streamInterface.getDeclaredMethods().stream().filter(m -> m.getName().equals("map")).findFirst().get();
    ResolvedTypeParameterDeclaration streamMapR = streamMap.findTypeParameter("T").get();
    ResolvedTypeVariable typeVariable = new ResolvedTypeVariable(streamMapR);
    stream = stream.deriveTypeParameters(stream.typeParametersMap().toBuilder().setValue(stream.getTypeDeclaration().getTypeParameters().get(0), typeVariable).build());
    ResolvedTypeParameterDeclaration tpToReplace = streamInterface.getTypeParameters().get(0);
    ResolvedType replaced = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeResolver), typeResolver);
    ResolvedType streamReplaced = stream.replaceTypeVariables(tpToReplace, replaced);
    assertEquals("java.util.stream.Stream<java.lang.String>", streamReplaced.describe());
}
Also used : TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) ReflectionInterfaceDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) Test(org.junit.Test)

Example 3 with ResolvedTypeParameterDeclaration

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

the class WildcardUsageTest method testReplaceParam.

@Test
public void testReplaceParam() {
    ResolvedTypeParameterDeclaration tpA = ResolvedTypeParameterDeclaration.onType("A", "foo.Bar", Collections.emptyList());
    ResolvedTypeParameterDeclaration tpB = ResolvedTypeParameterDeclaration.onType("B", "foo.Bar", Collections.emptyList());
    assertTrue(unbounded == unbounded.replaceTypeVariables(tpA, string));
    assertTrue(superFoo == superFoo.replaceTypeVariables(tpA, string));
    assertTrue(extendsFoo == extendsFoo.replaceTypeVariables(tpA, string));
    assertEquals(superString, superA.replaceTypeVariables(tpA, string));
    assertEquals(extendsString, extendsA.replaceTypeVariables(tpA, string));
    assertTrue(superA == superA.replaceTypeVariables(tpB, string));
    assertTrue(extendsA == extendsA.replaceTypeVariables(tpB, string));
}
Also used : ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) Test(org.junit.Test)

Example 4 with ResolvedTypeParameterDeclaration

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

the class ReflectionFactory method typeUsageFor.

public static ResolvedType typeUsageFor(java.lang.reflect.Type type, TypeSolver typeSolver) {
    if (type instanceof java.lang.reflect.TypeVariable) {
        java.lang.reflect.TypeVariable<?> tv = (java.lang.reflect.TypeVariable<?>) type;
        boolean declaredOnClass = tv.getGenericDeclaration() instanceof java.lang.reflect.Type;
        ResolvedTypeParameterDeclaration typeParameter = new ReflectionTypeParameter(tv, declaredOnClass, typeSolver);
        return new ResolvedTypeVariable(typeParameter);
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        ResolvedReferenceType rawType = typeUsageFor(pt.getRawType(), typeSolver).asReferenceType();
        List<java.lang.reflect.Type> actualTypes = new ArrayList<>();
        actualTypes.addAll(Arrays.asList(pt.getActualTypeArguments()));
        // we consume the actual types
        rawType = rawType.transformTypeParameters(tp -> typeUsageFor(actualTypes.remove(0), typeSolver)).asReferenceType();
        return rawType;
    } else if (type instanceof Class) {
        Class<?> c = (Class<?>) type;
        if (c.isPrimitive()) {
            if (c.getName().equals(Void.TYPE.getName())) {
                return ResolvedVoidType.INSTANCE;
            } else {
                return ResolvedPrimitiveType.byName(c.getName());
            }
        } else if (c.isArray()) {
            return new ResolvedArrayType(typeUsageFor(c.getComponentType(), typeSolver));
        } else {
            return new ReferenceTypeImpl(typeDeclarationFor(c, typeSolver), typeSolver);
        }
    } else if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        return new ResolvedArrayType(typeUsageFor(genericArrayType.getGenericComponentType(), typeSolver));
    } else if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        if (wildcardType.getLowerBounds().length > 0 && wildcardType.getUpperBounds().length > 0) {
            if (wildcardType.getUpperBounds().length == 1 && wildcardType.getUpperBounds()[0].getTypeName().equals("java.lang.Object")) {
            // ok, it does not matter
            }
        }
        if (wildcardType.getLowerBounds().length > 0) {
            if (wildcardType.getLowerBounds().length > 1) {
                throw new UnsupportedOperationException();
            }
            return ResolvedWildcard.superBound(typeUsageFor(wildcardType.getLowerBounds()[0], typeSolver));
        }
        if (wildcardType.getUpperBounds().length > 0) {
            if (wildcardType.getUpperBounds().length > 1) {
                throw new UnsupportedOperationException();
            }
            return ResolvedWildcard.extendsBound(typeUsageFor(wildcardType.getUpperBounds()[0], typeSolver));
        }
        return ResolvedWildcard.UNBOUNDED;
    } else {
        throw new UnsupportedOperationException(type.getClass().getCanonicalName() + " " + type);
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Arrays(java.util.Arrays) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) com.github.javaparser.symbolsolver.model.typesystem(com.github.javaparser.symbolsolver.model.typesystem) WildcardType(java.lang.reflect.WildcardType) AccessSpecifier(com.github.javaparser.ast.AccessSpecifier) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ArrayList(java.util.ArrayList) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) List(java.util.List) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ParameterizedType(java.lang.reflect.ParameterizedType) com.github.javaparser.resolution.types(com.github.javaparser.resolution.types) Modifier(java.lang.reflect.Modifier) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with ResolvedTypeParameterDeclaration

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

the class TypeInference method instantiationInference.

public Optional<InstantiationSet> instantiationInference(List<Expression> argumentExpressions, ResolvedMethodDeclaration methodDeclaration) {
    // if (methodCallExpr.getTypeArguments().isPresent()) {
    // throw new IllegalArgumentException("Type inference unnecessary as type arguments have been specified");
    // }
    // Given a method invocation that provides no explicit type arguments, the process to determine whether a
    // potentially applicable generic method m is applicable is as follows:
    // - Where P1, ..., Pp (p ≥ 1) are the type parameters of m, let α1, ..., αp be inference variables, and
    // let θ be the substitution [P1:=α1, ..., Pp:=αp].
    List<ResolvedTypeParameterDeclaration> Ps = methodDeclaration.getTypeParameters();
    List<InferenceVariable> alphas = InferenceVariable.instantiate(Ps);
    Substitution theta = Substitution.empty();
    for (int i = 0; i < Ps.size(); i++) {
        theta = theta.withPair(Ps.get(0), alphas.get(0));
    }
    // - An initial bound set, B0, is constructed from the declared bounds of P1, ..., Pp, as described in §18.1.3.
    BoundSet B0 = boundSetup(Ps, alphas);
    // - For all i (1 ≤ i ≤ p), if Pi appears in the throws clause of m, then the bound throws αi is implied.
    // These bounds, if any, are incorporated with B0 to produce a new bound set, B1.
    BoundSet B1 = B0;
    for (int i = 0; i < Ps.size(); i++) {
        ResolvedTypeParameterDeclaration Pi = Ps.get(i);
        if (appearInThrowsClause(Pi, methodDeclaration)) {
            B1 = B1.withBound(new ThrowsBound(alphas.get(i)));
        }
    }
    // - A set of constraint formulas, C, is constructed as follows.
    // 
    // Let F1, ..., Fn be the formal parameter types of m, and let e1, ..., ek be the actual argument expressions
    // of the invocation. Then:
    List<ResolvedType> Fs = formalParameterTypes(methodDeclaration);
    List<Expression> es = argumentExpressions;
    Optional<ConstraintFormulaSet> C = Optional.empty();
    if (!C.isPresent()) {
        C = testForApplicabilityByStrictInvocation(Fs, es, theta);
    }
    if (!C.isPresent()) {
        C = testForApplicabilityByLooseInvocation(Fs, es, theta);
    }
    if (!C.isPresent()) {
        C = testForApplicabilityByVariableArityInvocation(Fs, es, theta);
    }
    if (!C.isPresent()) {
        return Optional.empty();
    }
    // - C is reduced (§18.2) and the resulting bounds are incorporated with B1 to produce a new bound set, B2.
    BoundSet resultingBounds = C.get().reduce(typeSolver);
    BoundSet B2 = B1.incorporate(resultingBounds, typeSolver);
    if (B2.containsFalse()) {
        return Optional.empty();
    }
    Optional<InstantiationSet> instantiation = B2.performResolution(alphas, typeSolver);
    return instantiation;
}
Also used : ThrowsBound(com.github.javaparser.symbolsolver.resolution.typeinference.bounds.ThrowsBound) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ExpressionHelper.isStandaloneExpression(com.github.javaparser.symbolsolver.resolution.typeinference.ExpressionHelper.isStandaloneExpression) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

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