Unnecessary Code Detector

Recently I found a nice eclipse plugin for finding dead code. The standard eclipse check finds unused private fields and methods, only. You may use STRG+SHIFT+G for finding references to a public class or method in advance. But this approach is limited to search for methods/classes without references one by one, which is very time consuming. The UCDetector starts a search for methods and classses without any references to it in all projects in the current workspace respecting inheritance and presents the results in the ‘Problems’ view. Read More

Fetch strategy subselect for hibernate

The standard fetch strategy for collections in hibernate is lazy select fetching (hibernate performance). So if you retrieve some entities (for example via a query or a collection association) with a lazy collection association, subsequent calls to this lazy association will be retrieved one by one in further requests, for example. This is called the N+1-query problem, since there is one query for retrieving the entities itself and N additional queries for the lazy association. You can reduce the overhead by setting the hibernate.default_batch_fetch_size for your persistence unit in the persistence.xml. This reduces the problem to an N/batch_size +1-query problem. You can tell hibernate to use only 1+1-queries, if you set the fetch strategy to subselect fetching. This way, all entities in the association collections of the entities are retrieved in one query as soon as one of the associations is accessed. This will be helpfull only, if you do not pick one of the objects and retrieve the association, but iterate through your results and access the association of each of them. It is even contra productive otherwise. Read More

Interface with self-referencing generic type

The Comparable interface has a generic type parameter, which is used for self-referencing in subclasses (I don’t know any other use for it) :

public interface Comparable<E> {
  public int compareTo(E other);
}

public class ComparableInteger implements Comparable<ComparableInteger> {
  private int val;
  public void compareTo(ComparableInteger other) {
    return val > other ? 1 : (val == other ? 0 : -1);
}

But what if you need to access methods of the interface on the generic type parameter? Then you can do a recursive declaration of the type parameter. Read More

instanceof on Class objects

If you need to access the metadata of a class or be a bit less type safe than Java is by itself, you will have to get involved with the Java Reflection API.  At that the question may arise, how to check whether one class object is the super class of another. If you are working with objects, you may use the java keyword instanceof. But this won’t work as expected for class objects, since all class objects are instances of Class<?> . So Class<?> owns the method cls1.isAssignableFrom(cls2), checking whether cls1 is a super class/interface or the same as cls2. So it works just like instanceof on objects, but the order of the parameters is switched.

Transaction Propagation for Remote calls

I didn’t find a clear statement whether transactions are propagated for remote calls in Java EE 5. So I tested it by creating two enterprise applications being deployed in different ears to the same JBoss-5.1.0-GA. In short: YES, the transaction is propagated and a rollback on a calling method initiates a rollback on the remote transaction, too. The willing reader might read on in order to get to know the test setup. Read More

Virtualization for VPN on Shared Systems

If you are using a machine being shared between several persons it is not advisable to open a VPN connection, because the tunnel would be accessible for all users of the computer. This is often the case in environments with thin clients. Instead of using a notebook or some other dedicated system, there is another possibility. Stephan Windmüller pointed me at the following solution. Simply install a virtualizer like VirtualBox with an operating system emulating its own network hardware. You can use a VPN tunnel from inside the virtualized OS without sharing the connection with other users on the host machine.