JDK-7038363 : cast from object to primitive should be for source >= 1.7
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2011-04-20
  • Updated: 2011-05-17
  • Resolved: 2011-05-17
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b142Fixed
Related Reports
Relates :  
Relates :  
Description
From Jan Lahoda:
Hi,
    in JDK7's javac, casts like:
Object o = 42;
int i = (int) o;

are allowed, but these were not allowed in JDK6's javac. This is causing problems in NetBeans, as we are proposing "Add cast" fix and not showing an error for such casts, although the code is not compilable using pre-JDK6 compiler. This is a NetBeans bug:
http://netbeans.org/bugzilla/show_bug.cgi?id=197164

I think that such casts should be source level >= 1.7 only, which I tried to achieve in the attached patch.

Thanks,
    Jan

Comments
EVALUATION Yes.
21-04-2011

SUGGESTED FIX diff -r 2cd47be35522 make/netbeans/nb-javac/test/com/sun/tools/javac/code/TypesTest.java --- a/make/netbeans/nb-javac/test/com/sun/tools/javac/code/TypesTest.java Tue Apr 12 15:31:09 2011 +0200 +++ b/make/netbeans/nb-javac/test/com/sun/tools/javac/code/TypesTest.java Wed Apr 20 23:15:24 2011 +0200 @@ -39,6 +39,7 @@ import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Types; +import javax.tools.DiagnosticCollector; import javax.tools.JavaCompiler; import javax.tools.JavaFileObject; import javax.tools.SimpleJavaFileObject; @@ -109,4 +110,23 @@ }.scan(tree, null); } + public void test197164() throws IOException { + final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N + final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); + assert tool != null; + String code = "class Test { { Object o = 42; int i = (int) o; } }"; + DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>(); + final JavacTask ct = (JavacTask) tool.getTask(null, null, dc, Arrays.asList("-bootclasspath", bootPath, "-source", "1.7"), null, Arrays.asList(new MyFileObject(code))); + + ct.analyze(); + + assertTrue(dc.getDiagnostics().isEmpty()); + + DiagnosticCollector<JavaFileObject> dc2 = new DiagnosticCollector<JavaFileObject>(); + final JavacTask ct2 = (JavacTask) tool.getTask(null, null, dc2, Arrays.asList("-bootclasspath", bootPath, "-source", "1.6"), null, Arrays.asList(new MyFileObject(code))); + + ct2.analyze(); + + assertFalse(dc2.getDiagnostics().isEmpty()); + } } diff -r 2cd47be35522 src/share/classes/com/sun/tools/javac/code/Source.java --- a/src/share/classes/com/sun/tools/javac/code/Source.java Tue Apr 12 15:31:09 2011 +0200 +++ b/src/share/classes/com/sun/tools/javac/code/Source.java Wed Apr 20 23:15:24 2011 +0200 @@ -186,6 +186,9 @@ public boolean allowSimplifiedVarargs() { return compareTo(JDK1_7) >= 0; } + public boolean allowObjectToPrimitiveCast() { + return compareTo(JDK1_7) >= 0; + } public static SourceVersion toSourceVersion(Source source) { switch(source) { case JDK1_2: diff -r 2cd47be35522 src/share/classes/com/sun/tools/javac/code/Types.java --- a/src/share/classes/com/sun/tools/javac/code/Types.java Tue Apr 12 15:31:09 2011 +0200 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Wed Apr 20 23:15:24 2011 +0200 @@ -77,6 +77,7 @@ final Names names; final boolean allowGenerics; final boolean allowBoxing; + private final boolean allowObjectToPrimitiveCast; final ClassReader reader; final Source source; final Check chk; @@ -99,6 +100,7 @@ source = Source.instance(context); allowGenerics = source.allowGenerics(); allowBoxing = source.allowBoxing(); + allowObjectToPrimitiveCast = source.allowObjectToPrimitiveCast(); chk = Check.instance(context); capturedName = names.fromString("<captured wildcard>"); messages = JavacMessages.instance(context); @@ -959,7 +961,7 @@ return true; if (t.isPrimitive() != s.isPrimitive()) - return allowBoxing && (isConvertible(t, s, warn) || isConvertible(s, t, warn)); + return allowBoxing && (isConvertible(t, s, warn) || (isConvertible(s, t, warn) && allowObjectToPrimitiveCast)); if (warn != warnStack.head) { try {
20-04-2011