From ccdceb7db8d3a1ec6766a6656f87062fe2821076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linnea=20Gr=C3=A4f?= Date: Wed, 17 Jan 2024 20:44:17 +0100 Subject: [PATCH] Fix static property binding in moulconfig --- .../moulconfig/xml/XMLBoundProperties.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/io/github/moulberry/moulconfig/xml/XMLBoundProperties.java b/common/src/main/java/io/github/moulberry/moulconfig/xml/XMLBoundProperties.java index d72e861f2..8e9e7daf5 100644 --- a/common/src/main/java/io/github/moulberry/moulconfig/xml/XMLBoundProperties.java +++ b/common/src/main/java/io/github/moulberry/moulconfig/xml/XMLBoundProperties.java @@ -10,6 +10,7 @@ import java.lang.invoke.MethodHandles; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Map; import java.util.function.Consumer; @@ -88,7 +89,7 @@ public void set(T newValue) { throw new IllegalArgumentException("Bind target " + method + " is of the wrong type " + method.getReturnType() + " instead of " + clazz); if (method.getParameterCount() != 0) throw new RuntimeException("Bind target " + method + " is not a pure getter"); - MethodHandle unreflect = lookup.unreflect(method).bindTo(object); + var unreflect = bindSometimes(lookup.unreflect(method), method.getModifiers(), object); return new GetSetter() { @SneakyThrows @Override @@ -105,8 +106,8 @@ public void set(T newValue) { if (!TypeUtils.doesAExtendB(field.getType(), clazz)) throw new IllegalArgumentException("Bind target " + name + " is of the wrong type " + field.getType() + " instead of " + clazz); field.setAccessible(true); - var getter = lookup.unreflectGetter(field).bindTo(object); - var setter = lookup.unreflectSetter(field).bindTo(object); + var getter = bindSometimes(lookup.unreflectGetter(field), field.getModifiers(), object); + var setter = bindSometimes(lookup.unreflectSetter(field), field.getModifiers(), object); return new GetSetter() { @SneakyThrows @@ -122,4 +123,10 @@ public void set(T newValue) { } }; } + + private static MethodHandle bindSometimes(MethodHandle methodHandle, int modifiers, Object object) { + if (Modifier.isStatic(modifiers)) + return methodHandle; + return methodHandle.bindTo(object); + } }