aspose file tools*
The moose likes Java in General and the fly likes Metaprogramming tricks in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Metaprogramming tricks in Java" Watch "Metaprogramming tricks in Java" New topic
Author

Metaprogramming tricks in Java

Filip Croes
Greenhorn

Joined: Nov 28, 2008
Posts: 3
Hi,

I'm using a proprietary product Java API (java 1.4) that has the notion of a "session" that you receive from a "sessionPool".

They state that you should only get a session when needed and release it as quickly as possible, always in the scope of a method. So a typical method always looks as follows:



This is alot of boilerplate code that I would like to avoid in the future.

How could I achieve an automatic finally release of my session when I get one from the sessionPool?

In groovy I think I would create something like .withSession { session -> ... }, but I have to stick with Java in this case.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Welcome to JavaRanch

I can't think of an alternative in Java 1.4; there might be something like what you want added to Java 7, but that's not out yet.
Filip Croes
Greenhorn

Joined: Nov 28, 2008
Posts: 3
Thanks

Java 5 would be a possibility, but 6 or 7 is not supported and will not run in their JVM.

I was first thinking of Aspects, but I don't know the possibilities or limitations of this technology applied to this example.
[ November 28, 2008: Message edited by: Filip Croes ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
How about something like:





That eliminates some of the boilerplate, but now add the line noise of having to code up a class or anonymous inner class that implements the interface. But Java is a verbose language, you just have to learn to love it. :roll:
[ November 28, 2008: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

This is one of those few features of .NET that would come in handy.

.NET has an interface called IDisposable. Anything that implements that interface can be used in a "using" block:

This will call session.dispose() at the end of the using block.

If only Java had something similar...
[ November 29, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
You could do something like this with AspectJ.

Note that the signature of the method "example" is changed to accept a Session object.



Filip Croes
Greenhorn

Joined: Nov 28, 2008
Posts: 3
Thanks for the many suggestions!

Is there a way around the session method parameter in the AspectJ solution?

If I have a method without session and change my implementation so that suddenly I do need a session, then I would have to change my interface because of the additional session parameter. Furthermore, the calling class now needs to make the call to getSession(), which adds overhead somewhere else. In most cases, the SessionPool is injected by Spring configuration.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Metaprogramming tricks in Java
 
Similar Threads
Finally and Finalize
ReentrantReadWriteLock's lock mechanism
Critical Please advice
Errors in "SCJD Exam with J2SE 5" book
Why Compiler Error.