Code Monkey home page Code Monkey logo

javasymbolsolver's Introduction

JavaSymbolSolver has been integrated in JavaParser: development will continue there!

We will work on current issues opened here, but all new issues should be opened in the JavaParser project

JavaSymbolSolver

Maven Central Build Status

A Symbol Solver for Java built on top of JavaParser from the same team of committers.

Documentation

Currently the best source of documentation is in this book

You can also read this article:

How this complement JavaParser?

JavaParser is a parser: given a source file it recognizes the different syntatic element and produce an Abstract Syntax Tree (AST).

JavaSymbolSolver analyzes that AST and find the declarations connected to each element.

foo in the AST is just a name, JavaSymbolSolver can tell you if it refers to a parameter, a local variable, a field. It can also give you the type, tell you where the element has been defined and so on.

What can I use it for?

A Symbol Solver can associate a symbol in your code to its declaration. This is necessary to verify the type of an expression or to find the usage of a symbol (like a field or a local variable):

Consider this:

int a = 0;

void foo() {
    while (true) {
        String a = "hello!";
        Object foo = a + 1;
    }
}

In the expression a + 1 a parser (like JavaParser) is not able to tell us to which definition of a we are referring to and consequently it cannot tell us the type of a. The JavaSymbolSolver is able to do so.

How can I use it? Show me some code!

Take a look at JavaParserFacade. For example you can use it to find the type of an expression:

Node node = <get this node by parsing source code with JavaParser>
Type typeOfTheNode = JavaParserFacade.get(typeSolver).getType(node);

Easy, right?

The only configuration that it requires is part of the TypeSolver instance to pass it. A TypeSolver is the mechanism that is used to find the classes referenced in your code. For example your class could import or extend a given class and the TypeSolver will find it and build a model of it, later used to solve symbols. Basically there are four TypeSolver:

  • JavaParserTypeSolver: look for the type in a directory of source files
  • JarTypeSolver: look for the type in a JAR file
  • ReflectionTypeSolver: look for the type using reflection. This is needed because some classes are not available in any other way (for example the Object class). However this should be used exclusively for files in the java or javax packages
  • CombinedTypeSolver: permits to combine several instances of TypeSolvers

In the tests you can find an example of instantiating TypeSolvers:

CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver());
combinedTypeSolver.add(new JavaParserTypeSolver(new File("src/test/resources/javaparser_src/proper_source")));
combinedTypeSolver.add(new JavaParserTypeSolver(new File("src/test/resources/javaparser_src/generated")));

Typically to analize a project you want to create one instance of JavaParserTypeSolver for each source directory, one instance of JarTypeSolver for each dependency and one ReflectionTypeSolver then you can combine all of them in a CombinedTypeSolver and pass that around.

Tutorial on resolving method calls

We plan to write soon more examples and tutorials.

Status of the project

This project is more recent of JavaParser but it has been receiving some attention and it has been improving a lot recently.

It supports all features of Java 8 (lambdas, generic, type inference, etc.). Of course we expect some bugs to emerge from time to time but we are committed to help users solve them as soon as possible.

It has been also used in a commercial product from Coati.

License

This code is available under the Apache License.

Development

We use Travis to ensure our tests are passing all the time.

The dev-files dir contains configurations for the Eclipse and the IDEA formatters (I took them from the JavaParser project, thanks guys!).

The project is structured in this way:

  • We have nodes that wrap the JavaParser nodes (but can also wrap Javassist or JRE nodes)
  • The nodes contain all the information of the AST
  • Context classes contain the logic to solve methods, symbols and types in the respective context.
  • Default fallback behavior: ask the parent context for help (so if a variable identifier cannot be solved inside a MethodContext the underlying ClassDeclarationContext is asked and maybe we find out that the identifier actually refers to a field.

A more detailed description of the architecture of the project is available in Design.MD

Gradle usage

We suggest that you use the local gradle wrapper within java-symbol-solver to execute gradle tasks. If you're working on a Linux or Mac system, this is ./gradlew in the root directory of java-symbol-solver. On Windows, it's gradlew.bat. When executing gradle tasks via your IDE, make sure to configure your IDE to also use this wrapper. If you use intelliJ, you need the "Use default Gradle wrapper" option in the IntelliJ Gradle settings.

The following tasks are most relevant:

  • assemble: If you need to build the source code (the full command thus becomes .\gradlew assemble or .\gradlew.bat assemble depending on your Operating System).
  • check: To run the tests
  • check jacocoTestReport: To run the tests (if still necessary) and generate a test coverage report. This coverage report is then located at ./java-symbol-solver-testing/build/jacocoHtml/index.html relative to your project root.
  • install: To install the snapshot version of the project to your local maven repository, which can be useful to test changes you're working on against external projects.

In case you haven't worked with Gradle before, one thing of note is that Gradle will only perform tasks that are still relevant. Let's say you have subprojects foo and bar and you had previously compiled those. If you then make changes in foo and compile both again, only foo will be compiled. You'll see in the Gradle output that the compile task of bar is marked as UP-TO-DATE.

Contributing

I would absolutely love every possible kind of contributions: if you have questions, ideas, need help or want to propose a change just open an issue. Pull-requests are greatly appreciated.

Thanks to Malte Langkabel, Ayman Abdelghany, Evan Rittenhouse, Rachel Pau, Pavel Eremeev, Simone Basso, Rafael Vargas, Christophe Creeten, Fred Lefévère-Laoide, and Genadz Batsyan for their contributions!

The project has been created by Federico Tomassetti and it is currently co-maintained by the JavaParser team. Core contributors are currently Malte Langkabel and Panayiotis Pastos

javasymbolsolver's People

Contributors

aymandf avatar bassosimone avatar batterseapower avatar bfergerson avatar chrstphlbr avatar emanguy avatar ftomassetti avatar gottox avatar loren138 avatar maridproject avatar matozoid avatar mcesar avatar mlangkabel avatar pevers avatar selslack avatar staslev avatar tnterdan avatar whizzter avatar wimtibackx avatar xdrop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

javasymbolsolver's Issues

Do we have to guess the context where a symbol came from?

I've been looking through your example and JavaParserFacade.java. It looks like JavaParserFacade can only resolve types for the TypeSolver you pass to JavaParserFacade.get(). If that's the case, what's the point of JavaParserFacade storing multiple TypeSolvers? For example, if a class imported 3 other classes from different packages it seems I'd have to do this:

public void solveSymbolInClass()
{
    // Do some parsing, hit symbol s. Assume it's a descendent of Node and could possibly come from package1, package2, or package3
    // Make type solvers for the three packages
    JavaParserTypeSolver solverP1 = new JavaParser("src/package1/");
    JavaParserTypeSolver solverP2 = new JavaParser("src/package2/");
    JavaParserTypeSolver solverP3 = new JavaParser("src/package3");

    // Build a JavaParserFacade to find type for node s
    JavaParserFacade resolver = new JavaParserFacade(solverP1);
    resolver.get(solverP1).getType(s); // This may or may not get resolved
    resolver.get(solverP2).getType(s); // This may or may not get resolved
    resolver.get(solverP3).getType(s); // This may or may not get resolved
}

or this:

public void solveSymbolInClass()
{
    // Do some parsing, hit symbol s. Assume it's a descendent of Node and could possibly come from package1, package2, or package3
    // Make type solvers for the three packages
    JavaParserTypeSolver solverP1 = new JavaParser("src/package1/");
    JavaParserTypeSolver solverP2 = new JavaParser("src/package2/");
    JavaParserTypeSolver solverP3 = new JavaParser("src/package3");

    // Make a combined type solver for all 3 packages
    CombinedTypeSolver cts = new CombinedTypeSolver();
    cts.add(solverP1);
    cts.add(solverP2);
    cts.add(solverP3);

    // Build a JavaParserFacade to find type for node s
    JavaParserFacade resolver = new JavaParserFacade(cts);
    resolver.get(cts).getType(s);
}

Why is the get() function necessary?

GetAllFields missing

TypeDeclaration has some getAll... methods, but no way that I can see that gets me all fields, including those of ancestors. Can we get getAllFields?

Class name of TypeParameter

@ftomassetti: While you're in refactoring mode, I'd like to bring up a naming issue I've seen in the SymbolSolver codebase: Type parameters.

Sometimes they are calles TypeParameter, sometimes just parameter, sometimes TypeVariable and I'm not totally sure but maybe I've also seen some code calling them actual and formal parameters.
I think we should consistently stick to one path and call it TypeParameter and TypeArgument or call it FormalTypeParameter and ActualTypeParameter.

I've been working with Clang a lot over the last three years and they call it parameter and argument and I think the Javaparser also uses this convention. What do you think?

Apply the Apache License 2.0

This was already reported as the license in the Readme but we should attach the license description and add a notice to every source file.

IllegalStateException when using raw type instead of parameterized type

Consider the following code:

public class Test{
  class A <T> {
  }

  void foo() {
    A a = null;
    List<A<?>> l = null;
    l.add(a);
  }
}

Solving the call to l.add(a); throws an IllegalStateException because the method's expected parameter and the provided argument have a different amount of type arguments.

JavaParserFacade.solve(NameExpr) not working for Parameter of CatchClause

When using the JavaParserFacade to solve the NameExpr of a variable that has been defined as the parameter of a catch clause it does not seem to be resolved. When looking at the JavaParserFactory.getContext() I don't see something like a CatchClauseContext. Is that one missing at the moment? Or is this something that's intended?

Unable to solve Type defined in base class

It seems like the symbol solver is unable to solve a type that has been defined in a class' base class. Please refer to the following code snippet for an example:

public class Foo {
    public class Base {
        public class X {
        }
    }

    public class Derived extends Base {
        public X x = null;
    }
}

When trying to solve the type of Derived.x an UnsolvedSymbolException{context='unknown', name='X', typeSolver=null} is thrown (callstack is at the end).

I think the me.tomassetti.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration.solveType(...) is missing to look into extended or implemented classes/interfaces.

And here the callstack (it's not very spectacular):

Exception in thread "Thread-0" UnsolvedSymbolException{context='unknown', name='X', typeSolver=null}
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:444)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convert(JavaParserFacade.java:487)

        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convert(JavaParserFacade.java:483)

        at io.coati.JavaTypeNameResolver.getTypeName(JavaTypeNameResolver.java:48)
        ...

UnsupportedOperationException when solving MethodCallExpr on super

I just wanted to report this exception:

In my code I have an ASTVisitor that overrides the public void visit(final MethodCallExpr n, final Void v). Inside this method I'm calling JavaParserFacade.get(typeSolver).solveMethodAsUsage(n);
When running the ASTVisitor on the following code

public class Bla {
    public class Superclass {
        public void foo() {
        }
    }

    public class Subclass extends Superclass {
        public void foo() {
            super.foo();
        }
    }
}

this call results in an UnsupportedOperationException. Here's the callstack:

Exception in thread "Thread-3" java.lang.UnsupportedOperationException: com.github.javaparser.ast.expr.SuperExpr
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:394)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
        at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:156)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:496)
        at io.coati.JavaAstVisitor.visit(JavaAstVisitor.java:371)
        at io.coati.JavaAstVisitor.visit(JavaAstVisitor.java:1)
        at com.github.javaparser.ast.expr.MethodCallExpr.accept(MethodCallExpr.java:73)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:399)
        at com.github.javaparser.ast.stmt.ExpressionStmt.accept(ExpressionStmt.java:53)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:170)
        at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:58)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:562)
        at io.coati.JavaAstVisitor.visit(JavaAstVisitor.java:216)
        at io.coati.JavaAstVisitor.visit(JavaAstVisitor.java:1)
        at com.github.javaparser.ast.body.MethodDeclaration.accept(MethodDeclaration.java:111)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:223)
        at io.coati.JavaAstVisitor.visit(JavaAstVisitor.java:135)
        at io.coati.JavaAstVisitor.visit(JavaAstVisitor.java:1)
        at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:85)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:223)
        at io.coati.JavaAstVisitor.visit(JavaAstVisitor.java:135)
        at io.coati.JavaAstVisitor.visit(JavaAstVisitor.java:1)
        at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:85)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:251)
        at com.github.javaparser.ast.CompilationUnit.accept(CompilationUnit.java:84)
        at io.coati.JavaIndexer.processFile(JavaIndexer.java:107)

However, solving a call to this.foo() works fine.

Type name qualified by method name

I'm currently expanding our source code analysis tool (which is currently available for C/C++ only) to support java as well. First I was using Javaparser but it didn't quite match our requirements. By accident I stumbled upon a comment where @ftomassetti mentioned this project. And I have to say: It's absolutely AWESOME! Thanks for making this!

Ok, now to my issue.I have the following code I want to analyze:

package x;

public class Foo
{
    public void test(float p)
    {
        class Bar
        {
            public float f;
        }
    }

    public void test(int p)
    {
        class Bar
        {
            public int f;
        }
    }
}

When using an AstVisitor that specifies the following visit method

@Override public void visit(final ClassOrInterfaceDeclaration n, final Void v)
{
    String name = JavaParserFacade.get(m_typeSolver).getTypeDeclaration(n).getQualifiedName();
    System.out.println(name);
    super.visit(n, v);
}

I get the following output:

x.Foo
x.Foo.Bar
x.Foo.Bar

Here the problem is that both Bar classes may define completely different types but share the same qualified name. Would it be possible to include the name of the method that acts as the class' context into the qualified name so that the output reads

x.Foo
x.Foo.test(float).Bar
x.Foo.test(int).Bar

instead?

Return method signature

Instead of this:

MethodUsage solvedMethod = JavaParserFacade.get(typeSolver).solveMethodAsUsage(node);
                String qualifiedName=solvedMethod.declaringType().getQualifiedName()+"."+solvedMethod.getName()+"(";
                for(int i = 0; i<solvedMethod.getParamTypes().size();i++){

                    if(solvedMethod.getParamTypes().size()>1 && i != 0){
                        qualifiedName = qualifiedName.concat(",");
                    }
                    String type = solvedMethod.getParamTypes().get(i).describe();
                    qualifiedName = qualifiedName.concat(type.substring(type.lastIndexOf(".")+1));
                }
                qualifiedName = qualifiedName.concat(")");

we should have a single method call.

Get Declaration for Type

Today I've got another problem: Is it possible to fetch the com.github.javaparser.ast.body.ClassOrInterfaceDeclaration for a com.github.javaparser.ast.type.ClassOrInterfaceType?

I've seen that the JavaParserFacade can be used to convert a Type and a Node to a TypeUsage (right now I don't know which node to pass to that method). Afterwards the TypeUsage can be used to get the type's declaration (which is a me.tomassetti.symbolsolver.model.declarations.TypeDeclaration). I guess the wrappedNode of this declaration is what I'm looking for, but in the current implementation of the Java Symbol Solver it is a private field. Is it possible to add a public getter for this field?

The reason I need access to the AST node is type name resolution as discussed in #37.

Is this published?

The jar on Maven is almost empty.

$ wget http://central.maven.org/maven2/me/tomassetti/java-symbol-solver-logic/0.1.3/java-symbol-solver-logic-0.1.3.jar
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0  1195    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  6991  100  6991    0     0   152k      0 --:--:-- --:--:-- --:--:--  152k
$ unzip -l java-symbol-solver-logic-0.1.3.jar
Archive:  java-symbol-solver-logic-0.1.3.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2015-11-24 21:41   META-INF/
       25  2015-11-24 18:57   META-INF/MANIFEST.MF
        0  2015-11-24 18:57   me/
        0  2015-11-24 18:57   me/tomassetti/
        0  2015-11-24 18:57   me/tomassetti/symbolsolver/
        0  2015-11-24 21:41   me/tomassetti/symbolsolver/logic/
     4631  2015-11-24 21:41   me/tomassetti/symbolsolver/logic/GenericTypeInferenceLogic.class
     3153  2015-11-24 21:41   me/tomassetti/symbolsolver/logic/FunctionalInterfaceLogic.class
     1957  2015-11-24 21:41   me/tomassetti/symbolsolver/logic/AbstractTypeDeclaration.class
     3913  2015-11-24 21:41   me/tomassetti/symbolsolver/logic/AbstractClassDeclaration.class
---------                     -------
    13679                     10 files

Unable to solve method depending on type parameter of declaring type

To reproduce use the following code snippet:

  public class Base <T> {
    public void callee(T data) {
    }
  }

  public class Derived extends Base<String> {
    public void caller() {
      callee("test");
    }
  }

This code produces this exception:

java.lang.RuntimeException: Method 'callee' cannot be resolved in context callee("test") (line: 14) MethodCallExprContext{}

It seems like the MethodResolutionLogic does not regard the type parameters of the method's declaring type when testing if a method isApplicable.

Avoid infinite recursion when a class is using its own method

I had this problem when trying to solve the method usage for p.ClassOrInterfaceBodyDeclaration(false) in :

@FunctionalInterface
public interface ParseStart<R> {
    ParseStart<CompilationUnit> COMPILATION_UNIT = ASTParser::CompilationUnit;
    ParseStart<BlockStmt> BLOCK = ASTParser::Block;
    ParseStart<Statement> STATEMENT = ASTParser::BlockStatement;
    ParseStart<ImportDeclaration> IMPORT_DECLARATION= ASTParser::ImportDeclaration;
    ParseStart<Expression> EXPRESSION = ASTParser::Expression;
    ParseStart<AnnotationExpr> ANNOTATION = ASTParser::Annotation;
    ParseStart<BodyDeclaration<?>> ANNOTATION_BODY = ASTParser::AnnotationBodyDeclaration;
    ParseStart<BodyDeclaration<?>> CLASS_BODY = p -> p.ClassOrInterfaceBodyDeclaration(false);
    ParseStart<BodyDeclaration<?>> INTERFACE_BODY = p -> p.ClassOrInterfaceBodyDeclaration(true);

    R parse(ASTParser parser) throws ParseException;
}

Responsibility of JavaParserXxxDeclaration vs XxxContext

On the one hand side the JavaParserClassDeclaration has a method solveMethod that just asks the respective context to solve the method.

On the other hand side the method solveType is implemented in the JavaParserClassDeclaration and the respective context (in this case it's the ClassOrInterfaceDeclarationContext) just calls this implementation so solve a type.

So for me it seems a little unclear who's responsible for solving symbols, methods and types. The JavaParserXxxDeclaration or the XxxContext?

Generic Type without type parameters throws IndexOutOfBoundException

Great library and It is really helping me in my project.
I encountered problem with no workaround as of now and I have a solution too. If time permits I will fix and raise a pull request but In case it takes time and you have a luxury of spending time to give a quick fix, I am listing the solution here.

Problem Description:
Target Code

Map m = new HashMap();
m.put("a", "b");

Parsing Code;

javaParserFacade.getType(n); //where n is a MethodCallExpr or m.put("a", "b");

Throws IndexOutOfBoundException at location ReflectionClassDeclaration#solveMethodAsUsage#158

TypeUsage newValue = typeParameterValues.get(i); where typeParameterValues is empty list.

Possible Fix

for (int i = 0; i < getTypeParameters().size(); i++) {
                    String nameToReplace = getTypeParameters().get(i).getName();
                    TypeUsage newValue = null;
                    if ( typeParameterValues.size() == getTypeParameters.size() ) {
                        newValue = typeParameterValues.get(i);
                    } else {
                        newValue = parameterTypes.get(i);
                    }
                    methodUsage = methodUsage.replaceNameParam(nameToReplace, newValue);
                }

FieldAccessContext skips wrappedNode on solveSymbol(...)

I was just looking at the FieldAccessContext. It doesn't seem to do anything but asking its parent context when solving a symbol. Usually this seems fine except for the following situation:

public class Foo{
    private int bar;

    public void test(int bar){
        this.bar = bar;
    }
}

In this case the context of the method is asked to solve the name before the class context. So this.bar is solved as being the parameter of the test(...) method (which is not correct ;) ).

UnsolvedSymbolException when solving call to UnsupportedOperationException.getMessage()

To reproduce override the following method of the VoidVisitorAdapter:

public void visit(final MethodCallExpr n, final Void v) {
    JavaParserFacade.get(new JreTypeSolver()).solveMethodAsUsage(n);
    super.visit(n, v);
}

Additionally I added a visit method on ClassOrInterfaceType to evaluate if other content of the java.lang package is resolved correctly (and this works fine).

public void visit(final ClassOrInterfaceType n, final Void v) {
            TypeUsage typeUsage = JavaParserFacade.get(new JreTypeSolver()).convert(n, n);
            me.tomassetti.symbolsolver.model.declarations.TypeDeclaration td = typeUsage.asReferenceTypeUsage().getTypeDeclaration();
            System.out.println(td.getQualifiedName());
            super.visit(n, v);
        }

Using this visitor try to parse the following code and visit the resulting compilation unit:

public class Bla {
    public void main()
    {
        try
        {
            int i = 0;
        }
        catch (UnsupportedOperationException e)
        {
            String s;
            e.getMessage();
        }
    }
}

This results in an UnsolvedSymbolException with the following callstack:

Exception in thread "main" UnsolvedSymbolException{context='FOO Solving e', name='e', typeSolver=null}
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:213)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
        at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:156)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:496)
        at test.Main$JavaAstVisitor.visit(Main.java:24)
        at test.Main$JavaAstVisitor.visit(Main.java:22)
        at com.github.javaparser.ast.expr.MethodCallExpr.accept(MethodCallExpr.java:73)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:399)
        at com.github.javaparser.ast.stmt.ExpressionStmt.accept(ExpressionStmt.java:53)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:170)
        at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:58)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:192)
        at com.github.javaparser.ast.stmt.CatchClause.accept(CatchClause.java:66)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:743)
        at com.github.javaparser.ast.stmt.TryStmt.accept(TryStmt.java:72)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:170)
        at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:58)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:562)
        at com.github.javaparser.ast.body.MethodDeclaration.accept(MethodDeclaration.java:111)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:223)
        at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:85)
        at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:251)
        at com.github.javaparser.ast.CompilationUnit.accept(CompilationUnit.java:84)
        at test.Main.main(Main.java:45)

Uncaught exception, UnsolvedSymbolException(var11) at 473:JavaParserFacade

First of all, thanks a lot for your work in this amazing tools!. I'm using JavaParser and java-symbol-solver to make an analysis tool.

I'm triying to execute this code:

CombinedTypeSolver typeSolver = new CombinedTypeSolver();
typeSolver.add(new JreTypeSolver());
typeSolver.add(new JavaParserTypeSolver(new File("path/to/example/JavaProject")));
TypeUsage typeOfTheNode = JavaParserFacade.get(typeSolver).getType(node);

The Node in this example is a MethodCallExpr - "instance.em.createNativeQuery("shutdown").executeUpdate();" from the source file I'm trying to analyze, it's located inside the JavaProject path given to JavaParserTypeSolver):

package persistence;

import persistence.ExcepcionesPersis.DBConexionError;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class PersistenceManager {

    EntityManagerFactory emf;
    EntityManager em;
    static PersistenceManager instance = null;

    private PersistenceManager() {
        emf = Persistence.createEntityManagerFactory("Example");
        em = emf.createEntityManager();
    }

    public EntityManager getEntityManager() {
        return em;
    }

    public static void newConnection() throws DBConexionError {
        if (instance == null) {
            instance = new PersistenceManager();
        }
    }

    public static PersistenceManager instance() {
        return instance;
    }

    public static void closeConnection() {
        if (instance != null) {
            instance.em.getTransaction().begin();
            instance.em.createNativeQuery("shutdown").executeUpdate();
            instance.em.getTransaction().commit();
            instance.em.close();
            instance.emf.close();
            instance = null;
        }
    }

}

And the error I get:

Exception in thread "main" UnsolvedSymbolException{context='unknown', name='EntityManager', typeSolver=null}
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:438)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:428)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convert(JavaParserFacade.java:481)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convert(JavaParserFacade.java:477)
    at me.tomassetti.symbolsolver.javaparsermodel.declarations.JavaParserFieldDeclaration.getType(JavaParserFieldDeclaration.java:37)
    at me.tomassetti.symbolsolver.model.typesystem.ReferenceTypeUsage.getFieldType(ReferenceTypeUsage.java:131)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.FieldAccessContext.solveSymbolAsValue(FieldAccessContext.java:45)
    at me.tomassetti.symbolsolver.resolution.SymbolSolver.solveSymbolAsValue(SymbolSolver.java:42)
    at me.tomassetti.symbolsolver.resolution.SymbolSolver.solveSymbolAsValue(SymbolSolver.java:47)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:290)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:153)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:494)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:220)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:153)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:494)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:220)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
    at es.ujaen.Searcher$MethodCallVisitor.visit(Searcher.java:89)
    at com.github.javaparser.ast.expr.MethodCallExpr.accept(MethodCallExpr.java:73)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:407)
    at com.github.javaparser.ast.stmt.ExpressionStmt.accept(ExpressionStmt.java:53)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:169)
    at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:58)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:459)
    at com.github.javaparser.ast.stmt.IfStmt.accept(IfStmt.java:61)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:169)
    at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:58)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:567)
    at com.github.javaparser.ast.body.MethodDeclaration.accept(MethodDeclaration.java:113)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:231)
    at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:86)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:260)
    at es.ujaen.Searcher.search(Searcher.java:48)
    at es.ujaen.Searcher.searchReferences(Searcher.java:27)
    at es.ujaen.Engine.run(Engine.java:23)
    at es.ujaen.Main.main(Main.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Process finished with exit code 1

This is just an example of what I'm doing, I have tried diferent types of nodes and I always get the same exception.
Am I doing something wrong?

Build hangs on AnalyseJavaParserTest

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.203 sec
Running me.tomassetti.symbolsolver.resolution.ContextTest
Tests run: 23, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.315 sec
Running me.tomassetti.symbolsolver.resolution.AnalyseJavaParserTest
(nothing happens anymore)

Move java-symbol-solver under the JavaParser umbrella

This issue is intended to discuss how to move the project under JavaParser

  • Change license #95
  • Change packages (from me.tomassetti to com.github.javaparser) #97
  • Update the Readme to reflect the change #100
  • Transfer the project under the JavaParser organization
  • Do a release with the new coordinates #108
  • Uniform the build system to JavaParser #101
  • Adopt the JavaParser code style #103
  • Rename the project #99

Anything else?

In this issue we could discuss the list and then open single issues for each task.

StackOverflowError when solving call on method return type

To reproduce override the following method of the VoidVisitorAdapter:

public void visit(final MethodCallExpr n, final Void v) {
    JavaParserFacade.get(new JreTypeSolver()).solveMethodAsUsage(n);
    super.visit(n, v);
}

Using this visitor try to parse the following code and visit the resulting compilation unit:

public class Bla {
    public JavaTest getSelf()
    {
        return this;
    }

    public void bar()
    {
    }

    void test()
    {
        getSelf().getSelf().bar();
    }
}

This results in an StackOverflowError with the following callstack (at least the top of it ;) ):

Exception in thread "main" java.lang.StackOverflowError
        at java.lang.AbstractStringBuilder.append(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at com.github.javaparser.ast.visitor.DumpVisitor$SourcePrinter.print(DumpVisitor.java:133)
        at com.github.javaparser.ast.visitor.DumpVisitor.printArguments(DumpVisitor.java:256)
        at com.github.javaparser.ast.visitor.DumpVisitor.visit(DumpVisitor.java:927)
        at com.github.javaparser.ast.expr.MethodCallExpr.accept(MethodCallExpr.java:73)
        at com.github.javaparser.ast.Node.toString(Node.java:229)
        at java.lang.String.valueOf(Unknown Source)
        at java.lang.StringBuilder.append(Unknown Source)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:218)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
        at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:156)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:496)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:220)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
        at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:156)
        at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:184)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:496)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:220)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
        at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:156)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:496)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:220)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
        at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:156)
        at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:184)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:496)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:220)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
        at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:156)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:496)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:220)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
        at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)

Tests are failing on Windows

When running the java-symbol-solver-core tests on Windows I get errors like this one:
org.junit.ComparisonFailure: Line 1 of src/test/resources/javaparser_expected_output/com_github_javaparser_ast_stmt_CatchClause.txt is different from what is expected expected:<[ ]> but was:<[]>

When re-generating the expected output files and using git to compare them to the original ones, it says that they have been modified but I cannot see any changes. Maybe it's a line endings thing.

Requesting clearInstances() method for JavaParserFacade

When using the Javaparser and JavaSymbolSolver to index source code the memory required by the instance(s) of the JavaParserFacade is continuously growing with every analyzed source file. Right now there seems to be no way of getting rid of JavaParserFacades that are not in use anymore.

Therefore I'd like to request adding a method to clear those instances.

UnsupportedOperationException when solving an ArrayAccesExpr node with brackets

When I try to solve this kind of expression:
arrSQL[1]

This is what I get

java.lang.UnsupportedOperationException: com.github.javaparser.ast.expr.ArrayAccessExpr
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:392)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)

java.util.Arrays asList

Awesome tool this!

But I get an error when using Arrays.asList, see the following case.

Extend the generics.java.txt to:

package me.tomassetti.symbolsolver.javaparser;

import java.util.Optional;
import java.util.List;
import java.util.Arrays;

public final class Generics {

    Optional<String> s;
    Optional<Integer> i;
    Optional<Generics> g;

}

public final class SomeCollection<A> {

    public A a;
    public List<A> as;

    public void foo1(){
        new SomeCollection<String>().a;
    }

    public void foo2(){
        new SomeCollection<String>().as;
    }

    public List<Long> foo3(){
        return Arrays.asList(new Long[]{1L,2L});
    }

}

Create the function:

    @Test
    public void blaat() throws ParseException {
        CompilationUnit cu = parseSample("Generics");
        ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SomeCollection");

        MethodDeclaration method = Navigator.demandMethod(clazz, "foo3");

        ReturnStmt stmt = (ReturnStmt)method.getBody().getStmts().get(0);
        Expression expression = stmt.getExpr();
        TypeUsage typeUsage = JavaParserFacade.get(new JreTypeSolver()).getType(expression);
}

You will get the error:

Unsolved symbol in ReflectionClassDeclaration{clazz=java.util.Arrays} : Method 'asList' with parameterTypes [ReferenceTypeUsage{declaration=ReflectionClassDeclaration{clazz=java.lang.Long}, typeParameters=[]}]
UnsolvedSymbolException{context='ReflectionClassDeclaration{clazz=java.util.Arrays}', name='Method 'asList' with parameterTypes [ReferenceTypeUsage{declaration=ReflectionClassDeclaration{clazz=java.lang.Long}, typeParameters=[]}]', typeSolver=null}
    at me.tomassetti.symbolsolver.resolution.javaparser.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:96)
    at me.tomassetti.symbolsolver.resolution.javaparser.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:394)
    at me.tomassetti.symbolsolver.resolution.javaparser.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:150)
    at me.tomassetti.symbolsolver.resolution.javaparser.JavaParserFacade.getType(JavaParserFacade.java:116)
    at me.tomassetti.symbolsolver.resolution.javaparser.JavaParserFacade.getType(JavaParserFacade.java:110)
    at me.tomassetti.symbolsolver.resolution.GenericsTest.blaat(GenericsTest.java:185)
 ....

Any idea why?

Probably a ClassDeclaration is created wrapping a CtClass representing an interface

Exception in thread "main" java.lang.IllegalArgumentException: 
                            javassist.CtClassType@2d6a9952[changed public abstract interface class javax.persistence.EntityManagerFactory 
                            fields= 
                            constructors= 
                            methods=javassist.CtMethod@fa76482f[public abstract createEntityManager ()Ljavax/persistence/EntityManager;], 
                                    javassist.CtMethod@622ca8b8[public abstract createEntityManager (Ljava/util/Map;)Ljavax/persistence/EntityManager;], 
                                    javassist.CtMethod@33a644f9[public abstract close ()V], javassist.CtMethod@ebf0b295[public abstract isOpen ()Z], 
                                    ]
    at me.tomassetti.symbolsolver.javassistmodel.JavassistClassDeclaration.<init>(JavassistClassDeclaration.java:57)
    at me.tomassetti.symbolsolver.resolution.typesolvers.JarTypeSolver.tryToSolveType(JarTypeSolver.java:112)
    at me.tomassetti.symbolsolver.resolution.typesolvers.CombinedTypeSolver.tryToSolveType(CombinedTypeSolver.java:40)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.CompilationUnitContext.solveType(CompilationUnitContext.java:125)
    at me.tomassetti.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration.solveType(JavaParserClassDeclaration.java:333)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext.solveType(ClassOrInterfaceDeclarationContext.java:113)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:436)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:428)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convert(JavaParserFacade.java:481)
    at me.tomassetti.symbolsolver.javaparsermodel.declarations.JavaParserSymbolDeclaration.getType(JavaParserSymbolDeclaration.java:131)
    at me.tomassetti.symbolsolver.model.resolution.Value.from(Value.java:22)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.AbstractJavaParserContext.solveWithAsValue(AbstractJavaParserContext.java:72)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext.solveSymbolAsValue(ClassOrInterfaceDeclarationContext.java:77)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.ConstructorContext.solveSymbolAsValue(ConstructorContext.java:60)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.StatementContext.solveSymbolAsValue(StatementContext.java:66)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.StatementContext.solveSymbolAsValue(StatementContext.java:88)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveSymbolAsValue(MethodCallExprContext.java:197)
    at me.tomassetti.symbolsolver.resolution.SymbolSolver.solveSymbolAsValue(SymbolSolver.java:42)
    at me.tomassetti.symbolsolver.resolution.SymbolSolver.solveSymbolAsValue(SymbolSolver.java:47)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:211)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethodAsUsage(MethodCallExprContext.java:153)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:494)
    at es.ujaen.Searcher$MethodCallVisitor.visit(Searcher.java:151)
    at com.github.javaparser.ast.expr.MethodCallExpr.accept(MethodCallExpr.java:73)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:152)
    at com.github.javaparser.ast.expr.AssignExpr.accept(AssignExpr.java:76)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:407)
    at com.github.javaparser.ast.stmt.ExpressionStmt.accept(ExpressionStmt.java:53)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:169)
    at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:58)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:297)
    at com.github.javaparser.ast.body.ConstructorDeclaration.accept(ConstructorDeclaration.java:95)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:231)
    at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:86)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:260)
    at es.ujaen.Searcher.search(Searcher.java:70)
    at es.ujaen.Searcher.searchReferences(Searcher.java:35)
    at es.ujaen.Engine.run(Engine.java:23)
    at es.ujaen.Main.main(Main.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

typeSolver is null when trying to resolve ArrayList.size() function.

While trying to solve for a MethodCallExpr, ArrayList<Tire>.size(), I encounter a problem where the type solver becomes "null" when attempting to solve the local class "Tire".

Here is the code directory I'm testing:
TestData
|
|-- Car.java
|
|-- Tire.java

The code for both Car and Tire are in the output of running my program. The program creates a CombinedTypeSolver from both a JavaParserTypeSolver and the JreTypeSolver. It then uses a MethodDeclarationVisitor to visit the methods declared in Car.java. See output below:

java -cp "lib/javaparser/javaparser-core/target/javaparser-core-2.3.1-SNAPSHOT.jar:lib/java-symbol-solver/java-symbol-solver-core/build/libs/java-symbol-solver-core-0.2.0-SNAPSHOT.jar:lib/java-symbol-solver/java-symbol-solver-logic/build/libs/java-symbol-solver-logic-0.2.0-SNAPSHOT.jar:lib/java-symbol-solver/java-symbol-solver-model/build/libs/java-symbol-solver-model-0.2.0-SNAPSHOT.jar":build/ Test TestData/Car.java TestData/
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
----------------------------------------
Got base code directory as TestData/.
PARSING TESTDATA/CAR.JAVA:

import java.util.ArrayList;

public class Car {

    private int year;

    private String manufacturer;

    private ArrayList<Tire> tires;

    public  Car(String manufacturer, int year) {
        this.manufacturer = new String(manufacturer);
        this.year = year;
        this.tires = new ArrayList<Tire>();
        for (int tire = 0; tire < 4; tire++) {
            tires.add(new Tire());
        }
    }

    public int getYear() {
        return year;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public double getAverageTirePressure() {
        double totalTires = tires.size();
        double totalPressure = 0;
        for (Tire t : tires) {
            totalPressure += t.getPressure();
        }
        return totalPressure / totalTires;
    }
}

Type solve Tire: SymbolReference{correspondingDeclaration=Optional[JavaParserClassDeclaration{wrappedNode=public class Tire {

    private int pressure;

    private double health;

    public  Tire() {
        pressure = 100;
        health = 1.0;
    }

    public  Tire(int pressure, double health) {
        this.pressure = pressure;
        this.health = health;
    }

    public int getPressure() {
        return pressure;
    }

    public double getHealth() {
        return health;
    }
}}]}
Found method call: tires.size() called from object tires
Exception in thread "main" UnsolvedSymbolException{context='unknown', name='Tire', typeSolver=null}
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:438)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:428)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.lambda$convertToUsage$13(JavaParserFacade.java:443)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade$$Lambda$4/1618212626.apply(Unknown Source)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.LinkedList$LLSpliterator.forEachRemaining(LinkedList.java:1235)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:443)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:428)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.convert(JavaParserFacade.java:481)
    at me.tomassetti.symbolsolver.javaparsermodel.declarations.JavaParserSymbolDeclaration.getType(JavaParserSymbolDeclaration.java:131)
    at me.tomassetti.symbolsolver.model.resolution.Value.from(Value.java:22)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.AbstractJavaParserContext.solveWithAsValue(AbstractJavaParserContext.java:72)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext.solveSymbolAsValue(ClassOrInterfaceDeclarationContext.java:77)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodContext.solveSymbolAsValue(MethodContext.java:61)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.StatementContext.solveSymbolAsValue(StatementContext.java:57)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.StatementContext.solveSymbolAsValue(StatementContext.java:88)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveSymbolAsValue(MethodCallExprContext.java:197)
    at me.tomassetti.symbolsolver.resolution.SymbolSolver.solveSymbolAsValue(SymbolSolver.java:42)
    at me.tomassetti.symbolsolver.resolution.SymbolSolver.solveSymbolAsValue(SymbolSolver.java:47)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:211)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:136)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:130)
    at me.tomassetti.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethod(MethodCallExprContext.java:203)
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.solve(JavaParserFacade.java:122)
    at Test$FunctionCallVisitor.visit(Test.java:99)
    at Test$FunctionCallVisitor.visit(Test.java:88)
    at com.github.javaparser.ast.expr.MethodCallExpr.accept(MethodCallExpr.java:73)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:782)
    at com.github.javaparser.ast.body.VariableDeclarator.accept(VariableDeclarator.java:69)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:774)
    at com.github.javaparser.ast.expr.VariableDeclarationExpr.accept(VariableDeclarationExpr.java:77)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:407)
    at com.github.javaparser.ast.stmt.ExpressionStmt.accept(ExpressionStmt.java:53)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:169)
    at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:58)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:567)
    at Test$FunctionDeclarationVisitor.visit(Test.java:84)
    at Test$FunctionDeclarationVisitor.visit(Test.java:73)
    at com.github.javaparser.ast.body.MethodDeclaration.accept(MethodDeclaration.java:113)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:231)
    at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:85)
    at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:260)
    at Test.main(Test.java:46)

Symbol Solver without files

I want to create statistics about Java Source Files. I get them over network, so in the end i have an Inputstream containing valid Java (hopefully ;) ). I thought about first running JavaParser and then the symbol-solver to get the types of the variables and the methods being called. I know i don't get 100% accuracy, but i am not aiming for this. Speed is more important. Is this possible using this project? JavaParserTypeSolver only takes the file as an argument.
Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.