Is onConstructor property of the Lombok's @[No|All|Required]ArgsConstructor annotation work Java 7 style only?

huangapple go评论75阅读模式
英文:

Is onConstructor property of the Lombok's @[No|All|Required]ArgsConstructor annotation work Java 7 style only?

问题

根据文档,Lombok有3个用于生成构造函数的注解:

  • @NoArgsConstructor - 生成一个空构造函数;
  • @AllArgsConstructor - 生成一个初始化所有字段的构造函数;
  • @RequiredArgsConstructor - 生成一个只初始化final字段的构造函数。

它们都有一个onConstructor属性,允许您指定用于标记生成的构造函数的注解。

根据Javadoc,此功能的语法取决于JDK版本(与此无关;这是为了解决javac的错误)。

在JDK7之前:

@NoArgsConstructor(onConstructor=@__({@AnnotationsGoHere}))

从JDK8开始:

@NoArgsConstructor(onConstructor_={@AnnotationsGohere}) // 注意在onConstructor之后有一个下划线

我正在使用JDK8。然而,只有JDK7的变体对我起作用,而JDK8的变体不起作用(生成没有注解的构造函数)。

我在JDK11上进行了检查 - 结果相同。

我使用Refactor -> Delombok -> @Constructors进行了检查。

例如,像这样:

@AllArgsConstructor(onConstructor = @__(@Deprecated))
public class SomeClass {
}

将生成以下代码:

public class SomeClass {
    @Deprecated
    public SomeClass() {
    }
}

但像这样:

@AllArgsConstructor(onConstructor_ = @Deprecated)
public class SomeClass {
}

将生成如下代码:

public class SomeClass {
    public SomeClass() {
    }
}

我注意到Lombok网站上的文档只包含一个JDK7样式的示例。

Javadoc是否不正确还是我做错了什么?

英文:

According to the documentation, Lombok has 3 annotations for constructor generation:

  • @NoArgsConstructor - generates an empty constructor;
  • @AllArgsConstructor - generates a constructor that initializes all
    fields;
  • @RequiredArgsConstructor - generates a constructor that
    initializes only final fields.

They all have an onConstructor property that allows you to specify the annotations with which the generated constructor should be marked.

According to the Javadoc, the syntax for this feature depends on JDK version (nothing we can do about that; it's to work around javac bugs).

Up to JDK7:

@NoArgsConstructor(onConstructor=@__({@AnnotationsGoHere}))

From JDK8:

@NoArgsConstructor(onConstructor_={@AnnotationsGohere}) // note the underscore after onConstructor

I am working on JDK8. However, only the JDK7 variant works for me, while the JDK8 variant does not work (a constructor without annotations is generated).

I checked on JDK11 - same result.

I check with Refactor -> Delombok -> @Constructors.

For example, like this:

@AllArgsConstructor(onConstructor = @__(@Deprecated))
public class SomeClass {
}

the following code is generated:

public class SomeClass {
    @Deprecated
    public SomeClass() {
    }
}

But like so:

@AllArgsConstructor(onConstructor_ = @Deprecated)
public class SomeClass {
}

code like this is generated:

public class SomeClass {
    public SomeClass() {
    }
}

I noticed that the documentation on the Lombok site only contains a JDK7 style example.

The Javadoc is incorrect or am I doing something wrong?

答案1

得分: 0

我发现,并不是 Lombok 的 bug,而是 Lombok IntelliJ 插件 的 bug。

构造函数的注解会添加在编译后的代码中。

Lombok IntelliJ 插件的 Delombok 工具会错误地将 Lombok 的注解转换为普通的 Java 代码。

英文:

I found, that it's not the Lombok's bug, it's Lombok IntelliJ plugin bug.

Constructor annotations add in compiled code.

Delombok tool of Lombok IntelliJ plugin incorrect convert Lombok's annotations to vanilla Java code.

huangapple
  • 本文由 发表于 2020年8月21日 17:07:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63519839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定