< prev index next >

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

Print this page
*** 62,11 ***
      private static final MethodHandle OBJECTS_HASHCODE;
      private static final MethodHandle OBJECTS_TOSTRING;
      private static final MethodHandle OBJECT_EQ;
      private static final MethodHandle HASH_COMBINER;
  
!     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 {
--- 62,13 ---
      private static final MethodHandle OBJECTS_HASHCODE;
      private static final MethodHandle OBJECTS_TOSTRING;
      private static final MethodHandle OBJECT_EQ;
      private static final MethodHandle HASH_COMBINER;
  
!     /* 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 {

*** 389,18 ***
          requireNonNull(recordClass);
          requireNonNull(names);
          requireNonNull(getters);
          Arrays.stream(getters).forEach(Objects::requireNonNull);
          MethodType methodType;
!         if (type instanceof MethodType mt)
              methodType = mt;
!         else {
              methodType = null;
              if (!MethodHandle.class.equals(type))
                  throw new IllegalArgumentException(type.toString());
          }
          List<MethodHandle> getterList = List.of(getters);
          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);
--- 391,26 ---
          requireNonNull(recordClass);
          requireNonNull(names);
          requireNonNull(getters);
          Arrays.stream(getters).forEach(Objects::requireNonNull);
          MethodType methodType;
!         if (type instanceof MethodType mt) {
              methodType = mt;
!             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 >