I would like to use the fastest way to retrieve value from a property.
Should I use:
public Object getValue(Object obj, Field field) {
field.setAccessible(true);
return field.get(obj);
}
or should I use:
public Object getValue(Object obj, Field field) throws Exception {
StringBuilder methodName = new StringBuilder();
methodName.append("get");
methodName.append(field.getName().substring(0, 1).toUpperCase());
methodName.append(field.getName().substring(1, field.getName().length()));
for (Method method : methods) {
if (method.getName().equals(methodName.toString())) {
return method.invoke(obj);
}
}
}
or should I use:
public Object getValue(Object obj, Field field) throws Throwable {
MethodHandles.Lookup lookup = MethodHandles.lookup();
StringBuilder methodName = new StringBuilder();
methodName.append("get");
methodName.append(field.getName().substring(0, 1).toUpperCase());
methodName.append(field.getName().substring(1, field.getName().length()));
for (Method method : methods) {
if (method.getName().equals(methodName.toString())) {
MethodHandle mh = lookup.findVirtual(obj.getClass(), methodName.toString(), MethodType.methodType(field.getType()));
return mh.invoke(obj);
}
}
}
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire