Search in sources :

Example 31 with ResolvedMethodDeclaration

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

the class ReflectionMethodDeclarationTest method testGetGenericReturnType.

@Test
public void testGetGenericReturnType() {
    TypeSolver typeResolver = new ReflectionTypeSolver();
    ResolvedInterfaceDeclaration map = new ReflectionInterfaceDeclaration(Map.class, typeResolver);
    ResolvedMethodDeclaration put = map.getAllMethods().stream().filter(m -> m.getName().equals("put")).findFirst().get().getDeclaration();
    assertEquals(true, put.getReturnType().isTypeVariable());
    assertEquals(true, put.getReturnType().asTypeParameter().declaredOnType());
    assertEquals("java.util.Map.V", put.getReturnType().asTypeParameter().getQualifiedName());
}
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) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) Test(org.junit.Test)

Example 32 with ResolvedMethodDeclaration

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

the class ReferenceTypeTest method testReplaceTypeVariablesWithLambdaInBetween.

@Test
public void testReplaceTypeVariablesWithLambdaInBetween() {
    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 33 with ResolvedMethodDeclaration

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

the class ReflectionInterfaceDeclarationTest method testGetDeclaredMethods.

@Test
public void testGetDeclaredMethods() {
    TypeSolver typeResolver = new ReflectionTypeSolver();
    ResolvedReferenceTypeDeclaration list = new ReflectionInterfaceDeclaration(List.class, typeResolver);
    List<ResolvedMethodDeclaration> methods = list.getDeclaredMethods().stream().sorted((a, b) -> a.getName().compareTo(b.getName())).collect(Collectors.toList());
    if (isJava9()) {
        assertEquals(40, methods.size());
        assertEquals("clear", methods.get(4).getName());
        assertEquals(true, methods.get(4).isAbstract());
        assertEquals(0, methods.get(4).getNumberOfParams());
        assertEquals("contains", methods.get(5).getName());
        assertEquals(true, methods.get(5).isAbstract());
        assertEquals(1, methods.get(5).getNumberOfParams());
        assertEquals(true, methods.get(5).getParam(0).getType().isReferenceType());
        assertEquals(Object.class.getCanonicalName(), methods.get(5).getParam(0).getType().asReferenceType().getQualifiedName());
    } else {
        assertEquals(28, methods.size());
        assertEquals("clear", methods.get(4).getName());
        assertEquals(true, methods.get(4).isAbstract());
        assertEquals(0, methods.get(4).getNumberOfParams());
        assertEquals("contains", methods.get(5).getName());
        assertEquals(true, methods.get(5).isAbstract());
        assertEquals(1, methods.get(5).getNumberOfParams());
        assertEquals(true, methods.get(5).getParam(0).getType().isReferenceType());
        assertEquals(Object.class.getCanonicalName(), methods.get(5).getParam(0).getType().asReferenceType().getQualifiedName());
    }
}
Also used : ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) Collection(java.util.Collection) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test) HashMap(java.util.HashMap) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) List(java.util.List) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) Assert.assertEquals(org.junit.Assert.assertEquals) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 34 with ResolvedMethodDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration 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)

Example 35 with ResolvedMethodDeclaration

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

the class MethodVisitorAdapter method visit.

@Override
public void visit(final MethodCallExpr methodCall, final Object arg) {
    final Map<String, Object> params = (HashMap) arg;
    final String clsName = (String) params.get("clsName");
    final JavaParserFacade facade = (JavaParserFacade) params.get("facade");
    final App app = (App) params.get("app");
    logger.info("###### " + clsName + ": " + methodCall.getName());
    try {
        // //// !!!!!!!!!!! Methoden-Aufruf kann in den meisten Fällen nicht aufgelöst werden!!
        final SymbolReference<ResolvedMethodDeclaration> ref = facade.solve(methodCall);
        if (ref.isSolved()) {
            final String qualifiedSignature = ref.getCorrespondingDeclaration().getQualifiedSignature();
            // final String scopeString = scope.toString();
            final String parentNodeAsString = methodCall.getParentNode().toString();
            // logger.info("Resolved to " + qualifiedSignature + ", scope: " + scopeString + ", parent node: " + parentNodeAsString);
            logger.info("Resolved to " + qualifiedSignature + ", parent node: " + parentNodeAsString);
            final String calledMethodQualifiedName = StringUtils.replacePattern(qualifiedSignature, "\\(.*\\)", "");
            final String calledMethodQualifiedClassName = StringUtils.substringBeforeLast(calledMethodQualifiedName, ".");
            final String calledMethodName = StringUtils.substringAfterLast(calledMethodQualifiedName, ".");
            Method calledMethod = null;
            final JavaClass calledMethodClass = (JavaClass) app.nodeQuery(JavaClass.class).and(JavaClass.name, calledMethodQualifiedClassName).getFirst();
            if (calledMethodClass != null) {
                logger.info("└ Found called class in graph: " + calledMethodClass.getName());
                calledMethod = (Method) app.nodeQuery(Method.class).and(Method.name, calledMethodName).and(Method.classOrInterface, calledMethodClass).getFirst();
                if (calledMethod != null) {
                    logger.info("└ Found called method in graph: " + calledMethod.getProperty(Method.declaration));
                    final Optional<MethodDeclaration> callingMethod = methodCall.getAncestorOfType(MethodDeclaration.class);
                    if (callingMethod.isPresent()) {
                        final String callingMethodDeclaration = callingMethod.get().getDeclarationAsString();
                        logger.info("└ Calling method: " + callingMethodDeclaration);
                        final String callingMethodName = callingMethod.get().getNameAsString();
                        final Optional<TypeDeclaration> typeDecl = callingMethod.get().getAncestorOfType(TypeDeclaration.class);
                        if (typeDecl.isPresent()) {
                            final String callingMethodClassName = typeDecl.get().getNameAsString();
                            // Find compilation unit
                            final Optional<CompilationUnit> localCU = typeDecl.get().getAncestorOfType(CompilationUnit.class);
                            if (localCU.isPresent()) {
                                // Does it have a package declaration?
                                final Optional<PackageDeclaration> packageDecl = localCU.get().getPackageDeclaration();
                                if (packageDecl.isPresent()) {
                                    // Assemble qualified class name
                                    final String packageName = packageDecl.get().getNameAsString();
                                    final String fqcn = packageName + "." + callingMethodClassName;
                                    // Find class in graph
                                    final JavaClass callingClass = (JavaClass) app.nodeQuery(JavaClass.class).and(JavaClass.name, fqcn).getFirst();
                                    if (callingClass != null) {
                                        final Method method = (Method) app.nodeQuery(Method.class).and(Method.name, callingMethodName).and(Method.classOrInterface, callingClass).getFirst();
                                        if (method != null) {
                                            logger.info("Found calling method in graph: " + method.getName());
                                            final List<Method> methodsCalled = method.getProperty(Method.methodsCalled);
                                            methodsCalled.add(calledMethod);
                                            method.setProperty(Method.methodsCalled, methodsCalled);
                                            logger.info("Added " + calledMethod.getName() + " to list of methods called in " + method.getName());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (final Throwable t) {
        logger.info("Unable to resolve " + clsName, t);
    }
}
Also used : App(org.structr.core.app.App) CompilationUnit(com.github.javaparser.ast.CompilationUnit) HashMap(java.util.HashMap) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Method(org.structr.javaparser.entity.Method) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) JavaClass(org.structr.javaparser.entity.JavaClass) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

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