Platinum Partner
netbeans

Generics Compiler Bug?

I've been using generics for a few years, and I'm happy with them. I'm aware of the issues related to their design, and I agree that they could have been made a bit better (but I understand the trade-offs made for backward compability), but on the whole I consider them quite useful for writing better code. I haven't had any problems so far, but now I got stuck with a thing that looks like a compiler error. Consider this class that I'm using as a transactional decorator (*) in my NetBeans RCP stuff:
package it.tidalwave.bluemarine.persistence;

...

public abstract class TxTask
  {
    ...

    public abstract T run()
      throws E;
   
    public static <T> T execute (final TxTask<T, RuntimeException> task)
      {
        return execute(RuntimeException.class, task);
      }
   
    public static <T, E extends Throwable> T execute (final Class exceptionClass,
                                                      final TxTask<T, E> task)
      throws E
      {
            try
              {
                ...
                try
                  {
                    result = task.run();
                    ...
                  }
                catch (RetryException e)
                  {
                    ...
                  }
                catch (PersistenceException e)
                  {
                    ...
                  }
              }
            catch (Throwable t)
              {
                ...

                if (exceptionClass.isAssignableFrom(t.getClass()))
                  {
////////////////////// THE PROBLEM IS HERE
                    throw (E)t;
////////////////////// THE PROBLEM IS HERE
                  } 
                throw (t instanceof RuntimeException) ? (RuntimeException)t : new RuntimeException(t);
              }

            ...
          }
       
        ...
           
        return result;
      }
  }

The peculiarity of this class is that it uses a generic in the "throws" clause. This is ok with the language specifications, see for instance the excellent Angelika Langer's tutorial on generics (even though it's considered pretty useless in most cases, since you can't throw a generified exception because of type erasure - but in my code things are ok since I'm just rethrowing).

Now, the history of this issue is pretty complicated:

  1. The thing worked for several weeks, if not months (I remind you, in a NetBeans RCP project)
  2. At a certain point, I copied the class in the prototype of a web application, and I got a compiler error (see below); since I was focused on other things, I just commented out the offending part. blueMarine was still compiling ok.
  3. Since a few weeks, this is triggering the compiler error even in the NetBeans RCP project: but ONLY IF I BUILD THE WHOLE PROJECT (that is, if multiple *.java files are compiled at the same time). If I select the single TxTask.java file and compile it with the pop-up menu, it gets compiled ok (in fact, with this trick blueMarine builds, runs and passes the tests).

.../TxTask.java:169: unreported exception java.lang.Throwable; must be caught or declared to be thrown
throw (E)t;

This happens with the Java 5 compiler both on Mac OS X and Ubuntu Linux. I've asked for help in a couple of mailing lists so far, without success. So if you have any hints, please help: it is causing major pain, since it's breaking the CI on Hudson. The full source of the class is here: https://bluemarine.dev.java.net/svn/bluemarine/trunk/src/blueMarine-core/Core/Persistence/src/it/tidalwave/bluemarine/persistence/TxTask.java

(*) I'd like to get rid of this class sooner or later and use Spring or EJB3 annotations for transactions - but this won't happen before the next release, and I need to have CI working before that.

From http://weblogs.java.net/blog/fabriziogiudici/

 

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}