Tag Archives: java

Yet another DI framework…

Yet another Dependency Injection framework, just like Spring or Pico Container, has been released:

Guice is a new open-source Dependency Injection framework for Java 5 that is closing in on a 1.0 release.

…but this time brought to you by Google. Must see what’s new over there one of these days.


InfoQ – Guice: Fast and Light Dependency Injection Container

Technorati Tags: , , ,

Javapolis 2006 Presentations

Javapolis has become one of the major Java events in Europe, combining in one place and one week the latest trends in different kinds of presentations. Last year’s edition took place in December and now the videos of several presentations, hopefully all of them in the near future, are being released online, something really neat for people like me that couldn’t be there:

Javapolis 2006 Presentations

There’s more content, such as presentation slides in pdf, available at Javapolis homepage (requires fres registration).

Now that Sun has GPL’ed Java…

Call it vaporware, call it software development myth, call it whatever you wan’t but this has been hanging  around for quite a while. For some years, too many I would say, the big question in the Java world was when, or even wether, Sun was releasing its “crown jewel”, using the words of father of the Java programming language. That day has come and Sun relicenses Java under GPL, finally!…

Java has been the cradle of many successful open source projects (like Hibernate, Apache Lucene or Spring Framework to name a few), but has always remained a closed implementation since its beginning, something that never made sense any to me. That approach had its problems over the years, and a fine example of that is EJB: the previous implementations (until EJB 2.1) have grown to became a huge and complex API, and not close to what the developers were really needing; but with EJB 3.0, which had a much larger developer input, things were different and it’s a huge step forward. So that’s why I think this is a huge improvement, as I’m hoping that the inputs from the developer community will lead to even more improvements from now on, and eventually lead to the redesign of some the existing Java libraries.

But one question that comes to my mind right now: how will this affect alternative open source implementations of the Java runtime?

Technorati Tags: , , , ,

Don’t Let Hibernate Steal Your Identity

This was my first real issue with Hibernate when working, right after trying to understand how mapping files work and all the other "hey… if put this here what happens if I put this here".

The problem is simple, the default behaviour of the equals method doesn’t fit in a what you would need for an Hibernate object: it’s relies on the internal hash code generated for each instance, so it will not distinguish between two different instances that represent the same row in the database. The immediate solution is obvious, override equals and hashCode methods so we can, at least, compare the id’s of the objects to check whether they represent the same database entity, but this raises another problem: when an object hasn’t been saved in the database (the id field is still null) there’s no way to compare it to another one.

It’s becoming clear that problem is gets more complicated each time we look at it, but James Brundege came to the rescue and released a fine article where he takes this issue one step beyond, or even two.

ONJava.com: Don’t Let Hibernate Steal Your Identity

 

Technorati Tags: java, hibernate, orm, o/r mapping, database

Quick Link Of The Day: Exception Handling Antipatterns

A handy list of things not to be done in exception handling:

Exception Handling Antipatterns

Technorati Tags: ,

My Java Yellow Belt!!

I got my Java Yellow Belt… Only five more to black.

Downcasting with generics

Generics surely is a neat thing in Java 5.0, allowing us to have out-of-the-box type-safe collections, but while using this feature some questions may appear, like the how to use downcasting.

Suppose you have this:
MyBase a1 = new MyImpl();
MyBase a2 = new MyImpl();

List<MyBase> list = new ArrayList<MyBase>();
list.add(a1);
list.add(a2);

And, for some unknown reason, want to do this:
MyImpl[] array = null;
array = list.toArray(array);

Or this:
List<MyImpl> anotherList = list;

Or even this:
List<MyImpl> anotherList = (List<MyImpl>)list;

What happens?

Well, in the first case everything goes well, the elements copied to the array and correctly downcasted to the array’s type, just as long as MyImpl really is a subtype of MyInterface, if not you’ll get a nasty ArrayStoreException.

In the second and third cases you’ll always get an error in compile time, the List<MyImpl>and List<MyBase> are seen as two completely different types by the compiler so there’s no way to convert one into the other, even if MyBase is a supertype of MyImpl. One workaround is having a helper class that copies one collection into the other.

Technorati Tags:

From the ministry of confusing error messages

When JBoss says this:

Caused by:
java.lang.IllegalArgumentException: URL cannot be null

at org.jboss.webservice.metadata.jaxrpcmapping.
JavaWsdlMappingFactory.parse(JavaWsdlMappingFactory.java:55)

it may mean (well, it did in my case) that you’ve accidentally mis-typed the name of jaxrpc-mapping.xml. I guess the error arises whenever files referenced by webservices.xml don’t exactly match the actual file names. A reasonable problem, but rather an opaque error message.

In my case was a missing hack in maven.xml that renames a path in  webservices.xml file generated by XDoclet, basically a workaround to this bug, the source of the problem…

Thanks to Ian I figured this out, he saved me a considerable amount of debug time.

Technorati Tags: , , , ,

Using Scrapbook Pages

One of the most undervalued and forgotten features of Eclipse is the scrapbook page, which allows to execute Java expressions without having to create a new Java program. This is a neat way to quickly test an existing class or evaluate a code snippet.
To create a scrapbook page choose File -> New -> Other -> Java -> Java Run/Debug -> Scrapbook Page, choose a name and destination folder and that’s it.
Now there’s an empty page where regular Java expressions can be written and evaluated, to do so just select the expression and choose one of the availabe operations (either in the toolbar now available or the context menu).

Try this: write new java.util.Date(), selecting the text and clicking Display or Inspect.

Display will evaluate the expression and print the result directly on the scrapbook, basically the return of a toString() applied to that object or the return value of the selected Java method.

If the information displayed is not enough, there’s the Inspect feature (just like the example on the right) which shows an debug window with all the details of the selected object, and all this happens without executing any code or switching to the Debug perspective.

 

To evaluate code snippets there’s the Execute feature, this will execute a block of code, just like in a regular java program. A quick way to try this is to send something to the standard output: just write System.out.println("Scrapbook example"), select the text and hit Run, this should print something on the Console window (just like the example on the right).

Because a scrapbook page is created within a Java project, all the classpath libraries and projects references can be used; another nice feature is having the code assist, just like in a regular Java editor page. In a scrapbook page the class names fully qualified, there’s no, this isn’t much of a problem because the code completion handles this nicely, but if you’re working with unusually large class names you can check the Set Imports option, this will open new window where all the import expressions can be inserted (just the import section of a regular class file).

A scrapbook page is not as fully featured as the Java editor, there’s no outline and the code assist is somehow limited, but is not meant to that way; the idea is working with just a few lines, and this where the scrapbook page can be usefull: to quickly test Java expressions and code blocks present in a project in this kind of sandbox.

 

Technorati Tags:

Javapolis 2005

This years edition of JavaPolis, one of the finest Java events in Europe, took place last month in Antwerp, Belgium and delivered five days of conferences and presentations in every length and format you can think of. Javapolis is not vendor oriented so you can see different blends of the Java world together in the same event, an fine example of that is all the talks dedicated for each one of IDE currently used (of course Eclipse in Action had to be a do not miss).

For me one of the most interesting would have to be Maven 2.0 by Vincent Massol, one of the lead commiters of the project, to get bit deeper on what the new version of Maven, released in the late Summer, can do. Other interesting events are related to the current trends of the Java world: the evolution of Spring, EJB 3.0 or the SOA hype just to name a few.

Sadly I couldn’t see it live but now the slides are available online for most of the events and, hopefuly just like last year, soon we’ll have the videos too.
 

Technorati Tags:

Switch to our mobile site