# HG changeset patch # User goetz # Date 1528881306 -7200 # Wed Jun 13 11:15:06 2018 +0200 # Node ID 99863c49ddc8aa5702e3c65bdb0281d6602a2957 # Parent e775444c555e8b86462f296e806c7f7722e671c1 8204943: Improve message of ArrayStoreException. diff --git a/src/hotspot/share/oops/objArrayKlass.cpp b/src/hotspot/share/oops/objArrayKlass.cpp --- a/src/hotspot/share/oops/objArrayKlass.cpp +++ b/src/hotspot/share/oops/objArrayKlass.cpp @@ -235,7 +235,19 @@ // slow case: need individual subtype checks // note: don't use obj_at_put below because it includes a redundant store check if (!ArrayAccess::oop_arraycopy(s, src_offset, d, dst_offset, length)) { - THROW(vmSymbols::java_lang_ArrayStoreException()); + ResourceMark rm; + stringStream ss; + if (!bound->is_subtype_of(stype)) { + ss.print("arraycopy: type mismatch: can not copy %s[] into %s[]", + stype->external_name(), bound->external_name()); + } else { + // oop_arraycopy should return the index in the source array that + // contains the problematic oop. + ss.print("arraycopy: element type mismatch: can not cast one of the elements" + " of %s[] to the type of the destination array, %s", + stype->external_name(), bound->external_name()); + } + THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string()); } } } @@ -246,7 +258,11 @@ assert(s->is_objArray(), "must be obj array"); if (!d->is_objArray()) { - THROW(vmSymbols::java_lang_ArrayStoreException()); + ResourceMark rm; + stringStream ss; + ss.print("arraycopy: type mismatch: can not copy object[] into %s[]", + type2name_tab[ArrayKlass::cast(d->klass())->element_type()]); + THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string()); } // Check is all offsets and lengths are non negative diff --git a/src/hotspot/share/oops/typeArrayKlass.cpp b/src/hotspot/share/oops/typeArrayKlass.cpp --- a/src/hotspot/share/oops/typeArrayKlass.cpp +++ b/src/hotspot/share/oops/typeArrayKlass.cpp @@ -133,7 +133,12 @@ // Check destination if (!d->is_typeArray() || element_type() != TypeArrayKlass::cast(d->klass())->element_type()) { - THROW(vmSymbols::java_lang_ArrayStoreException()); + ResourceMark rm; + stringStream ss; + ss.print("arraycopy: type mismatch: can not copy %s[] into %s[]", + type2name_tab[ArrayKlass::cast(s->klass())->element_type()], + type2name_tab[ArrayKlass::cast(d->klass())->element_type()]); + THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string()); } // Check is all offsets and lengths are non negative diff --git a/src/hotspot/share/prims/jni.cpp b/src/hotspot/share/prims/jni.cpp --- a/src/hotspot/share/prims/jni.cpp +++ b/src/hotspot/share/prims/jni.cpp @@ -2630,7 +2630,28 @@ if (v == NULL || v->is_a(ObjArrayKlass::cast(a->klass())->element_klass())) { a->obj_at_put(index, v); } else { - THROW(vmSymbols::java_lang_ArrayStoreException()); + ResourceMark rm(THREAD); + stringStream ss; + int dims = 0; + Klass *elem_kl = ObjArrayKlass::cast(a->klass())->element_klass(); + BasicType elem_tp = T_ILLEGAL; + while (elem_kl != NULL && elem_kl->is_array_klass()) { + dims++; + if (elem_kl->is_objArray_klass()) { + elem_kl = ObjArrayKlass::cast(elem_kl)->element_klass(); + } else { + elem_tp = ArrayKlass::cast(elem_kl)->element_type(); + elem_kl = NULL; + } + } + ss.print("type mismatch: can not store %s to %s[%d]", + v->klass()->external_name(), + elem_kl != NULL ? elem_kl->external_name() : type2name_tab[elem_tp], + index); + for (; dims > 0; --dims) { + ss.print("[]"); + } + THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string()); } } else { char buf[jintAsStringSize]; diff --git a/test/hotspot/jtreg/runtime/exceptionMsgs/ArrayStoreException/ArrayStoreExceptionTest.java b/test/hotspot/jtreg/runtime/exceptionMsgs/ArrayStoreException/ArrayStoreExceptionTest.java new file mode 100644 --- /dev/null +++ b/test/hotspot/jtreg/runtime/exceptionMsgs/ArrayStoreException/ArrayStoreExceptionTest.java @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018 SAP SE. 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 + * @summary Test ArrayStoreException message. The message lists + * information about the array types involved. + * @library /test/lib + * @run main ArrayStoreExceptionTest + */ + +import java.util.Date; +import jdk.test.lib.Asserts; + +/** + * Tests the detailed messages of the ArrayStoreException. + */ +public class ArrayStoreExceptionTest { + + static { + System.loadLibrary("ArrayStoreExceptionTest"); + } + + static void testASMessages(Object from, Object to, String message) throws Exception { + try { + System.arraycopy(from, 1, to, 3, 2); + Asserts.fail("Expected ArrayStoreException not thrown"); + } catch (ArrayStoreException e) { + Asserts.assertEquals(e.getMessage(), message); + } + } + + static native void doNativeArrayStore(Object[] src, Object dst, int index); + + static void testNativeASMessages(Object[] array, Object elem, int index, String message) + throws Exception { + try { + doNativeArrayStore(array, elem, index); + Asserts.fail("Expected ArrayStoreException not thrown"); + } catch (ArrayStoreException e) { + Asserts.assertEquals(e.getMessage(), message); + } + } + + public static void main(String[] args) throws Exception { + try { + boolean[] za1 = new boolean[3]; + byte[] ba1 = new byte[3]; + short[] sa1 = new short[3]; + char[] ca1 = new char[3]; + int[] ia1 = new int[3]; + long[] la1 = new long[3]; + float[] fa1 = new float[3]; + double[] da1 = new double[3]; + Object[] oa1 = new Object[3]; + + boolean[] za2 = new boolean[9]; + byte[] ba2 = new byte[9]; + short[] sa2 = new short[9]; + char[] ca2 = new char[9]; + int[] ia2 = new int[9]; + long[] la2 = new long[9]; + float[] fa2 = new float[9]; + double[] da2 = new double[9]; + Object[] oa2 = new Object[9]; + + boolean[][] za3 = new boolean[9][9]; + byte[][] ba3 = new byte[9][9]; + short[][] sa3 = new short[9][9]; + char[][] ca3 = new char[9][9]; + int[][] ia3 = new int[9][9]; + long[][] la3 = new long[9][9]; + float[][] fa3 = new float[9][9]; + double[][] da3 = new double[9][9]; + Object[][] oa3 = new Object[9][9]; + + int[][][] ia4 = new int[9][9][9]; + Object[][][] oa4 = new Object[9][9][9]; + + + testASMessages(za1, ba2, "arraycopy: type mismatch: can not copy boolean[] into byte[]"); + testASMessages(ba1, sa2, "arraycopy: type mismatch: can not copy byte[] into short[]"); + testASMessages(sa1, ca2, "arraycopy: type mismatch: can not copy short[] into char[]"); + testASMessages(ca1, ia2, "arraycopy: type mismatch: can not copy char[] into int[]"); + testASMessages(ia1, la2, "arraycopy: type mismatch: can not copy int[] into long[]"); + testASMessages(la1, fa2, "arraycopy: type mismatch: can not copy long[] into float[]"); + testASMessages(fa1, da2, "arraycopy: type mismatch: can not copy float[] into double[]"); + testASMessages(da1, oa2, "arraycopy: type mismatch: can not copy double[] into object[]"); + testASMessages(oa1, za2, "arraycopy: type mismatch: can not copy object[] into boolean[]"); + + testASMessages(za1, oa2, "arraycopy: type mismatch: can not copy boolean[] into object[]"); + testASMessages(ba1, za2, "arraycopy: type mismatch: can not copy byte[] into boolean[]"); + testASMessages(sa1, ba2, "arraycopy: type mismatch: can not copy short[] into byte[]"); + testASMessages(ca1, sa2, "arraycopy: type mismatch: can not copy char[] into short[]"); + testASMessages(ia1, ca2, "arraycopy: type mismatch: can not copy int[] into char[]"); + testASMessages(la1, ia2, "arraycopy: type mismatch: can not copy long[] into int[]"); + testASMessages(fa1, la2, "arraycopy: type mismatch: can not copy float[] into long[]"); + testASMessages(da1, fa2, "arraycopy: type mismatch: can not copy double[] into float[]"); + testASMessages(oa1, da2, "arraycopy: type mismatch: can not copy object[] into double[]"); + + testASMessages(za3, ba2, "arraycopy: type mismatch: can not copy object[] into byte[]"); + testASMessages(ba3, sa2, "arraycopy: type mismatch: can not copy object[] into short[]"); + testASMessages(sa3, ca2, "arraycopy: type mismatch: can not copy object[] into char[]"); + testASMessages(ca3, ia2, "arraycopy: type mismatch: can not copy object[] into int[]"); + testASMessages(ia3, la2, "arraycopy: type mismatch: can not copy object[] into long[]"); + testASMessages(la3, fa2, "arraycopy: type mismatch: can not copy object[] into float[]"); + testASMessages(fa3, da2, "arraycopy: type mismatch: can not copy object[] into double[]"); + testASMessages(oa3, za2, "arraycopy: type mismatch: can not copy object[] into boolean[]"); + + testASMessages(za1, oa3, "arraycopy: type mismatch: can not copy boolean[] into object[]"); + testASMessages(ba1, za3, "arraycopy: type mismatch: can not copy byte[] into object[]"); + testASMessages(sa1, ba3, "arraycopy: type mismatch: can not copy short[] into object[]"); + testASMessages(ca1, sa3, "arraycopy: type mismatch: can not copy char[] into object[]"); + testASMessages(ia1, ca3, "arraycopy: type mismatch: can not copy int[] into object[]"); + testASMessages(la1, ia3, "arraycopy: type mismatch: can not copy long[] into object[]"); + testASMessages(fa1, la3, "arraycopy: type mismatch: can not copy float[] into object[]"); + testASMessages(da1, fa3, "arraycopy: type mismatch: can not copy double[] into object[]"); + + String[] Sa1 = new String[3]; + Date[] Da1 = new Date[3]; + String[] Sa2 = new String[9]; + Date[] Da2 = new Date[9]; + + for (int i = 0; i < 3; i++) { + Sa1[i] = "" + i; + oa1[i] = "" + i; + } + testASMessages(Sa1, Da2, + "arraycopy: type mismatch: can not copy java.lang.String[] " + + "into java.util.Date[]"); + testASMessages(oa1, Da2, + "arraycopy: element type mismatch: can not cast one of the " + + "elements of java.lang.Object[] to the type of the destination " + + "array, java.util.Date"); + + // These should succeed. + doNativeArrayStore(Sa1, "This is a string", 0); + doNativeArrayStore(oa1, "This is a string", 2); + + testNativeASMessages(Da1, "This is not a date", 0, + "type mismatch: can not store java.lang.String to java.util.Date[0]"); + testNativeASMessages(Da1, "This is not a date", 2, + "type mismatch: can not store java.lang.String to java.util.Date[2]"); + testNativeASMessages(oa3, "This is not a date", 2, + "type mismatch: can not store java.lang.String to java.lang.Object[2][]"); + testNativeASMessages(oa4, "This is not a date", 1, + "type mismatch: can not store java.lang.String to java.lang.Object[1][][]"); + testNativeASMessages(ia3, "This is not a date", 1, + "type mismatch: can not store java.lang.String to int[1][]"); + testNativeASMessages(ia4, "This is not a date", 2, + "type mismatch: can not store java.lang.String to int[2][][]"); + + } catch (Exception e) { + e.printStackTrace(); + Asserts.fail("Wrong exception thrown: " + e); + } + } +} diff --git a/test/hotspot/jtreg/runtime/exceptionMsgs/ArrayStoreException/libArrayStoreExceptionTest.c b/test/hotspot/jtreg/runtime/exceptionMsgs/ArrayStoreException/libArrayStoreExceptionTest.c new file mode 100644 --- /dev/null +++ b/test/hotspot/jtreg/runtime/exceptionMsgs/ArrayStoreException/libArrayStoreExceptionTest.c @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018 SAP SE. 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. + */ + +#include + +JNIEXPORT void JNICALL + Java_ArrayStoreExceptionTest_doNativeArrayStore(JNIEnv *env, jclass klass, + jobjectArray array, jobject element, jint index) { + (*env)->SetObjectArrayElement(env, array, index, element); +}