Description
FULL PRODUCT VERSION :
java version " 1.7.0_21 "
OpenJDK Runtime Environment (IcedTea 2.3.9) (7u21-2.3.9-1ubuntu1)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
* Linux 3.8.0-21-generic #32-Ubuntu SMP Tue May 14 22:16:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
* Windows 7 x86_64 ('ver' N/A at the moment)
A DESCRIPTION OF THE PROBLEM :
Under specific circumstances, @SuppressWarnings( " unchecked " ) doesn't work. The example follows (two classes, A.java and B.java, to be put in one directory), including the detailed description and a guess of the general pattern when the bug occurs:
--- A.java ---
import java.util.List;
public class A {
// The first one of either this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) (Object) B.STRING;
static void a() {
// or this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) (Object) B.STRING;
}
static void b() {
// or this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) f(B.STRING, null);
// or this:
@SuppressWarnings( " unchecked " )
List<String> b = (List) f(null, B.STRING);
}
static Object f(Object o, String s) {
return null;
}
// etc. causes
// A.java:x: warning: [unchecked] unchecked conversion
// List<String> x = (List) ...
// ^
// required: List<String>
// found: List
// 1 warning
// when 'touch B.java && javac A.java -Xlint:unchecked' is run.
//
// Remarks:
// * exhibited by javac 1.7.0_21 (Linux/Windows x64); doesn't occur for javac 1.6.0_27 (Linux/Windows x64)
//
// * if B.STRING is not declared 'final', or if it is referred to in any way in A prior to any of the unchecked
// casts above, like in
// public class A {
// String aux = B.STRING;
// ...
// the bug is not triggered.
//
// Summary: it seems that the bug appears for every suppressed unchecked cast containing the first occurrence
// (within the current class) of a reference to a 'final' (*) field of another class, where the latter class is
// deemed necessary to be (re)compiled by javac (in particular, when it hasn't been compiled yet).
//
// (*) 'static' modifier is not necessary for triggering the bug: even though without it the compilation fails with
// error(s), the unchecked-cast warning is still issued.
}
--- B.java ---
public class B {
public static final String STRING = null;
}
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. put A.java and B.java in one directory, copying their contents from 'Description' form above
2. run 'touch B.java && javac A.java -Xlint:unchecked'
3. for further details, refer to comments in A.java above
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
no warning issued
ACTUAL -
A.java:7: warning: [unchecked] unchecked conversion
List<String> a = (List) (Object) B.STRING;
^
required: List<String>
found: List
1 warning
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
--- A.java ---
import java.util.List;
public class A {
// The first one of either this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) (Object) B.STRING;
static void a() {
// or this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) (Object) B.STRING;
}
static void b() {
// or this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) f(B.STRING, null);
// or this:
@SuppressWarnings( " unchecked " )
List<String> b = (List) f(null, B.STRING);
}
static Object f(Object o, String s) {
return null;
}
// etc. causes
// A.java:x: warning: [unchecked] unchecked conversion
// List<String> x = (List) ...
// ^
// required: List<String>
// found: List
// 1 warning
// when 'touch B.java && javac A.java -Xlint:unchecked' is run.
//
// Remarks:
// * exhibited by javac 1.7.0_21 (Linux/Windows x64); doesn't occur for javac 1.6.0_27 (Linux/Windows x64)
//
// * if B.STRING is not declared 'final', or if it is referred to in any way in A prior to any of the unchecked
// casts above, like in
// public class A {
// String aux = B.STRING;
// ...
// the bug is not triggered.
//
// Summary: it seems that the bug appears for every suppressed unchecked cast containing the first occurrence
// (within the current class) of a reference to a 'final' (*) field of another class, where the latter class is
// deemed necessary to be (re)compiled by javac (in particular, when it hasn't been compiled yet).
//
// (*) 'static' modifier is not necessary for triggering the bug: even though without it the compilation fails with
// error(s), the unchecked-cast warning is still issued.
}
--- B.java ---
public class B {
public static final String STRING = null;
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
include any reference to the offending 'final' field prior to the problematic unchecked cast
java version " 1.7.0_21 "
OpenJDK Runtime Environment (IcedTea 2.3.9) (7u21-2.3.9-1ubuntu1)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
* Linux 3.8.0-21-generic #32-Ubuntu SMP Tue May 14 22:16:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
* Windows 7 x86_64 ('ver' N/A at the moment)
A DESCRIPTION OF THE PROBLEM :
Under specific circumstances, @SuppressWarnings( " unchecked " ) doesn't work. The example follows (two classes, A.java and B.java, to be put in one directory), including the detailed description and a guess of the general pattern when the bug occurs:
--- A.java ---
import java.util.List;
public class A {
// The first one of either this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) (Object) B.STRING;
static void a() {
// or this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) (Object) B.STRING;
}
static void b() {
// or this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) f(B.STRING, null);
// or this:
@SuppressWarnings( " unchecked " )
List<String> b = (List) f(null, B.STRING);
}
static Object f(Object o, String s) {
return null;
}
// etc. causes
// A.java:x: warning: [unchecked] unchecked conversion
// List<String> x = (List) ...
// ^
// required: List<String>
// found: List
// 1 warning
// when 'touch B.java && javac A.java -Xlint:unchecked' is run.
//
// Remarks:
// * exhibited by javac 1.7.0_21 (Linux/Windows x64); doesn't occur for javac 1.6.0_27 (Linux/Windows x64)
//
// * if B.STRING is not declared 'final', or if it is referred to in any way in A prior to any of the unchecked
// casts above, like in
// public class A {
// String aux = B.STRING;
// ...
// the bug is not triggered.
//
// Summary: it seems that the bug appears for every suppressed unchecked cast containing the first occurrence
// (within the current class) of a reference to a 'final' (*) field of another class, where the latter class is
// deemed necessary to be (re)compiled by javac (in particular, when it hasn't been compiled yet).
//
// (*) 'static' modifier is not necessary for triggering the bug: even though without it the compilation fails with
// error(s), the unchecked-cast warning is still issued.
}
--- B.java ---
public class B {
public static final String STRING = null;
}
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. put A.java and B.java in one directory, copying their contents from 'Description' form above
2. run 'touch B.java && javac A.java -Xlint:unchecked'
3. for further details, refer to comments in A.java above
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
no warning issued
ACTUAL -
A.java:7: warning: [unchecked] unchecked conversion
List<String> a = (List) (Object) B.STRING;
^
required: List<String>
found: List
1 warning
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
--- A.java ---
import java.util.List;
public class A {
// The first one of either this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) (Object) B.STRING;
static void a() {
// or this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) (Object) B.STRING;
}
static void b() {
// or this:
@SuppressWarnings( " unchecked " )
List<String> a = (List) f(B.STRING, null);
// or this:
@SuppressWarnings( " unchecked " )
List<String> b = (List) f(null, B.STRING);
}
static Object f(Object o, String s) {
return null;
}
// etc. causes
// A.java:x: warning: [unchecked] unchecked conversion
// List<String> x = (List) ...
// ^
// required: List<String>
// found: List
// 1 warning
// when 'touch B.java && javac A.java -Xlint:unchecked' is run.
//
// Remarks:
// * exhibited by javac 1.7.0_21 (Linux/Windows x64); doesn't occur for javac 1.6.0_27 (Linux/Windows x64)
//
// * if B.STRING is not declared 'final', or if it is referred to in any way in A prior to any of the unchecked
// casts above, like in
// public class A {
// String aux = B.STRING;
// ...
// the bug is not triggered.
//
// Summary: it seems that the bug appears for every suppressed unchecked cast containing the first occurrence
// (within the current class) of a reference to a 'final' (*) field of another class, where the latter class is
// deemed necessary to be (re)compiled by javac (in particular, when it hasn't been compiled yet).
//
// (*) 'static' modifier is not necessary for triggering the bug: even though without it the compilation fails with
// error(s), the unchecked-cast warning is still issued.
}
--- B.java ---
public class B {
public static final String STRING = null;
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
include any reference to the offending 'final' field prior to the problematic unchecked cast
Attachments
Issue Links
- duplicates
-
JDK-8016099 Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
- Closed
- relates to
-
JDK-8021112 Spurious unchecked warning reported by javac
- Closed