Javaparser: How do i get the wrapper node from resolvedType

Created on 13 Nov 2020  路  8Comments  路  Source: javaparser/javaparser

After resolving a Name expression
and Loging the resolved object in below code snippet , log shows there is a object named wrapperNode inside resolved Node , how can I access that wrapperNode.

I didnt found any method in ResolvedValueDeclaration class which can take me to wrapper Node and If I missed any thing please guide me

 ResolvedValueDeclaration resolved = nameExpr.resolve();
               LOG.info(resolved);

LOG: JavaParserSymbolDeclaration{name='cmd', wrappedNode=cmd = "mkdir " + folder}

Question (AST)

Most helpful comment

@MysterAitch

i tried below code and it help me getting me wrapperNode
JavaParserParameterDeclaration resolvedType = (JavaParserParameterDeclaration)nameExpr.resolve(); Node a= resolvedType.getWrappedNode();

However i tried your solutions as well , both are working ,

Raising an issue and talking to community about the issue always helps gathering more knowledge.
Thank you @MysterAitch @jlerbsc .

All 8 comments

@MysterAitch It would be a great pleasure , if you help me resolve this issue.

I'm on mobile so can't check, but have you tried getParentNode or similar?

ResolvedValueDeclaration class does not have any method like getParentNode

If the declaration is associated to an AST node method toAst() returns it, otherwise it returns empty.

As @jlerbsc mentions, the correct method is toAst() (I was wrong above, sorry!).

An example of this is ResolvedFieldDeclaration which extends AssociableToAST<FieldDeclaration> (AssociableToAST gives the toAst() method)

Looking a bit more closely, I don't see anything similar for NameExpr speficially. The screenshot below shows some classes that do implement it. Are you able to make use of any of these @f71uday ?

image

Sidenote: I'm not sure whether NameExpr _should_ be associable to the AST -- I don't immediately see why not (other than not having a wrappedNode field), but I've been away for a while and there might be a really obvious reason this is the case!

Edit: scratch that -- the NameExpr itself shouldn't have it.

Instead, the thing that the name expr represents should implement it (e.g. if it's a field, or an enum, or a declaration, or a class or a package, or whatever... THAT should implement it).

Consider the following example for how this works in practice:

    ResolvedValueDeclaration resolvedValueDeclaration = new NameExpr().resolve();
    if(resolvedValueDeclaration.isField()) {
        Optional<FieldDeclaration> fieldDeclaration = resolvedValueDeclaration.asField().toAst();
    } else if(resolvedValueDeclaration.isMethod()) {
        Optional<MethodDeclaration> methodDeclaration = resolvedValueDeclaration.asMethod().toAst();
    } else if(/*...*/) {
        /*...*/)
    }

@MysterAitch

i tried below code and it help me getting me wrapperNode
JavaParserParameterDeclaration resolvedType = (JavaParserParameterDeclaration)nameExpr.resolve(); Node a= resolvedType.getWrappedNode();

However i tried your solutions as well , both are working ,

Raising an issue and talking to community about the issue always helps gathering more knowledge.
Thank you @MysterAitch @jlerbsc .

Hi @f71uday we are glad we were able to help you. Now if you have no further questions on this subject could you close this issue please?

Was this page helpful?
0 / 5 - 0 ratings