Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><em>How can I obtain all declared method through MethodHandles.lookup()? How can I obtain all declared fields?</em></p> </blockquote> <p>Think of java.lang.invoke as a (fast performing) extension to reflection (java.lang.reflect) - i.e. "invoke" classes are dependent upon "reflection" classes.</p> <ul> <li><p>You obtain reference to all methods/constructors/fields via reflection (java.lang.Class and java.lang.reflect):</p> <pre><code>java.lang.Class&lt;?&gt; 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&lt;?&gt;[] 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&lt;?&gt;[] publicInheritedConstructors = someClass.getConstructors(); java.lang.reflect.Method[] publicInheritedMethods = someClass.getMethods(); java.lang.reflect.Field[] publicInheritedFields = someClass.getFields(); </code></pre></li> <li><p>You convert them to MethodHandles via java.lang.invoke.MethodHandles.Lookup:</p> <pre><code>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&lt;?&gt; 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 } </code></pre></li> <li><p>You can determine if the signature of methods/constructors/fields via java.lang.reflect:</p> <pre><code>// 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&lt;?&gt;[] paramTypes = declaredMethod.getParameterTypes(); Class&lt;?&gt; returnType = declaredMethod.getReturnType(); // Note: if Class is non-static inner class, first parameter of // getParameterTypes() is the enclosing class // Same method calls for declaredConstructor </code></pre></li> <li><p>You can determine if the methods/constructors/fields are static via java.lang.reflect:</p> <pre><code>int modifiers = method.getModifiers(); // same method for constructor/field boolean isStatic = java.lang.Modifier.isStatic(modifiers); </code></pre></li> </ul> <blockquote> <p><em>What is difference betweeen MethodHandle.invoke(), MethodHandle.invokeExact() and MethodHandle.invokeWithArguments()?</em></p> </blockquote> <ul> <li>see <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html#invoke%28java.lang.Object...%29" rel="nofollow noreferrer">http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html#invoke%28java.lang.Object...%29</a></li> <li>If the <code>MethodHandle</code> is for a non-static method, the first parameter provided to these methods is an instance of the <code>Class</code> which declares the method. The method is invoked on this instance of the class (or on the Class itself for static methods). If the <code>Class</code> is a non-static inner class, the second parameter is an instance of the enclosing/declaring class. The subsequent parameters are the method signature parameters, in order.</li> <li><strong><code>invokeExact</code></strong> does not do automatic compatible type conversion on input parameters. It requires parameter values (or parameter expressions) to be an exact type match to the method signature, with each parameter provided as a separate argument OR all arguments provided together as an array (signature: <code>Object invokeExact(Object... args)</code>). </li> <li><strong><code>invoke</code></strong> requires the parameter values (or parameter expressions) to be type compatible match to the method signature - automatic type conversions are performed, with each parameter provided as a separate argument OR all arguments provided together as an array (signature: Object invoke(Object... args)) </li> <li><strong><code>invokeWithArguments</code></strong> requires the parameter values (or parameter expressions) to be type compatible match to the method signature - automatic type conversions are performed, with each parameter provided within a List (signature: <code>Object invokeWithArguments(List&lt;?&gt; arguments)</code>) </li> </ul> <blockquote> <p><em>I will be appreciate for tutorial about using MethodHandle API for Java devloper</em></p> </blockquote> <p>There's not much out there, unfortunately. You could try the following. Hopefully, I've given enough info above :^)</p> <p><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html" rel="nofollow noreferrer">http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html</a><br> <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandles.Lookup.html" rel="nofollow noreferrer">http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandles.Lookup.html</a><br> <a href="http://www.java7developer.com/blog/?p=191" rel="nofollow noreferrer">http://www.java7developer.com/blog/?p=191</a><br> <a href="http://www.oraclejavamagazine-digital.com/javamagazine/20130102?pg=52&amp;search_term=methodhandle&amp;doc_id=-1#pg50" rel="nofollow noreferrer">http://www.oraclejavamagazine-digital.com/javamagazine/20130102?pg=52&amp;search_term=methodhandle&amp;doc_id=-1#pg50</a><br> <a href="http://rads.stackoverflow.com/amzn/click/1617290068" rel="nofollow noreferrer">http://www.amazon.com/Well-Grounded-Java-Developer-techniques-programming/dp/1617290068</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload