rahul b
rahul b

Reputation: 21

Javaparser AST variable declaration

How to get all static final declaration information with line number inside a class using JavaParser.

Example

public class demo {
  private static final int x;
  private static final int y;
  private static final int z;
  // some code
  }

Ouput is

private static final integer type variable x at Line 1 private static final integer type variable y at Line 2 private static final integer type variable z at Line 3

Upvotes: 1

Views: 623

Answers (1)

Federico Tomassetti
Federico Tomassetti

Reputation: 2248

It is very simple: just use a VoidVisitorAdapter and ovveride this method:

public void visit(final FieldDeclaration n, final A arg)

In this way you can access all fields. You just need to call getModifiers to verify the field has the static declaration.

To get the line just call getBeginLine on the FieldDeclaration.

For additional help you can look here: http://tomassetti.me/getting-started-with-javaparser-analyzing-java-code-programmatically/ Source: I am a JavaParser contributor

Upvotes: 1

Related Questions