有没有一种方法可以将方法引用用于jshell中的顶级功能? [英] Is there a way to use method references for top-level functions in jshell?

查看:67
本文介绍了有没有一种方法可以将方法引用用于jshell中的顶级功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在jshell中这样做:

Suppose I do this in jshell:

jshell> void printIsEven(int i) {
   ...>     System.out.println(i % 2 == 0);
   ...> }
|  created method printIsEven(int)

jshell> List<Integer> l = Arrays.asList(7,5,4,8,5,9);
l ==> [7, 5, 4, 8, 5, 9]

jshell> l.forEach(/* ??? */); // is it possible to use a method reference here?

在普通程序中,我可以在名为MyClass的类的非静态上下文中编写l.forEach(this::printIsEven)或在静态上下文中编写l.forEach(MyClass::printIsEven).

In a normal program I could write l.forEach(this::printIsEven) in a non-static context or l.forEach(MyClass::printIsEven) in the static context of a class named MyClass.

在jshell中使用this::printIsEven不起作用,因为jshell在静态上下文中执行语句,但是您不能使用静态方法引用,因为没有类名可前缀::printIsEven,而尝试l.forEach(::printIsEven)只是语法错误.

Using this::printIsEven in jshell doesn't work because jshell executes statements in a static context, but you can't use a static method reference because there's no class name to prefix ::printIsEven, and trying l.forEach(::printIsEven) is just a syntax error.

推荐答案

您可以为此创建一个新类:

You can create a new class for that:

jshell> class Foo { static void printIsEven(int i) {
   ...>     System.out.println(i % 2 == 0);
   ...> }}
|  created class Foo

jshell> Arrays.asList(1,2,3).forEach(Foo::printIsEven)
false
true
false

从技术上讲,它不再是顶级功能,而是可以达到预期的效果.

Technically it is no longer a top-level function, but it achieves the desired effect.

现在,如果您知道并且仍然想引用顶级方法...

Now, if you knew that and still want to reference top-level methods...

据我所知,为外壳保留状态"的顶级类"是jdk.jshell.JShell,但是jdk.jshell.JShell::printIsEven会导致Error: invalid method reference.您已经提到过不可能将顶级方法设为静态(Modifier 'static' not permitted in top-level declarations, ignored).

As far as I can tell, the "top-level class" that holds "state" for the shell is jdk.jshell.JShell, but jdk.jshell.JShell::printIsEven results in Error: invalid method reference. And you already mentioned it's not possible to make top-level methods static (Modifier 'static' not permitted in top-level declarations, ignored).

快速浏览 JEP 后,这似乎是故意的.而且,实际上是从上面提到的在新类中定义静态方法" .

After a quick look at the JEP, it seems intentional. And it actually mentions the "define-static-method-in-new-class" approach from above.

我正在猜测顶级类"需要特殊的魔术才能能够重新定义方法&其他顶级声明,其局限性可能源于JVM自身的局限性,即在运行时重新定义类/方法的能力. 来源很有趣,但我无法从中得出有意义的答案.

I'm guessing the top-level "class" needs special magic to be able to redefine methods & other top-level declarations, and the limitations might derive from the JVM's own limitations in its ability to redefine classes/methods at runtime. The source is interesting but I'm not able to derive a meaningful answer from that.

所以,我有点被带走了.这是你的错.
我仍然认为无法在jshell中获得对顶级方法的方法引用,但是...我之前对可能是错误原因的猜测.

So, I kinda got carried away. This is your fault.
I still think it's not possible to obtain a method reference to a top-level method in jshell, but... my previous guess about the reasons why is probably wrong.

以下内容显示,在jshell中,当您重新定义"一个类时,旧的类仍然存在:评估上下文只是移动了一些映射来解析对新类定义的进一步引用.

The following shows that in jshell, when you "redefine" a class, the old class is still there: the evaluation context just shifts some mappings to resolve further references to the new class definition.

jshell> class A { static int v=1; void m() { System.out.println(getClass() + " v=" + v); } }
|  created class A

jshell> new A().m()
class REPL.$JShell$11$A v=1

// Changing static value of "v"
jshell> class A { static int v=2; void m() { System.out.println(getClass() + " v=" + v); } }
|  modified class A

// Actually not modified, this is still the same class (and as a result the static init of v has not been reexecuted, so, still 1)
jshell> new A().m()
class REPL.$JShell$11$A v=1

// Let's add a boolean field to change the structure
jshell> class A { static int v=3; boolean x=false; void m() { System.out.println(getClass() + " v=" + v); } }
|  replaced class A

// Notice new class name:
jshell> new A().m()
class REPL.$JShell$11B$A v=3

// But old version is still there, only hidden a bit by evaluation context:
jshell> Class.forName("REPL.$JShell$11$A").getDeclaredField("v").getInt(null)
$7 ==> 1

jshell> Class.forName("REPL.$JShell$11B$A").getDeclaredField("v").getInt(null)
$8 ==> 3

所以这个小小的演示表明它与JVM内部用于类重新定义没有任何关系,因为这里没有发生此类事情.

So this little demo suggests it has nothing to do with JVM internals for class redefinition, because no such thing happens here.

然后我想查看所有已加载类的列表:

Then I wanted to see the list of all loaded classes:

jshell> Class c = Thread.currentThread().getContextClassLoader().getClass()
c ==> class jdk.jshell.execution.DefaultLoaderDelegate$RemoteClassLoader

jshell> while (c != java.lang.ClassLoader.class) { c = c.getSuperclass(); System.out.println(c); }
class java.net.URLClassLoader
class java.security.SecureClassLoader
class java.lang.ClassLoader

jshell> c.getDeclaredField("classes").setAccessible(true)
|  java.lang.reflect.InaccessibleObjectException thrown: Unable to make field private final java.util.Vector java.lang.ClassLoader.classes accessible: module java.base does not "opens java.lang" to unnamed module @7494e528
|        at AccessibleObject.checkCanSetAccessible (AccessibleObject.java:337)
|        at AccessibleObject.checkCanSetAccessible (AccessibleObject.java:281)
|        at Field.checkCanSetAccessible (Field.java:175)
|        at Field.setAccessible (Field.java:169)
|        at (#26:1)

啊,是的,Java 9模块...该死的:)

Ah, yes, Java 9 modules... dammit :)

哦,今天就到此为止.

这篇关于有没有一种方法可以将方法引用用于jshell中的顶级功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆