[Git][java-team/invokebinder][upstream] New upstream version 1.14

Emmanuel Bourg (@ebourg) gitlab at salsa.debian.org
Mon Oct 28 09:49:54 GMT 2024



Emmanuel Bourg pushed to branch upstream at Debian Java Maintainers / invokebinder


Commits:
aa99df19 by Emmanuel Bourg at 2024-10-28T10:46:35+01:00
New upstream version 1.14
- - - - -


4 changed files:

- pom.xml
- src/main/java/com/headius/invokebinder/Binder.java
- src/main/java/com/headius/invokebinder/transform/Collect.java
- src/test/java/com/headius/invokebinder/BinderTest.java


Changes:

=====================================
pom.xml
=====================================
@@ -3,7 +3,7 @@
     <groupId>com.headius</groupId>
     <artifactId>invokebinder</artifactId>
     <packaging>bundle</packaging>
-    <version>1.13</version>
+    <version>1.14</version>
     <name>invokebinder</name>
     <url>http://maven.apache.org</url>
 


=====================================
src/main/java/com/headius/invokebinder/Binder.java
=====================================
@@ -1586,7 +1586,7 @@ public class Binder {
     }
 
     /**
-     * Same as {@link #invokeSpecial(MethodHandles.Lookup, String, Class<?>)} but using the default lookup for this
+     * Same as {@link #invokeSpecial(MethodHandles.Lookup, String, Class)} but using the default lookup for this
      * binder.
      */
     public MethodHandle invokeSpecial(String name, Class<?> caller) throws NoSuchMethodException, IllegalAccessException {


=====================================
src/main/java/com/headius/invokebinder/transform/Collect.java
=====================================
@@ -16,13 +16,14 @@
 package com.headius.invokebinder.transform;
 
 import com.headius.invokebinder.Binder;
+import com.headius.invokebinder.Util;
 
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
 
 /**
- * An argument-boxing transform with a fixed incoming size.
+ * A filter that takes multiple arguments and replaces them with zero or one argument of a new type.
  *
  * Equivalent call: MethodHandle.asCollector(Class, int) or MethodHandles.collectArguments
  */
@@ -31,61 +32,68 @@ public class Collect extends Transform {
     private final MethodType source;
     private final int index;
     private final int count;
-    private final Class<?> arrayType;
+    private final Class<?> resultType;
     private final MethodHandle collector;
 
-    public Collect(MethodType source, int index, Class<?> arrayType) {
+    public Collect(MethodType source, int index, Class<?> resultType) {
         this.source = source;
         this.index = index;
         this.count = source.parameterCount() - index;
-        this.arrayType = arrayType;
+        this.resultType = resultType;
         this.collector = null;
     }
 
-    public Collect(MethodType source, int index, Class<?> arrayType, MethodHandle collector) {
+    public Collect(MethodType source, int index, Class<?> resultType, MethodHandle collector) {
         this.source = source;
         this.index = index;
         this.count = source.parameterCount() - index;
-        this.arrayType = arrayType;
+        this.resultType = resultType;
         this.collector = collector;
     }
 
-    public Collect(MethodType source, int index, int count, Class<?> arrayType) {
+    public Collect(MethodType source, int index, int count, Class<?> resultType) {
         this.source = source;
         this.index = index;
         this.count = count;
-        this.arrayType = arrayType;
+        this.resultType = resultType;
         this.collector = null;
     }
 
-    public Collect(MethodType source, int index, int count, Class<?> arrayType, MethodHandle collector) {
+    public Collect(MethodType source, int index, int count, Class<?> resultType, MethodHandle collector) {
         this.source = source;
         this.index = index;
         this.count = count;
-        this.arrayType = arrayType;
+        this.resultType = resultType;
         this.collector = collector;
     }
 
     public MethodHandle up(MethodHandle target) {
-        if (onlyTail()) {
-            // fast path for tail args
-            if (collector == null) {
-                return target.asCollector(arrayType, count);
+        if (collector == null) {
+            if (Util.isJava9()) {
+                // Java 9 can collect a subset of non-tail arguments
+                return target.asCollector(index, resultType, count);
+            } else {
+                if (onlyTail()) {
+                    // tail arguments can be array-collected on all Java versions
+                    return target.asCollector(resultType, count);
+                } else {
+                    // non-tail arguments must be permuted prior to Java 9
+                    Permutes permutes = buildPermutes(source, target.type());
+
+                    Binder binder = preparePermuteBinder(permutes);
+                    return binder.invoke(target);
+                }
             }
-
-            return MethodHandles.collectArguments(target, index, collector);
         } else {
-            Permutes permutes = buildPermutes(source, target.type());
-
-            Binder binder = preparePermuteBinder(permutes);
-            return binder.invoke(target);
+            // custom collector always collects only as many args as it accepts
+            return MethodHandles.collectArguments(target, index, collector);
         }
     }
 
     private Binder preparePermuteBinder(Permutes permutes) {
         return Binder.from(source)
                 .permute(permutes.movePermute)
-                .collect(source.parameterCount() - count, arrayType, collector)
+                .collect(source.parameterCount() - count, resultType)
                 .permute(permutes.moveBackPermute);
     }
 
@@ -94,20 +102,31 @@ public class Collect extends Transform {
 
         return type
                 .dropParameterTypes(index, index + count)
-                .insertParameterTypes(index, arrayType);
+                .insertParameterTypes(index, resultType);
     }
 
     private void assertTypesAreCompatible() {
-        Class<?> componentType = arrayType.getComponentType();
-        for (int i = index; i < index + count; i++) {
-            Class<?> in = source.parameterType(i);
-            assert in.isAssignableFrom(componentType)
-                    : "incoming type " + in.getName() + " not compatible with " + componentType.getName() + "[]";
+        if (collector == null) {
+            // default array collector
+            assert resultType.isArray() : "no collector provided but target type is not array";
+            Class<?> componentType = resultType.getComponentType();
+            for (int i = index; i < index + count; i++) {
+                Class<?> in = source.parameterType(i);
+                assert in.isAssignableFrom(componentType)
+                        : "incoming type " + in.getName() + " not compatible with " + componentType.getName() + "[]";
+            }
+        } else {
+            for (int i = 0; i < count; i++) {
+                Class<?> in = source.parameterType(index + i);
+                Class<?> out = collector.type().parameterType(i);
+                assert in.isAssignableFrom(out) : "incoming type " + in.getName() + " not compatible with " + out;
+            }
+            assert collector.type().returnType().isAssignableFrom(resultType);
         }
     }
 
     public String toString() {
-        return "collect at " + index + " into " + arrayType.getName();
+        return "collect at " + index + " into " + resultType.getName();
     }
 
     public String toJava(MethodType incoming) {
@@ -115,7 +134,7 @@ public class Collect extends Transform {
         if (onlyTail()) {
             if (collector == null) {
                 builder.append("handle = handle.asCollector(");
-                buildClassArgument(builder, arrayType);
+                buildClassArgument(builder, resultType);
                 builder
                         .append(", ")
                         .append(count)
@@ -128,7 +147,7 @@ public class Collect extends Transform {
                         .append(count)
                         .append(", ");
 
-                buildClassArgument(builder, arrayType);
+                buildClassArgument(builder, resultType);
 
                 builder.append(");");
             }


=====================================
src/test/java/com/headius/invokebinder/BinderTest.java
=====================================
@@ -378,7 +378,7 @@ public class BinderTest {
                 .invoke(Subjects.StringIntegersStringHandle);
 
         assertEquals(methodType(String.class, String.class, Integer.class, Integer.class, Integer.class, String.class), handle2.type());
-        assertEquals("[foo, [1, 2, 3], bar]", (String)handle2.invokeExact("foo", new Integer(1), new Integer(2), new Integer(3), "bar"));
+        assertEquals("[foo, [1, 2, 3], bar]", (String)handle2.invokeExact("foo", Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), "bar"));
     }
 
     public static String[] newStringArray(String s1, String s2) {



View it on GitLab: https://salsa.debian.org/java-team/invokebinder/-/commit/aa99df1900cc580c2eebd7b75b036c5c4ce42842

-- 
View it on GitLab: https://salsa.debian.org/java-team/invokebinder/-/commit/aa99df1900cc580c2eebd7b75b036c5c4ce42842
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-java-commits/attachments/20241028/275d7c80/attachment.htm>


More information about the pkg-java-commits mailing list