Skip to content

Commit

Permalink
Fix static property binding in moulconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed Jan 17, 2024
1 parent 4c64f95 commit ccdceb7
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T>() {
@SneakyThrows
@Override
Expand All @@ -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<T>() {

@SneakyThrows
Expand All @@ -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);
}
}

0 comments on commit ccdceb7

Please sign in to comment.