[Bug 859] javac to strict when checking cast

bugzilla-daemon at icedtea.classpath.org bugzilla-daemon at icedtea.classpath.org
Wed Jan 25 04:23:45 PST 2012


http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=859

Krzysztof Daniel <kdaniel at redhat.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |

--- Comment #2 from Krzysztof Daniel <kdaniel at redhat.com> 2012-01-25 12:23:45 UTC ---
Java Language Specification
(http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5)
says:

"The detailed rules for compile-time legality of a casting conversion of a
value of compile-time reference type S to a compile-time reference type T are
as follows:

    If S is a class type:
        If T is a class type, then either |S| <: |T|, or |T| <: |S|; otherwise
a compile-time error occurs. Furthermore, if there exists a supertype X of T,
and a supertype Y of S, such that both X and Y are provably distinct
parameterized types (§4.5), and that the erasures of X and Y are the same, a
compile-time error occurs."

I should get runtime error, not compile type error. Java generally allows for
moving up and down in an inheritance hierarchy during casting. Look for example
at the following code:

Number n = new AtomicLong();
BigDecimal bd = (BigDecimal) n; //2

The second line is also compiled correctly, although it will generate
ClassCastException. The code being subject of this bug is just a little bit
more advanced version - it uses two hierarchies (Collection and HashSet,
EventRequest and StepRequest), and in general it should compile.

I think that compile errors are allowed only when classes are provably
distinct. In this case they are not, as there is a chance that the code will
work as expected.

-- 
Configure bugmail: http://icedtea.classpath.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


More information about the distro-pkg-dev mailing list