Search in sources :

Example 16 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class ReflectionMethodResolutionLogic method solveMethodAsUsage.

static Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext, List<ResolvedType> typeParameterValues, ResolvedReferenceTypeDeclaration scopeType, Class clazz) {
    if (typeParameterValues.size() != scopeType.getTypeParameters().size()) {
        // if it is zero we are going to ignore them
        if (!scopeType.getTypeParameters().isEmpty()) {
            // Parameters not specified, so default to Object
            typeParameterValues = new ArrayList<>();
            for (int i = 0; i < scopeType.getTypeParameters().size(); i++) {
                typeParameterValues.add(new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver));
            }
        }
    }
    List<MethodUsage> methods = new ArrayList<>();
    for (Method method : clazz.getMethods()) {
        if (method.getName().equals(name) && !method.isBridge() && !method.isSynthetic()) {
            ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
            MethodUsage methodUsage = replaceParams(typeParameterValues, scopeType, methodDeclaration);
            methods.add(methodUsage);
        }
    }
    for (ResolvedReferenceType ancestor : scopeType.getAncestors()) {
        SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(ancestor.getTypeDeclaration(), name, argumentsTypes, typeSolver);
        if (ref.isSolved()) {
            ResolvedMethodDeclaration correspondingDeclaration = ref.getCorrespondingDeclaration();
            MethodUsage methodUsage = replaceParams(typeParameterValues, ancestor.getTypeDeclaration(), correspondingDeclaration);
            methods.add(methodUsage);
        }
    }
    if (scopeType.getAncestors().isEmpty()) {
        ReferenceTypeImpl objectClass = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
        SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(objectClass.getTypeDeclaration(), name, argumentsTypes, typeSolver);
        if (ref.isSolved()) {
            MethodUsage usage = replaceParams(typeParameterValues, objectClass.getTypeDeclaration(), ref.getCorrespondingDeclaration());
            methods.add(usage);
        }
    }
    final List<ResolvedType> finalTypeParameterValues = typeParameterValues;
    argumentsTypes = argumentsTypes.stream().map((pt) -> {
        int i = 0;
        for (ResolvedTypeParameterDeclaration tp : scopeType.getTypeParameters()) {
            pt = pt.replaceTypeVariables(tp, finalTypeParameterValues.get(i));
            i++;
        }
        return pt;
    }).collect(Collectors.toList());
    return MethodResolutionLogic.findMostApplicableUsage(methods, name, argumentsTypes, typeSolver);
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodUsage(com.github.javaparser.resolution.MethodUsage) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 17 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class JavassistInterfaceDeclaration method getAncestors.

@Override
public List<ResolvedReferenceType> getAncestors() {
    List<ResolvedReferenceType> ancestors = new ArrayList<>();
    try {
        for (CtClass interfaze : ctClass.getInterfaces()) {
            ResolvedReferenceType superInterfaze = JavassistFactory.typeUsageFor(interfaze, typeSolver).asReferenceType();
            ancestors.add(superInterfaze);
        }
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    ancestors = ancestors.stream().filter(a -> a.getQualifiedName() != Object.class.getCanonicalName()).collect(Collectors.toList());
    ancestors.add(new ReferenceTypeImpl(typeSolver.solveType(Object.class.getCanonicalName()), typeSolver));
    return ancestors;
}
Also used : CtClass(javassist.CtClass) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) NotFoundException(javassist.NotFoundException)

Example 18 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class ReflectionClassAdapter method getAncestors.

public List<ResolvedReferenceType> getAncestors() {
    List<ResolvedReferenceType> ancestors = new LinkedList<>();
    if (getSuperClass() != null) {
        ReferenceTypeImpl superClass = getSuperClass();
        ancestors.add(superClass);
    } else {
        ReferenceTypeImpl object = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
        ancestors.add(object);
    }
    ancestors.addAll(getInterfaces());
    for (int i = 0; i < ancestors.size(); i++) {
        ResolvedReferenceType ancestor = ancestors.get(i);
        if (ancestor.hasName() && ancestor.getQualifiedName().equals(Object.class.getCanonicalName())) {
            ancestors.remove(i);
            i--;
        }
    }
    return ancestors;
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)

Example 19 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class ReflectionClassAdapter method getSuperClass.

public ReferenceTypeImpl getSuperClass() {
    if (clazz.getGenericSuperclass() == null) {
        return null;
    }
    java.lang.reflect.Type superType = clazz.getGenericSuperclass();
    if (superType instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) superType;
        List<ResolvedType> typeParameters = Arrays.stream(parameterizedType.getActualTypeArguments()).map((t) -> ReflectionFactory.typeUsageFor(t, typeSolver)).collect(Collectors.toList());
        return new ReferenceTypeImpl(new ReflectionClassDeclaration(clazz.getSuperclass(), typeSolver), typeParameters, typeSolver);
    }
    return new ReferenceTypeImpl(new ReflectionClassDeclaration(clazz.getSuperclass(), typeSolver), typeSolver);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) NullType(com.github.javaparser.symbolsolver.model.typesystem.NullType) java.util(java.util) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) TypeVariable(java.lang.reflect.TypeVariable) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Field(java.lang.reflect.Field) com.github.javaparser.resolution.declarations(com.github.javaparser.resolution.declarations) Collectors(java.util.stream.Collectors) LambdaArgumentTypePlaceholder(com.github.javaparser.symbolsolver.javaparsermodel.LambdaArgumentTypePlaceholder) FunctionalInterfaceLogic(com.github.javaparser.symbolsolver.logic.FunctionalInterfaceLogic) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) ParameterizedType(java.lang.reflect.ParameterizedType) Annotation(java.lang.annotation.Annotation) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 20 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class MyObjectProvider method byName.

@Override
public ResolvedReferenceType byName(String qualifiedName) {
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedReferenceTypeDeclaration typeDeclaration = typeSolver.solveType(qualifiedName);
    if (!typeDeclaration.getTypeParameters().isEmpty()) {
        throw new UnsupportedOperationException();
    }
    return new ReferenceTypeImpl(typeDeclaration, typeSolver);
}
Also used : ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)

Aggregations

ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)27 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)16 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)13 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)12 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)12 Test (org.junit.Test)9 MethodUsage (com.github.javaparser.resolution.MethodUsage)5 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)4 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)4 ResolvedTypeVariable (com.github.javaparser.resolution.types.ResolvedTypeVariable)4 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)4 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)3 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)3 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)3 Context (com.github.javaparser.symbolsolver.core.resolution.Context)3 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)3 File (java.io.File)3 Collectors (java.util.stream.Collectors)3