< prev index next >

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

Print this page
*** 66,11 ***
      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<>();
      private static final HashMap<Class<?>, MethodHandle> primitiveHashers = new HashMap<>();
      private static final HashMap<Class<?>, MethodHandle> primitiveToString = new HashMap<>();
  
      static {
          try {
--- 66,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;
  
!     /* 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 {

*** 245,17 ***
      }
  
      /**
       * Generates a method handle for the {@code toString} method for a given data class
       * @param receiverClass   the data class
       * @param getters         the list of getters
       * @param names           the names
       * @return the method handle
       */
      private static MethodHandle makeToString(Class<?> receiverClass,
!                                             List<MethodHandle> getters,
!                                             List<String> names) {
          // This is a pretty lousy algorithm; we spread the receiver over N places,
          // apply the N getters, apply N toString operations, and concat the result with String.format
          // Better to use String.format directly, or delegate to StringConcatFactory
          // Also probably want some quoting around String components
  
--- 247,19 ---
      }
  
      /**
       * 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(Class<?> receiverClass,
!                                              String simpleName,
!                                              List<MethodHandle> getters,
+                                              List<String> names) {
          // This is a pretty lousy algorithm; we spread the receiver over N places,
          // apply the N getters, apply N toString operations, and concat the result with String.format
          // Better to use String.format directly, or delegate to StringConcatFactory
          // Also probably want some quoting around String components
  

*** 263,11 ***
  
          int[] invArgs = new int[getters.size()];
          Arrays.fill(invArgs, 0);
          MethodHandle[] filters = new MethodHandle[getters.size()];
          StringBuilder sb = new StringBuilder();
!         sb.append(receiverClass.getSimpleName()).append("[");
          for (int i=0; i<getters.size(); i++) {
              MethodHandle getter = getters.get(i); // (R)T
              MethodHandle stringify = stringifier(getter.type().returnType()); // (T)String
              MethodHandle stringifyThisField = MethodHandles.filterArguments(stringify, 0, getter);    // (R)String
              filters[i] = stringifyThisField;
--- 267,11 ---
  
          int[] invArgs = new int[getters.size()];
          Arrays.fill(invArgs, 0);
          MethodHandle[] filters = new MethodHandle[getters.size()];
          StringBuilder sb = new StringBuilder();
!         sb.append(simpleName).append("[");
          for (int i=0; i<getters.size(); i++) {
              MethodHandle getter = getters.get(i); // (R)T
              MethodHandle stringify = stringifier(getter.type().returnType()); // (T)String
              MethodHandle stringifyThisField = MethodHandles.filterArguments(stringify, 0, getter);    // (R)String
              filters[i] = stringifyThisField;

*** 340,36 ***
          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);
              }
              case "hashCode" -> {
!                 if (methodType != null && !methodType.equals(MethodType.methodType(int.class, recordClass)))
                      throw new IllegalArgumentException("Bad method type: " + methodType);
!                 yield makeHashCode(recordClass, getterList);
              }
              case "toString" -> {
!                 if (methodType != null && !methodType.equals(MethodType.methodType(String.class, recordClass)))
                      throw new IllegalArgumentException("Bad method type: " + methodType);
                  List<String> nameList = "".equals(names) ? List.of() : List.of(names.split(";"));
                  if (nameList.size() != getterList.size())
                      throw new IllegalArgumentException("Name list and accessor list do not match");
!                 yield makeToString(recordClass, getterList, nameList);
              }
              default -> throw new IllegalArgumentException(methodName);
          };
          return methodType != null ? new ConstantCallSite(handle) : handle;
      }
--- 344,45 ---
          requireNonNull(recordClass);
          requireNonNull(names);
          requireNonNull(getters);
          Arrays.stream(getters).forEach(Objects::requireNonNull);
          MethodType methodType;
!         Class<?> receiverType = recordClass.isPrimitiveClass() ? recordClass.asValueType() : recordClass;
+         if (type instanceof MethodType mt) {
              methodType = mt;
!             if (mt.parameterType(0) != receiverType) {
+                 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) != receiverType) {
+                 throw new IllegalArgumentException("Bad receiver type: " + getter);
+             }
+         }
          MethodHandle handle = switch (methodName) {
              case "equals"   -> {
!                 if (methodType != null && !methodType.equals(MethodType.methodType(boolean.class, receiverType, Object.class)))
                      throw new IllegalArgumentException("Bad method type: " + methodType);
!                 yield makeEquals(receiverType, getterList);
              }
              case "hashCode" -> {
!                 if (methodType != null && !methodType.equals(MethodType.methodType(int.class, receiverType)))
                      throw new IllegalArgumentException("Bad method type: " + methodType);
!                 yield makeHashCode(receiverType, getterList);
              }
              case "toString" -> {
!                 if (methodType != null && !methodType.equals(MethodType.methodType(String.class, receiverType)))
                      throw new IllegalArgumentException("Bad method type: " + methodType);
                  List<String> nameList = "".equals(names) ? List.of() : List.of(names.split(";"));
                  if (nameList.size() != getterList.size())
                      throw new IllegalArgumentException("Name list and accessor list do not match");
!                 yield makeToString(receiverType, recordClass.getSimpleName(), getterList, nameList);
              }
              default -> throw new IllegalArgumentException(methodName);
          };
          return methodType != null ? new ConstantCallSite(handle) : handle;
      }
< prev index next >