我已经从GitHub导入了类似这样的代码:
文件1:Settings.java
import lombok.Builder;
import lombok.Data;
import org.apache.pdfbox.pdmodel.font.PDFont;
@Data
@Builder
public class Settings {
private PDFont font;
private Integer fontSize;
//code
}
现在这是它的用法
文件2:
private Settings settings = Settings.builder()
.font(DEFAULT_FONT)
.fontSize(DEFAULT_FONT_SIZE)
.build();
请帮我用我绝对不想使用Lombok的简单代码将这两个部分转换。
答案 0 :(得分:2)
由于Settings类中只有两个字段,因此您可以创建如下的构造函数-
public class Settings {
private PDFont font;
private Integer fontSize;
public Settings(PDFont font, Integer fontSize) {
this.font = font;
this.fontSize = fontSize;
}
public PDFont getPDFont() {
return this.font;
}
public Integer getFontSize() {
return this.fontSize;
}
}
用法-
private Settings settings = new Settings(DEFAULT_FONT, DEFAULT_FONT_SIZE);
答案 1 :(得分:2)
如果您使用的是IntelliJ,并且已经安装了Lombok插件,则可以右键单击编辑器,依次转到Refactor
和Delombok
。它在大多数情况下都有效,但有时无效。我不建议您这样做,这会使代码更丑陋,并迫使您将库作为项目中的代码而非依赖项检入。只需使用Lombok,这是一个很棒的插件。
答案 2 :(得分:1)
如果我很了解您需要做的Delombok(通过使用插件或命令行),然后将生成的代码(用于构建器等)复制到您的代码库中。关于@Data
注释,建议您使用IDE的生成功能
答案 3 :(得分:1)
这是一个完整的示例,其中包含展开的代码。
public class Settings {
private PDFont font;
private Integer fontSize;
private Settings(Builder builder) {
this.font = builder.font;
this.fontSize = builder.fontSize;
}
public PDFont getFont() {
return this.font;
}
public void setFont(PDFont font) {
this.font = font;
}
public Integer getFontSize() {
return this.fontSize;
}
public void setFontSize(Integer fontSize) {
this.fontSize = fontSize;
}
@Override public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != getClass()) return false;
Settings other = (Settings)obj;
return java.util.Objects.equals(this.font, other.font)
&& java.util.Objects.equals(this.fontSize, other.fontSize);
}
@Override public int hashCode() {
return Objects.hash(this.font, this.fontSize);
}
@Override public String toString() {
return "Settings{font="+this.font+",fontSize="+this.fontSize+"}";
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
PDFont font;
Integer fontSize;
private Builder() {
}
public Builder font(PDFont font) {
this.font = font;
return this;
}
public Builder fontSize(Integer fontSize) {
this.fontSize = fontSize;
return this;
}
public Settings build() {
return new Settings(this);
}
}
// Code
}
答案 4 :(得分:0)
检查其网站:Project Lombok
每个Lombok注释都有示例和比较,包括:
使用特定注释时,您可以检查Lombok生成了哪些代码。此外,还有一个 “香草Java” 示例,或者是“ With Lombok” 与“ Vanilla Java” 部分
答案 5 :(得分:0)
这是您的“ Delombocked” Settings
课程。
public class Settings {
private PDFont font;
private Integer fontSize;
@java.beans.ConstructorProperties( {"font", "fontSize"})
Settings(PDFont font, Integer fontSize) {
this.font = font;
this.fontSize = fontSize;
}
public static SettingsBuilder builder() {
return new SettingsBuilder();
}
public PDFont getFont() {
return this.font;
}
public Integer getFontSize() {
return this.fontSize;
}
public void setFont(PDFont font) {
this.font = font;
}
public void setFontSize(Integer fontSize) {
this.fontSize = fontSize;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Settings)) {
return false;
}
final Settings other = (Settings) o;
if (!other.canEqual((Object) this)) {
return false;
}
final Object this$font = this.getFont();
final Object other$font = other.getFont();
if (this$font == null ? other$font != null : !this$font.equals(other$font)) {
return false;
}
final Object this$fontSize = this.getFontSize();
final Object other$fontSize = other.getFontSize();
if (this$fontSize == null ? other$fontSize != null : !this$fontSize.equals(other$fontSize)) {
return false;
}
return true;
}
protected boolean canEqual(final Object other) {
return other instanceof Settings;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $font = this.getFont();
result = result * PRIME + ($font == null ? 43 : $font.hashCode());
final Object $fontSize = this.getFontSize();
result = result * PRIME + ($fontSize == null ? 43 : $fontSize.hashCode());
return result;
}
public String toString() {
return "Main.Settings(font=" + this.getFont() + ", fontSize=" + this.getFontSize() + ")";
}
public static class SettingsBuilder {
private PDFont font;
private Integer fontSize;
SettingsBuilder() {
}
public Settings.SettingsBuilder font(PDFont font) {
this.font = font;
return this;
}
public Settings.SettingsBuilder fontSize(Integer fontSize) {
this.fontSize = fontSize;
return this;
}
public Settings build() {
return new Settings(font, fontSize);
}
public String toString() {
return "Main.Settings.SettingsBuilder(font=" + this.font + ", fontSize=" + this.fontSize + ")";
}
}
//code
}