如何在Java 9中使用新的BeanInfo批注 [英] How to use new BeanInfo Annotations in Java 9

查看:148
本文介绍了如何在Java 9中使用新的BeanInfo批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JEP 256:BeanInfo注释 提供了 JavaBean 所以这个:

public class Person {

    private String name ;

    public String getName( ) {
        return this.name ;
    }

    public void setName( String nameArg ) {
        this.name = nameArg ;
    }

}

...将变成这样:

import java.beans.BeanProperty;

public class Person {

    @BeanProperty
    public String name ;

}

但是,当我在IntelliJ 2017.2.2的Java 9项目中尝试此操作时,在IDE上出现"@"注释的错误,提示:

Yet when I try this in a Java 9 project in IntelliJ 2017.2.2, I get error in the IDE on the "@" annotation saying:

"@ BeanProperty"不适用于字段

'@BeanProperty' not applicable to field

编译器报告错误:

错误:(8,5)java:注释类型不适用于这种声明

Error:(8, 5) java: annotation type not applicable to this kind of declaration

➠我是否误解了这些新注释的目的?还是我有一些语法问题?

➠ Have I misunderstood the purpose of these new annotations? Or do I have some syntax problem?

除了上面链接的JEP和JavaDoc外,我没有找到其他文档.

I have not found any documentation other than the JEP and JavaDoc linked above.

我正在尝试使用Java 9的最新候选版本,目前是macOS Sierra 10.12.6上的Java 9 + 181.

I am experimenting with the recent release candidates for Java 9, currently Java 9+181 on macOS Sierra 10.12.6.

推荐答案

javadoc 表示BeanProperty@Target(METHOD).看起来这是自定义 PropertyDescriptor BeanInfo 实现.我认为它不是要像 龙目岛 一样工作. (感谢谢天谢地,出于显式方法的所有原因,请参见为什么使用getter和setter?是个好主意.)

The javadoc says BeanProperty is @Target(METHOD). Looks like it’s a way to customize PropertyDescriptors without having to create a BeanInfo implementation. I don’t think it was intended to work like Lombok. (And thank goodness—see Why use getters and setters? for all the reasons explicit methods are a good idea.)

这篇关于如何在Java 9中使用新的BeanInfo批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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