Search in sources :

Example 1 with ResolvedMethodDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.

the class ReferenceTypeTest method testReplaceTypeVariables.

@Test
public void testReplaceTypeVariables() {
    TypeSolver typeResolver = new ReflectionTypeSolver();
    ResolvedInterfaceDeclaration streamInterface = new ReflectionInterfaceDeclaration(Stream.class, typeResolver);
    ResolvedReferenceType stream = new ReferenceTypeImpl(streamInterface, typeResolver);
    ResolvedMethodDeclaration streamMap = streamInterface.getDeclaredMethods().stream().filter(m -> m.getName().equals("map")).findFirst().get();
    ResolvedTypeParameterDeclaration streamMapR = streamMap.findTypeParameter("T").get();
    ResolvedTypeVariable typeVariable = new ResolvedTypeVariable(streamMapR);
    stream = stream.deriveTypeParameters(stream.typeParametersMap().toBuilder().setValue(stream.getTypeDeclaration().getTypeParameters().get(0), typeVariable).build());
    ResolvedTypeParameterDeclaration tpToReplace = streamInterface.getTypeParameters().get(0);
    ResolvedType replaced = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeResolver), typeResolver);
    ResolvedType streamReplaced = stream.replaceTypeVariables(tpToReplace, replaced);
    assertEquals("java.util.stream.Stream<java.lang.String>", streamReplaced.describe());
}
Also used : TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) ReflectionInterfaceDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) Test(org.junit.Test)

Example 2 with ResolvedMethodDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.

the class JavaParserClassDeclarationTest method testSolveMethodNotExistingBecauseOfTypeParameters.

@Test
public void testSolveMethodNotExistingBecauseOfTypeParameters() {
    JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolverNewCode.solveType("com.github.javaparser.ast.body.ConstructorDeclaration");
    SymbolReference<ResolvedMethodDeclaration> res = null;
    ResolvedReferenceType stringType = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(String.class, typeSolverNewCode);
    ResolvedReferenceType rawClassType = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(Class.class, typeSolverNewCode);
    ResolvedReferenceType classOfStringType = (ResolvedReferenceType) rawClassType.replaceTypeVariables(rawClassType.getTypeDeclaration().getTypeParameters().get(0), stringType);
    res = constructorDeclaration.solveMethod("isThrows", ImmutableList.of(classOfStringType));
    assertEquals(false, res.isSolved());
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 3 with ResolvedMethodDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.

the class ReflectionMethodResolutionLogic method solveMethod.

static SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> parameterTypes, boolean staticOnly, TypeSolver typeSolver, ResolvedReferenceTypeDeclaration scopeType, Class clazz) {
    List<ResolvedMethodDeclaration> methods = new ArrayList<>();
    Predicate<Method> staticOnlyCheck = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
    for (Method method : clazz.getMethods()) {
        if (method.isBridge() || method.isSynthetic() || !method.getName().equals(name) || !staticOnlyCheck.test(method))
            continue;
        ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
        methods.add(methodDeclaration);
    }
    for (ResolvedReferenceType ancestor : scopeType.getAncestors()) {
        SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(ancestor.getTypeDeclaration(), name, parameterTypes, staticOnly, typeSolver);
        if (ref.isSolved()) {
            methods.add(ref.getCorrespondingDeclaration());
        }
    }
    if (scopeType.getAncestors().isEmpty()) {
        ReferenceTypeImpl objectClass = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
        SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(objectClass.getTypeDeclaration(), name, parameterTypes, staticOnly, typeSolver);
        if (ref.isSolved()) {
            methods.add(ref.getCorrespondingDeclaration());
        }
    }
    return MethodResolutionLogic.findMostApplicable(methods, name, parameterTypes, typeSolver);
}
Also used : ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Predicate(java.util.function.Predicate) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) List(java.util.List) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) Context(com.github.javaparser.symbolsolver.core.resolution.Context) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) Method(java.lang.reflect.Method) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)

Example 4 with ResolvedMethodDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.

the class CompilationUnitContext method solveMethod.

@Override
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly, TypeSolver typeSolver) {
    for (ImportDeclaration importDecl : wrappedNode.getImports()) {
        if (importDecl.isStatic()) {
            if (importDecl.isAsterisk()) {
                String importString = importDecl.getNameAsString();
                if (this.wrappedNode.getPackageDeclaration().isPresent() && this.wrappedNode.getPackageDeclaration().get().getName().getIdentifier().equals(packageName(importString)) && this.wrappedNode.getTypes().stream().anyMatch(it -> it.getName().getIdentifier().equals(toSimpleName(importString)))) {
                    // a lower level so this will fail
                    return SymbolReference.unsolved(ResolvedMethodDeclaration.class);
                }
                ResolvedTypeDeclaration ref = typeSolver.solveType(importString);
                SymbolReference<ResolvedMethodDeclaration> method = MethodResolutionLogic.solveMethodInType(ref, name, argumentsTypes, true, typeSolver);
                if (method.isSolved()) {
                    return method;
                }
            } else {
                String qName = importDecl.getNameAsString();
                if (qName.equals(name) || qName.endsWith("." + name)) {
                    String typeName = getType(qName);
                    ResolvedTypeDeclaration ref = typeSolver.solveType(typeName);
                    SymbolReference<ResolvedMethodDeclaration> method = MethodResolutionLogic.solveMethodInType(ref, name, argumentsTypes, true, typeSolver);
                    if (method.isSolved()) {
                        return method;
                    }
                }
            }
        }
    }
    return SymbolReference.unsolved(ResolvedMethodDeclaration.class);
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) JavaParserAnnotationDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserAnnotationDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Name(com.github.javaparser.ast.expr.Name) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) List(java.util.List) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) CompilationUnit(com.github.javaparser.ast.CompilationUnit) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) JavaParserEnumDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumDeclaration) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)

Example 5 with ResolvedMethodDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.

the class ReflectionMethodDeclarationTest method testGetSignature.

@Test
public void testGetSignature() {
    TypeSolver typeResolver = new ReflectionTypeSolver();
    ResolvedClassDeclaration object = new ReflectionClassDeclaration(Object.class, typeResolver);
    ResolvedInterfaceDeclaration list = new ReflectionInterfaceDeclaration(List.class, typeResolver);
    ResolvedMethodDeclaration hashCode = object.getAllMethods().stream().filter(m -> m.getName().equals("hashCode")).findFirst().get().getDeclaration();
    ResolvedMethodDeclaration equals = object.getAllMethods().stream().filter(m -> m.getName().equals("equals")).findFirst().get().getDeclaration();
    ResolvedMethodDeclaration containsAll = list.getAllMethods().stream().filter(m -> m.getName().equals("containsAll")).findFirst().get().getDeclaration();
    ResolvedMethodDeclaration subList = list.getAllMethods().stream().filter(m -> m.getName().equals("subList")).findFirst().get().getDeclaration();
    assertEquals("hashCode()", hashCode.getSignature());
    assertEquals("equals(java.lang.Object)", equals.getSignature());
    assertEquals("containsAll(java.util.Collection<? extends java.lang.Object>)", containsAll.getSignature());
    assertEquals("subList(int, int)", subList.getSignature());
}
Also used : List(java.util.List) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) Map(java.util.Map) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test) ResolvedClassDeclaration(com.github.javaparser.resolution.declarations.ResolvedClassDeclaration) Assert.assertEquals(org.junit.Assert.assertEquals) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) ResolvedClassDeclaration(com.github.javaparser.resolution.declarations.ResolvedClassDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) Test(org.junit.Test)

Aggregations

ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)35 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)27 Test (org.junit.Test)27 CompilationUnit (com.github.javaparser.ast.CompilationUnit)23 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)19 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)12 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)12 Context (com.github.javaparser.symbolsolver.core.resolution.Context)11 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)11 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)11 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)8 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)7 List (java.util.List)7 MethodUsage (com.github.javaparser.resolution.MethodUsage)6 ResolvedInterfaceDeclaration (com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration)6 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)5 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)5 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)5 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)5