Search in sources :

Example 1 with Package

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

the class JavaParserModule method handlePackageFolder.

private void handlePackageFolder(final Folder folder, final Folder parentFolder) {
    // Folder contains a package-info.java so it must be a package
    String[] parts = folder.getPath().split("src/main/java/");
    if (parts.length > 1) {
        final PropertyMap identifyingProperties = new PropertyMap();
        final PropertyMap allProperties = new PropertyMap();
        // Convert path to package path
        String path = StringUtils.replaceAll(parts[1], "/", ".");
        identifyingProperties.put(Package.name, path);
        allProperties.putAll(identifyingProperties);
        allProperties.put(Package.folder, folder);
        // Check if we are contained in a module:
        // Find the closest ancestor folder which has a module
        Module mod = null;
        Package pkg = null;
        Folder possibleModuleParentFolder = parentFolder;
        // Continue until root folder or a module was found
        while (possibleModuleParentFolder != null && mod == null) {
            try {
                mod = app.nodeQuery(Module.class).and(Module.folder, possibleModuleParentFolder).getFirst();
                pkg = app.nodeQuery(Package.class).and(Module.folder, possibleModuleParentFolder).getFirst();
            } catch (FrameworkException ignore) {
            }
            if (pkg != null) {
                // Parent folder contains a package
                allProperties.put(Package.parent, pkg);
            } else if (mod != null) {
                // Parent folder contains a module
                allProperties.put(Package.module, mod);
                break;
            }
            // Continue while loop
            possibleModuleParentFolder = possibleModuleParentFolder.getParent();
        }
        getOrCreate(Package.class, identifyingProperties, allProperties);
        logger.info("Created or found package '" + path + "' in folder " + folder.getPath());
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) Package(org.structr.javaparser.entity.Package) StructrModule(org.structr.module.StructrModule) Module(org.structr.javaparser.entity.Module) Folder(org.structr.web.entity.Folder)

Example 2 with Package

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

Aggregations

PropertyMap (org.structr.core.property.PropertyMap)2 Package (org.structr.javaparser.entity.Package)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)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 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)1 ResolvedValueDeclaration (com.github.javaparser.resolution.declarations.ResolvedValueDeclaration)1 UnsolvedSymbolException (com.github.javaparser.symbolsolver.javaparsermodel.UnsolvedSymbolException)1 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)1 Optional (java.util.Optional)1 FrameworkException (org.structr.common.error.FrameworkException)1 ClassOrInterface (org.structr.javaparser.entity.ClassOrInterface)1 Method (org.structr.javaparser.entity.Method)1 Module (org.structr.javaparser.entity.Module)1 StructrModule (org.structr.module.StructrModule)1 File (org.structr.web.entity.File)1 Folder (org.structr.web.entity.Folder)1