Search in sources :

Example 1 with Method

use of org.structr.javaparser.entity.Method in project structr by structr.

the class JavaParserModule method parseJavaFilesAndSolveTypes.

private void parseJavaFilesAndSolveTypes(final Folder folder) {
    if (ignoreTests && "test".equals(folder.getName())) {
        return;
    }
    for (final File file : folder.getFiles()) {
        if (file.getContentType().equals("text/x-java")) {
            final String javaFileName = file.getName();
            if (javaFileName.equals("package-info.java") || javaFileName.equals("testPackage-info.java")) {
            } else {
                final String javaContent = file.getFavoriteContent();
                ClassOrInterface clsOrIface = null;
                CompilationUnit cu = null;
                try {
                    cu = JavaParser.parse(javaContent);
                    for (final TypeDeclaration type : cu.findAll(TypeDeclaration.class)) {
                        SymbolReference<? extends ResolvedValueDeclaration> decl = facade.solve(type.getName());
                        if (type.isClassOrInterfaceDeclaration()) {
                            org.structr.javaparser.entity.Package pkg = null;
                            if (cu.getPackageDeclaration().isPresent()) {
                                pkg = handlePackage(cu.getPackageDeclaration().get());
                            }
                            clsOrIface = handleClassOrInterface(type, pkg);
                        }
                    }
                    for (final BodyDeclaration t : cu.findAll(BodyDeclaration.class)) {
                        if (t instanceof CallableDeclaration) {
                            final CallableDeclaration callable = t.asCallableDeclaration();
                            if (t instanceof ConstructorDeclaration) {
                            // final ConstructorDeclaration cd = t.asConstructorDeclaration();
                            // logger.info("Constructor found: " + cd.getNameAsString());
                            // 
                            // final SymbolReference<ResolvedReferenceTypeDeclaration> constructorRef = typeSolver.tryToSolveType(cd.getNameAsString());
                            // if (constructorRef.isSolved()) {
                            // 
                            // logger.info("Solved constructor: " + cd.getNameAsString());
                            // //final ResolvedReferenceTypeDeclaration decl = constructorRef.getCorrespondingDeclaration();
                            // }
                            } else if (t instanceof MethodDeclaration) {
                                final MethodDeclaration md = t.asMethodDeclaration();
                                final String methodName = md.getNameAsString();
                                logger.info("Method found: " + methodName);
                                // Create methods and link to class
                                final PropertyMap identifyingMethodProperties = new PropertyMap();
                                identifyingMethodProperties.put(Method.name, methodName);
                                final PropertyMap methodProperties = new PropertyMap();
                                methodProperties.putAll(identifyingMethodProperties);
                                methodProperties.put(Method.classOrInterface, clsOrIface);
                                methodProperties.put(Method.declaration, md.getDeclarationAsString());
                                final Optional<BlockStmt> block = md.getBody();
                                if (block.isPresent()) {
                                    methodProperties.put(Method.body, block.get().toString());
                                }
                                final String symbolName = StringUtils.substringAfterLast(clsOrIface.getName(), ".") + "." + md.getNameAsString();
                                try {
                                    final SymbolReference<? extends ResolvedValueDeclaration> methodRef = facade.solve(md.getName());
                                    if (methodRef.isSolved()) {
                                        final ResolvedValueDeclaration decl = methodRef.getCorrespondingDeclaration();
                                        if (decl.isMethod()) {
                                            final String mName = decl.asMethod().getName();
                                            final String signature = decl.asMethod().getSignature();
                                            logger.info("Solved method: " + methodRef.toString() + ", signature: " + signature);
                                            methodProperties.put(Method.resolved, true);
                                        }
                                    }
                                } catch (final UnsolvedSymbolException ignore) {
                                }
                                getOrCreate(Method.class, identifyingMethodProperties, methodProperties);
                                logger.info("Created (or found) method " + symbolName);
                            }
                        // final NodeList<Parameter> parameters = callable.getParameters();
                        // 
                        // List<JsonResult> parameterList = new ArrayList<>();
                        // 
                        // parameters.forEach((p) -> {
                        // 
                        // JsonResult param = new JsonResult();
                        // 
                        // param.addName(p);
                        // param.addType(p.getType());
                        // param.addModifiers(p);
                        // 
                        // parameterList.add(param);
                        // });
                        }
                    }
                } catch (Throwable ignore) {
                }
            }
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) UnsolvedSymbolException(com.github.javaparser.symbolsolver.javaparsermodel.UnsolvedSymbolException) ClassOrInterface(org.structr.javaparser.entity.ClassOrInterface) Package(org.structr.javaparser.entity.Package) Optional(java.util.Optional) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Method(org.structr.javaparser.entity.Method) PropertyMap(org.structr.core.property.PropertyMap) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) CallableDeclaration(com.github.javaparser.ast.body.CallableDeclaration) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) File(org.structr.web.entity.File) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 2 with Method

use of org.structr.javaparser.entity.Method 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

CompilationUnit (com.github.javaparser.ast.CompilationUnit)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)2 Method (org.structr.javaparser.entity.Method)2 PackageDeclaration (com.github.javaparser.ast.PackageDeclaration)1 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)1 CallableDeclaration (com.github.javaparser.ast.body.CallableDeclaration)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)1 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)1 ResolvedValueDeclaration (com.github.javaparser.resolution.declarations.ResolvedValueDeclaration)1 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)1 UnsolvedSymbolException (com.github.javaparser.symbolsolver.javaparsermodel.UnsolvedSymbolException)1 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 App (org.structr.core.app.App)1 PropertyMap (org.structr.core.property.PropertyMap)1 ClassOrInterface (org.structr.javaparser.entity.ClassOrInterface)1 JavaClass (org.structr.javaparser.entity.JavaClass)1 Package (org.structr.javaparser.entity.Package)1