简体   繁体   English

是否可以在Java 9中注释lambda表达式?

[英]Will it be possible to annotate lambda expression in Java 9?

This question is now over 3 years old and specifically addressed Java 8, with the accepted answer also citing the Java SE 8 Final Specification . 这个问题现在已经超过3年了,并且专门针对Java 8,接受的答案也引用了Java SE 8最终规范

I would be interested if something regarding this question will change in Java 9: Is there any way to annotate a lambda expression similar to annotating a corresponding anonymous class? 如果在Java 9中有关于这个问题的内容会发生变化,我会感兴趣:有没有办法注释类似于注释相应匿名类的lambda表达式?


Example: 例:

Annotation: 注解:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
public @interface MyTypeAnnotation {
    public String value();
}

Working annotation of anonymous class: 匿名类的工作注释:

Consumer<String> consumer = new @MyTypeAnnotation("Hello ") Consumer<String>() {
    @Override
    public void accept(String str) {
        System.out.println(str);
    }
};

Annotating a lamba expression, currently not working in Java 8: 注释lamba表达式,目前在Java 8中不起作用:

Consumer<String> myAnnotatedConsumer = @MyTypeAnnotation("Hello") (p -> System.out.println(p));

The existence of a Stackoverflow question is not sufficient to indicate that such a feature is planned, not even that someone is ever considering it. Stackoverflow问题的存在不足以表明这样的功能是有计划的,甚至没有人在考虑它。

If you look at the list of JEPs , you will see that there is no such JEP, not even in a draft state, suggesting such a feature. 如果查看JEP 列表 ,您会看到没有这样的JEP,即使在草稿状态下也没有这样的建议。

Also, if you look at the current state of Java 9's LambdaMetafactory , you will see that no change has been made to support passing the meta information that would be necessary to generate a runtime class with recorded annotation data. 此外,如果您查看Java 9的LambdaMetafactory的当前状态,您将看到没有进行任何更改以支持传递生成具有记录的注释数据的运行时类所必需的元信息。

There seems to be some desire to add plenty of meta-information to what actually should be a small piece of throw-away code, but I doubt that the language designer will follow it. 似乎有一些想要添加大量的元信息到实际上应该是一小块丢失的代码,但我怀疑语言设计师会遵循它。 Lambda expressions are meant to define a function which encapsulates behavior , not an alternative way to describe an anonymous class. Lambda表达式用于定义封装行为的函数,而不是描述匿名类的替代方法。 The long term evolution will rather lead to lambda expression implementations which are even less of an ordinary class. 长期演变将导致lambda表达式实现,甚至更少是普通类。

it's interesting that they do annotate the inner class that "represents" the lambda expression in InnerClassLambdaMetafactory.spinInnerClass via : 有趣的是,他们通过以下方式注释“代表” InnerClassLambdaMetafactory.spinInnerClass的lambda表达式的内部类:

 mv.visitAnnotation("Ljava/lang/invoke/LambdaForm$Hidden;", true)

But that is annotating a class obviously, not a lambda per-se. 但这显然是在诠释一个阶级,而不是一个lambda本身。

Annotating a lambda would require changes to the invokedynamic and implicitly the LambdaMetafactory as far as I can see - and what would happen when invokedynamic would not create a class for the lambda, but something else , what would happen to those annotations? 注释拉姆达将需要改变invokedynamic和隐含的LambdaMetafactory据我可以看到-当invokedynamic 不会为拉姆达创建一个类,但别的东西 ,会发生什么样的注解,会发生什么?

According to the What's new in Oracle JDK 9 page, No. This has not changed in Java 9. 根据Oracle JDK 9页面中的新功能,请注意 ,这在Java 9中没有改变。

Of course, that is not definitive, but the JLS for Java 9 has not been released yet. 当然,这不是确定的,但Java 9的JLS尚未发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM