Frage

Compile time error: The generic class may not subclass java.lang.Throwable

public class TestGenericClass<E> extends Exception {

/*Above line will give compile error, the generic class TestGenericClass<E> may 
  not subclass java.lang.Throwable*/

    public TestGenericClass(String msg) {
        super(msg);
    }
}

Above compile time error is for the reason given in § jls-8.1.2 as below, and explained in this question:

It is a compile-time error if a generic class is a direct or indirect subclass of Throwable(§11.1.1).

This restriction is needed since the catch mechanism of the Java Virtual Machine works only with non-generic classes.

Question:

  • How it is restricted that subclass of java.lang.Throwable will not be generic class?

  • Or more generic question will be, how to restrict that subclasses of any class cannot be generic?

War es hilfreich?

Lösung

How it is restricted that subclass of java.lang.Throwable will not be generic class?

Here's how OpenJDK compiler performs the check:

import com.sun.tools.javac.code.Symbol.*;   

private void attribClassBody(Env<AttrContext> env, ClassSymbol c) {
    ....

    // Check that a generic class doesn't extend Throwable
    if (!c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType))
        log.error(tree.extending.pos(), "generic.throwable");

As you can see forbidden type is kind of harcoded, so you can't use the same technique for your custom class without compiler code customization.

Full source code

Andere Tipps

How it is restricted that subclass of java.lang.Throwable will not be generic class?

This was a decision to write a special case into the compiler itself. The reason why is detailed in this question. Basically, this is to do with a type being reifiable. You can read about this term here. In short, a type is reifiable if it's type is fully available at compile time. Generic types, for example, are not reifiable, because their types are removed by type erasure. Objects appearing in a catch block need to be reifiable.

Or more generic question, how to restrict that subclasses of a class cannot be generic?

Well there's a few options..

At present, there is no option within the normal bounds of Java to do this. It doesn't have some sort of final implementation that prevents genericity being applied to subclasses. The closest thing you can get to this, as explained in the comments, is to extend the compiler and add a rule specifically for your class. This solution sends shivers down my spine. Dodge it. It means that your code will only behave with your version of Java, and that anyone else who wants to use your code will have to install the same version.

Obviously, the other option is to extend Throwable, but this really isn't a good idea. It adds a whole bunch of functionality to your class, and adds a lot of new methods to the interface of your class, that you will never use. From an OOP point of view, you're sacrificing the integrity of your class for the benefit of having this feature.

If you're willing to delay the error until run time, you can use use reflection in the superclass constructor(s) to see if the subclass declares any type parameters.

public class Example {
    public static class ProhibitsGenericSubclasses {
        public ProhibitsGenericSubclasses() {
            if (getClass().getTypeParameters().length > 0)
                throw new AssertionError("ProhibitsGenericSubclasses prohibits generic subclasses (see docs)");
        }
    }

    public static class NonGenericSubclass extends ProhibitsGenericSubclasses {}
    public static class GenericSubclass<T> extends ProhibitsGenericSubclasses {}

    public static void main(String[] args) {
        System.out.println(new NonGenericSubclass());
        System.out.println(new GenericSubclass<Object>());
    }
}

This code prints

Example$NonGenericSubclass@15db9742
Exception in thread "main" java.lang.AssertionError: ProhibitsGenericSubclasses prohibits generic subclasses (see docs)
    at Example$ProhibitsGenericSubclasses.<init>(Example.java:12)
    at Example$GenericSubclass.<init>(Example.java:17)
    at Example.main(Example.java:21)

If you want to prohibit type parameters of all classes in the hierarchy, not just the most-derived class, you'll need to walk up the hierarchy using Class#getSuperclass().

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top