From a05ae2bc38d85c08e93d725aa23c40663794c3b1 Mon Sep 17 00:00:00 2001 From: Michiel Meeuwissen Date: Fri, 1 Aug 2025 22:20:09 +0200 Subject: [PATCH 1/2] I think we may have a use case for method handles here? --- .../abstractalgebra/padic/PAdicIntegers.java | 6 ++- .../meeuw/configuration/ReflectionUtils.java | 13 +++++ .../AbstractAlgebraicIntOperator.java | 6 ++- .../operators/AlgebraicBinaryOperator.java | 5 +- .../math/operators/AlgebraicIntOperator.java | 21 ++++----- .../BasicAlgebraicBinaryOperator.java | 47 ++++++++----------- .../operators/BasicAlgebraicIntOperator.java | 14 ++++-- .../BasicAlgebraicUnaryOperator.java | 41 ++++++++-------- .../operators/BasicComparisonOperator.java | 21 +++++---- .../meeuw/math/operators/BasicFunction.java | 21 +++++---- .../math/operators/OperatorInterface.java | 8 ++-- .../math/operators/BasicFunctionTest.java | 3 +- 12 files changed, 113 insertions(+), 93 deletions(-) diff --git a/mihxil-algebra/src/main/java/org/meeuw/math/abstractalgebra/padic/PAdicIntegers.java b/mihxil-algebra/src/main/java/org/meeuw/math/abstractalgebra/padic/PAdicIntegers.java index 5fdb36967..ec8ff118c 100644 --- a/mihxil-algebra/src/main/java/org/meeuw/math/abstractalgebra/padic/PAdicIntegers.java +++ b/mihxil-algebra/src/main/java/org/meeuw/math/abstractalgebra/padic/PAdicIntegers.java @@ -6,6 +6,8 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import static org.meeuw.configuration.ReflectionUtils.getDeclaredMethodHandle; + import org.meeuw.math.IntegerUtils; import org.meeuw.math.Randomizable; import org.meeuw.math.abstractalgebra.Cardinality; @@ -24,9 +26,9 @@ public class PAdicIntegers implements Field, Randomizable { - public static final AlgebraicIntOperator LEFT_SHIFT = new AbstractAlgebraicIntOperator("left_shift", getDeclaredMethod(PAdicInteger.class, "leftShift", int.class), (e, i) -> e + "<<" + i); + public static final AlgebraicIntOperator LEFT_SHIFT = new AbstractAlgebraicIntOperator("left_shift", getDeclaredMethodHandle(PAdicInteger.class, "leftShift", int.class), (e, i) -> e + "<<" + i); - public static final AlgebraicIntOperator RIGHT_SHIFT = new AbstractAlgebraicIntOperator("right_shift", getDeclaredMethod(PAdicInteger.class, "rightShift", int.class), (e, i) -> e + ">>" + i); + public static final AlgebraicIntOperator RIGHT_SHIFT = new AbstractAlgebraicIntOperator("right_shift", getDeclaredMethodHandle(PAdicInteger.class, "rightShift", int.class), (e, i) -> e + ">>" + i); final int base; diff --git a/mihxil-configuration/src/main/java/org/meeuw/configuration/ReflectionUtils.java b/mihxil-configuration/src/main/java/org/meeuw/configuration/ReflectionUtils.java index 399388a0c..8191eb5ab 100644 --- a/mihxil-configuration/src/main/java/org/meeuw/configuration/ReflectionUtils.java +++ b/mihxil-configuration/src/main/java/org/meeuw/configuration/ReflectionUtils.java @@ -18,6 +18,8 @@ import lombok.SneakyThrows; import lombok.extern.java.Log; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; import java.lang.reflect.*; import java.util.*; import java.util.function.Consumer; @@ -52,10 +54,21 @@ public static Method getDeclaredMethod(Class clazz, String name, Class... return clazz.getDeclaredMethod(name, params); } + @SneakyThrows + public static MethodHandle getDeclaredMethodHandle(Class clazz, String name, Class... params) { + return MethodHandles.publicLookup().unreflect(clazz.getDeclaredMethod(name, params)); + } + + public static Method getDeclaredBinaryMethod(Class clazz, String name) { return getDeclaredMethod(clazz, name, clazz); } + @SneakyThrows + public static MethodHandle getDeclaredBinaryMethodHandle(Class clazz, String name) { + return MethodHandles.publicLookup().unreflect(getDeclaredBinaryMethod(clazz, name)); + } + /** * Borrowed from stackoverflow */ diff --git a/mihxil-math/src/main/java/org/meeuw/math/operators/AbstractAlgebraicIntOperator.java b/mihxil-math/src/main/java/org/meeuw/math/operators/AbstractAlgebraicIntOperator.java index fe7b8da0b..b8a7f0e65 100644 --- a/mihxil-math/src/main/java/org/meeuw/math/operators/AbstractAlgebraicIntOperator.java +++ b/mihxil-math/src/main/java/org/meeuw/math/operators/AbstractAlgebraicIntOperator.java @@ -15,6 +15,8 @@ */ package org.meeuw.math.operators; +import java.lang.invoke.MethodHandle; + import lombok.Getter; import lombok.SneakyThrows; import lombok.extern.java.Log; @@ -33,13 +35,13 @@ public class AbstractAlgebraicIntOperator implements AlgebraicIntOperator { @Getter - final Method method; + final MethodHandle method; final BiFunction stringify; final String name; - public AbstractAlgebraicIntOperator(String name, Method method, BiFunction stringify) { + public AbstractAlgebraicIntOperator(String name, MethodHandle method, BiFunction stringify) { this.name = name; this.method = method; this.stringify = stringify; diff --git a/mihxil-math/src/main/java/org/meeuw/math/operators/AlgebraicBinaryOperator.java b/mihxil-math/src/main/java/org/meeuw/math/operators/AlgebraicBinaryOperator.java index ff97dac11..e9f4cf023 100644 --- a/mihxil-math/src/main/java/org/meeuw/math/operators/AlgebraicBinaryOperator.java +++ b/mihxil-math/src/main/java/org/meeuw/math/operators/AlgebraicBinaryOperator.java @@ -87,11 +87,12 @@ default String getSymbol() { @Override default > Method getMethodFor(E e) { - try { + /* try { return e.getClass().getMethod(getMethod().getName(), e.getStructure().getElementClass()); } catch (NoSuchMethodException nsme) { return OperatorInterface.super.getMethodFor(e); - } + }*/ + return null; } /** diff --git a/mihxil-math/src/main/java/org/meeuw/math/operators/AlgebraicIntOperator.java b/mihxil-math/src/main/java/org/meeuw/math/operators/AlgebraicIntOperator.java index 0a65c691f..37f83b1e8 100644 --- a/mihxil-math/src/main/java/org/meeuw/math/operators/AlgebraicIntOperator.java +++ b/mihxil-math/src/main/java/org/meeuw/math/operators/AlgebraicIntOperator.java @@ -15,6 +15,8 @@ */ package org.meeuw.math.operators; +import java.lang.invoke.MethodHandle; + import lombok.SneakyThrows; import java.lang.reflect.InvocationTargetException; @@ -46,21 +48,16 @@ default String getSymbol() { @SuppressWarnings("unchecked") @SneakyThrows - static > E apply(AlgebraicIntOperator operator, Method method, E e, int i) { + static > E apply(AlgebraicIntOperator operator, MethodHandle method, E e, int i) { try { return (E) method.invoke(e, i); } catch (IllegalArgumentException iae) { - try { - // It is possible that the operation is defined, but the class does not extend the correct class - // e.g. an odd integer implements negation, but it is not an additive group (negation is possible inside the algebra, but addition itself isn't). - return (E) e.getClass().getMethod(method.getName(), int.class).invoke(e, i); - } catch (NoSuchMethodException noSuchMethodError) { - throw new NoSuchOperatorException("No operation " + operator + " found on " + e, noSuchMethodError); - } catch (InvocationTargetException ex) { - throw ex.getCause(); - } - } catch (InvocationTargetException ex) { - throw ex.getCause(); + + // It is possible that the operation is defined, but the class does not extend the correct class + // e.g. an odd integer implements negation, but it is not an additive group (negation is possible inside the algebra, but addition itself isn't). + //return (E) e.getClass().getMethod(method.getName(), int.class).invoke(e, i); + throw iae; + } } diff --git a/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicBinaryOperator.java b/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicBinaryOperator.java index 4cce1e39f..ccdef3142 100644 --- a/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicBinaryOperator.java +++ b/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicBinaryOperator.java @@ -18,8 +18,8 @@ import lombok.Getter; import lombok.SneakyThrows; +import java.lang.invoke.MethodHandle; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.function.BinaryOperator; import org.checkerframework.checker.nullness.qual.Nullable; @@ -27,8 +27,8 @@ import org.meeuw.math.exceptions.InvalidAlgebraicResult; import org.meeuw.math.exceptions.NoSuchOperatorException; -import static org.meeuw.configuration.ReflectionUtils.getDeclaredBinaryMethod; -import static org.meeuw.configuration.ReflectionUtils.getDeclaredMethod; +import static org.meeuw.configuration.ReflectionUtils.getDeclaredBinaryMethodHandle; +import static org.meeuw.configuration.ReflectionUtils.getDeclaredMethodHandle; /** * The basic operations of arithmetic @@ -42,8 +42,8 @@ public enum BasicAlgebraicBinaryOperator implements AlgebraicBinaryOperator { * @see MagmaElement#operate(MagmaElement) */ OPERATION( - getDeclaredBinaryMethod(MagmaElement.class, "operate"), "*", - getDeclaredMethod(Group.class, "unity"), + getDeclaredBinaryMethodHandle(MagmaElement.class, "operate"), "*", + getDeclaredMethodHandle(Group.class, "unity"), BasicAlgebraicUnaryOperator.INVERSION, 2 ), @@ -52,8 +52,8 @@ public enum BasicAlgebraicBinaryOperator implements AlgebraicBinaryOperator { * @see AdditiveSemiGroupElement#plus(AdditiveSemiGroupElement) */ ADDITION( - getDeclaredBinaryMethod(AdditiveSemiGroupElement.class, "plus"), "+", - getDeclaredMethod(AdditiveMonoid.class, "zero"), + getDeclaredBinaryMethodHandle(AdditiveSemiGroupElement.class, "plus"), "+", + getDeclaredMethodHandle(AdditiveMonoid.class, "zero"), BasicAlgebraicUnaryOperator.NEGATION, 2 ), @@ -62,7 +62,7 @@ public enum BasicAlgebraicBinaryOperator implements AlgebraicBinaryOperator { * @see AdditiveGroupElement#minus(AdditiveGroupElement) */ SUBTRACTION( - getDeclaredBinaryMethod(AdditiveGroupElement.class, "minus"), "-", + getDeclaredBinaryMethodHandle(AdditiveGroupElement.class, "minus"), "-", ADDITION.unity, ADDITION.inverse, 2 @@ -73,8 +73,8 @@ public enum BasicAlgebraicBinaryOperator implements AlgebraicBinaryOperator { * @see MultiplicativeSemiGroupElement#times(MultiplicativeSemiGroupElement) */ MULTIPLICATION( - getDeclaredBinaryMethod(MultiplicativeSemiGroupElement.class, "times"), "⋅", - getDeclaredMethod(MultiplicativeMonoid.class, "one"), + getDeclaredBinaryMethodHandle(MultiplicativeSemiGroupElement.class, "times"), "⋅", + getDeclaredMethodHandle(MultiplicativeMonoid.class, "one"), BasicAlgebraicUnaryOperator.RECIPROCAL, 3 ), @@ -83,7 +83,7 @@ public enum BasicAlgebraicBinaryOperator implements AlgebraicBinaryOperator { * @see MultiplicativeGroupElement#dividedBy(MultiplicativeGroupElement) */ DIVISION( - getDeclaredBinaryMethod(MultiplicativeGroupElement.class, "dividedBy"), "/", + getDeclaredBinaryMethodHandle(MultiplicativeGroupElement.class, "dividedBy"), "/", MULTIPLICATION.unity, MULTIPLICATION.inverse, 3 @@ -93,7 +93,7 @@ public enum BasicAlgebraicBinaryOperator implements AlgebraicBinaryOperator { * @see CompleteFieldElement#pow(CompleteFieldElement) */ POWER( - getDeclaredBinaryMethod(CompleteFieldElement.class, "pow"), "^", + getDeclaredBinaryMethodHandle(CompleteFieldElement.class, "pow"), "^", MULTIPLICATION.unity, null, 4 @@ -101,10 +101,10 @@ public enum BasicAlgebraicBinaryOperator implements AlgebraicBinaryOperator { @Getter - final Method method; + final MethodHandle method; @Getter - final Method unity; + final MethodHandle unity; @Getter @Nullable @@ -119,7 +119,8 @@ public enum BasicAlgebraicBinaryOperator implements AlgebraicBinaryOperator { final int precedence; - BasicAlgebraicBinaryOperator(Method method, String symbol, Method unity, BasicAlgebraicUnaryOperator inverse, int precedence) { + @SneakyThrows + BasicAlgebraicBinaryOperator(MethodHandle method, String symbol, MethodHandle unity, BasicAlgebraicUnaryOperator inverse, int precedence) { this.method = method; this.symbol = symbol; this.stringify = (a, b) -> a + " " + symbol + " " + b; @@ -134,12 +135,12 @@ public enum BasicAlgebraicBinaryOperator implements AlgebraicBinaryOperator { @SneakyThrows public > E apply(E element1, E element2) { try { - if (!method.getParameterTypes()[0].isInstance(element1)) { + /* if (!method.getParameterTypes()[0].isInstance(element1)) { throw new NoSuchOperatorException(element1.getClass().getSimpleName() + " " + element1 + " has no operator '" + method.getName() + "'"); - } + }*/ E result = (E) method.invoke(element1, element2); if (result == null) { - throw new InvalidAlgebraicResult("" + method + "(" + element1 + ',' + element2 + ") resulted null"); + throw new InvalidAlgebraicResult(method + "(" + element1 + ',' + element2 + ") resulted null"); } return result; } catch (InvocationTargetException ite) { @@ -175,15 +176,7 @@ public > E inverse(E element) { @SuppressWarnings("unchecked") @SneakyThrows public , S extends AlgebraicStructure> E unity(S structure) { - try { - return (E) unity.invoke(structure); - } catch (IllegalAccessException e) { - throw new IllegalArgumentException(unity.getDeclaringClass().getName() + "." + unity.getName() + "(" + structure + " ): " + e.getMessage()); - } catch (IllegalArgumentException iae) { - throw new NoSuchOperatorException("for unity of " + structure + ":" + iae.getMessage(), iae); - } catch (InvocationTargetException e) { - throw e.getCause(); - } + return (E) unity.invoke(structure); } @Override diff --git a/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicIntOperator.java b/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicIntOperator.java index aaa96d8ac..483414994 100644 --- a/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicIntOperator.java +++ b/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicIntOperator.java @@ -15,12 +15,16 @@ */ package org.meeuw.math.operators; +import java.lang.invoke.MethodHandle; + import lombok.Getter; import lombok.SneakyThrows; import lombok.extern.java.Log; import java.lang.reflect.Method; +import static org.meeuw.configuration.ReflectionUtils.getDeclaredMethodHandle; + import org.meeuw.math.abstractalgebra.*; import org.meeuw.math.text.TextUtils; @@ -38,7 +42,7 @@ public enum BasicAlgebraicIntOperator implements AlgebraicIntOperator { * Raising to an integer power, which is defined for all {@link MultiplicativeSemiGroupElement}s. */ POWER( - getDeclaredMethod(MultiplicativeSemiGroupElement.class, "pow", int.class), + getDeclaredMethodHandle(MultiplicativeSemiGroupElement.class, "pow", int.class), (s, i) -> withBracketsIfNeeded(s) + TextUtils.superscript(i) ), @@ -46,7 +50,7 @@ public enum BasicAlgebraicIntOperator implements AlgebraicIntOperator { * Taking the n-th root of an element, which is defined for all {@link CompleteFieldElement}s. */ ROOT( - getDeclaredMethod(CompleteFieldElement.class, "root", int.class), + getDeclaredMethodHandle(CompleteFieldElement.class, "root", int.class), (s, i) -> TextUtils.superscript(i) + "√" + withBracketsIfNeeded(s) ), @@ -54,7 +58,7 @@ public enum BasicAlgebraicIntOperator implements AlgebraicIntOperator { * Taking the n-th tetration of an element. */ TETRATION( - getDeclaredMethod(CompleteFieldElement.class, "tetration", int.class), + getDeclaredMethodHandle(CompleteFieldElement.class, "tetration", int.class), (s, i) -> TextUtils.superscript(i) + withBracketsIfNeeded(s) ) ; @@ -70,11 +74,11 @@ private static boolean needsBrackets(CharSequence s) { @Getter - final Method method; + final MethodHandle method; final java.util.function.BiFunction stringify; - BasicAlgebraicIntOperator(Method method, java.util.function.BiFunction stringify) { + BasicAlgebraicIntOperator(MethodHandle method, java.util.function.BiFunction stringify) { this.method = method; this.stringify = stringify; } diff --git a/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicUnaryOperator.java b/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicUnaryOperator.java index da9cb53c9..cb68247d0 100644 --- a/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicUnaryOperator.java +++ b/mihxil-math/src/main/java/org/meeuw/math/operators/BasicAlgebraicUnaryOperator.java @@ -15,6 +15,8 @@ */ package org.meeuw.math.operators; +import java.lang.invoke.MethodHandle; + import lombok.Getter; import lombok.SneakyThrows; import lombok.extern.java.Log; @@ -22,6 +24,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import static org.meeuw.configuration.ReflectionUtils.getDeclaredMethodHandle; + import org.meeuw.math.abstractalgebra.*; import org.meeuw.math.exceptions.NoSuchOperatorException; @@ -40,7 +44,7 @@ public enum BasicAlgebraicUnaryOperator implements AlgebraicUnaryOperator { * @see AlgebraicElement#self() */ IDENTIFY( - getDeclaredMethod(AlgebraicElement.class, "self"), + getDeclaredMethodHandle(AlgebraicElement.class, "self"), (s) -> !s.isEmpty() && s.charAt(0) == '+' ? s : "+" + s ), @@ -48,14 +52,14 @@ public enum BasicAlgebraicUnaryOperator implements AlgebraicUnaryOperator { * @see AdditiveGroupElement#negation() */ NEGATION( - getDeclaredMethod(AdditiveGroupElement.class, "negation"), + getDeclaredMethodHandle(AdditiveGroupElement.class, "negation"), (s) -> !s.isEmpty() && s.charAt(0) == '-' ? "+" + s.subSequence(1, s.length()) : "-" + s), /** * @see MultiplicativeGroupElement#reciprocal() */ RECIPROCAL( - getDeclaredMethod(MultiplicativeGroupElement.class, "reciprocal"), + getDeclaredMethodHandle(MultiplicativeGroupElement.class, "reciprocal"), (s) -> s + superscript(-1) ), @@ -63,7 +67,7 @@ public enum BasicAlgebraicUnaryOperator implements AlgebraicUnaryOperator { * @see GroupElement#inverse() */ INVERSION( - getDeclaredMethod(GroupElement.class, "inverse"), + getDeclaredMethodHandle(GroupElement.class, "inverse"), (s) -> "inverse(" + s + ")" ), @@ -71,7 +75,7 @@ public enum BasicAlgebraicUnaryOperator implements AlgebraicUnaryOperator { * @see MultiplicativeSemiGroupElement#sqr() */ SQR( - getDeclaredMethod(MultiplicativeSemiGroupElement.class, "sqr"), + getDeclaredMethodHandle(MultiplicativeSemiGroupElement.class, "sqr"), (s) -> (s.toString().contains(" ") ? "(" + s + ")" : s ) + superscript(2) ), @@ -80,7 +84,7 @@ public enum BasicAlgebraicUnaryOperator implements AlgebraicUnaryOperator { * @see CompleteFieldElement#sqrt() */ SQRT( - getDeclaredMethod(CompleteFieldElement.class, "sqrt"), + getDeclaredMethodHandle(CompleteFieldElement.class, "sqrt"), (s) -> "√" + (s.length() > 1 ?"(" + s + ")" : s) ), @@ -88,7 +92,7 @@ public enum BasicAlgebraicUnaryOperator implements AlgebraicUnaryOperator { * @see CompleteFieldElement#sin() */ SIN( - getDeclaredMethod(CompleteFieldElement.class, "sin"), + getDeclaredMethodHandle(CompleteFieldElement.class, "sin"), (s) -> "sin(" + s + ")" ), @@ -96,7 +100,7 @@ public enum BasicAlgebraicUnaryOperator implements AlgebraicUnaryOperator { * @see CompleteFieldElement#cos() */ COS( - getDeclaredMethod(CompleteFieldElement.class, "cos"), + getDeclaredMethodHandle(CompleteFieldElement.class, "cos"), (s) -> "cos(" + s + ")" ), @@ -104,7 +108,7 @@ public enum BasicAlgebraicUnaryOperator implements AlgebraicUnaryOperator { * @see CompleteFieldElement#exp() */ EXP( - getDeclaredMethod(CompleteFieldElement.class, "exp"), + getDeclaredMethodHandle(CompleteFieldElement.class, "exp"), (s) -> "exp(" + s + ")" ), @@ -112,7 +116,7 @@ public enum BasicAlgebraicUnaryOperator implements AlgebraicUnaryOperator { * @see CompleteFieldElement#ln() */ LN( - getDeclaredMethod(CompleteFieldElement.class, "ln"), + getDeclaredMethodHandle(CompleteFieldElement.class, "ln"), (s) -> "ln(" + s + ")" ), @@ -120,7 +124,7 @@ public enum BasicAlgebraicUnaryOperator implements AlgebraicUnaryOperator { * @see CompleteFieldElement#sinh() */ SINH( - getDeclaredMethod(CompleteFieldElement.class, "sinh"), + getDeclaredMethodHandle(CompleteFieldElement.class, "sinh"), (s) -> "sinh(" + s + ")" ), @@ -128,18 +132,18 @@ public enum BasicAlgebraicUnaryOperator implements AlgebraicUnaryOperator { * @see CompleteFieldElement#cosh() */ COSH( - getDeclaredMethod(CompleteFieldElement.class, "cosh"), + getDeclaredMethodHandle(CompleteFieldElement.class, "cosh"), (s) -> "cosh(" + s + ")" ) ; @Getter - final Method method; + final MethodHandle method; final java.util.function.UnaryOperator stringify; - BasicAlgebraicUnaryOperator(Method method, java.util.function.UnaryOperator stringify) { + BasicAlgebraicUnaryOperator(MethodHandle method, java.util.function.UnaryOperator stringify) { this.method = method; this.stringify = stringify; } @@ -151,13 +155,12 @@ public > E apply(E e) { try { return (E) method.invoke(e); } catch (IllegalArgumentException iae) { - try { + // It is possible that the operation is defined, but the class does not extend the correct class // e.g. an odd integer implements negation, but it is not an additive group (negation is possible inside the algebra, but addition itself isn't). - return (E) e.getClass().getMethod(method.getName()).invoke(e); - } catch (java.lang.NoSuchMethodException noSuchMethodError) { - throw new NoSuchOperatorException("No operation " + this + " found on " + e, noSuchMethodError); - } + //return (E) e.getClass().getMethod(method.getName()).invoke(e); + throw iae; + } catch (InvocationTargetException ex) { throw ex.getCause(); } diff --git a/mihxil-math/src/main/java/org/meeuw/math/operators/BasicComparisonOperator.java b/mihxil-math/src/main/java/org/meeuw/math/operators/BasicComparisonOperator.java index baf29e3a8..6ce0b9413 100644 --- a/mihxil-math/src/main/java/org/meeuw/math/operators/BasicComparisonOperator.java +++ b/mihxil-math/src/main/java/org/meeuw/math/operators/BasicComparisonOperator.java @@ -18,6 +18,7 @@ import lombok.Getter; import lombok.SneakyThrows; +import java.lang.invoke.MethodHandle; import java.lang.reflect.Method; import java.util.NavigableSet; import java.util.function.BinaryOperator; @@ -26,7 +27,7 @@ import org.meeuw.math.abstractalgebra.AlgebraicElement; import org.meeuw.math.abstractalgebra.StrictlyOrdered; -import static org.meeuw.configuration.ReflectionUtils.getDeclaredBinaryMethod; +import static org.meeuw.configuration.ReflectionUtils.getDeclaredBinaryMethodHandle; /** * The basic operators to compare two elements. Works on two things of the same type, returning a @@ -41,7 +42,7 @@ public enum BasicComparisonOperator implements AlgebraicComparisonOperator { * @see AlgebraicElement#eq */ EQ( - getDeclaredBinaryMethod(AlgebraicElement.class, "eq"), + getDeclaredBinaryMethodHandle(AlgebraicElement.class, "eq"), (a, b) -> a + " ≈ " + b ), @@ -49,7 +50,7 @@ public enum BasicComparisonOperator implements AlgebraicComparisonOperator { * @see AlgebraicElement#neq */ NEQ( - getDeclaredBinaryMethod(AlgebraicElement.class, "neq"), + getDeclaredBinaryMethodHandle(AlgebraicElement.class, "neq"), (a, b) -> a + " ≉ " + b ), @@ -57,7 +58,7 @@ public enum BasicComparisonOperator implements AlgebraicComparisonOperator { * @see Object#equals(Object) */ EQUALS( - getDeclaredBinaryMethod(Object.class, "equals"), + getDeclaredBinaryMethodHandle(Object.class, "equals"), (a, b) -> a + " = " + b ), @@ -65,7 +66,7 @@ public enum BasicComparisonOperator implements AlgebraicComparisonOperator { * @see StrictlyOrdered#lt */ LT( - getDeclaredBinaryMethod(StrictlyOrdered.class, "lt"), + getDeclaredBinaryMethodHandle(StrictlyOrdered.class, "lt"), (a, b) -> a + " < " + b ), @@ -73,7 +74,7 @@ public enum BasicComparisonOperator implements AlgebraicComparisonOperator { * @see StrictlyOrdered#lte */ LTE( - getDeclaredBinaryMethod(StrictlyOrdered.class, "lte"), + getDeclaredBinaryMethodHandle(StrictlyOrdered.class, "lte"), (a, b) -> a + " ≲ " + b ), @@ -81,7 +82,7 @@ public enum BasicComparisonOperator implements AlgebraicComparisonOperator { * @see StrictlyOrdered#gt */ GT( - getDeclaredBinaryMethod(StrictlyOrdered.class, "gt"), + getDeclaredBinaryMethodHandle(StrictlyOrdered.class, "gt"), (a, b) -> a + " > " + b ), @@ -89,7 +90,7 @@ public enum BasicComparisonOperator implements AlgebraicComparisonOperator { * @see StrictlyOrdered#gte */ GTE( - getDeclaredBinaryMethod(StrictlyOrdered.class, "gte"), + getDeclaredBinaryMethodHandle(StrictlyOrdered.class, "gte"), (a, b) -> a + " ≳ " + b ) ; @@ -103,12 +104,12 @@ public enum BasicComparisonOperator implements AlgebraicComparisonOperator { @Getter - final Method method; + final MethodHandle method; @Getter final BinaryOperator stringify; - BasicComparisonOperator(Method method, java.util.function.BinaryOperator stringify) { + BasicComparisonOperator(MethodHandle method, java.util.function.BinaryOperator stringify) { this.method = method; this.stringify = stringify; } diff --git a/mihxil-math/src/main/java/org/meeuw/math/operators/BasicFunction.java b/mihxil-math/src/main/java/org/meeuw/math/operators/BasicFunction.java index 1a9e6512b..b8961061c 100644 --- a/mihxil-math/src/main/java/org/meeuw/math/operators/BasicFunction.java +++ b/mihxil-math/src/main/java/org/meeuw/math/operators/BasicFunction.java @@ -19,14 +19,14 @@ import lombok.SneakyThrows; import lombok.extern.java.Log; +import java.lang.invoke.MethodHandle; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import org.meeuw.math.numbers.Sizeable; import org.meeuw.math.numbers.SizeableScalar; import org.meeuw.math.text.TextUtils; -import static org.meeuw.configuration.ReflectionUtils.getDeclaredMethod; +import static org.meeuw.configuration.ReflectionUtils.getDeclaredMethodHandle; @Log public enum BasicFunction implements GenericFunction { @@ -34,27 +34,27 @@ public enum BasicFunction implements GenericFunction { /** * @see Sizeable#abs() */ - ABS(getDeclaredMethod(Sizeable.class, "abs"), (s) -> "|" + s + "|"), + ABS(getDeclaredMethodHandle(Sizeable.class, "abs"), (s) -> "|" + s + "|"), /** * Returns the 'decimal' (actually {@link java.math.BigDecimal}) presentation of the given object. * @see SizeableScalar#bigDecimalValue() */ - DECIMAL(getDeclaredMethod(SizeableScalar.class, "bigDecimalValue"), (s) -> s + TextUtils.subscript("=")), + DECIMAL(getDeclaredMethodHandle(SizeableScalar.class, "bigDecimalValue"), (s) -> s + TextUtils.subscript("=")), /** * Returns an 'integer' (actually {@link java.math.BigInteger}) version of the given object. * This may involve rounding. * @see SizeableScalar#bigIntegerValue() */ - INTEGER(getDeclaredMethod(SizeableScalar.class, "bigIntegerValue"), (s) -> "⌊" + s + "⌉"); + INTEGER(getDeclaredMethodHandle(SizeableScalar.class, "bigIntegerValue"), (s) -> "⌊" + s + "⌉"); @Getter - final Method method; + final MethodHandle method; final java.util.function.UnaryOperator stringify; - BasicFunction(Method method, java.util.function.UnaryOperator stringify) { + BasicFunction(MethodHandle method, java.util.function.UnaryOperator stringify) { this.method = method; this.stringify = stringify; } @@ -65,10 +65,11 @@ public enum BasicFunction implements GenericFunction { public R apply(T t) { try { try { - return (R) method.invoke(t); + return (R) method.invokeExact(t); } catch (IllegalArgumentException iae) { - log.fine(this + " on " + t + " but " + t.getClass() + " not a " + method.getDeclaringClass()); - return (R) t.getClass().getMethod(method.getName(), method.getParameterTypes()).invoke(t); + //log.fine(this + " on " + t + " but " + t.getClass() + " not a " + method.getDeclaringClass()); + //return (R) t.getClass().getMethod(method.getName(), method.getParameterTypes()).invoke(t); + throw new IllegalArgumentException(iae.getMessage()); } } catch (InvocationTargetException ex) { throw ex.getCause(); diff --git a/mihxil-math/src/main/java/org/meeuw/math/operators/OperatorInterface.java b/mihxil-math/src/main/java/org/meeuw/math/operators/OperatorInterface.java index a73708dfb..7fc4cb4a9 100644 --- a/mihxil-math/src/main/java/org/meeuw/math/operators/OperatorInterface.java +++ b/mihxil-math/src/main/java/org/meeuw/math/operators/OperatorInterface.java @@ -17,6 +17,7 @@ import lombok.SneakyThrows; +import java.lang.invoke.MethodHandle; import java.lang.reflect.Method; import java.util.Comparator; import java.util.Optional; @@ -40,12 +41,12 @@ default int ordinal() { * Returns a {@link Method method} associated with this operator. * @throws UnsupportedOperationException if no such method is defined, e.g. because the operator is {@link AlgebraicUnaryOperator#compose composite}. */ - default Method getMethod() { + default MethodHandle getMethod() { throw new UnsupportedOperationException(); } default > boolean isAlgebraicFor(E e) { - return ! getNonAlgebraic(e).isPresent(); + return getNonAlgebraic(e).isEmpty(); } default > Optional getNonAlgebraic(E e) { return Optional.ofNullable(getMethodFor(e).getAnnotation(NonAlgebraic.class)); @@ -53,6 +54,7 @@ default > Optional getNonAlgebraic(E @SneakyThrows default > Method getMethodFor(E e) { - return e.getClass().getMethod(getMethod().getName(), getMethod().getParameterTypes()); + throw new UnsupportedOperationException(); + //return e.getClass().getMethod(getMethod().getName(), getMethod().getParameterTypes()); } } diff --git a/mihxil-math/src/test/java/org/meeuw/test/math/operators/BasicFunctionTest.java b/mihxil-math/src/test/java/org/meeuw/test/math/operators/BasicFunctionTest.java index 279cb3d26..02d46eea8 100644 --- a/mihxil-math/src/test/java/org/meeuw/test/math/operators/BasicFunctionTest.java +++ b/mihxil-math/src/test/java/org/meeuw/test/math/operators/BasicFunctionTest.java @@ -17,6 +17,7 @@ import lombok.EqualsAndHashCode; +import java.lang.invoke.WrongMethodTypeException; import java.util.function.Function; import org.junit.jupiter.api.Test; @@ -65,7 +66,7 @@ protected B abs() { public void abs() { assertThatThrownBy(() -> { BasicFunction.ABS.apply("tests"); - }).isInstanceOf(NoSuchMethodException.class); + }).isInstanceOf(WrongMethodTypeException.class); assertThat(((A) BasicFunction.ABS.apply(new A(-1))).i).isEqualTo(1); From 2d7e47f5ed31627037ffb9c6af23bc3423720a42 Mon Sep 17 00:00:00 2001 From: Michiel Meeuwissen Date: Fri, 1 Aug 2025 22:20:49 +0200 Subject: [PATCH 2/2] renames test configuration --- .run/{Solver.run.xml => Solver 24 8 8 3 3.run.xml} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename .run/{Solver.run.xml => Solver 24 8 8 3 3.run.xml} (70%) diff --git a/.run/Solver.run.xml b/.run/Solver 24 8 8 3 3.run.xml similarity index 70% rename from .run/Solver.run.xml rename to .run/Solver 24 8 8 3 3.run.xml index 062df0338..fa0a65273 100644 --- a/.run/Solver.run.xml +++ b/.run/Solver 24 8 8 3 3.run.xml @@ -1,8 +1,8 @@ - + - \ No newline at end of file +