+2  A: 

That sounds like a lot of work just to generate getter and setter methods. Why not just use eclipses getter and setter generator:

generate getters and setters screenshot

krock
I don't use eclipse :-( . I think you missed the @Get @Set requirement in my question.
cibercitizen1
Consider using an IDE and avoid having all kinds of voodoo in your build process. You will appreciate it some day.
Thorbjørn Ravn Andersen
Thanks. But, what when you change of IDE? I use most of the time vi. Anyway, I prefer something independent of the editor.
cibercitizen1
+4  A: 
  • a) You could use a JVM language (compatible to Java) that supports that
  • b) I would write it in Scala

Seriously, if you feel the pain of the language and don't want to use crutches like byte code weavers or external libs, you should look for greener pastures.

Landei
Seconded. Do look into Scala, it's sufficiently similar to Java that you won't feel lost, yet it does away (among other things) with the need to explicitly code getters and setters. DEATH TO BOILERPLATE! Umm, pardon my revolutionary enthusiasm.
Carl Smotricz
These are not Java. :-(
cibercitizen1
@cibercitizen1 - it depends what level you're looking at. They're not Java *source code*, but they are Java *classes*. You can call a class written in Scala from a class written in Java (and vice-versa) just as naturally as a "native" class. The only difference is that Scala lets you (but doesn't force you to) use different syntax, which is **exactly what you want** in your question.
Andrzej Doyle
'Use another programming language' type answers are usually worth a downvote... Besides: [LONG LIVE BOILERPLATE](http://www.bigredhair.com/boilerplate/BPpix/bp.motorcar.tn.jpg)
Andreas_D
+4  A: 

Practically spoken, I usually use approach 3 - I just code the fields and use eclipse IDE to autogenerate getter and setter methods. (netbeans and IntelliJ should have similiar features)

With the IDE's autogeneration tools you can even define the visibility of the generated getter/setter methods (you might want to limit the setters visibility to private) or edit the template if you need additional code the the method bodies.

Andreas_D
I don't use eclipse :-( . I think you missed the @Get @Set requirement in my question.
cibercitizen1
@cibercitzen1 - yes, I missed them, because you simply didn't say, @Get and @Set are strong *requirements*. But why don't you use an IDE like eclipse, netbeans, JDeveloper, IntelliJ, ...?
Andreas_D
Well I usually use vi or emacs. But, the main reason is I want something independent of the editor.
cibercitizen1
+3  A: 

You are missing something, which I'd say is the best approach if you want to go down this route:

1c) At compile time. Compile the class as normal, then use an APT annotation processor to modify the .class file (not the source file) to add the appropriate get and set methods to the bytecode.

This way your source stays pristine, you don't have to faff about with temporary files, and the actual compiled class is exactly as you want it to be.


Still, this is using a sledgehammer to open a nut. It's do-able, but now your code isn't strictly Java in the sense that if someone takes it and runs javac on it, they'll get a class without getters and setters. This will make debugging difficult as all the line numbers will be messed up, and unless you're very sure you've done the right thing with your annotation processor, you won't even be able to see the source of the getters and setters to correct mistakes. Static analysis tools will tell you that the attribute is never used, etc.

I'm with the general consensus - just generate them in the source file using the methods that every IDE gives you. This takes barely any more time than writing annotations, and is understood by every developer and tool out there. Java doesn't have properties - get over it. :-)

Andrzej Doyle
I agree, either switch languages (Scala is pretty cool) and make life easier that way, or switch to a better IDE and let it fill in the boilerplate. Is there perhaps more to your requirement, like you HAVE TO generate java source, and you get source in the format given, perhaps?
Java Drinker
I agree that the point is "Java doesn't have properties". Thanks for the new suggestion, I didn't know. I'll look for how to apt-process a .class file, instead of a .java one.
cibercitizen1
+1  A: 

I don't know why another obvious choice hasn't been suggested, but I would consider another approach, where you just define interfaces, and use a library that implements specified setters, getters, and internal fields needed.

So you would specify something like

public interface Bean { public int getX(); public void setX(int value); }

and ask library to instantiate type for that (and maybe convenience methods for creating instances of type). If necessary, annotations could be used for further configuration if necessary, but for get/set style beans that is not the case.

This is different from your (2), which would not work as is unless I am missing something: thing is, you must have methods available during compilation time. And that is what using interfaces would solve. Code generation would be needed but could be automated.

I would expect such libraries to exist, but if not, writing general-purpose one should be quite doable using byte code generation libs (asm, cglib, janino, javassist, whatever).

Writing such lib for simple use case is heavy-weight of course, but using one would seem to make sense.

... and it may well be that using an IDE would be easiest to solve specific case you have, whatever it is. :-)

StaxMan