- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
import java.lang.invoke.*;
public class InvokeDynamicDemo {
public static double doubleIt(double d){
System.out.print("Doubling it");
return d*2;
}
public static void main(String[] args) throws Throwable {
MethodHandles.Lookup lookUp = MethodHandles.lookup();
MethodHandle doubleIt = lookUp.findStatic(InvokeDynamicDemo.class, "doubleIt", MethodType.methodType(double.class,double.class));
doubleIt.invokeExact(2.0D); // Exception
//doubleIt.invoke(2.0D); // No exception thrown
}
}
Exception in thread "main" java.lang.invoke.WrongMethodTypeException: expected (double)double but found (double)void at java.lang.invoke.Invokers.newWrongMethodTypeException(Invokers.java:340) at java.lang.invoke.Invokers.checkExactType(Invokers.java:351) at InvokeDynamicDemo.main(InvokeDynamicDemo.java:32)
这段代码有什么问题,我不明白。请帮忙。
最佳答案
问题是您没有使用 invokeExact
方法的结果。我以前没有见过这个方法,但看起来 Java 编译器必须以一种非常特殊的方式来处理它。来自 MethodHandle
documentation :
As is usual with virtual methods, source-level calls to invokeExact and invoke compile to an invokevirtual instruction. More unusually, the compiler must record the actual argument types, and may not perform method invocation conversions on the arguments. Instead, it must generate instructions that push them on the stack according to their own unconverted types. The method handle object itself is pushed on the stack before the arguments. The compiler then generates an
invokevirtual
instruction that invokes the method handle with a symbolic type descriptor which describes the argument and return types.To issue a complete symbolic type descriptor, the compiler must also determine the return type. This is based on a cast on the method invocation expression, if there is one, or else
Object
if the invocation is an expression, or elsevoid
if the invocation is a statement. The cast may be to a primitive type (but notvoid
).
目前,您在不使用结果的情况下调用该方法,因此编译器推断您希望它是一个 void
方法 - 因此是 (double)void
部分异常。
如果您将调用更改为:
double result = (double) doubleIt.invokeExact(2.0);
...那么编译器就知道您期望的返回类型,并可以创建适当的符号类型描述符。
关于java - MethodHandle invokeExact 一个带有返回值和参数的静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47843569/
如何为像 int[]::new 这样的数组构造函数获取 MethodHandle? 这行不通: public static void main(String[] args) throws Throwa
我有一个项目在 Android Studio 或 Gradle 中构建时开始抛出此错误: com.android.tools.r8.ApiLevelException: MethodHandle.in
我正在尝试在我的 Android 应用程序中使用 SOAP 服务。我正在尝试关注 this guide .当我尝试运行我的应用时出现错误: Error: MethodHandle.invoke and
我正在 Android 中开发一个应用程序,我想在其中使用热敏打印机打印收据上的图像。 为了完成此任务,我在我的应用程序中添加了 escpos-coffee 包。 打印正在使用的图像导入java.im
我正在开发 android 原生应用程序,我已经为他们准备好了 图书馆项目包含这些应用程序的所有常用工具。 我正在使用 jfrog artifactory 导入我的库,并且运行良好;现在我想使用 gi
我有一个应用程序,当我将 android studio 更新到 3.4.2 版本并更新 gradle 时,我不得不强制更改一些库,例如 androidx 用于 appcompatibility 和注释
我正在开发一个 Android 应用程序(在我很新的 Android Studio 中),其中包含许多导入的外部 .jar的,模拟器中的开发很顺利,但是当我开始构建APK时,我得到了以下错误: FAI
实现 'org.apache.poi:poi:5.0.0' 当我使用 Run 'app' 在模拟器/手机上运行时,上述依赖项工作正常,但是 当我尝试构建 apk 时,出现以下错误。 com.andro
java.lang.invoke.MethodHandle 和 friend 允许通过一系列 combinators 生成适合 JIT 的运行时代码. 如何在 MethodHandles 中创建局部变
我正在努力思考如何有效地使用 MethodHandles,让我失望的一件事是尝试在调试期间执行 MethodHandles。这是一些示例代码,说明了我的问题。 public class Metric
是否可以将方法引用(例如 SomeClass::someMethod)转换为 MethodHandle 实例?我想要编译时检查的好处(确保类和方法存在)以及使用 MethodHandle API 来检
我正在尝试用 MethodHandle 替换反射调用,但可变参数似乎无法处理。 我的反射调用器目前看起来像这样: public class Invoker { private final Me
我正在研究 JDK 1.7 的新功能,但我无法理解 MethodHandle 的设计用途?我理解(直接)调用静态方法(以及在这种情况下直接使用核心反射 API)。我也理解(直接)调用虚拟方法(非静态,
假设我在给定类型(类/接口(interface))中有三个方法: public void foo(Integer integer); public void foo(Number number); p
鉴于此代码: MethodType mt = MethodType.methodType(void.class, DomainObject.class); NOOP_METHOD = RULE_MET
使用 Java 1.8.0_92,尝试运行 MethodHandles 的示例。 filterArguments() ,抛出以下异常: Exception in thread "main" java.
import java.lang.invoke.*; public class InvokeDynamicDemo { public static double doub
我有一个 Java 7 代码,我正在使用 MethodHanlde .代码是: import java.lang.invoke.MethodHandle; import java.lang.invok
我对方法句柄的方法参数感到困惑。我首先构建了一个guardwithtest方法句柄,如下所示: public class App { public static void trueTarget
MethodHandles.Lookup.defineClass在运行时从字节数组生成一个新类。 返回类在什么情况下可以被垃圾回收?它是否在与 Lookup 对象关联的类加载器的生命周期内保留,或者如
我是一名优秀的程序员,十分优秀!