javaparser UnsolvedSymbolException - Class used on the class being parsed, uses another class that is unresolved

落爺英雄遲暮 提交于 2020-07-07 16:20:22

问题


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!


回答1:


here is the mantainer of JavaSymbolSolver. When you a define your JavaParserTypeSolver instances you should point to the root of the source directory, because JavaSymbolSolver will look for classes calculating the path from the root of the source directory considering the package. For example if you specify JavaSymbolSolver(new File("/src/foo")) and then look for class a.b.C.java it will check if a file named /src/foo/a/b/C.java exists. You do not specify the packages directories. If you specify JavaSymbolSolver(new File("/src/foo/a/b")) it would look for file src/foo/a/b/a/b/C.java which does not exist.

Does it help?



来源:https://stackoverflow.com/questions/43840302/javaparser-unsolvedsymbolexception-class-used-on-the-class-being-parsed-uses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!