Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/runtime/defmeth/ObjectMethodOverridesTest.java
41159 views
/*1* Copyright (c) 2013, 2021, 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/*24* @test25*26* @modules java.base/jdk.internal.org.objectweb.asm:+open java.base/jdk.internal.org.objectweb.asm.util:+open27* @library /vmTestbase /test/lib28*29* @comment build retransform.jar in current dir30* @run driver vm.runtime.defmeth.shared.BuildJar31*32* @run driver jdk.test.lib.FileInstaller . .33* @run main/othervm/native34* -agentlib:redefineClasses35* -javaagent:retransform.jar36* vm.runtime.defmeth.ObjectMethodOverridesTest37*/38package vm.runtime.defmeth;3940import java.util.Set;4142import nsk.share.TestFailure;43import vm.runtime.defmeth.shared.DefMethTest;44import vm.runtime.defmeth.shared.data.*;45import vm.runtime.defmeth.shared.data.method.body.*;46import vm.runtime.defmeth.shared.builder.TestBuilder;4748import static vm.runtime.defmeth.shared.ExecutionMode.*;49import static vm.runtime.defmeth.shared.data.method.body.CallMethod.IndexbyteOp.*;5051/**52* Test that default methods don't override methods inherited from Object class.53*/54public class ObjectMethodOverridesTest extends DefMethTest {5556public static void main(String[] args) {57DefMethTest.runTest(ObjectMethodOverridesTest.class,58/* majorVer */ Set.of(MAX_MAJOR_VER),59/* flags */ Set.of(0),60/* redefine */ Set.of(false, true),61/* execMode */ Set.of(DIRECT, REFLECTION, INVOKE_EXACT, INVOKE_GENERIC, INVOKE_WITH_ARGS, INDY));62}6364/* protected Object clone() */65public void testClone(TestBuilder b) {66Interface I = b.intf("I")67.defaultMethod("clone", "()Ljava/lang/Object;")68.throw_(TestFailure.class)69.build()70.build();7172ConcreteClass C = b.clazz("C").implement(I)73.concreteMethod("m", "()V")74// force an invokevirtual MR75.invoke(CallMethod.Invoke.VIRTUAL,76b.clazzByName("C"), b.clazzByName("C"),77"clone", "()Ljava/lang/Object;", METHODREF)78.build()79.build();8081b.test().callSite(C, C, "m", "()V")82.throws_(CloneNotSupportedException.class)83.done();84}8586/* boolean equals(Object obj) */87public void testEquals(TestBuilder b) throws ReflectiveOperationException {88Interface I = b.intf("I")89.defaultMethod("equals", "(Ljava/lang/Object;)Z")90.throw_(TestFailure.class)91.build()92.build();9394ConcreteClass C = b.clazz("C").implement(I).build();9596b.test().callSite(I, C, "equals", "(Ljava/lang/Object;)Z")97.ignoreResult()98.done();99b.test().callSite(C, C, "equals", "(Ljava/lang/Object;)Z")100.ignoreResult()101.done();102}103104/* void finalize() */105public void testFinalize(TestBuilder b) {106Interface I = b.intf("I")107.defaultMethod("finalize", "()V")108.throw_(TestFailure.class)109.build()110.build();111112ConcreteClass C = b.clazz("C").implement(I)113.concreteMethod("m", "()V")114// force an invokevirtual MR115.invoke(CallMethod.Invoke.VIRTUAL,116b.clazzByName("C"), b.clazzByName("C"), "finalize", "()V", METHODREF)117.build()118.build();119120b.test().callSite(C, C, "m", "()V")121.ignoreResult()122.done();123}124125/* final Class<?> getClass() */126public void testGetClass(TestBuilder b) throws Exception {127Interface I = b.intf("I")128.defaultMethod("getClass", "()Ljava/lang/Class;")129.sig("()Ljava/lang/Class<*>;")130.throw_(TestFailure.class)131.build()132.build();133134ConcreteClass C = b.clazz("C").implement(I).build();135136b.test().loadClass(I).throws_(IncompatibleClassChangeError.class).done();137b.test().loadClass(C).throws_(IncompatibleClassChangeError.class).done();138}139140/* int hashCode() */141public void testHashCode(TestBuilder b) {142Interface I = b.intf("I")143.defaultMethod("hashCode", "()I")144.throw_(TestFailure.class)145.build()146.build();147148ConcreteClass C = b.clazz("C").implement(I).build();149150b.test().callSite(I, C, "hashCode", "()I")151.ignoreResult()152.done();153b.test().callSite(C, C, "hashCode", "()I")154.ignoreResult()155.done();156}157158159/* final void notify() */160public void testNotify(TestBuilder b) throws Exception {161Interface I = b.intf("I")162.defaultMethod("notify", "()V")163.throw_(TestFailure.class)164.build()165.build();166167ConcreteClass C = b.clazz("C").implement(I).build();168169b.test().loadClass(I).throws_(IncompatibleClassChangeError.class).done();170b.test().loadClass(C).throws_(IncompatibleClassChangeError.class).done();171}172173/* void notifyAll() */174public void testNotifyAll(TestBuilder b) {175Interface I = b.intf("I")176.defaultMethod("notifyAll", "()V")177.throw_(TestFailure.class)178.build()179.build();180181ConcreteClass C = b.clazz("C").implement(I).build();182183b.test().loadClass(I).throws_(IncompatibleClassChangeError.class).done();184b.test().loadClass(C).throws_(IncompatibleClassChangeError.class).done();185}186187/* String toString() */188public void testToString(TestBuilder b) {189Interface I = b.intf("I")190.defaultMethod("toString()", "()Ljava/lang/String;")191.throw_(TestFailure.class)192.build()193.build();194195ConcreteClass C = b.clazz("C").implement(I).build();196197if (factory.getExecutionMode() == "REFLECTION") {198// Class.get*Method() don't find any implicitly declared method from Object on interfaces.199b.test().callSite(I, C, "toString", "()Ljava/lang/String;")200.throws_(NoSuchMethodException.class)201.done();202} else {203b.test().callSite(I, C, "toString", "()Ljava/lang/String;")204.ignoreResult()205.done();206}207b.test().callSite(C, C, "toString", "()Ljava/lang/String;")208.ignoreResult()209.done();210}211212/* final void wait() */213public void testWait(TestBuilder b) {214Interface I = b.intf("I")215.defaultMethod("wait", "()V")216.throw_(TestFailure.class)217.build()218.build();219220ConcreteClass C = b.clazz("C").implement(I).build();221222b.test().loadClass(I).throws_(IncompatibleClassChangeError.class).done();223b.test().loadClass(C).throws_(IncompatibleClassChangeError.class).done();224}225226/* final void wait(long timeout) */227public void testTimedWait(TestBuilder b) {228Interface I = b.intf("I")229.defaultMethod("wait", "(J)V")230.throw_(TestFailure.class)231.build()232.build();233234ConcreteClass C = b.clazz("C").implement(I).build();235236b.test().loadClass(I).throws_(IncompatibleClassChangeError.class).done();237b.test().loadClass(C).throws_(IncompatibleClassChangeError.class).done();238}239240/* final void wait(long timeout, int nanos) */241public void testTimedWait1(TestBuilder b) {242Interface I = b.intf("I")243.defaultMethod("wait", "(JI)V")244.throw_(TestFailure.class)245.build()246.build();247248ConcreteClass C = b.clazz("C").implement(I).build();249250b.test().loadClass(I).throws_(IncompatibleClassChangeError.class).done();251b.test().loadClass(C).throws_(IncompatibleClassChangeError.class).done();252}253}254255256