Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a Functional interface implementation for Fields?

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
like image 822
wolfram77 Avatar asked Dec 06 '22 18:12

wolfram77


2 Answers

You are making it too difficult that it needs to be. When you have a Field, you can directly invoke unreflectGetter on the lookup factory to retrieve a MethodHandle:

Produces a method handle giving read access to a reflected field. The type of the method handle will have a return type of the field's value type. If the field is static, the method handle will take no arguments. Otherwise, its single argument will be the instance containing the field.

public static Supplier<Object> getter(Object obj, Class<?> cls, Field f) {
    f.setAccessible(true);
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    return () -> {
        try {
            MethodHandle handle = lookup.unreflectGetter(f);
            return Modifier.isStatic(f.getModifiers()) ? handle.invoke() : handle.invoke(obj);
        } catch (Throwable t) {
            throw new IllegalArgumentException(t);
        }
    };
}

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.

like image 80
Tunaki Avatar answered Dec 09 '22 14:12

Tunaki


Functional style lets you think about such things in new ways. Instead of a reflection-based approach like

Supplier getter(Object obj, Class<?> cls, Field f){...}

try something like

static <O,F> Supplier<F> getter(O obj, Function<O,F> extractor) {
    return () -> extractor.apply(obj);
}

which you would invoke like

Supplier<Integer> getWeight = getter(animal, a -> a.weight);
Integer weight = getWeight.get();

Is a -> a.weight any harder than coming up with a Field via reflection?

One advantage is that you could use fields or methods as needed, e.g., if you added a getter for weight,

Supplier<Integer> getWeight = getter(animal, Animal::getWeight);

A similar setter factory might be

static <O,F> Consumer<F> setter(O obj, BiConsumer<O,F> modifier) {
    return field -> modifier.accept(obj,field);
}

Invoked like this

Consumer<Integer> setWeight = setter(animal, (a, w) -> a.weight = w);
setWeight.accept(90);
like image 22
Hank D Avatar answered Dec 09 '22 13:12

Hank D