Menu

Access FieldDeclaration from ClassDeclaratio?

Developers
widheg
2009-11-05
2012-10-08
  • widheg

    widheg - 2009-11-05

    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!

     
  • Tobias Gutzmann

    Tobias Gutzmann - 2009-11-06

    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

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.