Search in sources :

Example 31 with ResolvedReferenceType

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

the class JavaParserClassDeclaration method getAncestors.

@Override
public List<ResolvedReferenceType> getAncestors() {
    List<ResolvedReferenceType> ancestors = new ArrayList<>();
    // We want to avoid infinite recursion in case of Object having Object as ancestor
    if (!(Object.class.getCanonicalName().equals(getQualifiedName()))) {
        ResolvedReferenceType superclass = getSuperClass();
        if (superclass != null) {
            ancestors.add(superclass);
        }
        if (wrappedNode.getImplementedTypes() != null) {
            for (ClassOrInterfaceType implemented : wrappedNode.getImplementedTypes()) {
                ResolvedReferenceType ancestor = toReferenceType(implemented);
                ancestors.add(ancestor);
            }
        }
    }
    return ancestors;
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 32 with ResolvedReferenceType

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

the class JavaParserEnumDeclaration method getAncestors.

@Override
public List<ResolvedReferenceType> getAncestors() {
    List<ResolvedReferenceType> ancestors = new ArrayList<>();
    ResolvedReferenceType enumClass = ReflectionFactory.typeUsageFor(Enum.class, typeSolver).asReferenceType();
    ResolvedTypeParameterDeclaration eTypeParameter = enumClass.getTypeDeclaration().getTypeParameters().get(0);
    enumClass = enumClass.deriveTypeParameters(new ResolvedTypeParametersMap.Builder().setValue(eTypeParameter, new ReferenceTypeImpl(this, typeSolver)).build());
    ancestors.add(enumClass);
    if (wrappedNode.getImplementedTypes() != null) {
        for (ClassOrInterfaceType implementedType : wrappedNode.getImplementedTypes()) {
            SymbolReference<ResolvedTypeDeclaration> implementedDeclRef = new SymbolSolver(typeSolver).solveTypeInType(this, implementedType.getName().getId());
            if (!implementedDeclRef.isSolved()) {
                throw new UnsolvedSymbolException(implementedType.getName().getId());
            }
            ancestors.add(new ReferenceTypeImpl((ResolvedReferenceTypeDeclaration) implementedDeclRef.getCorrespondingDeclaration(), typeSolver));
        }
    }
    return ancestors;
}
Also used : UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver)

Example 33 with ResolvedReferenceType

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

the class JavaParserTypeAdapter method isAssignableBy.

public boolean isAssignableBy(ResolvedReferenceTypeDeclaration other) {
    List<ResolvedReferenceType> ancestorsOfOther = other.getAllAncestors();
    ancestorsOfOther.add(new ReferenceTypeImpl(other, typeSolver));
    for (ResolvedReferenceType ancestorOfOther : ancestorsOfOther) {
        if (ancestorOfOther.getQualifiedName().equals(this.getQualifiedName())) {
            return true;
        }
    }
    return false;
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)

Example 34 with ResolvedReferenceType

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

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

the class BoundSet method findPairsOfCommonAncestors.

private List<Pair<ResolvedReferenceType, ResolvedReferenceType>> findPairsOfCommonAncestors(ResolvedReferenceType r1, ResolvedReferenceType r2) {
    List<ResolvedReferenceType> set1 = new LinkedList<>();
    set1.add(r1);
    set1.addAll(r1.getAllAncestors());
    List<ResolvedReferenceType> set2 = new LinkedList<>();
    set2.add(r2);
    set2.addAll(r2.getAllAncestors());
    List<Pair<ResolvedReferenceType, ResolvedReferenceType>> pairs = new LinkedList<>();
    for (ResolvedReferenceType rtFrom1 : set1) {
        for (ResolvedReferenceType rtFrom2 : set2) {
            if (rtFrom1.getTypeDeclaration().equals(rtFrom2.getTypeDeclaration())) {
                pairs.add(new Pair<>(rtFrom1, rtFrom2));
            }
        }
    }
    return pairs;
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Pair(com.github.javaparser.utils.Pair)

Aggregations

ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)55 Test (org.junit.Test)34 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)28 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)15 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)14 MethodUsage (com.github.javaparser.resolution.MethodUsage)9 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)9 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)8 Collectors (java.util.stream.Collectors)8 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 ResolvedTypeVariable (com.github.javaparser.resolution.types.ResolvedTypeVariable)5 AccessSpecifier (com.github.javaparser.ast.AccessSpecifier)4 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4 JavaParserClassDeclaration (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration)4 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)4 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)4 ImmutableList (com.google.common.collect.ImmutableList)4 Before (org.junit.Before)4