2

I have a Spring boot project and I want to parse it and file the dependencies between classes I am using the JavaSymbolSolver to find out the Class Name

 public static void main(String[] args) throws Exception {

        Set<Map<String, Set<String>>> entries = new HashSet<>();
        String jdkPath = "/usr/lib/jvm/java-11-openjdk-amd64/";
        List<File> projectFiles = FileHandler.readJavaFiles(new File("/home/dell/MySpace/Tekit/soon-back/src/main"));
        CombinedTypeSolver combinedSolver = new CombinedTypeSolver
                (
                        new JavaParserTypeSolver(new File("/home/dell/MySpace/Tekit/soon-back/src/main/java/")),
                        new JavaParserTypeSolver(new File(jdkPath)),
                        new ReflectionTypeSolver()
                );

        JavaSymbolSolver symbolSolver = new JavaSymbolSolver(combinedSolver);
        StaticJavaParser.getConfiguration().setSymbolResolver(symbolSolver);
        CompilationUnit cu = null;

        try {
            cu = StaticJavaParser.parse(projectFiles.get(7));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        List<ClassOrInterfaceDeclaration> classes = new ArrayList<>();
        TypeDeclarationImp typeDeclarationImp = new TypeDeclarationImp();
        typeDeclarationImp.visit(cu, classes);
        Set<String> collect = classes.stream()
                .map(classOrInterfaceDeclaration -> {
                    List<MethodCallExpr> collection = new ArrayList<>();
                    MethodInvocationImp methodInvocationImp = new MethodInvocationImp();
                    classOrInterfaceDeclaration.accept(methodInvocationImp, collection);
                    return collection;
                })
                .flatMap(Collection::stream)
                .map(methodCallExpr -> {
                    return methodCallExpr
                            .getScope()
                            .stream()
                            .filter(Expression::isNameExpr)
                            .map(Expression::calculateResolvedType)
                            .map(ResolvedType::asReferenceType)
                            .map(ResolvedReferenceType::getQualifiedName)
                            .map(s -> s.split("\\."))
                            .map(strings -> strings[strings.length - 1])
                            .collect(Collectors.toSet());
                })
                .filter(expressions -> expressions.size() != 0)
                .flatMap(Collection::stream)
                .collect(Collectors.toSet());
        collect.forEach(System.out::println);
    }

I am facing this issue

Exception in thread "main" UnsolvedSymbolException{context='SecurityContextHolder', name='Solving SecurityContextHolder', cause='null'}
  • could you tell me if it is necessary to indicate all the libraries used by the project to parse it or there is another way for that
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Tarfa Ali
  • 23
  • 4
  • 1
    I have solved the problem by providing all the dependencies of the project, when anyone wants to use the JavaParser it's mandatory to provide the all libs that the project depends on (preparing context to do the solve symbols) another hint you need a compiled project without any errors else the project will not be parsed – Tarfa Ali Nov 10 '20 at 09:18

1 Answers1

1

It's not entirely correct. If you only want to traverse the AST you don't need to provide project dependencies but if you want for example to know the type of a variable you must use the symbol solver and declare all the dependencies of the project to it. Furthermore Javaparser can recover from parsing error (see https://matozoid.github.io/2017/06/11/parse-error-recovery.html)

jpl
  • 347
  • 3
  • 11