Path: blob/master/test/jdk/java/lang/Class/ArrayMethods.java
41149 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 498737525* @summary make sure clone() isn't reflected and that Cloneable and26* Serializable are found27*/2829import java.lang.reflect.*;30import java.util.Arrays;3132public class ArrayMethods {33public int failed = 0;3435public static void main(String[] args) throws Exception {36ArrayMethods m = new ArrayMethods();3738m.testGetMethod();39m.testGetMethods();40m.testGetDeclaredMethod();41m.testGetDeclaredMethods();42m.testGetInterfaces();4344if (m.failed != 0)45throw new RuntimeException("Test failed, check log for details");46}4748public void testGetMethod() {49try {50Method m = new String[0].getClass().getMethod("clone", (Class<?>[])null);5152failed++;53System.out.println("getMethod(\"clone\", null) Should not find clone()");54} catch (NoSuchMethodException e) {55; //all good56}57}5859public void testGetMethods() {60Method[] m = new Integer[0][0][0].getClass().getMethods();61for (Method mm : m)62if(mm.getName().contentEquals("clone")) {63failed++;64System.out.println("getMethods() Should not find clone()");65}66}6768public void testGetDeclaredMethod() {69try {70Method m = new Object[0][0].getClass().getDeclaredMethod("clone", (Class<?>[])null);7172failed++;73System.out.println("getDeclaredMethod(\"clone\", null) Should not find clone()");7475} catch (NoSuchMethodException e) {76; //all good77}78}7980public void testGetDeclaredMethods() {81Method[] m = new Throwable[0][0][0][0].getClass().getDeclaredMethods();82if (m.length != 0) {83failed++;84System.out.println("getDeclaredMethods().length should be 0");85}86}8788public void testGetInterfaces() {89Class<?>[] is = new Integer[0].getClass().getInterfaces();90boolean thisFailed = false;9192if (is.length != 2)93thisFailed = true;9495if (!is[0].getCanonicalName().equals("java.lang.Cloneable"))96thisFailed = true;9798if (!is[1].getCanonicalName().equals("java.io.Serializable"))99thisFailed = true;100101if (thisFailed) {102failed++;103System.out.println(Arrays.asList(is));104System.out.println("Should contain exactly Cloneable, Serializable in that order.");105}106}107}108109110