# HG changeset patch # User jjg # Date 1303772169 25200 # Node ID 4c5f13798b8d9c04de04e7228e65e9367e7b172f # Parent fb84cfca28a137e108fd430a5650431624c4c8c4 7038363: cast from object to primitive should be for source >= 1.7 Reviewed-by: mcimadamore diff -r fb84cfca28a1 -r 4c5f13798b8d src/share/classes/com/sun/tools/javac/code/Source.java --- a/src/share/classes/com/sun/tools/javac/code/Source.java Mon Apr 25 15:50:30 2011 -0700 +++ b/src/share/classes/com/sun/tools/javac/code/Source.java Mon Apr 25 15:56:09 2011 -0700 @@ -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 fb84cfca28a1 -r 4c5f13798b8d src/share/classes/com/sun/tools/javac/code/Types.java --- a/src/share/classes/com/sun/tools/javac/code/Types.java Mon Apr 25 15:50:30 2011 -0700 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Mon Apr 25 15:56:09 2011 -0700 @@ -74,8 +74,9 @@ final JavacMessages messages; final Names names; final boolean allowBoxing; + final boolean allowCovariantReturns; + final boolean allowObjectToPrimitiveCast; final ClassReader reader; - final Source source; final Check chk; List warnStack = List.nil(); final Name capturedName; @@ -92,9 +93,11 @@ context.put(typesKey, this); syms = Symtab.instance(context); names = Names.instance(context); - allowBoxing = Source.instance(context).allowBoxing(); + Source source = Source.instance(context); + allowBoxing = source.allowBoxing(); + allowCovariantReturns = source.allowCovariantReturns(); + allowObjectToPrimitiveCast = source.allowObjectToPrimitiveCast(); reader = ClassReader.instance(context); - source = Source.instance(context); chk = Check.instance(context); capturedName = names.fromString(""); messages = JavacMessages.instance(context); @@ -949,8 +952,9 @@ return true; if (t.isPrimitive() != s.isPrimitive()) - return allowBoxing && (isConvertible(t, s, warn) || isConvertible(s, t, warn)); - + return allowBoxing && ( + isConvertible(t, s, warn) + || (allowObjectToPrimitiveCast && isConvertible(s, t, warn))); if (warn != warnStack.head) { try { warnStack = warnStack.prepend(warn); @@ -3070,7 +3074,7 @@ if (hasSameArgs(r1, r2)) return covariantReturnType(r1.getReturnType(), r2res, warner); - if (!source.allowCovariantReturns()) + if (!allowCovariantReturns) return false; if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner)) return true; @@ -3087,7 +3091,7 @@ public boolean covariantReturnType(Type t, Type s, Warner warner) { return isSameType(t, s) || - source.allowCovariantReturns() && + allowCovariantReturns && !t.isPrimitive() && !s.isPrimitive() && isAssignable(t, s, warner); @@ -3293,7 +3297,7 @@ } if (giveWarning && !isReifiable(reverse ? from : to)) warn.warn(LintCategory.UNCHECKED); - if (!source.allowCovariantReturns()) + if (!allowCovariantReturns) // reject if there is a common method signature with // incompatible return types. chk.checkCompatibleAbstracts(warn.pos(), from, to); @@ -3320,7 +3324,7 @@ Type t2 = to; if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments())) return false; - if (!source.allowCovariantReturns()) + if (!allowCovariantReturns) // reject if there is a common method signature with // incompatible return types. chk.checkCompatibleAbstracts(warn.pos(), from, to); diff -r fb84cfca28a1 -r 4c5f13798b8d test/tools/javac/types/CastObjectToPrimitiveTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/types/CastObjectToPrimitiveTest.java Mon Apr 25 15:56:09 2011 -0700 @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 7038363 + * @summary cast from object to primitive should be for source >= 1.7 + * @compile/fail/ref=CastObjectToPrimitiveTest.out -XDrawDiagnostics -Xlint:-options -source 5 CastObjectToPrimitiveTest.java + * @compile/fail/ref=CastObjectToPrimitiveTest.out -XDrawDiagnostics -Xlint:-options -source 6 CastObjectToPrimitiveTest.java + * @compile CastObjectToPrimitiveTest.java + */ + +class CastObjectToPrimitiveTest { + void m() { + Object o = 42; + int i = (int) o; + } +} diff -r fb84cfca28a1 -r 4c5f13798b8d test/tools/javac/types/CastObjectToPrimitiveTest.out --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/types/CastObjectToPrimitiveTest.out Mon Apr 25 15:56:09 2011 -0700 @@ -0,0 +1,2 @@ +CastObjectToPrimitiveTest.java:36:23: compiler.err.prob.found.req: (compiler.misc.inconvertible.types), java.lang.Object, int +1 error