Javaparser: JavaParserFieldDeclaration vs. ReflectionFieldDeclaration and how to get initializer

Created on 29 Mar 2018  路  3Comments  路  Source: javaparser/javaparser

My challenge is to get the initializer to an FieldAccessExpression.
What I tried so far ist calling public SymbolReference solve(FieldAccessExpr fieldAccessExpr) of JavaParserFacade which should return SymbolReference

That works, if I get an JavaParserFieldDeclaration:

SymbolReference<ResolvedFieldDeclaration> resFieldDecl = JavaParserFacade.get(typeSolver)
                               .solve(expr.asFieldAccessExpr());
if (resFieldDecl .getCorrespondingDeclaration() instanceof JavaParserFieldDeclaration) {
    JavaParserFieldDeclaration jFieldDecl = (JavaParserFieldDeclaration) resFieldDecl
                              .getCorrespondingDeclaration();
    FieldDeclaration fieldDecl = jFieldDecl.getWrappedNode();
    Optional<VariableDeclarator> init = fieldDecl.getVariables().stream()
                              .filter(vd -> vd.getName().asString().equals(fieldName)).findFirst();
        ...
}

But sometimes I also get an ReflectionFieldDeclaration instead of a JavaParserFieldDeclaration - which is okay since they both implement ResolvedFieldDeclaration.
But why am I getting two different types? I don't unterstand the difference.
In all cases the fields are definied and used this way:

public class B extends A {
      public static final String FIELD1 = "field1";
}

...

String s = ((B) object).get(B.FIELD1);

And how do I get the initializer of a ReflectionFieldDeclaration?

Question (JP usage)

Most helpful comment

Thank you @malteskoruppa for your answer. Now I do understand the difference.

In contrast, a ReflectionFieldDeclaration is created when you try to resolve a field access expression of a field that is contained somewhere in the Java standard library

In fact the resolved field is not contained in a standard library but in another project of mine. So I simply forgot to add the path of the second project to the CombinedTypeSolver.

All 3 comments

Hi!
@ftomassetti is able to answer this one.

Federico will be able to give you a better answer, but my understanding is that a JavaParserFieldDeclaration represents a field declaration in source code that is present in the source code that you have provided to the Java Symbol Solver, i.e., source code for which there actually _exists_ a FieldDeclaration node in the code that you are analyzing, because the symbol solver actually parsed that node using the JavaParser core.

In contrast, a ReflectionFieldDeclaration is created when you try to resolve a field access expression of a field that is contained somewhere in the Java standard library (i.e., any field declaration that is buried somewhere in a java.* package). Hence, you do not immediately have the source code for it. Still, the symbol solver has an internal model of the Java standard library and can resolve the field declaration for you and provide you with some basic information about it.

However, since no corresponding FieldDeclaration node exists in the latter case, it is not possible for you to get this node from a ReflectionFieldDeclaration -- and, thus, you cannot get the initializer either. I don't know if it is maybe possible to use the open source implementation of the standard library and provide that to the symbol solver as well, so that the corresponding field declaration in the standard library actually gets parsed and you end up with a JavaParserFieldDeclaration instead of a ReflectionFieldDeclaration... it may be worth a try. :-)

Thank you @malteskoruppa for your answer. Now I do understand the difference.

In contrast, a ReflectionFieldDeclaration is created when you try to resolve a field access expression of a field that is contained somewhere in the Java standard library

In fact the resolved field is not contained in a standard library but in another project of mine. So I simply forgot to add the path of the second project to the CombinedTypeSolver.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nerzid picture nerzid  路  4Comments

nough1 picture nough1  路  5Comments

Ulriks90 picture Ulriks90  路  4Comments

sangram82 picture sangram82  路  3Comments

matozoid picture matozoid  路  3Comments