Search in sources :

Example 26 with ResolvedReferenceType

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

the class GenericsResolutionTest method genericsInheritance.

@Test
public void genericsInheritance() {
    CompilationUnit cu = parseSample("MethodTypeParams");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "VoidVisitorAdapter");
    MethodDeclaration method = Navigator.demandMethod(clazz, "visit");
    MethodCallExpr call = Navigator.findMethodCall(method, "accept").get();
    Expression thisRef = call.getArguments().get(0);
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ResolvedType voidVisitorAdapterOfA = javaParserFacade.getType(thisRef);
    List<ResolvedReferenceType> allAncestors = voidVisitorAdapterOfA.asReferenceType().getAllAncestors();
    assertEquals(2, allAncestors.size());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 27 with ResolvedReferenceType

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

the class JavaParserFacadeResolutionTest method typeDeclarationSuperClassImplicitlyIncludeObject.

@Test
public void typeDeclarationSuperClassImplicitlyIncludeObject() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
    ResolvedTypeDeclaration typeDeclaration = JavaParserFacade.get(new ReflectionTypeSolver()).getTypeDeclaration(clazz);
    ResolvedReferenceType superclass = typeDeclaration.asClass().getSuperClass();
    assertEquals(Object.class.getCanonicalName(), superclass.getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test)

Example 28 with ResolvedReferenceType

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

the class MethodsResolutionLogicTest method compatibilityShouldConsiderAlsoTypeVariablesNegative.

@Test
public void compatibilityShouldConsiderAlsoTypeVariablesNegative() {
    JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolver.solveType("com.github.javaparser.ast.body.ConstructorDeclaration");
    ResolvedReferenceType stringType = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(String.class, typeSolver);
    ResolvedReferenceType rawClassType = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(Class.class, typeSolver);
    assertEquals(true, rawClassType.isRawType());
    ResolvedReferenceType classOfStringType = (ResolvedReferenceType) rawClassType.replaceTypeVariables(rawClassType.getTypeDeclaration().getTypeParameters().get(0), stringType);
    MethodUsage mu = constructorDeclaration.getAllMethods().stream().filter(m -> m.getDeclaration().getSignature().equals("isThrows(java.lang.Class<? extends java.lang.Throwable>)")).findFirst().get();
    assertEquals(false, MethodResolutionLogic.isApplicable(mu, "isThrows", ImmutableList.of(classOfStringType), typeSolver));
}
Also used : JavaParserClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) MethodUsage(com.github.javaparser.resolution.MethodUsage) Test(org.junit.Test)

Example 29 with ResolvedReferenceType

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

the class MethodsResolutionLogicTest method compatibilityShouldConsiderAlsoTypeVariablesPositive.

@Test
public void compatibilityShouldConsiderAlsoTypeVariablesPositive() {
    JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolver.solveType("com.github.javaparser.ast.body.ConstructorDeclaration");
    ResolvedReferenceType runtimeException = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(RuntimeException.class, typeSolver);
    ResolvedReferenceType rawClassType = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(Class.class, typeSolver);
    ResolvedReferenceType classOfRuntimeType = (ResolvedReferenceType) rawClassType.replaceTypeVariables(rawClassType.getTypeDeclaration().getTypeParameters().get(0), runtimeException);
    MethodUsage mu = constructorDeclaration.getAllMethods().stream().filter(m -> m.getDeclaration().getSignature().equals("isThrows(java.lang.Class<? extends java.lang.Throwable>)")).findFirst().get();
    assertEquals(true, MethodResolutionLogic.isApplicable(mu, "isThrows", ImmutableList.of(classOfRuntimeType), typeSolver));
}
Also used : JavaParserClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) MethodUsage(com.github.javaparser.resolution.MethodUsage) Test(org.junit.Test)

Example 30 with ResolvedReferenceType

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

the class AnonymousClassDeclarationContext method solveType.

@Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
    List<com.github.javaparser.ast.body.TypeDeclaration> typeDeclarations = myDeclaration.findMembersOfKind(com.github.javaparser.ast.body.TypeDeclaration.class);
    Optional<SymbolReference<ResolvedTypeDeclaration>> exactMatch = typeDeclarations.stream().filter(internalType -> internalType.getName().getId().equals(name)).findFirst().map(internalType -> SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration(internalType)));
    if (exactMatch.isPresent()) {
        return exactMatch.get();
    }
    Optional<SymbolReference<ResolvedTypeDeclaration>> recursiveMatch = typeDeclarations.stream().filter(internalType -> name.startsWith(String.format("%s.", internalType.getName()))).findFirst().map(internalType -> JavaParserFactory.getContext(internalType, typeSolver).solveType(name.substring(internalType.getName().getId().length() + 1), typeSolver));
    if (recursiveMatch.isPresent()) {
        return recursiveMatch.get();
    }
    Optional<SymbolReference<ResolvedTypeDeclaration>> typeArgumentsMatch = wrappedNode.getTypeArguments().map(nodes -> ((NodeWithTypeArguments<?>) nodes).getTypeArguments().orElse(new NodeList<>())).orElse(new NodeList<>()).stream().filter(type -> type.toString().equals(name)).findFirst().map(matchingType -> SymbolReference.solved(new JavaParserTypeParameter(new TypeParameter(matchingType.toString()), typeSolver)));
    if (typeArgumentsMatch.isPresent()) {
        return typeArgumentsMatch.get();
    }
    // Look into extended classes and implemented interfaces
    for (ResolvedReferenceType ancestor : myDeclaration.getAncestors()) {
        // look at names of extended classes and implemented interfaces (this may not be important because they are checked in CompilationUnitContext)
        if (ancestor.getTypeDeclaration().getName().equals(name)) {
            return SymbolReference.solved(ancestor.getTypeDeclaration());
        }
        // look into internal types of extended classes and implemented interfaces
        try {
            for (ResolvedTypeDeclaration internalTypeDeclaration : ancestor.getTypeDeclaration().internalTypes()) {
                if (internalTypeDeclaration.getName().equals(name)) {
                    return SymbolReference.solved(internalTypeDeclaration);
                }
            }
        } catch (UnsupportedOperationException e) {
        // just continue using the next ancestor
        }
    }
    return getParent().solveType(name, typeSolver);
}
Also used : JavaParserFactory(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory) NodeList(com.github.javaparser.ast.NodeList) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeParameter(com.github.javaparser.ast.type.TypeParameter) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) NodeWithTypeArguments(com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) JavaParserAnonymousClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserAnonymousClassDeclaration) List(java.util.List) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) TypeParameter(com.github.javaparser.ast.type.TypeParameter) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)

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