Search in sources :

Example 1 with JavaParserTypeParameter

use of com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter in project javaparser by javaparser.

the class JavaParserTypeParameterResolutionTest method declaredOnMethodPositiveCase.

@Test
public void declaredOnMethodPositiveCase() {
    CompilationUnit cu = parseSample("MethodTypeParameter");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ClassOrInterfaceDeclaration classDecl = Navigator.demandClass(cu, "Foo");
    MethodDeclaration methodDecl = Navigator.demandMethod(classDecl, "usage");
    MethodCallExpr callToFoo = (MethodCallExpr) Navigator.findReturnStmt(methodDecl).getExpression().get();
    ResolvedMethodDeclaration methodDeclaration = javaParserFacade.solve(callToFoo).getCorrespondingDeclaration();
    for (ResolvedTypeParameterDeclaration tp : methodDeclaration.getTypeParameters()) {
        assertTrue(tp instanceof JavaParserTypeParameter);
        assertEquals("C", tp.getName());
        assertEquals(true, tp.declaredOnMethod());
        assertEquals(false, tp.declaredOnType());
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 2 with JavaParserTypeParameter

use of com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter 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)

Example 3 with JavaParserTypeParameter

use of com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter in project javaparser by javaparser.

the class JavaParserTypeParameterResolutionTest method declaredOnMethodNegativeCase.

@Test
public void declaredOnMethodNegativeCase() {
    CompilationUnit cu = parseSample("ClassTypeParameter");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ClassOrInterfaceDeclaration classDecl = Navigator.demandClass(cu, "Foo");
    MethodDeclaration methodDecl = Navigator.demandMethod(classDecl, "usage");
    MethodCallExpr callToFoo = (MethodCallExpr) Navigator.findReturnStmt(methodDecl).getExpression().get();
    ResolvedMethodDeclaration methodDeclaration = javaParserFacade.solve(callToFoo).getCorrespondingDeclaration();
    ResolvedReferenceTypeDeclaration typeDeclaration = methodDeclaration.declaringType();
    assertEquals(2, typeDeclaration.getTypeParameters().size());
    assertTrue(typeDeclaration.getTypeParameters().get(0) instanceof JavaParserTypeParameter);
    assertEquals("A", typeDeclaration.getTypeParameters().get(0).getName());
    assertEquals(false, typeDeclaration.getTypeParameters().get(0).declaredOnMethod());
    assertEquals(true, typeDeclaration.getTypeParameters().get(0).declaredOnType());
    assertTrue(typeDeclaration.getTypeParameters().get(1) instanceof JavaParserTypeParameter);
    assertEquals("B", typeDeclaration.getTypeParameters().get(1).getName());
    assertEquals(false, typeDeclaration.getTypeParameters().get(1).declaredOnMethod());
    assertEquals(true, typeDeclaration.getTypeParameters().get(1).declaredOnType());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Aggregations

ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)3 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)3 JavaParserTypeParameter (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter)3 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)3 CompilationUnit (com.github.javaparser.ast.CompilationUnit)2 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)2 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)2 Test (org.junit.Test)2 NodeList (com.github.javaparser.ast.NodeList)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 NodeWithTypeArguments (com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments)1 TypeParameter (com.github.javaparser.ast.type.TypeParameter)1 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)1 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)1 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)1 ResolvedValueDeclaration (com.github.javaparser.resolution.declarations.ResolvedValueDeclaration)1 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)1