我想分析我开始使用的类之间的依赖关系 JavaParser它是SymbolResolver。但在解析 Eclipse Scout 示例项目中的多个方法引用时,它始终失败。 。 Scout 使用它自己的 BEAN manager它在 jvm 启动时将 java 类加载到列表中,这使得在运行时加载和卸载类更加灵活。但 Eclipse IDE 能够以某种方式解决依赖关系。这是我用于解析 Eclipse Scout 项目的工作示例:
private static String getFullyQualifiedName(MethodCallExpr exp) {
String result = "";
try {
result = exp.getName() + " --> " + exp.resolve().getQualifiedSignature();
} catch (RuntimeException e) {
result = "!unable to resolve! " + exp.getName();
}
return result;
}
private static void runAnalysis(String sourceFolder) {
final ProjectRoot projectRoot = new SymbolSolverCollectionStrategy().collect(new File(sourceFolder).toPath());
projectRoot.getSourceRoots().forEach(sourceRoot -> sourceRoot.tryToParseParallelized()
.forEach(parsedSource -> parsedSource.getResult().get().findAll(MethodCallExpr.class)
.forEach(exp -> System.out.println(parsedSource.getResult().get().getPackageDeclaration().get().getNameAsString()
+ "." + parsedSource.getResult().get().getStorage().get().getFileName()
+ " (" + exp.getBegin().get().line + ") "
+ getFullyQualifiedName(exp)))));
}
我将所有 Maven 依赖项 JAR 添加到源根文件夹以及所有源代码中,并且我仅使用 Scout 中的简单 helloworld 示例。对我来说,它工作的原因和时间与它无法解析 MethodCallEx 的时间似乎是相当随机的。 Java Symbol Solver 甚至能够解析一些 BEAN.get() 依赖项,这很好。
成功的输出如下所示:
scout.ui.html.UiServletFilter.java (66) destroy --> org.eclipse.scout.rt.server.commons.authentication.DevelopmentAccessController.destroy()
以及这样的失败输出:
scout.server.helloworld.HelloWorldService.java (15) !unable to resolve! getUserId
但是 Eclipse IDE 能够解析所有类和方法调用。
最佳答案
您的分析是在运行时进行还是在基于源代码的 IDE 中进行?前者是关于 Scout runtime 和调用 BEANS 时使用的 BeanManager 的问题,后者是关于 Scout SDK 的问题,您可以下载 here :面向 Scout 开发人员的 Eclipse IDE。
我假设您想分析源代码。当您下载上述 Eclipse 软件包时,您将获得带有 Eclipse Scout 附加插件的 Eclipse IDE。这些插件使用Eclipse平台提供的工具来分析Scout类。因此,我建议您查看 Eclipse Scout SDK source-code 并使用相同的工具进行分析。确保您选择的发布分支与您的 Scout 项目版本相匹配。
关于用于 Eclipse Scout 的 JavaParser 和 SymbolSolver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59683390/