How do I retrieve FieldDeclarations from a ClassDeclaration? (In comparison,
one can retrieve ParameterDeclarations from a MethodDeclaration, and one can
via the visitor pattern retrieve LocalVariableDeclarations from Statement.)
Note - This topic is about FieldDeclarations, not FieldSpecifications.
Background: I intend to collect the retrieval of type argument in visitVariableDeclaration(), which is called from visitFieldDeclaration(), visitParameterDeclaration() and from LocalVariableDeclaration(). I cannot retrieve type arguments from a FieldSpecification (since I need a TypeReference to do so). But since I cannot retrieve (a list of) FieldDeclaration, I currently don't come into visitFieldDeclaration() and hence VisitVariableDeclaration() is not called for field declarations.
Thanks in advance for hints!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
use TypeDeclaration.getMembers(), iterate over it (watch out, the return value
may be "null" if there are no members whatsoever), and on each element try "m
instanceof FieldDeclaration".
Alternative, use getFields() and then FieldSpecification.getParent(). Tthis
will return a FieldDeclaration. In the current release, you will have to cast
it to FieldDeclaration, in the current SVN version that is not necessary any
longer.
However, there is a difference:
int a, b;
the former approach will return this FieldDeclaration only once, while the
latter will return it twice, since there are two FieldSpecifactions in this
FieldDeclaration.
Best regards,
Tobias
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
How do I retrieve FieldDeclarations from a ClassDeclaration? (In comparison,
one can retrieve ParameterDeclarations from a MethodDeclaration, and one can
via the visitor pattern retrieve LocalVariableDeclarations from Statement.)
Note - This topic is about FieldDeclarations, not FieldSpecifications.
Background: I intend to collect the retrieval of type argument in visitVariableDeclaration(), which is called from visitFieldDeclaration(), visitParameterDeclaration() and from LocalVariableDeclaration(). I cannot retrieve type arguments from a FieldSpecification (since I need a TypeReference to do so). But since I cannot retrieve (a list of) FieldDeclaration, I currently don't come into visitFieldDeclaration() and hence VisitVariableDeclaration() is not called for field declarations.
Thanks in advance for hints!
Hej,
use TypeDeclaration.getMembers(), iterate over it (watch out, the return value
may be "null" if there are no members whatsoever), and on each element try "m
instanceof FieldDeclaration".
Alternative, use getFields() and then FieldSpecification.getParent(). Tthis
will return a FieldDeclaration. In the current release, you will have to cast
it to FieldDeclaration, in the current SVN version that is not necessary any
longer.
However, there is a difference:
int a, b;
the former approach will return this FieldDeclaration only once, while the
latter will return it twice, since there are two FieldSpecifactions in this
FieldDeclaration.
Best regards,
Tobias