I'm not sure if I described the issue well in the title, but here is the background: I want to parse a java source code, say TestClassOne.java.
TestClassOne uses another class "TestClassTwo" declared as instance variable. Now, TestClassTwo has a class TestClassThree declared as instance variable.
So now, the problem arises when my target class-TestClassOne, gets a reference of TestClassThree via TestClassTwo, thus method test() below:
public class TestClassOne {
private TestClassTwo testTwo = new TestClassTwo();
public void test() {
TestClassThree three = testTwo.getTestThree();
}
}
This is the 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)
Here are my codes:
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());
}
}
TestClassOne:
import javaparser_pkg2.TestClassTwo;
import javaparser_pkg3.TestClassThree;
public class TestClassOne {
private TestClassTwo testTwo = new TestClassTwo();
public void test() {
TestClassThree three = testTwo.getTestThree();
}
}
TestClassTwo:
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;
}
}
TestClassThree:
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;
}
}
I have added the directory of TestClassThree as combinedTypeResolver (see above) but still does not work:
combinedTypeSolver.add(
new JavaParserTypeSolver(new File("..src/javaparser_pkg3")));
How should this be done properly?
Thanks and Regards!