1 /*
   2  * Copyright (c) 2013, 2018, 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 package vm.runtime.defmeth;
  25 
  26 import nsk.share.TestFailure;
  27 import nsk.share.test.TestBase;
  28 import vm.runtime.defmeth.shared.DefMethTest;
  29 import vm.runtime.defmeth.shared.data.*;
  30 import static vm.runtime.defmeth.shared.data.method.body.CallMethod.Invoke.*;
  31 import static vm.runtime.defmeth.shared.data.method.body.CallMethod.IndexbyteOp.*;
  32 import vm.runtime.defmeth.shared.data.method.body.*;
  33 import vm.runtime.defmeth.shared.builder.TestBuilder;
  34 
  35 /**
  36  * Test that default methods don't override methods inherited from Object class.
  37  */
  38 public class ObjectMethodOverridesTest extends DefMethTest {
  39 
  40     public static void main(String[] args) {
  41         TestBase.runTest(new ObjectMethodOverridesTest(), args);
  42     }
  43 
  44     /* protected Object clone() */
  45     public void testClone() throws Exception {
  46         TestBuilder b = factory.getBuilder();
  47 
  48         Interface I = b.intf("I")
  49                 .defaultMethod("clone", "()Ljava/lang/Object;")
  50                     .throw_(TestFailure.class)
  51                 .build()
  52             .build();
  53 
  54         ConcreteClass C = b.clazz("C").implement(I)
  55                 .concreteMethod("m", "()V")
  56                     // force an invokevirtual MR
  57                     .invoke(CallMethod.Invoke.VIRTUAL,
  58                             b.clazzByName("C"), b.clazzByName("C"),
  59                             "clone", "()Ljava/lang/Object;", METHODREF)
  60                 .build()
  61             .build();
  62 
  63         b.test().callSite(C, C, "m", "()V")
  64                 .throws_(CloneNotSupportedException.class)
  65                 .done()
  66 
  67         .run();
  68     }
  69 
  70     /* boolean equals(Object obj) */
  71     public void testEquals() throws Exception {
  72         TestBuilder b = factory.getBuilder();
  73 
  74         Interface I = b.intf("I")
  75                 .defaultMethod("equals", "(Ljava/lang/Object;)Z")
  76                     .throw_(TestFailure.class)
  77                 .build()
  78             .build();
  79 
  80         ConcreteClass C = b.clazz("C").implement(I).build();
  81 
  82         ClassLoader cl = b.build();
  83         Object c = cl.loadClass("C").newInstance();
  84 
  85         c.equals(this);
  86     }
  87 
  88     /* void finalize() */
  89     public void testFinalize() throws Exception {
  90         TestBuilder b = factory.getBuilder();
  91 
  92         Interface I = b.intf("I")
  93                 .defaultMethod("finalize", "()V")
  94                     .throw_(TestFailure.class)
  95                 .build()
  96             .build();
  97 
  98         ConcreteClass C = b.clazz("C").implement(I)
  99                 .concreteMethod("m", "()V")
 100                     // force an invokevirtual MR
 101                     .invoke(CallMethod.Invoke.VIRTUAL,
 102                             b.clazzByName("C"), b.clazzByName("C"), "finalize", "()V", METHODREF)
 103                 .build()
 104             .build();
 105 
 106         b.test().callSite(C, C, "m", "()V")
 107                 .ignoreResult()
 108                 .done()
 109 
 110         .run();
 111     }
 112 
 113     /* final Class<?> getClass() */
 114     public void testGetClass() throws Exception {
 115         TestBuilder b = factory.getBuilder();
 116 
 117         Interface I = b.intf("I")
 118                 .defaultMethod("getClass", "()Ljava/lang/Class;")
 119                     .sig("()Ljava/lang/Class<*>;")
 120                     .throw_(TestFailure.class)
 121                 .build()
 122             .build();
 123 
 124         ConcreteClass C = b.clazz("C").implement(I).build();
 125 
 126         b.test().loadClass(I).throws_(VerifyError.class).done()
 127         .run();
 128     }
 129 
 130     /* int hashCode() */
 131     public void testHashCode() throws Exception {
 132         TestBuilder b = factory.getBuilder();
 133 
 134         Interface I = b.intf("I")
 135                 .defaultMethod("hashCode", "()I")
 136                     .throw_(TestFailure.class)
 137                 .build()
 138             .build();
 139 
 140         ConcreteClass C = b.clazz("C").implement(I).build();
 141 
 142         ClassLoader cl = b.build();
 143         Object c = cl.loadClass("C").newInstance();
 144 
 145         c.hashCode();
 146     }
 147 
 148 
 149     /* final void notify() */
 150     public void testNotify() throws Exception {
 151         TestBuilder b = factory.getBuilder();
 152 
 153         Interface I = b.intf("I")
 154                 .defaultMethod("notify", "()V")
 155                     .throw_(TestFailure.class)
 156                 .build()
 157             .build();
 158 
 159         ConcreteClass C = b.clazz("C").implement(I).build();
 160 
 161         b.test().loadClass(I).throws_(VerifyError.class).done()
 162         .run();
 163     }
 164 
 165     /* void notifyAll() */
 166     public void testNotifyAll() throws Exception {
 167         TestBuilder b = factory.getBuilder();
 168 
 169         Interface I = b.intf("I")
 170                 .defaultMethod("notifyAll", "()V")
 171                     .throw_(TestFailure.class)
 172                 .build()
 173             .build();
 174 
 175         ConcreteClass C = b.clazz("C").implement(I).build();
 176 
 177         b.test().loadClass(I).throws_(VerifyError.class).done()
 178         .run();
 179     }
 180 
 181     /* String toString() */
 182     public void testToString() throws Exception {
 183         TestBuilder b = factory.getBuilder();
 184 
 185         Interface I = b.intf("I")
 186                 .defaultMethod("toString()", "()Ljava/lang/String;")
 187                     .throw_(TestFailure.class)
 188                 .build()
 189             .build();
 190 
 191         ConcreteClass C = b.clazz("C").implement(I).build();
 192 
 193         ClassLoader cl = b.build();
 194         Object c = cl.loadClass("C").newInstance();
 195 
 196         c.toString();
 197     }
 198 
 199     /* final void wait() */
 200     public void testWait() throws Exception {
 201         TestBuilder b = factory.getBuilder();
 202 
 203         Interface I = b.intf("I")
 204                 .defaultMethod("wait", "()V")
 205                     .throw_(TestFailure.class)
 206                 .build()
 207             .build();
 208 
 209         ConcreteClass C = b.clazz("C").implement(I).build();
 210 
 211         b.test().loadClass(I).throws_(VerifyError.class).done()
 212         .run();
 213     }
 214 
 215     /* final void wait(long timeout) */
 216     public void testTimedWait() throws Exception {
 217         TestBuilder b = factory.getBuilder();
 218 
 219         Interface I = b.intf("I")
 220                 .defaultMethod("wait", "(J)V")
 221                     .throw_(TestFailure.class)
 222                 .build()
 223             .build();
 224 
 225         ConcreteClass C = b.clazz("C").implement(I).build();
 226 
 227         b.test().loadClass(I).throws_(VerifyError.class).done()
 228         .run();
 229     }
 230 
 231     /* final void wait(long timeout, int nanos) */
 232     public void testTimedWait1() throws Exception {
 233         TestBuilder b = factory.getBuilder();
 234 
 235         Interface I = b.intf("I")
 236                 .defaultMethod("wait", "(JI)V")
 237                     .throw_(TestFailure.class)
 238                 .build()
 239             .build();
 240 
 241         ConcreteClass C = b.clazz("C").implement(I).build();
 242 
 243         b.test().loadClass(I).throws_(VerifyError.class).done()
 244         .run();
 245     }
 246 }