1 /*
   2  * Copyright (c) 2013, 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 /* @test
  25  * @bug 4987375
  26  * @summary make sure clone() isn't reflected and that Cloneable and
  27  *          Serializable are found
  28  */
  29 
  30 import java.lang.reflect.*;
  31 import java.util.Arrays;
  32 
  33 public class ArrayMethods {
  34     public int failed = 0;
  35 
  36     public static void main(String[] args) throws Exception {
  37         ArrayMethods m = new ArrayMethods();
  38 
  39         m.testGetMethod();
  40         m.testGetMethods();
  41         m.testGetDeclaredMethod();
  42         m.testGetDeclaredMethods();
  43         m.testGetInterfaces();
  44 
  45         if (m.failed != 0)
  46             throw new RuntimeException("Test failed, check log for details");
  47     }
  48 
  49     public void testGetMethod() {
  50         try {
  51             Method m = new String[0].getClass().getMethod("clone", (Class<?>[])null);
  52 
  53             failed++;
  54             System.out.println("getMethod(\"clone\", null) Should not find clone()");
  55         } catch (NoSuchMethodException e) {
  56             ; //all good
  57         }
  58     }
  59 
  60     public void testGetMethods() {
  61         Method[] m = new Integer[0][0][0].getClass().getMethods();
  62         for (Method mm : m)
  63             if(mm.getName().contentEquals("clone")) {
  64                 failed++;
  65                 System.out.println("getMethods() Should not find clone()");
  66             }
  67     }
  68 
  69     public void testGetDeclaredMethod() {
  70         try {
  71             Method m = new Object[0][0].getClass().getDeclaredMethod("clone", (Class<?>[])null);
  72 
  73             failed++;
  74             System.out.println("getDeclaredMethod(\"clone\", null) Should not find clone()");
  75 
  76         } catch (NoSuchMethodException e) {
  77             ; //all good
  78         }
  79     }
  80 
  81     public void testGetDeclaredMethods() {
  82         Method[] m = new Throwable[0][0][0][0].getClass().getDeclaredMethods();
  83         if (m.length != 0) {
  84             failed++;
  85             System.out.println("getDeclaredMethods().length should be 0");
  86         }
  87     }
  88 
  89     public void testGetInterfaces() {
  90         Class<?>[] is = new Integer[0].getClass().getInterfaces();
  91         boolean thisFailed = false;
  92 
  93         if (is.length != 2)
  94             thisFailed = true;
  95 
  96         if (!is[0].getCanonicalName().equals("java.lang.Cloneable"))
  97             thisFailed = true;
  98 
  99         if (!is[1].getCanonicalName().equals("java.io.Serializable"))
 100             thisFailed = true;
 101 
 102         if (thisFailed) {
 103             failed++;
 104             System.out.println(Arrays.asList(is));
 105             System.out.println("Should contain exactly Cloneable, Serializable in that order.");
 106         }
 107     }
 108 }