Search in sources :

Example 41 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade in project javaparser by javaparser.

the class MethodsResolutionTest method solveMethodInInterfaceParent.

@Test
public void solveMethodInInterfaceParent() {
    CompilationUnit cu = parseSample("MethodCalls");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MethodCalls");
    MethodDeclaration method = Navigator.demandMethod(clazz, "inheritedInterfaceMethod");
    MethodCallExpr expression = Navigator.findMethodCall(method, "toString").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodUsage call1 = javaParserFacade.solveMethodAsUsage(expression);
    assertEquals("java.lang.Object.toString()", call1.getQualifiedSignature());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) 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) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 42 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade in project javaparser by javaparser.

the class GenericsResolutionTest method genericCollectionWithWildcardsAndExtensions.

/*@Test
    public void genericCollectionWithWildcardsAndExtensionsPrep() {
        CompilationUnit cu = parseSample("GenericCollectionWithExtension");
        ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
        MethodDeclaration method = Navigator.demandMethod(clazz, "bar");
        ReturnStmt returnStmt = Navigator.findReturnStmt(method);

        TypeSolver typeSolver = new JreTypeSolver();
        MethodCallExpr call = (MethodCallExpr) returnStmt.getExpr();
        JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);

        List<TypeUsage> params = new ArrayList<>();
        if (call.getArgs() != null) {
            for (Expression param : call.getArgs()) {
                params.add(javaParserFacade.getType(param, false));
            }
        }
        Context context = JavaParserFactory.getContext(call, typeSolver);

        ReferenceTypeUsage typeOfScope = javaParserFacade.getType(call.getScope()).asReferenceType();
        me.tomassetti.symbolsolver.model.declarations.TypeDeclaration typeDeclaration = typeOfScope.getTypeDeclaration();
        List<TypeUsage> typeParametersValues = typeOfScope.typeParametersValues();

        List<MethodUsage> methods = new ArrayList<>();
        for (Method m : List.class.getMethods()) {
            if (m.getName().equals("addAll") && !m.isBridge() && !m.isSynthetic()) {
                me.tomassetti.symbolsolver.model.declarations.MethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(m, typeSolver);
                if (methods.size() == 0) {
                    // ok, e' il primo
                    ReferenceTypeUsage paramType = methodDeclaration.getParam(0).getType(typeSolver).asReferenceType();
                    assertTrue(paramType.asReferenceType().typeParametersValues().get(0).isWildcard());
                }
                MethodUsage mu = new MethodUsage(methodDeclaration, typeSolver);
                int i = 0;
                for (TypeParameter tp : typeDeclaration.getTypeParameters()) {
                    mu = mu.replaceTypeParameter(tp.getName(), typeParametersValues.get(i));
                    i++;
                }
                methods.add(mu);
            }

        }

        assertTrue(MethodResolutionLogic.isApplicable(methods.get(0), "addAll", params, typeSolver));
        //Optional<MethodUsage> methodUsage = MethodResolutionLogic.findMostApplicableUsage(methods, "addAll", params, typeSolver);

        //Optional<MethodUsage> methodUsage = typeDeclaration.solveMethodAsUsage("addAll", params, typeSolver, context, typeParametersValues);

        //Optional<MethodUsage> methodUsage = context.solveMethodAsUsage("addAll", params, typeSolver);

        //assertTrue(methodUsage.isPresent());

    }*/
@Test
public void genericCollectionWithWildcardsAndExtensions() {
    CompilationUnit cu = parseSample("GenericCollectionWithExtension");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
    MethodDeclaration method = Navigator.demandMethod(clazz, "bar");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    TypeSolver typeSolver = new ReflectionTypeSolver();
    Expression returnStmtExpr = returnStmt.getExpression().get();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ResolvedType type = javaParserFacade.getType(returnStmtExpr);
    assertEquals(false, type.isTypeVariable());
    assertEquals("boolean", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) 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) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 43 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade in project javaparser by javaparser.

the class JavaParserTypeParameterResolutionTest method testGenericArguments.

private void testGenericArguments(String containingMethodName) {
    CompilationUnit cu = parseSample("GenericMethodArguments");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ClassOrInterfaceDeclaration classDecl = Navigator.demandClass(cu, "GenericMethodArguments");
    MethodDeclaration containingMethod = Navigator.demandMethod(classDecl, containingMethodName);
    MethodCallExpr bar = Navigator.findMethodCall(containingMethod, "apply").get();
    assertTrue(javaParserFacade.solve(bar).isSolved());
}
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) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 44 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade 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 45 with JavaParserFacade

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

JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)45 CompilationUnit (com.github.javaparser.ast.CompilationUnit)44 Test (org.junit.Test)42 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)40 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)34 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)27 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)23 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)23 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)21 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)19 Expression (com.github.javaparser.ast.expr.Expression)18 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)14 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)8 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)8 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)7 MethodUsage (com.github.javaparser.resolution.MethodUsage)6 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)5 File (java.io.File)5 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)2 JavaParserTypeParameter (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter)2