hotspot/test/runtime/reflect/ArrayGetIntException.java
changeset 27405 ea143278766c
equal deleted inserted replaced
27404:33c0f343cd5c 27405:ea143278766c
       
     1 /*
       
     2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6191224
       
    27  * @summary (reflect) Misleading detail string in IllegalArgumentException thrown by Array.get<Type>
       
    28  * @run main ArrayGetIntException
       
    29  */
       
    30 import java.io.*;
       
    31 import java.lang.reflect.Array;
       
    32 
       
    33 public class ArrayGetIntException {
       
    34     public static void main(String[] args) throws Exception {
       
    35         Object[] objArray = {new Integer(Integer.MAX_VALUE)};
       
    36 
       
    37         // this access is legal
       
    38         try {
       
    39             System.out.println(Array.get(objArray, 0));
       
    40             System.out.println("Test #1 PASSES");
       
    41         } catch(Exception e) {
       
    42             failTest("Test #1 FAILS - legal access denied" + e.getMessage());
       
    43         }
       
    44 
       
    45         // this access is not legal, but needs to generate the proper exception message
       
    46         try {
       
    47             System.out.println(Array.getInt(objArray, 0));
       
    48             failTest("Test #2 FAILS - no exception");
       
    49         } catch(Exception e) {
       
    50             System.out.println(e);
       
    51             if (e.getMessage().equals("Argument is not an array of primitive type")) {
       
    52                 System.out.println("Test #2 PASSES");
       
    53             } else {
       
    54                 failTest("Test #2 FAILS - incorrect message: " + e.getMessage());
       
    55             }
       
    56         }
       
    57 
       
    58         // this access is not legal, but needs to generate the proper exception message
       
    59         try {
       
    60             System.out.println(Array.getInt(new Object(), 0));
       
    61             failTest("Test #3 FAILS - no exception");
       
    62         } catch(Exception e) {
       
    63             System.out.println(e);
       
    64             if (e.getMessage().equals("Argument is not an array")) {
       
    65                 System.out.println("Test #3 PASSES");
       
    66             } else {
       
    67                 failTest("Test #3 FAILS - incorrect message: " + e.getMessage());
       
    68             }
       
    69         }
       
    70     }
       
    71 
       
    72     private static void failTest(String errStr) {
       
    73         System.out.println(errStr);
       
    74         throw new Error(errStr);
       
    75     }
       
    76 }