[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help with Java coding utility

Hi Emmanuele,

on the top of my head, this seems doable as an AST transformation:
- build your AST
- iterate on the list of (public?) fields of each class node:
    - for each field, create a method node in a bottom-up fashion that
contains the corresponding simple statement (AAssignStatement or something
like that). Do this for both the setter and the getter.
    - add the 2 methods to the list of methods embedded in the class node by
using the appropriate setter of the class node
- pretty-print the transformed AST (preferably with your own pretty-printer,
if you want the original formatting of your source file: this actually may
not be that obvious...)

Hope it helps!

----- Original Message -----
From: "Etienne M. Gagnon" <egagnon@j-meg.com>
To: "sablecc-list" <sablecc-list@sable.mcgill.ca>; "Emmanuele Sordini"
Sent: Saturday, May 20, 2000 9:54 AM
Subject: Fw: Help with Java coding utility

> Date: Sat, 20 May 2000 15:45:06 +0100
> From: Emmanuele Sordini <vega@ulisse.it>
> To: SableCC Mailing List <sablecc-list@sable.mcgill.ca>
> Subject: Help with Java coding utility
> To all SableCC members
> I'm quite new to SableCC (in fact, I downloaded it only a few days ago).
> I was searching for a parser/lexer written in Java, I looked through
> some alternatives and I found this to be probably the best. Therefore,
> I would like to congratulate Etienne Gagnon on his outstanding work.
> I'm also seeking help, because I would like to carry out a project of
> mine, and I thought a parser like SableCC would be of some help with
> this: I hereby give a short description.
> When coding in Java, except for trivial cases I'm used to declare class
> attributes (fields) as private or protected, and then to provide access
> to them via getter/setter methods. Adding a getter/setter pair can
> be quite a bore if the class is not very simple. Therefore, here's
> what I would like to carry out with SableCC:
> 1) For all classes in a Java file (apart from inner classes), no matter
>    if they are public or not, I would like my parser to generate a
>    getter/setter method pair, and to put them at the end of the class
>    declaration, just before the closing brace. As a result of this
>    automated operation, the rest of the file should not be altered:
>    comments, indentation should remain exactly in place.
> 3) The name of the setter/getter for the attribute "someField" should
>    be: "setSomeField" and "getSomeField"; that is, the "get" or "set"
>    words plus the field's name with the first letter turned to
>    upper case.
> 3) Here's an example of what the whole thing should look like:
> public class Foo
> {
>   protected A firstAttribute;
>   protectd B secondAttribute;
>   // constructors and methods
>   // Automatically-generated section
>   public A getFirstAttribute()
>   {
>     return firstAttribute;
>   }
>   public void setFirstAttribute(A a)
>   {
>     firstAttribute = a;
>   }
>   // End of section
> }
> I started dabbling with the Java 1.1 example bundled with the
> SableCC distribution. So far, by subclassing the DepthFirstAdapter
> class I've only come up with something that stores into a list
> every (type, fieldName) pair for each attribute. Now I'm in trouble
> because I would like to write the code generator and make it
> insert the getter/setter section in every class **without**
> modifying its content in **any** way, comments included.
> I apologize for this long post. I hope to have stirred the fellow
> members' curiosity. I will be deeply grateful for every hints/
> pointers/suggestions that could help me achieve this goal.
> Thanks in advance,
> Emmanuele Sordini