Код здесь,
class Bar {
private int a;
void aMethod() {
while (true) {
String a = "0";
a = a + "1";
}
}
}
Я хочу получить реальный тип a. Я пытался использовать код ниже,
public class GetTypeOfReference {
private static final String FILE_PATH = "src\\com\\test\\Bar.java";
public static void main(String[] args) throws FileNotFoundException {
TypeSolver typeSolver = new CombinedTypeSolver();
JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
StaticJavaParser.getConfiguration().setSymbolResolver(symbolSolver);
CompilationUnit cu = StaticJavaParser.parse(new File(FILE_PATH));
cu.findAll(AssignExpr.class).forEach(ae -> {
ResolvedType resolvedType = ae.calculateResolvedType();
System.out.println(ae.toString() + " is a: " + resolvedType.describe());
});
}
}
Но получил ошибку, Исключение в потоке "main" UnsolvedSymbolException {context = 'null', name = 'String', причина = 'null'} в com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage (JavaParserFacade.java:509. .javaparser.symbolsolver.model.resolution.Value.from (Value.java:40) в java.base / java.util.stream.ReferencePipeline $ 3 $ 1.accept (ReferencePipeline.java:195) в java.base / java.util.stream.ReferencePipeline $ 2 $ 1.accept (ReferencePipeline.java:177) в java.base / java.util.LinkedList $ LLSpliterator.tryAdvance (LinkedList.java:1253) в java.base / java.util.stream.ReferencePipeline.forEachWithClance(ReferencePipeline.java:127) в java.base / java.util.stream.AbstractPipeline.copyIntoWithCancel (AbstractPipeline.java:502) вjava.base / java.util.stream.AbstractPipeline.copyInto (AbstractPipeline.java:488) в java.base / java.util.stream.AbstractPipeline.wrapAndCopyInto (AbstractPipeline.java:474) в java.base / java.util. stream.FindOps $ FindOp.evaluateSequential (FindOps.java:152) в java.base / java.util.stream.AbstractPipeline.evaluate (AbstractPipeline.java:234) в java.base / java.util.stream.ReferencePipeline.findFirst (ReferencePipeline.java! 135) на com.github.javaparser.symbolsolver.resolution.SymbolSolver.solveSymbolAsValue (SymbolSolver.java:68) на com.github.javaparser.symbolsolver.resolution.SymbolSolver.solveSymbolAsValue.gavav.jav. .symbolsolver.javaparsermodel.TypeExtractor.visit (TypeExtractor.java:277) в.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit (TypeExtractor.java:44) в com.github.javaparser.ast.expr.NameExpr.accept (NameExpr.java:79) в com.github.javaparser.delmbol. .TypeExtractor.visit (TypeExtractor.java:98) в com.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit (TypeExtractor.java:44) в com.github.javaparser.ast.expr.AssignExprpr: 135) в com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete (JavaParserFacade.java:448) в com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFubade.javjj .jj .jj. javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType (JavaParserFacade.java:292) в com.github.javaparser.symbolsolver.JavaSymbolSolver.calculateType (JavaSymbolSolver. Expression.java:564) at test.GetTypeOfReference.lambda $ 0 (GetTypeOfReference.java:32)в java.base / java.util.ArrayList.forEach (ArrayList.java:1378) в test.GetTypeOfReference.main (GetTypeOfReference.java:31)
Как правильно сделать это?