Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Generics Compiler Bug?

DZone's Guide to

Generics Compiler Bug?

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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/

 

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}