【发布时间】:2020-04-28 04:54:57
【问题描述】:
我想分析我已经开始使用 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 能够解析所有类和方法调用。
【问题讨论】:
-
我现在已经将我的整个本地 maven 存储库
C:\Users\jofroe\.m2添加到示例项目的源路径中,它能够解决许多额外的依赖关系,甚至一些具有 BEAN 访问权限。
标签: java eclipse javaparser eclipse-scout javasymbolsolver