Consider a field weight
in class Animal
. I want to be able to create a getter and setter functional interface objects for manipulating this field.
class Animal {
int weight;
}
My current approach is similar to one used for methods:
public static Supplier getter(Object obj, Class<?> cls, Field f) throws Exception {
boolean isstatic = Modifier.isStatic(f.getModifiers());
MethodType sSig = MethodType.methodType(f.getType());
Class<?> dCls = Supplier.class;
MethodType dSig = MethodType.methodType(Object.class);
String dMthd = "get";
MethodType dType = isstatic? MethodType.methodType(dCls) : MethodType.methodType(dCls, cls);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle fctry = LambdaMetafactory.metafactory(lookup, dMthd, dType, dSig, lookup.unreflectGetter(f), sSig).getTarget();
fctry = !isstatic && obj!=null? fctry.bindTo(obj) : fctry;
return (Supplier)fctry.invoke();
}
But this gives the following error:
java.lang.invoke.LambdaConversionException: Unsupported MethodHandle kind: getField x.Animal.weight:()int
UPDATE
I am trying to create a class ObjectMap
implementing interface Map
, which basically tries to represent an object as a Map
, where the object can be of any type. Was currently using Field.get()
and Field.set()
for manipulating fields in get()
and put()
methods, and using above mentioned approach to create Supplier
and Consumer
objects for invoking getter and setter methods. I was wondering if i could merge the two separate methods into one.
Example class which could be used as a Map
through ObjectMap
:
public class ThisCanBeAnything {
/* fields */
public String normalField;
private int hiddenFiled;
private String hiddenReadonlyField;
/* getters and setters */
public int hiddenField() {
return hiddenField;
}
public void hiddenField(int v) {
System.out.println("set: hiddenField="+v);
hiddenField = v;
}
public String hiddenReadonlyField() {
return hiddenReadonlyField;
}
}
And here is the expected usage:
Object o = new ThisCanBeAnything();
Map m = new ObjectMap(o);
m.put("normalField", "Normal");
System.out.println(m.get("normalField")); // Normal
m.put("hiddenField", 1); // set: hiddenField=1
System.out.println(m.get("hiddenField")); // 1
m.put("hiddenReadonlyField", 1); // does not do anything
System.out.println(m.get("hiddenReadonlyField")); // null
Functional style lets you think about such things in new ways. Instead of a reflection-based approach like
try something like
which you would invoke like
Is
a -> a.weight
any harder than coming up with aField
via reflection?One advantage is that you could use fields or methods as needed, e.g., if you added a getter for weight,
A similar setter factory might be
Invoked like this
I know it's a late answer, but I have developed a library that you can use to turn any
MethodHandle
into a lambda function. The performance is the same as if you would manually implement the function with direct access.The impl is based around the fact that static final
MethodHandle
s are being inlined to point of being as fast as direct access. More info on this can be found here: How can I improve performance of Field.set (perhap using MethodHandles)?The library can be found here: https://github.com/LanternPowered/Lmbda. For now you will have to use Jitpack to access it (small library so it won't take long to compile): https://jitpack.io/#LanternPowered/Lmbda
An example for setting a field on a object:
And getting a field from a object:
You can’t bind a
MethodHandle
bearing a direct access to a field to a function interface instance, but you can bind the accessor method of theField
instance:Though in this specific example you may consider generating an
IntSupplier
instead:…
You are making it too difficult that it needs to be. When you have a
Field
, you can directly invokeunreflectGetter
on the lookup factory to retrieve aMethodHandle
:This returns a supplier of the value of the field. Depending on the accessibility of the field, you might need to invoke
setAccessible(true)
.Note that method handles and the reflection API also differs in terms of performance and might be faster.
You can directly write the lambda, you don't need the
LambdaMetafactory
at all:Or a runtime-typesafe version:
e.g.: