如何解决在上下文中解析类型
我有一个Spring启动项目,我想解析它并归档使用JavaSymbolSolver找出类名的类之间的依赖关系
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);
}
我正面临这个问题
Exception in thread "main" UnsolvedSymbolException{context='SecurityContextHolder',name='Solving SecurityContextHolder',cause='null'}
- 您能告诉我是否有必要指出该项目使用的所有库来解析它,还是有另一种方法呢?
解决方法
这并不完全正确。如果只想遍历AST,则不需要提供项目依赖项,但是例如,如果您想知道变量的类型,则必须使用符号求解器并将项目的所有依赖项声明给它。 此外,Javaparser可以从解析错误中恢复(请参见https://matozoid.github.io/2017/06/11/parse-error-recovery.html)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。