VarHandle 显示以下错误 -
Exception in thread "main" java.lang.NoSuchMethodError: VarHandle.compareAndSet(VarHandleExample,int,int)void
at java.base/java.lang.invoke.MethodHandleNatives.newNoSuchMethodErrorOnVarHandle(MethodHandleNatives.java:492)
at java.base/java.lang.invoke.MethodHandleNatives.varHandleOperationLinkerMethod(MethodHandleNatives.java:445)
at java.base/java.lang.invoke.MethodHandleNatives.linkMethodImpl(MethodHandleNatives.java:378)
at java.base/java.lang.invoke.MethodHandleNatives.linkMethod(MethodHandleNatives.java:366)
at j9.VarHandleExample.update(VarHandleExample.java:23)
at j9.VarHandleExample.main(VarHandleExample.java:14)
我的程序是:
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
public class VarHandleExample {
public int publicTestVariable = 10;
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
VarHandleExample e= new VarHandleExample();
e.update();
}
public void update() throws NoSuchFieldException, IllegalAccessException {
VarHandle publicIntHandle = MethodHandles.lookup()
.in(VariableHandlesTest.class)
.findVarHandle(VarHandleExample.class, "publicTestVariable", int.class);
publicIntHandle.compareAndSet(this, 10, 100); // CAS
}
}
最佳答案
这似乎是 JVM/JDK/Spec/Doc 中的一个错误,这取决于编译器如何翻译签名多态方法的签名。
compareAndSet 标有@MethodHandle.PolymorphicSignature。这在语义上(释义)意味着在调用站点找到的任何签名都将用于调用该方法。这主要是为了防止参数的装箱。
VarHandle 中 compareAndSet 的完整签名是:
public final native
@MethodHandle.PolymorphicSignature
@HotSpotIntrinsicCandidate
boolean compareAndSet(Object... args);
请注意,它返回一个 boolean,但错误向我们显示 VM 正在尝试链接 VarHandle.compareAndSet(VarHandleExample,int,int)void,它有一个不同的返回类型。这也可以在 Eclipse 编译器生成的字节码中看到:
publicIntHandle.compareAndSet(this, 10, 100); // CAS
(部分)翻译为:
25: invokevirtual #55 // Method java/lang/invoke/VarHandle.compareAndSet:(LVarHandleExample;II)V
(注意那里的注释向我们展示了常量池中用于链接方法的方法引用常量的签名)
因此在运行时,VM 似乎会尝试找到一个以 V(即 void)作为返回类型的方法,而这实际上并不存在。
javac 另一方面生成这个签名:
25: invokevirtual #11 // Method java/lang/invoke/VarHandle.compareAndSet:(LVarHandleExample;II)Z
其中返回类型是 Z(意思是 boolean)而不是 V。
您可以通过使用返回值将返回类型显式设为 boolean 来解决此问题:
boolean b = publicIntHandle.compareAndSet(this, 10, 100); // CAS
或者在不需要该值的情况下使用空白 if:
if(publicIntHandle.compareAndSet(this, 10, 100)); // CAS
现在进入语言律师部分。
我能找到关于签名多态方法(标有@PolymorphicSignature 的方法)的有限信息 [ 1 ](语言规范中没有任何内容)。规范中的编译器应该如何派生和翻译签名多态方法的描述符似乎没有任何规定。
也许最有趣的是来自 jvms-5.4.3.3 的这段话(强调我的):
If C declares exactly one method with the name specified by the method reference, and the declaration is a signature polymorphic method (§2.9.3), then method lookup succeeds. All the class names mentioned in the descriptor are resolved (§5.4.3.1).
The resolved method is the signature polymorphic method declaration. It is not necessary for C to declare a method with the descriptor specified by the method reference.
在这种情况下,C 是 VarHandle,被查找的方法是 compareAndSet,描述符是 ( LVarHandleExample;II)Z 或 (LVarHandleExample;II)V 取决于编译器。
同样有趣的是关于 Signature Polymorphism 的 javadoc :
When the JVM processes bytecode containing signature polymorphic calls, it will successfully link any such call, regardless of its symbolic type descriptor. (In order to retain type safety, the JVM will guard such calls with suitable dynamic type checks, as described elsewhere.)
VarHandle 确实只有一个名为 compareAndSet 的方法,并且它是签名多态的,因此查找应该 成功。恕我直言,在这种情况下抛出异常是 VM 的问题,因为根据规范,描述符和返回类型应该无关紧要。
javac 发出 Z 作为描述符中的返回类型似乎也有问题。根据同一 javadoc 部分:
The unusual part is that the symbolic type descriptor is derived from the actual argument and return types, not from the method declaration.
但是,javac 发出的描述符肯定取决于方法声明。
所以根据规范/文档,这里似乎有 2 个错误;
在您正在使用的 VM 中,它错误地无法链接签名多态方法。我也可以使用 OpenJDK 64-Bit Server VM (build 13-internal+0-adhoc.Jorn.jdk, mixed mode, sharing) 重现它,这是最新的 OpenJDK 源。
在 javac 中发出错误的描述符返回类型。
我假设规范是主要权威,但在这种情况下似乎更可能是规范/文档在实现之后没有更新,这就是 eclipsec 的问题。
我的 email to jdk-dev 得到了回复, answered丹·史密斯。
对于 2.)
javac is correct here. See jls-15.12.3-400-B
"If the signature polymorphic method is either void or has a return type other than Object, the compile-time result is the result of the invocation type of the compile-time declaration"
The informal description in javadoc that you reference is incomplete, and I've filed a bug to fix that: https://bugs.openjdk.java.net/browse/JDK-8216511
所以看起来 Eclipse 正在为调用生成错误的描述符,而不是 javac。
对于 1.)
You are correct. A link-time NoSuchMethodError is not specified here. Rather, per the VarHandle javadoc, we should see a run-time WrongMethodTypeException.
Bug report: https://bugs.openjdk.java.net/browse/JDK-8216520
关于java.lang.NoSuchMethodError : VarHandle. compareAndSet(VariableHandlesExample,State,State)无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52962939/
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
我正在尝试使用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
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
我正在尝试使用Capistrano部署带有puma的Rails应用程序。在部署结束时它尝试运行bundleexecpumactl-S/home/deployer/production/shared/sockets/puma.state重启失败了/undefinedmethod`has_key?'forfalse:FalseClass.我只是为puma.state创建了一个空文件。我的问题是这个文件到底是什么,里面应该有什么? 最佳答案 Puma有一个状态文件,记录了进程的PID。如果你是第一次部署,你应该删除.state文件,然后做
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg
我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.