changeset 49053:7c1c51c340e6 lworld

8198748: [lworld] Javac should insert null checks at casts to value types.
author sadayapalam
date Mon, 12 Mar 2018 11:46:54 +0530
parents 17cf8bef476a
children 44a050354dfc
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java test/langtools/tools/javac/valhalla/lworld-values/CastNoNullCheckTest.java test/langtools/tools/javac/valhalla/lworld-values/CastNullCheckTest.java
diffstat 4 files changed, 108 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Mon Mar 12 09:20:32 2018 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Mon Mar 12 11:46:54 2018 +0530
@@ -30,6 +30,7 @@
 import com.sun.tools.javac.code.*;
 import com.sun.tools.javac.code.Kinds.KindSelector;
 import com.sun.tools.javac.code.Scope.WriteableScope;
+import com.sun.tools.javac.code.Source.Feature;
 import com.sun.tools.javac.jvm.*;
 import com.sun.tools.javac.main.Option.PkgInfo;
 import com.sun.tools.javac.tree.*;
@@ -2592,6 +2593,9 @@
             tree.expr = translate(tree.expr, tree.type);
         else
             tree.expr = translate(tree.expr);
+        if (Feature.VALUE_TYPES.allowedInSource(source) && types.isValue(tree.type))
+            if (!types.isSameType(tree.expr.type, tree.clazz.type))
+                tree.expr = attr.makeNullCheck(tree.expr);
         result = tree;
     }
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Mon Mar 12 09:20:32 2018 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Mon Mar 12 11:46:54 2018 +0530
@@ -113,6 +113,10 @@
      */
     boolean allowModules;
 
+    /** Switch: allow value types.
+     */
+    boolean allowValueTypes;
+
    /** Lint option: warn about classfile issues
      */
     boolean lintClassfile;
@@ -269,7 +273,7 @@
         Source source = Source.instance(context);
         allowSimplifiedVarargs = Feature.SIMPLIFIED_VARARGS.allowedInSource(source);
         allowModules     = Feature.MODULES.allowedInSource(source);
-
+        allowValueTypes = Feature.VALUE_TYPES.allowedInSource(source);
         saveParameterNames = options.isSet(PARAMETERS);
 
         profile = Profile.instance(context);
@@ -2925,8 +2929,11 @@
             flags &= ~ACC_MODULE;
             flags |= MODULE;
         }
-        if ((flags & ACC_VALUE) != 0)
-            flags = (flags ^ ACC_VALUE) | VALUE;
+        if ((flags & ACC_VALUE) != 0) {
+            flags &= ~ACC_VALUE;
+            if (allowValueTypes)
+                flags |= VALUE;
+        }
         return flags & ~ACC_SUPER; // SUPER and SYNCHRONIZED bits overloaded
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/valhalla/lworld-values/CastNoNullCheckTest.java	Mon Mar 12 11:46:54 2018 +0530
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2018 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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
+ * @summary Check that casting to a value type involves no null check when values are not recognized in source.
+ *
+ * @compile Point.java
+ * @compile -source 10 CastNoNullCheckTest.java
+ * @run main CastNoNullCheckTest
+ */
+
+public class CastNoNullCheckTest {
+    public static void main(String... args) {
+        Object o = null;
+        Point p = (Point) o; // No NPE expected.
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/valhalla/lworld-values/CastNullCheckTest.java	Mon Mar 12 11:46:54 2018 +0530
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2018 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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
+ * @summary Check that casting to a value type involves a null check.
+ *
+ * @compile Point.java
+ * @run main CastNullCheckTest
+ */
+
+public class CastNullCheckTest {
+    public static void main(String... args) {
+        int caught = 0;
+
+        Object o = null;
+        try {
+            Point p = (Point) o;
+        } catch (NullPointerException npe) {
+            caught++;
+        }
+
+        o = __MakeDefault Point();
+        try {
+            Point p = (Point) o;
+        } catch (NullPointerException npe) {
+            caught++;
+        }
+        if (caught != 1)
+            throw new AssertionError("NPE missing !");
+    }
+}
\ No newline at end of file