1 /*
  2  * Copyright (c) 2021, 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 /*
 25  * @test
 26  * @summary test that PrimitiveObject interface is injected correctly
 27  * @library /test/lib /test/jdk/lib/testlibrary/bytecode /test/jdk/java/lang/invoke/common
 28  * @build jdk.experimental.bytecode.BasicClassBuilder
 29  * @compile TestPrimitiveObject.java
 30  * @compile PrimitiveType.java PrimitiveTypeSpecified.java
 31  * @compile AbstractSpecified.java InterfaceSpecified.java
 32  * @compile PrimitiveWithSuper.java PrimitiveWithInterface.java
 33  * @run main/othervm -verify TestPrimitiveObject
 34  */
 35 
 36 import java.lang.invoke.*;
 37 import jdk.experimental.bytecode.*;
 38 
 39 public class TestPrimitiveObject {
 40 
 41     public static void main(String[] args) {
 42         checkNegativePrimitiveObjects();
 43         checkPositivePrimitiveObjects();
 44         checkIcceOnInvalidSupers();
 45     }
 46 
 47     static void checkNegativePrimitiveObjects() {
 48         Class[] clazzes = new Class[] {
 49             String.class, Comparable.class, Number.class
 50         };
 51         for (Class clazz : clazzes) {
 52             checkPrimitiveObject(clazz, false);
 53         }
 54     }
 55 
 56     static void checkPositivePrimitiveObjects() {
 57         Class[] clazzes = new Class[] {
 58             PrimitiveType.class, PrimitiveTypeSpecified.class,
 59             AbstractSpecified.class, InterfaceSpecified.class,
 60             PrimitiveWithSuper.class, PrimitiveWithInterface.class
 61         };
 62         for (Class clazz : clazzes) {
 63             checkPrimitiveObject(clazz, true);
 64         }
 65     }
 66 
 67     static void checkPrimitiveObject(Class c, boolean subtype) {
 68         boolean s;
 69         try {
 70             c.asSubclass(PrimitiveObject.class);
 71             s = true;
 72         } catch(ClassCastException e) {
 73             s = false;
 74         }
 75         if (subtype != s) {
 76             if (subtype) {
 77                 throw new RuntimeException("Type " + c.getName() + " is missing PrimitiveObject");
 78             } else {
 79                 throw new RuntimeException("Type " + c.getName() + " should not implement PrimitiveObject");
 80             }
 81         }
 82     }
 83 
 84     // Define classes that implement PrimitiveObject but are invalid supers
 85     static void checkIcceOnInvalidSupers() {
 86         MethodHandles.Lookup mhLookup = MethodHandles.lookup();
 87         checkIcce(mhLookup, createClass().build());
 88         checkIcce(mhLookup, createAbstractWithField().build());
 89         checkIcce(mhLookup, createAbstractIdentity().build());
 90         checkIcce(mhLookup, createIdentity().build());
 91     }
 92 
 93     static ClassBuilder createClass() {
 94         return new BasicClassBuilder("ANormalClass", 62, 0)
 95             .withSuperclass("java/lang/Object")
 96             .withSuperinterface("java/lang/PrimitiveObject");
 97     }
 98 
 99     static ClassBuilder createAbstractWithField() {
100         return new BasicClassBuilder("AbstractWithField", 62, 0)
101             .withSuperclass("java/lang/Object")
102             .withFlags(Flag.ACC_ABSTRACT)
103             .withField("aFieldWhichIsIllegalAsAnAbstractSuperToPrimitiveObject", "I")
104             .withSuperinterface("java/lang/PrimitiveObject");
105     }
106 
107     static ClassBuilder createAbstractIdentity() {
108         return new BasicClassBuilder("AbstractIdentity", 62, 0)
109             .withSuperclass("java/lang/Object")
110             .withFlags(Flag.ACC_ABSTRACT)
111             .withSuperinterface("java/lang/IdentityObject")
112             .withSuperinterface("java/lang/PrimitiveObject");
113     }
114 
115   static ClassBuilder createIdentity() {
116         return new BasicClassBuilder("Identity", 62, 0)
117             .withSuperclass("java/lang/Object")
118             .withSuperinterface("java/lang/IdentityObject")
119             .withSuperinterface("java/lang/PrimitiveObject");
120     }
121 
122     static void checkIcce(MethodHandles.Lookup mhLookup, byte[] clazzBytes) {
123         try {
124             mhLookup.defineClass(clazzBytes);
125             throw new RuntimeException("Expected IncompatibleClassChangeError");
126         }
127         catch (IllegalAccessException ill) { throw new RuntimeException(ill); }
128         catch (IncompatibleClassChangeError icce) { System.out.println(icce); }
129     }
130 
131 }