< prev index next >

src/java.base/share/classes/java/lang/runtime/ObjectMethods.java

Print this page
@@ -70,11 +70,13 @@
      private static final MethodHandle OBJECT_HASHCODE;
      private static final MethodHandle OBJECT_TO_STRING;
      private static final MethodHandle STRING_FORMAT;
      private static final MethodHandle HASH_COMBINER;
  
-     private static final HashMap<Class<?>, MethodHandle> primitiveEquals = new HashMap<>();
+     /* package-private */
+     static final HashMap<Class<?>, MethodHandle> primitiveEquals = new HashMap<>();
+ 
      private static final HashMap<Class<?>, MethodHandle> primitiveHashers = new HashMap<>();
      private static final HashMap<Class<?>, MethodHandle> primitiveToString = new HashMap<>();
  
      static {
          try {

@@ -249,10 +251,11 @@
      }
  
      /**
       * Generates a method handle for the {@code toString} method for a given data class
       * @param receiverClass   the data class
+      * @param simpleName      the simple name of the record class
       * @param getters         the list of getters
       * @param names           the names
       * @return the method handle
       */
      private static MethodHandle makeToString(MethodHandles.Lookup lookup,

@@ -410,18 +413,26 @@
          requireNonNull(recordClass);
          requireNonNull(names);
          requireNonNull(getters);
          Arrays.stream(getters).forEach(Objects::requireNonNull);
          MethodType methodType;
-         if (type instanceof MethodType mt)
+         if (type instanceof MethodType mt) {
              methodType = mt;
-         else {
+             if (mt.parameterType(0) != recordClass) {
+                 throw new IllegalArgumentException("Bad method type: " + mt);
+             }
+         } else {
              methodType = null;
              if (!MethodHandle.class.equals(type))
                  throw new IllegalArgumentException(type.toString());
          }
          List<MethodHandle> getterList = List.of(getters);
+         for (MethodHandle getter : getterList) {
+             if (getter.type().parameterType(0) != recordClass) {
+                 throw new IllegalArgumentException("Bad receiver type: " + getter);
+             }
+         }
          MethodHandle handle = switch (methodName) {
              case "equals"   -> {
                  if (methodType != null && !methodType.equals(MethodType.methodType(boolean.class, recordClass, Object.class)))
                      throw new IllegalArgumentException("Bad method type: " + methodType);
                  yield makeEquals(recordClass, getterList);
< prev index next >