Search in sources :

Example 6 with ResolvedTypeVariable

use of com.github.javaparser.resolution.types.ResolvedTypeVariable in project javaparser by javaparser.

the class VoidTypeTest method setup.

@Before
public void setup() {
    typeSolver = new ReflectionTypeSolver();
    OBJECT = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
    STRING = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
    arrayOfBooleans = new ResolvedArrayType(ResolvedPrimitiveType.BOOLEAN);
    arrayOfListOfA = new ResolvedArrayType(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(List.class, typeSolver), ImmutableList.of(new ResolvedTypeVariable(ResolvedTypeParameterDeclaration.onType("A", "foo.Bar", Collections.emptyList()))), typeSolver));
}
Also used : ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) ReflectionInterfaceDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Before(org.junit.Before)

Example 7 with ResolvedTypeVariable

use of com.github.javaparser.resolution.types.ResolvedTypeVariable in project javaparser by javaparser.

the class ExpressionCompatibleWithType method replaceTypeVariablesWithInferenceVariables.

private MethodType replaceTypeVariablesWithInferenceVariables(MethodType methodType) {
    // Find all type variable
    Map<ResolvedTypeVariable, InferenceVariable> correspondences = new HashMap<>();
    List<ResolvedType> newFormalArgumentTypes = new LinkedList<>();
    for (ResolvedType formalArg : methodType.getFormalArgumentTypes()) {
        newFormalArgumentTypes.add(replaceTypeVariablesWithInferenceVariables(formalArg, correspondences));
    }
    ResolvedType newReturnType = replaceTypeVariablesWithInferenceVariables(methodType.getReturnType(), correspondences);
    return new MethodType(methodType.getTypeParameters(), newFormalArgumentTypes, newReturnType, methodType.getExceptionTypes());
}
Also used : ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) HashMap(java.util.HashMap) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) LinkedList(java.util.LinkedList)

Example 8 with ResolvedTypeVariable

use of com.github.javaparser.resolution.types.ResolvedTypeVariable 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 9 with ResolvedTypeVariable

use of com.github.javaparser.resolution.types.ResolvedTypeVariable 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)

Example 10 with ResolvedTypeVariable

use of com.github.javaparser.resolution.types.ResolvedTypeVariable in project javaparser by javaparser.

the class InferenceContextTest method setup.

@Before
public void setup() {
    typeSolver = new ReflectionTypeSolver();
    string = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
    object = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
    listOfString = listOf(string);
    tpE = mock(ResolvedTypeParameterDeclaration.class);
    when(tpE.getName()).thenReturn("T");
    listOfE = listOf(new ResolvedTypeVariable(tpE));
}
Also used : ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Before(org.junit.Before)

Aggregations

ResolvedTypeVariable (com.github.javaparser.resolution.types.ResolvedTypeVariable)13 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)9 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)7 Before (org.junit.Before)7 List (java.util.List)5 ResolvedArrayType (com.github.javaparser.resolution.types.ResolvedArrayType)4 ReflectionInterfaceDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration)4 ImmutableList (com.google.common.collect.ImmutableList)4 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)3 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)3 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)3 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)3 Test (org.junit.Test)3 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)2 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)2 HashMap (java.util.HashMap)2 AccessSpecifier (com.github.javaparser.ast.AccessSpecifier)1 MethodUsage (com.github.javaparser.resolution.MethodUsage)1 ResolvedInterfaceDeclaration (com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration)1 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)1