每个GitHub 存储库都有一个描述 字段和一个可选的网站 字段。如何使用AJAX访问(任何 GitHub 存储库的)这些字段?
有两种方法我试图获得给定函数的MethodHandle.
方法1
Method m = MyClass.class.getMethod("myMethod", String.class, Map.class);
MethodHandle mh = MethodHandles.lookup().unreflect(m);
Run Code Online (Sandbox Code Playgroud)
方法2
MethodType type = MethodType.methodType(void.class, String.class, Map.class);
MethodHandle mh = MethodHandles.lookup().findVirtual(MyClass.class, "myMethod", type);
Run Code Online (Sandbox Code Playgroud)
它们之间有什么区别?
考虑weightclass 中的一个字段Animal。我希望能够创建用于操作此字段的getter和setter功能接口对象。
class Animal {
int weight;
}
Run Code Online (Sandbox Code Playgroud)
我目前的方法类似于用于方法的方法:
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(); …Run Code Online (Sandbox Code Playgroud)