如何通过MethodHandles.lookup()获取所有声明的方法?如何获取所有声明的字段?
MethodHandle.invoke()、MethodHandle.invokeExact() 和 MethodHandle.invokeWithArguments()
另外,我将不胜感激为 Java 开发人员 使用 MethodHandle API 的教程。我强调,我是在静态类型语言纯旧 Java 上编程,我不是 JVM 开发人员,特别是我对整个字节码废话(invokedynamic)不感兴趣。我想弄清楚如何使用这个新 API 而不是 Java Core API。
EDITED-2:
@Glen Best 下面提供了一些引用我只想提供一个 http://www.oraclejavamagazine-digital.com/javamagazine/20130102?pg=52&search_term=methodhandle&doc_id=-1#pg50这正是我一直在寻找的。我发现实际上有一些新词汇。例如,target 实际上是指 MethodHandle(而不是要进行调度的对象),而 call site 实际上是代码“调用”“函数指针”又名 MethodHandle。此外,必须了解 MethodHandle API 不是核心反射 API 的替代品,而不是补充它。例如,您无法使用 MethodHandle “发现”所有方法,而您需要 Core Reflection API。但是当你“找到”你想要的方法时,你可以切换到 MethodHandle,例如,绑定(bind)它的一些参数或“改变”(适应)它的签名,例如可变参数。
已编辑:
我仍在试图找出答案。我写了一些测试,想和大家分享。
package alexander.berkovich;
import static org.junit.Assert.assertSame;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.BeforeClass;
import org.junit.Test;
public class MethodHandlerCleanTest {
public static MethodHandles.Lookup lookup;
@BeforeClass
public static void asetUp(){
lookup = MethodHandles.lookup();
}
public static class Check {
public void primitive(final int i){
}
public void wrapper(final Integer i){
}
}
@Test
public void testPrimitive() throws Throwable {
Check check = new Check();
MethodType type = MethodType.methodType(void.class, int.class);
MethodHandle mh = lookup.findVirtual(Check.class, "primitive", type);
mh.invokeWithArguments(check, 1);
mh.invoke(check, (short)2);
mh.invoke(check, Integer.valueOf(3));
Method method = Check.class.getMethod("primitive", int.class);
method.invoke(check, (short)20);
method.invoke(check, Integer.valueOf(21));
}
@Test
public void testWrapper() throws Throwable {
Check check = new Check();
MethodType type = MethodType.methodType(void.class, Integer.class);
MethodHandle mh = lookup.findVirtual(Check.class, "wrapper", type);
mh.invoke(check, 2);
Method method = Check.class.getMethod("wrapper", Integer.class);
method.invoke(check, 20);
}
@SuppressWarnings("unused")
public static class StaticInnerClass {
public static String staticName;
public String name;
public void foo(){}
public static void staticFoo(){}
}
@Test
public void testStaticInnerClassStaticField() throws Throwable {
MethodHandle mhSet = lookup.findStaticSetter(StaticInnerClass.class, "staticName", String.class);
String expected = "mama";
mhSet.invoke(expected);
MethodHandle mhGet = lookup.findStaticGetter(StaticInnerClass.class, "staticName", String.class);
Object obj = mhGet.invoke();
String value = (String)obj;
assertSame(expected, value);
}
@Test
public void testStaticInnerClassField() throws Throwable {
StaticInnerClass sut = new StaticInnerClass();
Field f = StaticInnerClass.class.getDeclaredField("name");
MethodHandle mhSetUnreflect = lookup.unreflectSetter(f);
String expectedUnreflect = "unreflect";
mhSetUnreflect.invoke(sut, expectedUnreflect);
MethodHandle mhSet = lookup.findSetter(StaticInnerClass.class, "name", String.class);
String expected = "mama";
mhSet.invoke(sut, expected);
MethodHandle mhGet = lookup.findGetter(StaticInnerClass.class, "name", String.class);
Object obj = mhGet.invoke(sut);
String value = (String)obj;
assertSame(expected, value);
}
@Test
public void testStaticInnerClassConstructor() throws Throwable {
StaticInnerClass sut = new StaticInnerClass();
MethodType type = MethodType.methodType(void.class);
MethodHandle mh = lookup.findConstructor(StaticInnerClass.class, type);
mh.invoke();
}
@Test
public void testStaticInnerClassMethod() throws Throwable {
StaticInnerClass sut = new StaticInnerClass();
MethodType type = MethodType.methodType(void.class);
MethodHandle mh = lookup.findVirtual(StaticInnerClass.class, "foo", type);
mh.invoke(sut);
}
@Test
public void testStaticInnerClassStaticMethod() throws Throwable {
MethodType type = MethodType.methodType(void.class);
MethodHandle mh = lookup.findStatic(StaticInnerClass.class, "staticFoo", type);
mh.invoke();
}
@SuppressWarnings("unused")
private class InnerClass {
public String name;
public void foo(){}
}
@Test
public void testInnerClassField() throws Throwable {
InnerClass sut = new InnerClass();
MethodHandle mhSet = lookup.findSetter(InnerClass.class, "name", String.class);
String expected = "mama";
mhSet.invoke(sut, expected);
MethodHandle mhGet = lookup.findGetter(InnerClass.class, "name", String.class);
Object obj = mhGet.invoke(sut);
String value = (String)obj;
assertSame(expected, value);
}
@Test
public void testInnerClassConstructor() throws Throwable {
MethodType type = MethodType.methodType(void.class, MethodHandlerCleanTest.class);
//default constructor is private
Field field = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
field.setAccessible(true);
MethodHandles.Lookup trustedLookup = (MethodHandles.Lookup)
field
.get(null);
MethodHandle mh = trustedLookup.findConstructor(InnerClass.class, type);
mh.invoke(this);
}
@Test
public void testInnerClassMethod() throws Throwable {
InnerClass sut = new InnerClass();
MethodType type = MethodType.methodType(void.class);
MethodHandle mh = lookup.findVirtual(InnerClass.class, "foo", type);
mh.invoke(sut);
}
}
最佳答案
How can I obtain all declared method through MethodHandles.lookup()? How can I obtain all declared fields?
将 java.lang.invoke 视为反射 (java.lang.reflect) 的(快速执行)扩展 - 即“调用”类依赖于“反射”类。
您通过反射(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);
What is difference betweeen MethodHandle.invoke(), MethodHandle.invokeExact() and MethodHandle.invokeWithArguments()?
MethodHandle对于非静态方法,提供给这些方法的第一个参数是 Class 的实例它声明了方法。该方法在该类的实例上调用(或在静态方法的类本身上)。如果 Class是非静态内部类,第二个参数是封闭/声明类的实例。后面的参数依次为方法签名参数。invokeExact 不对输入参数进行自动兼容类型转换。它要求参数值(或参数表达式)与方法签名的类型完全匹配,每个参数作为单独的参数提供,或者所有参数作为数组一起提供(签名:Object invokeExact(Object... args))。 invoke 要求参数值(或参数表达式)与方法签名的类型兼容 - 执行自动类型转换,每个参数作为单独的参数提供或所有参数作为数组一起提供(签名:对象调用(对象...参数)) invokeWithArguments 要求参数值(或参数表达式)与方法签名的类型兼容 - 执行自动类型转换,每个参数都在 List 中提供(签名:Object invokeWithArguments(List<?> arguments))I will be appreciate for tutorial about using 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/MethodHandles.Lookup.html
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.amazon.com/Well-Grounded-Java-Developer-techniques-programming/dp/1617290068
关于java - 关于 MethodHandle API 的一些基本问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16005824/
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我