• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multi File Templates in Eclipse (Code Generation?)

 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic