Wednesday, March 6, 2013

Code Generator (Java)

So I'm taking a Java class at my local community college and I realized how tedious it is to code out accessors and mutators for each class level variable in our assignments. I noticed the frequency of my copypastas and decided enough was enough! So I wrote a small application to handle this for me. Basically, it's a window with two TextBoxes (i/o) and one Button (to generate code). It takes a list of "simple declarations" and with a little bit of string manipulation creates a public accessor and a public mutator for each specific variable in the list. The datatypes aren't hard-coded, so it just uses what you give it.

When the application runs, I give a synopsis on how to use it (in the "Input" TextBox).


So we paste our list of class level variable declarations.


After the text is pasted into the "Input" textbox, we can click the "Generate" button.


Click the "Output" TextBox once to copy the accessors and mutators to the Clipboard (to use in your source).


In the future I could parse the lines for multiple variable declarations on a single line, or strip the assignment operators and everything beyond them. Until then, this will work. :]

To download the source files, click here.
To download the binary, click here.

2 comments:

  1. "Accessories" and "Mutations" is an interesting way to put it. I always called them "Getters" and "Setters", but I can see where the phrases came from because I have heard them referred to as "Accessors" and "Mutators".

    ReplyDelete
  2. I'd rather call them "getters" and "setters" too. In C#, there are the "get" and "set" keywords for class properties. The words "accessor" and "mutator" don't occur much outside the realm of computer science.

    ReplyDelete