aspose file tools*
The moose likes IDEs, Version Control and other tools and the fly likes Multi File Templates in Eclipse (Code Generation?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Multi File Templates in Eclipse (Code Generation?)" Watch "Multi File Templates in Eclipse (Code Generation?)" New topic
Author

Multi File Templates in Eclipse (Code Generation?)

Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
I often encounter a situation in which I create a Java class and one or more AspectJ aspects that are associated with the Java class.
It is beginning to feel tedious to fill in all this by hand, so I am wondering if there is something in Eclipse that would allow me to create some sort of template.
I know I can create code templates for single classes, but in this case the template is to contain one Java class and at least one aspect, i.e. multiple files, in which common information is to be entered.
Thanks in advance!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

The point of Aspect-Oriented programming is that the aspects should be externally wrappable without having to go in and muck around with the wrapped source code. Maybe you need a better AOP framework. Spring does AOP on my persistence classes with little or no changes to the source code.

Eclipse does have some very powerful templating facilities, however. I helped someone design one the other day. He needed to be able to retrofit a lot of code with environment setup/tear-down functionality, and he wasn't fortunate enough to be permitted any sort of AOP framework at all.

We put together a template. Now he can highlight the code of interest, click a menu option, and the template not only wraps the old code, it even corrects for elements that move around as result of the wrapping process.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Disregard the part about aspects - I use AspectJ and it fulfills my needs. Also, the point of the question was not only to answer this particular problem, but to find a general approach in situations like the described.
I am beginning to suspect that I should use some kind of MDA approach and then a tool, like AndroMDA, to generate code.
I am quite new to this area - does anyone has any recommendations on where to begin; tools, literature, tutorials etc?
Thanks!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why would each new class require all new aspects? That seems somewhat counter to their purpose (both classes *and* aspects).

In any case, no, Eclipse doesn't support this out-of-the-box.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
I am experimenting with domain driven design with the Naked Objects framework and use aspects to separate out the following things:
- Getter and setters.
Boilerplate code that rarely is interesting - placed in a separate aspect.
- Framework dependencies.
Inheritance of classes from the Naked Objects framework and methods which only purpose is to serve the framework are placed in a separate aspect.
This enables isolation of these dependencies and quick elimination of dependency on the framework, since I currently look at it as a design tool which I do not intend to use in the finished application.
- Validation code.
Any validation code required is also placed in a separate aspect.

Entity classes contain only instance field declarations and business methods.

Yes, this results in more files and more to type (unless you use a tool to generate code, thus my original question).
Part of the experiment is whether separation of concerns in the above manner makes the code easier to understand and maintain.
My current feeling is that the entity classes are very clean, but I need further time to decide whether this is beneficial to me and to find the drawbacks.
Best wishes!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Hmm, I see-that's an interesting approach. For me this isn't really what aspects are for, I guess.

For example, to "eliminate" getters and setters, when I need to, I just create base classes (in fact I was just having a discussion the other day with some co-workers about this, where I suggested basically shadowing *all* public methods in a base class, allowing all the design-by-contract, getters/setters, etc. to exist one level "above" (or below, depending on your point of view) the code that does the real work.

Similarly validation--if I was going to make that into an aspect I'd be more likely to make the validation *execution* an aspect, but the validation logic itself would be either in a base class, or in an injectable implementation, etc.

But if there's custom code in each aspect it ceases to be a cross-cutting concern. But I understand what you're getting at--I'd be curious to see an example of a (small) typical class and the aspects you're introducing. I could be wrong about the level of complexity I think you're adding right now.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
David Newton wrote:Hmm, I see-that's an interesting approach. For me this isn't really what aspects are for, I guess.

Well, I used to have similar feelings until I saw how Spring Roo uses Inter-Type Declarations. ITD was one feature of AspectJ that I had, more or less, overlooked until I begun with my current experiment.

It also allows me to add environment-specific behaviour or data to a class from a common library.
Example: I have a class Foo that is used both on the client and server side of some application. Foo is located in a library that is used by both the client and server. However, on the server side Foo needs to have some functionality, or contain some data, that is specific to the server side and does not apply to the client side. I can use ITD to add the desired data/behaviour in the server application, without having to modify the common library.
Perhaps such situations are a sign of poor design or something else. Regardless, I have experienced situations in which I could have had use for the the above technique, had I known more.


For example, to "eliminate" getters and setters, when I need to, I just create base classes...

Yes, this is also a feasible approach and I have used it on occasions.

Similarly validation--if I was going to make that into an aspect I'd be more likely to make the validation *execution* an aspect, but the validation logic itself would be either in a base class, or in an injectable implementation, etc.

In this case it is the framework, Naked Objects, that sets the rules regarding how to implement validation methods. As far as I understand, the best I can do in order not to mix validation with business logic is extracting the validation to an aspect that uses ITD.

I'll prepare a small example and post it later. It would be interesting to hear your thoughts on it.
Best wishes!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'll look into the ITDs; I'm not sure I knew about that, or if I think it's a good idea or not ;) An example would be interesting--I'll check the docs as well.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Here is my version of a Car entity from an example I am studying. First the core Java class:

So far, the methods onModifyOwner and onClearOwner that is to implement business logic when a car changes, or looses, owner does nothing.

Second, the aspect containing the getter and setter methods:

Yes, there are annotations in this class from the Naked Objects framework. These can be refactored out to the next aspect, the one containing the Naked Objects specific stuff. However, for the time being, I will settle with annotating the getters directly.

Next, the aspect containing the Naked Objects specific methods, superclass etc:


Finally, an aspect containing validation of car numbers. The validation is done differently depending on the current locale the application is running:

As in the comments, validation only takes place when the entity class is running in the Naked Objects framework.

That's it so far. I don't know if it was simple or short, though.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
The idea is that a lot of the code above could be generated automatically, perhaps using an MDA approach.
Any code added to an entity "by hand" can be written in a separate aspect, again using ITD.
The code, with the Java class and the different automatically generate aspects can be regenerated at any time, without touching the code implemented "by hand". Of course the handwritten code may require modifications after the other code has been regenerated.

So, now I have a plan but am still looking for the most appropriate tools. I am also very interested in hearing any criticism of this idea - I guess this is nothing new under the sun and perhaps there is someone already gained experience in connection to approaches like this.
I am NOT looking for some fancy tools that allows roundtrip-engineering or graphical modeling. I want a text-only model that is later transferred to code (one-way only). I expect to have to be able to modify the language used in the model, depending on the domain model I am working on.
Best wishes!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I've done lots of code generation stuff with DSLs and Velocity or FreeMarker; it's almost always really simple. Back in the day I used Jython, now I use JRuby or Groovy to define the DSL and produce the output.

(I haven't had a chance to think about the idea itself yet, but I will this week :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multi File Templates in Eclipse (Code Generation?)