Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find the first ancestor of a type? #4335

Open
PELock opened this issue Mar 8, 2024 · 1 comment
Open

Find the first ancestor of a type? #4335

PELock opened this issue Mar 8, 2024 · 1 comment
Labels
Question (JP usage) How to use JP to inspect/modify the AST - please close your question once answered!

Comments

@PELock
Copy link

PELock commented Mar 8, 2024

I'm trying to find the first instance of ClassOrInterfaceDeclaration for the nested Java class methods, right now I use

 Optional<ClassOrInterfaceDeclaration> topNode = nestedMethodNode.findAncestor(ClassOrInterfaceDeclaration.class);

But it works only for the nearest ancestor.

What is the proper way of finding the top most class declaration of the nested method (multilevel)?

@jlerbsc jlerbsc added the Question (JP usage) How to use JP to inspect/modify the AST - please close your question once answered! label Mar 8, 2024
@jlerbsc
Copy link
Collaborator

jlerbsc commented Mar 15, 2024

You can call findAncestor recursively until you no longer find the decal you're looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question (JP usage) How to use JP to inspect/modify the AST - please close your question once answered!
Projects
None yet
Development

No branches or pull requests

2 participants