Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/TestInstanceCloneUtils.java
41149 views
/*1* Copyright (c) 2015, 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*/2223package compiler.arraycopy;2425import java.lang.reflect.Field;26import java.lang.reflect.Method;27import java.lang.reflect.Modifier;28import java.util.HashMap;2930abstract class TestInstanceCloneUtils {31static class Base implements Cloneable {32void initialize(Class c, int i) {33for (Field f : c.getDeclaredFields()) {34setVal(f, i);35i++;36}37if (c != Base.class) {38initialize(c.getSuperclass(), i);39}40}4142Base(boolean initialize) {43if (initialize) {44initialize(getClass(), 0);45}46}4748void setVal(Field f, int i) {49try {50if (f.getType() == int.class) {51f.setInt(this, i);52return;53} else if (f.getType() == short.class) {54f.setShort(this, (short)i);55return;56} else if (f.getType() == byte.class) {57f.setByte(this, (byte)i);58return;59} else if (f.getType() == long.class) {60f.setLong(this, i);61return;62}63} catch(IllegalAccessException iae) {64throw new RuntimeException("Getting fields failed");65}66throw new RuntimeException("unexpected field type");67}6869int getVal(Field f) {70try {71if (f.getType() == int.class) {72return f.getInt(this);73} else if (f.getType() == short.class) {74return (int)f.getShort(this);75} else if (f.getType() == byte.class) {76return (int)f.getByte(this);77} else if (f.getType() == long.class) {78return (int)f.getLong(this);79}80} catch(IllegalAccessException iae) {81throw new RuntimeException("Setting fields failed");82}83throw new RuntimeException("unexpected field type");84}8586boolean fields_equal(Class c, Base o) {87for (Field f : c.getDeclaredFields()) {88if (getVal(f) != o.getVal(f)) {89return false;90}91}92if (c != Base.class) {93return fields_equal(c.getSuperclass(), o);94}95return true;96}9798public boolean equals(Object obj) {99return fields_equal(getClass(), (Base)obj);100}101102String print_fields(Class c, String s) {103for (Field f : c.getDeclaredFields()) {104if (s != "") {105s += "\n";106}107s = s + f + " = " + getVal(f);108}109if (c != Base.class) {110return print_fields(c.getSuperclass(), s);111}112return s;113}114115public String toString() {116return print_fields(getClass(), "");117}118119int fields_sum(Class c, int s) {120for (Field f : c.getDeclaredFields()) {121s += getVal(f);122}123if (c != Base.class) {124return fields_sum(c.getSuperclass(), s);125}126return s;127}128129public int sum() {130return fields_sum(getClass(), 0);131}132133}134135static class A extends Base {136int i1;137int i2;138int i3;139int i4;140int i5;141142A(boolean initialize) {143super(initialize);144}145146public Object clone() throws CloneNotSupportedException {147return super.clone();148}149}150151static class B extends A {152int i6;153154B(boolean initialize) {155super(initialize);156}157}158159static final class D extends Base {160byte i1;161short i2;162long i3;163int i4;164int i5;165166D(boolean initialize) {167super(initialize);168}169170public Object clone() throws CloneNotSupportedException {171return super.clone();172}173}174175static final class E extends Base {176int i1;177int i2;178int i3;179int i4;180int i5;181int i6;182int i7;183int i8;184int i9;185186E(boolean initialize) {187super(initialize);188}189190public Object clone() throws CloneNotSupportedException {191return super.clone();192}193}194195static final class F extends Base {196F(boolean initialize) {197super(initialize);198}199200public Object clone() throws CloneNotSupportedException {201return super.clone();202}203}204205static class G extends Base {206int i1;207int i2;208int i3;209210G(boolean initialize) {211super(initialize);212}213214public Object myclone() throws CloneNotSupportedException {215return clone();216}217}218219static class H extends G {220int i4;221int i5;222223H(boolean initialize) {224super(initialize);225}226227public Object clone() throws CloneNotSupportedException {228return super.clone();229}230}231232static class J extends Base {233int i1;234int i2;235int i3;236237J(boolean initialize) {238super(initialize);239}240241public Object myclone() throws CloneNotSupportedException {242return clone();243}244}245246static class K extends J {247int i4;248int i5;249250K(boolean initialize) {251super(initialize);252}253254}255256static final A a = new A(true);257static final B b = new B(true);258static final D d = new D(true);259static final E e = new E(true);260static final F f = new F(true);261static final G g = new G(true);262static final H h = new H(true);263static final J j = new J(true);264static final K k = new K(true);265266final HashMap<String,Method> tests = new HashMap<>();267{268for (Method m : this.getClass().getDeclaredMethods()) {269if (m.getName().matches("m[0-9]+")) {270assert(Modifier.isStatic(m.getModifiers())) : m;271tests.put(m.getName(), m);272}273}274}275276boolean success = true;277278void doTest(Base src, String name) throws Exception {279Method m = tests.get(name);280281for (int i = 0; i < 20000; i++) {282boolean failure = false;283Base res = null;284int s = 0;285Class retType = m.getReturnType();286if (retType.isPrimitive()) {287if (!retType.equals(Void.TYPE)) {288s = (int)m.invoke(null, src);289failure = (s != src.sum());290} else {291m.invoke(null, src);292}293} else {294res = (Base)m.invoke(null, src);295failure = !res.equals(src);296}297if (failure) {298System.out.println("Test " + name + " failed");299System.out.println("source: ");300System.out.println(src);301System.out.println("result: ");302if (m.getReturnType().isPrimitive()) {303System.out.println(s);304} else {305System.out.println(res);306}307success = false;308break;309}310}311}312313}314315316