你是如何编译这个的?
对我来说,这听起来像是一个已知的 Eclipse 错误。
我刚刚检查了 javac 和这段代码:
import java.lang.invoke.*;
public class ScratchMH {
private static ScratchMH instance = null;
public ScratchMH() {
super();
}
private void run() throws Throwable {
Object x, y; String s; int i;
MethodType mt; MethodHandle mh;
MethodHandles.Lookup lookup = MethodHandles.lookup();
// mt is (char,char)String
mt = MethodType.methodType(String.class, char.class, char.class);
mh = lookup.findVirtual(String.class, "replace", mt);
s = (String) mh.invokeExact("daddy",'d','n');
// invokeExact(Ljava/lang/String;CC)Ljava/lang/String;
System.out.println(s);
}
public static void main(String[] args) throws Throwable {
instance = new ScratchMH();
instance.run();
}
}
似乎工作正常:
ariel-2:src boxcat$ javac scratch/clj/ScratchMH.java
ariel-2:src boxcat$ java scratch/clj/ScratchMH
nanny
ariel-2:src boxcat$
javap 输出的相关部分似乎也很正常:
35: invokevirtual #8 // Method java/lang/invoke/MethodHandles$Lookup.findVirtual:(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;
38: astore 6
40: aload 6
42: ldc #9 // String daddy
44: bipush 100
46: bipush 110
48: invokevirtual #10 // Method java/lang/invoke/MethodHandle.invokeExact:(Ljava/lang/String;CC)Ljava/lang/String;
51: astore_3