1 /*
  2  * Copyright (c) 2017, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang.runtime;
 27 
 28 import java.lang.invoke.ConstantCallSite;
 29 import java.lang.invoke.MethodHandle;
 30 import java.lang.invoke.MethodHandles;
 31 import java.lang.invoke.MethodType;
 32 import java.lang.invoke.TypeDescriptor;
 33 import java.security.AccessController;
 34 import java.security.PrivilegedAction;
 35 import java.util.Arrays;
 36 import java.util.HashMap;
 37 import java.util.List;
 38 import java.util.Objects;
 39 
 40 import static java.util.Objects.requireNonNull;
 41 
 42 /**
 43  * Bootstrap methods for state-driven implementations of core methods,
 44  * including {@link Object#equals(Object)}, {@link Object#hashCode()}, and
 45  * {@link Object#toString()}.  These methods may be used, for example, by
 46  * Java compiler implementations to implement the bodies of {@link Object}
 47  * methods for record classes.
 48  *
 49  * @since 16
 50  */
 51 public class ObjectMethods {
 52 
 53     private ObjectMethods() { }
 54 
 55     private static final MethodType DESCRIPTOR_MT = MethodType.methodType(MethodType.class);
 56     private static final MethodType NAMES_MT = MethodType.methodType(List.class);
 57     private static final MethodHandle FALSE = MethodHandles.constant(boolean.class, false);
 58     private static final MethodHandle TRUE = MethodHandles.constant(boolean.class, true);
 59     private static final MethodHandle ZERO = MethodHandles.constant(int.class, 0);
 60     private static final MethodHandle CLASS_IS_INSTANCE;
 61     private static final MethodHandle OBJECT_EQUALS;
 62     private static final MethodHandle OBJECTS_EQUALS;
 63     private static final MethodHandle OBJECTS_HASHCODE;
 64     private static final MethodHandle OBJECTS_TOSTRING;
 65     private static final MethodHandle OBJECT_EQ;
 66     private static final MethodHandle OBJECT_HASHCODE;
 67     private static final MethodHandle OBJECT_TO_STRING;
 68     private static final MethodHandle STRING_FORMAT;
 69     private static final MethodHandle HASH_COMBINER;
 70 
 71     /* package-private */
 72     static final HashMap<Class<?>, MethodHandle> primitiveEquals = new HashMap<>();
 73 
 74     private static final HashMap<Class<?>, MethodHandle> primitiveHashers = new HashMap<>();
 75     private static final HashMap<Class<?>, MethodHandle> primitiveToString = new HashMap<>();
 76 
 77     static {
 78         try {
 79             Class<ObjectMethods> OBJECT_METHODS_CLASS = ObjectMethods.class;
 80             MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();
 81             MethodHandles.Lookup lookup = MethodHandles.lookup();
 82 
 83             @SuppressWarnings("removal")
 84             ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
 85                 @Override public ClassLoader run() { return ClassLoader.getPlatformClassLoader(); }
 86             });
 87 
 88             CLASS_IS_INSTANCE = publicLookup.findVirtual(Class.class, "isInstance",
 89                                                          MethodType.methodType(boolean.class, Object.class));
 90             OBJECT_EQUALS = publicLookup.findVirtual(Object.class, "equals",
 91                                                      MethodType.methodType(boolean.class, Object.class));
 92             OBJECT_HASHCODE = publicLookup.findVirtual(Object.class, "hashCode",
 93                                                        MethodType.fromMethodDescriptorString("()I", loader));
 94             OBJECT_TO_STRING = publicLookup.findVirtual(Object.class, "toString",
 95                                                         MethodType.methodType(String.class));
 96             STRING_FORMAT = publicLookup.findStatic(String.class, "format",
 97                                                     MethodType.methodType(String.class, String.class, Object[].class));
 98             OBJECTS_EQUALS = publicLookup.findStatic(Objects.class, "equals",
 99                                                      MethodType.methodType(boolean.class, Object.class, Object.class));
100             OBJECTS_HASHCODE = publicLookup.findStatic(Objects.class, "hashCode",
101                                                        MethodType.methodType(int.class, Object.class));
102             OBJECTS_TOSTRING = publicLookup.findStatic(Objects.class, "toString",
103                                                        MethodType.methodType(String.class, Object.class));
104 
105             OBJECT_EQ = lookup.findStatic(OBJECT_METHODS_CLASS, "eq",
106                                           MethodType.methodType(boolean.class, Object.class, Object.class));
107             HASH_COMBINER = lookup.findStatic(OBJECT_METHODS_CLASS, "hashCombiner",
108                                               MethodType.fromMethodDescriptorString("(II)I", loader));
109 
110             primitiveEquals.put(byte.class, lookup.findStatic(OBJECT_METHODS_CLASS, "eq",
111                                                               MethodType.fromMethodDescriptorString("(BB)Z", loader)));
112             primitiveEquals.put(short.class, lookup.findStatic(OBJECT_METHODS_CLASS, "eq",
113                                                                MethodType.fromMethodDescriptorString("(SS)Z", loader)));
114             primitiveEquals.put(char.class, lookup.findStatic(OBJECT_METHODS_CLASS, "eq",
115                                                               MethodType.fromMethodDescriptorString("(CC)Z", loader)));
116             primitiveEquals.put(int.class, lookup.findStatic(OBJECT_METHODS_CLASS, "eq",
117                                                              MethodType.fromMethodDescriptorString("(II)Z", loader)));
118             primitiveEquals.put(long.class, lookup.findStatic(OBJECT_METHODS_CLASS, "eq",
119                                                               MethodType.fromMethodDescriptorString("(JJ)Z", loader)));
120             primitiveEquals.put(float.class, lookup.findStatic(OBJECT_METHODS_CLASS, "eq",
121                                                                MethodType.fromMethodDescriptorString("(FF)Z", loader)));
122             primitiveEquals.put(double.class, lookup.findStatic(OBJECT_METHODS_CLASS, "eq",
123                                                                 MethodType.fromMethodDescriptorString("(DD)Z", loader)));
124             primitiveEquals.put(boolean.class, lookup.findStatic(OBJECT_METHODS_CLASS, "eq",
125                                                                  MethodType.fromMethodDescriptorString("(ZZ)Z", loader)));
126 
127             primitiveHashers.put(byte.class, lookup.findStatic(Byte.class, "hashCode",
128                                                                MethodType.fromMethodDescriptorString("(B)I", loader)));
129             primitiveHashers.put(short.class, lookup.findStatic(Short.class, "hashCode",
130                                                                 MethodType.fromMethodDescriptorString("(S)I", loader)));
131             primitiveHashers.put(char.class, lookup.findStatic(Character.class, "hashCode",
132                                                                MethodType.fromMethodDescriptorString("(C)I", loader)));
133             primitiveHashers.put(int.class, lookup.findStatic(Integer.class, "hashCode",
134                                                               MethodType.fromMethodDescriptorString("(I)I", loader)));
135             primitiveHashers.put(long.class, lookup.findStatic(Long.class, "hashCode",
136                                                                MethodType.fromMethodDescriptorString("(J)I", loader)));
137             primitiveHashers.put(float.class, lookup.findStatic(Float.class, "hashCode",
138                                                                 MethodType.fromMethodDescriptorString("(F)I", loader)));
139             primitiveHashers.put(double.class, lookup.findStatic(Double.class, "hashCode",
140                                                                  MethodType.fromMethodDescriptorString("(D)I", loader)));
141             primitiveHashers.put(boolean.class, lookup.findStatic(Boolean.class, "hashCode",
142                                                                   MethodType.fromMethodDescriptorString("(Z)I", loader)));
143 
144             primitiveToString.put(byte.class, lookup.findStatic(Byte.class, "toString",
145                                                                 MethodType.methodType(String.class, byte.class)));
146             primitiveToString.put(short.class, lookup.findStatic(Short.class, "toString",
147                                                                  MethodType.methodType(String.class, short.class)));
148             primitiveToString.put(char.class, lookup.findStatic(Character.class, "toString",
149                                                                 MethodType.methodType(String.class, char.class)));
150             primitiveToString.put(int.class, lookup.findStatic(Integer.class, "toString",
151                                                                MethodType.methodType(String.class, int.class)));
152             primitiveToString.put(long.class, lookup.findStatic(Long.class, "toString",
153                                                                 MethodType.methodType(String.class, long.class)));
154             primitiveToString.put(float.class, lookup.findStatic(Float.class, "toString",
155                                                                  MethodType.methodType(String.class, float.class)));
156             primitiveToString.put(double.class, lookup.findStatic(Double.class, "toString",
157                                                                   MethodType.methodType(String.class, double.class)));
158             primitiveToString.put(boolean.class, lookup.findStatic(Boolean.class, "toString",
159                                                                    MethodType.methodType(String.class, boolean.class)));
160         }
161         catch (ReflectiveOperationException e) {
162             throw new RuntimeException(e);
163         }
164     }
165 
166     private static int hashCombiner(int x, int y) {
167         return x*31 + y;
168     }
169 
170     private static boolean eq(Object a, Object b) { return a == b; }
171     private static boolean eq(byte a, byte b) { return a == b; }
172     private static boolean eq(short a, short b) { return a == b; }
173     private static boolean eq(char a, char b) { return a == b; }
174     private static boolean eq(int a, int b) { return a == b; }
175     private static boolean eq(long a, long b) { return a == b; }
176     private static boolean eq(float a, float b) { return Float.compare(a, b) == 0; }
177     private static boolean eq(double a, double b) { return Double.compare(a, b) == 0; }
178     private static boolean eq(boolean a, boolean b) { return a == b; }
179 
180     /** Get the method handle for combining two values of a given type */
181     private static MethodHandle equalator(Class<?> clazz) {
182         return (clazz.isPrimitive()
183                 ? primitiveEquals.get(clazz)
184                 : OBJECTS_EQUALS.asType(MethodType.methodType(boolean.class, clazz, clazz)));
185     }
186 
187     /** Get the hasher for a value of a given type */
188     private static MethodHandle hasher(Class<?> clazz) {
189         return (clazz.isPrimitive()
190                 ? primitiveHashers.get(clazz)
191                 : OBJECTS_HASHCODE.asType(MethodType.methodType(int.class, clazz)));
192     }
193 
194     /** Get the stringifier for a value of a given type */
195     private static MethodHandle stringifier(Class<?> clazz) {
196         return (clazz.isPrimitive()
197                 ? primitiveToString.get(clazz)
198                 : OBJECTS_TOSTRING.asType(MethodType.methodType(String.class, clazz)));
199     }
200 
201     /**
202      * Generates a method handle for the {@code equals} method for a given data class
203      * @param receiverClass   the data class
204      * @param getters         the list of getters
205      * @return the method handle
206      */
207     private static MethodHandle makeEquals(Class<?> receiverClass,
208                                           List<MethodHandle> getters) {
209         MethodType rr = MethodType.methodType(boolean.class, receiverClass, receiverClass);
210         MethodType ro = MethodType.methodType(boolean.class, receiverClass, Object.class);
211         MethodHandle instanceFalse = MethodHandles.dropArguments(FALSE, 0, receiverClass, Object.class); // (RO)Z
212         MethodHandle instanceTrue = MethodHandles.dropArguments(TRUE, 0, receiverClass, Object.class); // (RO)Z
213         MethodHandle isSameObject = OBJECT_EQ.asType(ro); // (RO)Z
214         MethodHandle isInstance = MethodHandles.dropArguments(CLASS_IS_INSTANCE.bindTo(receiverClass), 0, receiverClass); // (RO)Z
215         MethodHandle accumulator = MethodHandles.dropArguments(TRUE, 0, receiverClass, receiverClass); // (RR)Z
216 
217         for (MethodHandle getter : getters) {
218             MethodHandle equalator = equalator(getter.type().returnType()); // (TT)Z
219             MethodHandle thisFieldEqual = MethodHandles.filterArguments(equalator, 0, getter, getter); // (RR)Z
220             accumulator = MethodHandles.guardWithTest(thisFieldEqual, accumulator, instanceFalse.asType(rr));
221         }
222 
223         return MethodHandles.guardWithTest(isSameObject,
224                                            instanceTrue,
225                                            MethodHandles.guardWithTest(isInstance, accumulator.asType(ro), instanceFalse));
226     }
227 
228     /**
229      * Generates a method handle for the {@code hashCode} method for a given data class
230      * @param receiverClass   the data class
231      * @param getters         the list of getters
232      * @return the method handle
233      */
234     private static MethodHandle makeHashCode(Class<?> receiverClass,
235                                             List<MethodHandle> getters) {
236         MethodHandle accumulator = MethodHandles.dropArguments(ZERO, 0, receiverClass); // (R)I
237 
238         // @@@ Use loop combinator instead?
239         for (MethodHandle getter : getters) {
240             MethodHandle hasher = hasher(getter.type().returnType()); // (T)I
241             MethodHandle hashThisField = MethodHandles.filterArguments(hasher, 0, getter);    // (R)I
242             MethodHandle combineHashes = MethodHandles.filterArguments(HASH_COMBINER, 0, accumulator, hashThisField); // (RR)I
243             accumulator = MethodHandles.permuteArguments(combineHashes, accumulator.type(), 0, 0); // adapt (R)I to (RR)I
244         }
245 
246         return accumulator;
247     }
248 
249     /**
250      * Generates a method handle for the {@code toString} method for a given data class
251      * @param receiverClass   the data class
252      * @param simpleName      the simple name of the record class
253      * @param getters         the list of getters
254      * @param names           the names
255      * @return the method handle
256      */
257     private static MethodHandle makeToString(Class<?> receiverClass,
258                                              String simpleName,
259                                              List<MethodHandle> getters,
260                                              List<String> names) {
261         // This is a pretty lousy algorithm; we spread the receiver over N places,
262         // apply the N getters, apply N toString operations, and concat the result with String.format
263         // Better to use String.format directly, or delegate to StringConcatFactory
264         // Also probably want some quoting around String components
265 
266         assert getters.size() == names.size();
267 
268         int[] invArgs = new int[getters.size()];
269         Arrays.fill(invArgs, 0);
270         MethodHandle[] filters = new MethodHandle[getters.size()];
271         StringBuilder sb = new StringBuilder();
272         sb.append(simpleName).append("[");
273         for (int i=0; i<getters.size(); i++) {
274             MethodHandle getter = getters.get(i); // (R)T
275             MethodHandle stringify = stringifier(getter.type().returnType()); // (T)String
276             MethodHandle stringifyThisField = MethodHandles.filterArguments(stringify, 0, getter);    // (R)String
277             filters[i] = stringifyThisField;
278             sb.append(names.get(i)).append("=%s");
279             if (i != getters.size() - 1)
280                 sb.append(", ");
281         }
282         sb.append(']');
283         String formatString = sb.toString();
284         MethodHandle formatter = MethodHandles.insertArguments(STRING_FORMAT, 0, formatString)
285                                               .asCollector(String[].class, getters.size()); // (R*)String
286         if (getters.size() == 0) {
287             // Add back extra R
288             formatter = MethodHandles.dropArguments(formatter, 0, receiverClass);
289         }
290         else {
291             MethodHandle filtered = MethodHandles.filterArguments(formatter, 0, filters);
292             formatter = MethodHandles.permuteArguments(filtered, MethodType.methodType(String.class, receiverClass), invArgs);
293         }
294 
295         return formatter;
296     }
297 
298     /**
299      * Bootstrap method to generate the {@link Object#equals(Object)},
300      * {@link Object#hashCode()}, and {@link Object#toString()} methods, based
301      * on a description of the component names and accessor methods, for either
302      * {@code invokedynamic} call sites or dynamic constant pool entries.
303      *
304      * For more detail on the semantics of the generated methods see the specification
305      * of {@link java.lang.Record#equals(Object)}, {@link java.lang.Record#hashCode()} and
306      * {@link java.lang.Record#toString()}.
307      *
308      *
309      * @param lookup       Every bootstrap method is expected to have a {@code lookup}
310      *                     which usually represents a lookup context with the
311      *                     accessibility privileges of the caller. This is because
312      *                     {@code invokedynamic} call sites always provide a {@code lookup}
313      *                     to the corresponding bootstrap method, but this method just
314      *                     ignores the {@code lookup} parameter
315      * @param methodName   the name of the method to generate, which must be one of
316      *                     {@code "equals"}, {@code "hashCode"}, or {@code "toString"}
317      * @param type         a {@link MethodType} corresponding the descriptor type
318      *                     for the method, which must correspond to the descriptor
319      *                     for the corresponding {@link Object} method, if linking
320      *                     an {@code invokedynamic} call site, or the
321      *                     constant {@code MethodHandle.class}, if linking a
322      *                     dynamic constant
323      * @param recordClass  the record class hosting the record components
324      * @param names        the list of component names, joined into a string
325      *                     separated by ";", or the empty string if there are no
326      *                     components. This parameter is ignored if the {@code methodName}
327      *                     parameter is {@code "equals"} or {@code "hashCode"}
328      * @param getters      method handles for the accessor methods for the components
329      * @return             a call site if invoked by indy, or a method handle
330      *                     if invoked by a condy
331      * @throws IllegalArgumentException if the bootstrap arguments are invalid
332      *                                  or inconsistent
333      * @throws NullPointerException if any argument but {@code lookup} is {@code null},
334      *                              in the case of the {@code getters} argument, its
335      *                              contents cannot be {@code null} either
336      * @throws Throwable if any exception is thrown during call site construction
337      */
338     public static Object bootstrap(MethodHandles.Lookup lookup, String methodName, TypeDescriptor type,
339                                    Class<?> recordClass,
340                                    String names,
341                                    MethodHandle... getters) throws Throwable {
342         requireNonNull(methodName);
343         requireNonNull(type);
344         requireNonNull(recordClass);
345         requireNonNull(names);
346         requireNonNull(getters);
347         Arrays.stream(getters).forEach(Objects::requireNonNull);
348         MethodType methodType;
349         Class<?> receiverType = recordClass.isPrimitiveClass() ? recordClass.asValueType() : recordClass;
350         if (type instanceof MethodType mt) {
351             methodType = mt;
352             if (mt.parameterType(0) != receiverType) {
353                 throw new IllegalArgumentException("Bad method type: " + mt);
354             }
355         } else {
356             methodType = null;
357             if (!MethodHandle.class.equals(type))
358                 throw new IllegalArgumentException(type.toString());
359         }
360         List<MethodHandle> getterList = List.of(getters);
361         for (MethodHandle getter : getterList) {
362             if (getter.type().parameterType(0) != receiverType) {
363                 throw new IllegalArgumentException("Bad receiver type: " + getter);
364             }
365         }
366         MethodHandle handle = switch (methodName) {
367             case "equals"   -> {
368                 if (methodType != null && !methodType.equals(MethodType.methodType(boolean.class, receiverType, Object.class)))
369                     throw new IllegalArgumentException("Bad method type: " + methodType);
370                 yield makeEquals(receiverType, getterList);
371             }
372             case "hashCode" -> {
373                 if (methodType != null && !methodType.equals(MethodType.methodType(int.class, receiverType)))
374                     throw new IllegalArgumentException("Bad method type: " + methodType);
375                 yield makeHashCode(receiverType, getterList);
376             }
377             case "toString" -> {
378                 if (methodType != null && !methodType.equals(MethodType.methodType(String.class, receiverType)))
379                     throw new IllegalArgumentException("Bad method type: " + methodType);
380                 List<String> nameList = "".equals(names) ? List.of() : List.of(names.split(";"));
381                 if (nameList.size() != getterList.size())
382                     throw new IllegalArgumentException("Name list and accessor list do not match");
383                 yield makeToString(receiverType, recordClass.getSimpleName(), getterList, nameList);
384             }
385             default -> throw new IllegalArgumentException(methodName);
386         };
387         return methodType != null ? new ConstantCallSite(handle) : handle;
388     }
389 }