gpt4 book ai didi

javaparser UnsolvedSymbolException - 正在解析的类上使用的类,使用了另一个未解析的类

转载 作者:行者123 更新时间:2023-11-30 02:32:47 40 4
gpt4 key购买 nike

我不确定我是否在标题中很好地描述了这个问题,但背景是这样的:我想解析一个java源代码,比如TestClassOne.java。

TestClassOne 使用另一个类“TestClassTwo”声明为实例变量。现在,TestClassTwo 有一个类 TestClassThree 被声明为实例变量。

现在,当我的目标类 TestClassOne 通过 TestClassTwo 获取 TestClassThree 的引用时,问题就出现了,因此方法 test() 如下:

 public class TestClassOne {    
private TestClassTwo testTwo = new TestClassTwo();

public void test() {
TestClassThree three = testTwo.getTestThree();
}
}

这是异常(exception):

Exception in thread "main" UnsolvedSymbolException{context='unknown', name='TestClassThree', typeSolver=null}
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:418)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:395)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsageVariableType(JavaParserFacade.java:387)
at com.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit(TypeExtractor.java:302)
at com.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit(TypeExtractor.java:34)
at com.github.javaparser.ast.expr.VariableDeclarationExpr.accept(VariableDeclarationExpr.java:104)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:371)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:263)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:257)

这是我的代码:

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseException;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
public class TestParser {

public static void main(String[] args) throws FileNotFoundException, ParseException {

TypeCalculatorVisitor visitor = new TypeCalculatorVisitor();

CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver());

combinedTypeSolver
.add(new JavaParserTypeSolver(new File("..src/javaparser")));

combinedTypeSolver.add(
new JavaParserTypeSolver(new File("..src/javaparser_pkg2")));

combinedTypeSolver.add(
new JavaParserTypeSolver(new File("..src/javaparser_pkg3")));

CompilationUnit agendaCu = JavaParser.parse(new FileInputStream(
new File("..src/javaparser/TestClassOne.java")));

agendaCu.accept(visitor, JavaParserFacade.get(combinedTypeSolver));

System.out.println(visitor.getParseResult());
}

}

测试类一:

import javaparser_pkg2.TestClassTwo;
import javaparser_pkg3.TestClassThree;

public class TestClassOne {
private TestClassTwo testTwo = new TestClassTwo();

public void test() {
TestClassThree three = testTwo.getTestThree();
}
}

测试类二:

import javaparser_pkg3.TestClassThree;

public class TestClassTwo {
private int age;
private String status;

private TestClassThree testThree = new TestClassThree();

public TestClassThree getTestThree() {
return testThree;
}
public void setTestThree(TestClassThree testThree) {
this.testThree = testThree;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}

}

测试类三:

package javaparser_pkg3;

public class TestClassThree {
private String name;
private int ID;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}

}

我已将 TestClassThree 的目录添加为组合类型解析器(见上文),但仍然不起作用:

combinedTypeSolver.add(
new JavaParserTypeSolver(new File("..src/javaparser_pkg3")));

应该如何正确完成此操作?

感谢和问候!

最佳答案

这里是JavaSymbolSolver的维护器。当您定义您的JavaParserTypeSolver时例如,您应该指向源目录的根目录,因为 JavaSymbolSolver 将查找类,计算从考虑包的源目录根目录开始的路径。例如,如果您指定 JavaSymbolSolver(new File("/src/foo"))然后寻找类a.b.C.java它将检查是否有名为 /src/foo/a/b/C.java 的文件存在。您没有指定包目录。如果指定JavaSymbolSolver(new File("/src/foo/a/b"))它将查找文件 src/foo/a/b/a/b/C.java这是不存在的。

有帮助吗?

关于javaparser UnsolvedSymbolException - 正在解析的类上使用的类,使用了另一个未解析的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43840302/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com