Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "illegal generic type for instanceof" when using local classes

I have the following Java code that uses a local class.

import java.util.Arrays;

public class X<T> {
    void m() {
        class Z {}

        for (Object o : Arrays.asList(1, 2, 3))
            if (o instanceof Z) {}
    }
}

It does not compile with the following error message:

X.java:8: error: illegal generic type for instanceof
            if (o instanceof Z) {}
                             ^
1 error

I understand that the local class Z inherits the generic type signature of X<T>, being an inner class. The same kind of compilation error appears in this example, where Z is not local, but still inner:

import java.util.Arrays;

public class X<T> {
    class Z {}

    void m() {
        for (Object o : Arrays.asList(1, 2, 3))
            if (o instanceof Z) {} // Compilation error
    }
}

It can be worked around either by making Z non-inner / static:

import java.util.Arrays;

public class X<T> {
    static class Z {}

    void m() {
        for (Object o : Arrays.asList(1, 2, 3))
            if (o instanceof Z) {} // Compiles now
    }
}

Or by qualifying X.Z:

import java.util.Arrays;

public class X<T> {
    class Z {}

    void m() {
        for (Object o : Arrays.asList(1, 2, 3)) {
            if (o instanceof X.Z) {}    // Compiles now
            if (o instanceof X<?>.Z) {} // Also
        }
    }
}

But how can I qualify a local class, or work around this limitation, without changing the local class itself?

like image 543
Lukas Eder Avatar asked Apr 16 '19 08:04

Lukas Eder


4 Answers

To me this seems to be an oversight or limitation in the Java language and I do not think it is possible.

The referenced type in an instanceof expression must be reifiable according to JLS 4.7, meaning that it must be expressed as a reifiable type by its fully qualified name. At the same time, JLS 6.7 states that local classes do not have a fully qualified name, they can therefore not be expressed as reifiable.

If you declare Z as generic, the instanceof operator treats Z as a raw type where all generic properties to it - in this case the enclosing class - are considered raw as well. (Similar to a generic methods of a raw type being considered as raw despite any generic signature. This is a measure to retain backwards compatiblity of type generification.) Since any raw type is reifiable, declaring Z to be generic will compile.

like image 108
Rafael Winterhalter Avatar answered Nov 17 '22 23:11

Rafael Winterhalter


A possible workaround is to use reflection:

import java.util.Arrays;

public class X<T> {
    void m() {
        class Z {}

        for (Object o : Arrays.asList(1, 2, 3))
            if (Z.class.isInstance(o)) {}
    }
}
like image 28
Lukas Eder Avatar answered Nov 17 '22 23:11

Lukas Eder


Apparently, by making Z generic compilation succeeds. I expected that to require <T> as the type parameter, but you just have to make it generic, so anything will do

import java.util.Arrays;

public class X<T> {
    void m() {
        class Z<Anything> {}

        for (Object o : Arrays.asList(1, 2, 3))
            if (Z.class.isInstance(o)) {}
    }
}

Proper solution would be qualify the local class, but I don't think you can. Either you refactor it to a private static class or that's probably the best you can get.

like image 6
Edoardo Vacchi Avatar answered Nov 18 '22 00:11

Edoardo Vacchi


This should work either. Using reflection too. But seems a valid solution.

import java.util.Arrays;

public class X<T> {


    void m() {

        class Z2 {
        }

        for(Object o: Arrays.asList(1,2,3)) {
            if(Z2.class.isAssignableFrom(o.getClass())) {

            }
        }

    }

}
like image 3
JWThewes Avatar answered Nov 18 '22 00:11

JWThewes