栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

有关MethodHandle API的一些基本问题

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有关MethodHandle API的一些基本问题

如何通过MethodHandles.lookup()获取所有声明的方法? 如何获取所有声明的字段?

将java.lang.invoke视为反射(java.lang.reflect)的(快速执行)扩展-即“ invoke”类依赖于“
reflection”类。

  • 您可以通过反射(java.lang.Class和java.lang.reflect)获得对所有方法/构造函数/字段的引用:
        java.lang.Class<?> someClass = ...;  // obtain a Class somehow    // Returns all constructors/methods/fields declared in class,     // whether public/protected/package/private,     // but does NOT include definitions from any ancestors:    java.lang.reflect.Constructor<?>[] declaredConstructors = someClass.getDeclaredConstructors();    java.lang.reflect.Method[] declaredMethods = someClass.getDeclaredMethods();    java.lang.reflect.Field[] declaredFields   = someClass.getDeclaredFields();    // Returns all constructors/methods/fields declared as public members     // in the class AND all ancestors:     java.lang.reflect.Constructor<?>[] publicInheritedConstructors = someClass.getConstructors();    java.lang.reflect.Method[] publicInheritedMethods = someClass.getMethods();    java.lang.reflect.Field[] publicInheritedFields   = someClass.getFields();
  • 您可以通过java.lang.invoke.MethodHandles.Lookup将它们转换为MethodHandles:
        java.lang.invoke.MethodType mt;     java.lang.invoke.MethodHandle mh;    java.lang.invoke.MethodHandles.Lookup lookup = MethodHandles.lookup();    // process methods    for (java.lang.reflect.Method method: declaredMethods) {        mh = lookup.unreflect(method);        // can call mh.invokeExact (requiring first parameter to be the class'         // object instance upon which the method will be invoked, followed by         // the methodparameter types, with an exact match parameter and return         // types) or        // mh.invoke/invokeWithArguments (requiring first parameter to be the         // class' object instance upon which the method will be invoked,         // followed by the method parameter types, with compatible conversions         // performed on input/output types)    }    // process constructors    for (java.lang.reflect.Constructor<?> constructor: declaredConstructors) {        mh = lookup.unreflectConstructor(constructor);        // can call mh.invokeExact or        // mh.invoke/invokeWithArguments     }    // process field setters    for (java.lang.reflect.Field field: declaredFields) {        mh = lookup.unreflectSetter(field);        // can call mh.invokeExact or        // mh.invoke/invokeWithArguments     }    // process field getters    for (java.lang.reflect.Field field: declaredFields) {        mh = lookup.unreflectGetter(field);        // can call mh.invokeExact or        // mh.invoke/invokeWithArguments     }
  • 您可以通过java.lang.reflect确定方法/构造函数/字段的签名:
        // If generics involved in method signature:    Type[] paramTypes = method.getGenericParameterTypes();     Type returnType = method.getGenericReturnType();     // Note: if Class is non-static inner class, first parameter of     // getGenericParameterTypes() is the enclosing class    // If no generics involved in method signature:    Class<?>[] paramTypes = declaredMethod.getParameterTypes();     Class<?> returnType = declaredMethod.getReturnType();     // Note: if Class is non-static inner class, first parameter of     // getParameterTypes() is the enclosing class    // Same method calls for declaredConstructor
  • 您可以通过java.lang.reflect确定方法/构造函数/字段是否是静态的:
    int modifiers = method.getModifiers();  // same method for constructor/field

    boolean isStatic = java.lang.Modifier.isStatic(modifiers);

MethodHandle.invoke(),MethodHandle.invokeExact()和MethodHandle.invokeWithArguments()之间有什么区别?

  • 参见http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html#invoke%28java.lang.Object...%29
  • 如果
    MethodHandle
    表示非静态方法,则提供给这些方法的第一个参数是
    Class
    声明该方法的实例。在该类的实例上调用该方法(对于静态方法,则在Class本身上)。如果
    Class
    s是非静态内部类,则第二个参数是封闭/声明类的实例。后续参数依次是方法签名参数。
  • invokeExact
    不会对输入参数进行自动兼容的类型转换。它要求参数值(或参数表达式)是与方法签名完全匹配的类型,每个参数作为单独的参数提供,或者所有参数作为数组一起提供(签名:)
    Object invokeExact(Object... args)
  • invoke
    要求参数值(或参数表达式)类型兼容以匹配方法签名-执行自动类型转换,每个参数作为单独的参数提供,或所有参数作为数组一起提供(签名:Object invoke(Object .. 。args))
  • invokeWithArguments
    需要的参数值(或参数表达式)是类型兼容匹配方法签名-自动类型转换被执行,以列表内提供的每个参数(签名:
    Object invokeWithArguments(List<?> arguments)

对于使用MethodHandle API for Java devloper的教程,我将不胜感激。

不幸的是,那里没有太多东西。您可以尝试以下方法。希望我已经在上面给出了足够的信息:^)

[http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html](http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html)  [http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandles.Lookup.html](http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandles.Lookup.html)  [http://www.java7developer.com/blog/?p=191](http://www.java7developer.com/blog/?p=191)  [http://www.oraclejavamagazine-digital.com/javamagazine/20130102?pg=52&search_term=methodhandle&doc_id=-1#pg50](http://www.oraclejavamagazine-digital.com/javamagazine/20130102?pg=52&search_term=methodhandle&doc_id=-1#pg50)  [http: //www.amazon.com/Well-Grounded-Java-Developer-techniques-programming/dp/1617290068](https://rads.stackoverflow.com/amzn/click/com/1617290068)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414026.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号