Search code examples
javajavadoc

Javadoc bug for sealed generic interface?


The following code has javadoc produce an error, while the code compiles correctly. The minimal reproducible example consists of a sealed interface with a single implementation, both of which are generic.

The interface:

package mypackage;

/**
 * This is a sealed, generic interface. The only allowed implementation is
 * Class.
 */
public sealed interface Interface<T> permits Class<T> {
    // empty by design
}

The implementing class:

package mypackage;

/**
 * This is the only allowed implementation of Interface
 */
public final class Class<T> implements Interface<T> {
    // empty by design
}

Javadoc produces the following error message:

[...]\mypackage\Interface.java:7: error: '{' expected
public sealed interface Interface<T> permits Class<T> {
                                                  ^

This occurs whenever I try to create a sealed generic interface with a generic implementation, so I think it must be a bug. Any opinions?


Solution

  • Generics are not allowed with the permits clause, therefore you should use the raw Class type in the first place:

    sealed interface Interface<T> permits Class {}
    

    and then, add the generic type when you declare your class:

    class Class<T> implements interface<T> {} 
    

    As @Mark Rotteveel suggested in the comments, the problem might be harder to spot in Eclipse due to this unfixed bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=576378

    Here is a more elaborate discussion about this topic: How to use sealed classes with generics?