Java 带有嵌套缩进的Eclipse toString()生成器
Eclipse有一个方便的模板,可以自动为类生成Java 带有嵌套缩进的Eclipse toString()生成器,java,eclipse,tostring,spring-tool-suite,Java,Eclipse,Tostring,Spring Tool Suite,Eclipse有一个方便的模板,可以自动为类生成toString()方法。您可以通过点击Alt+Shift+S并点击“生成toString()…”来访问它 从那里,您可以选择在派生的toString()中包含哪些字段,并设置其他选项以确定如何生成该字段 我想使用它为大量类快速生成toString()方法 举个例子,这里有一个歌曲类: public class Song { private String title; private int lengthInSeconds;
toString()
方法。您可以通过点击Alt
+Shift
+S
并点击“生成toString()…
”来访问它
从那里,您可以选择在派生的toString()
中包含哪些字段,并设置其他选项以确定如何生成该字段
我想使用它为大量类快速生成toString()
方法
举个例子,这里有一个歌曲
类:
public class Song {
private String title;
private int lengthInSeconds;
public Song(String title, int lengthInSeconds) {
this.title = title;
this.lengthInSeconds = lengthInSeconds;
}
// getters and setters...
}
这里有一个专辑
类,它包含一组歌曲
s:
public class Album {
private Song[] songs;
private int songCount;
public Album(Song[] songs) {
this.songs = songs;
this.songCount = songs.length;
}
//getters...
}
我目前使用此模板生成toString()
方法(使用“StringBuilder/StringBuffer-chained calls”选项):
我现在可以使用它为歌曲生成toString()
:
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("class Song {\n ");
if (title != null)
builder.append("title: ").append(title).append(",\n ");
builder.append("lengthInSeconds: ").append(lengthInSeconds).append("\n}");
return builder.toString();
}
对于相册
:
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("class Album {\n ");
if (songs != null)
builder.append("songs: ").append(Arrays.toString(songs)).append(",\n ");
builder.append("songCount: ").append(songCount).append("\n}");
return builder.toString();
}
//Song
@Override
public String toString() {
CustomToStringBuilder builder = new CustomToStringBuilder(this);
builder.appendItem("title", title).appendItem("lengthInSeconds", lengthInSeconds);
return builder.getString();
}
//Album
@Override
public String toString() {
CustomToStringBuilder builder = new CustomToStringBuilder(this);
builder.appendItem("songs", songs).appendItem("songCount", songCount);
return builder.getString();
}
现在,假设我创建了一个相册,并想测试它的toString()
,如下所示:
@Test
public void testToString() {
Song[] songs = new Song[] {
new Song("We Will Rock You", 200),
new Song("Beat it", 150),
new Song("Piano Man", 400) };
Album album = new Album(songs);
System.out.println(album);
}
class Album {
songs: [class Song {
title: We Will Rock You,
lengthInSeconds: 200
}, class Song {
title: Beat it,
lengthInSeconds: 150
}, class Song {
title: Piano Man,
lengthInSeconds: 400
}],
songCount: 3
}
这就是我得到的:
class Album {
songs: [class Song {
title: We Will Rock You,
lengthInSeconds: 200
}, class Song {
title: Beat it,
lengthInSeconds: 150
}, class Song {
title: Piano Man,
lengthInSeconds: 400
}],
songCount: 3
}
但我想要的是一个可以嵌套缩进的生成器,如下所示:
@Test
public void testToString() {
Song[] songs = new Song[] {
new Song("We Will Rock You", 200),
new Song("Beat it", 150),
new Song("Piano Man", 400) };
Album album = new Album(songs);
System.out.println(album);
}
class Album {
songs: [class Song {
title: We Will Rock You,
lengthInSeconds: 200
}, class Song {
title: Beat it,
lengthInSeconds: 150
}, class Song {
title: Piano Man,
lengthInSeconds: 400
}],
songCount: 3
}
如果每个对象中有更多的类,那么继续这样做,如果这有意义的话
在调用其toString()
之前,我尝试创建一个方法,用4个空格和一个换行符替换换行符:
其想法是它可以将append中的“\n”
转换为“\n”
等等,但我不确定是否可以在eclipse模板中调用函数
有人知道怎么做吗?我已经检查了文档,但它是相当稀疏的。我也看了一遍,但我没有看到任何类似的问题
作为参考,我特别使用了Spring工具套件4.0.1版Release。这不是我希望的方式,但我确实有一个解决方案。通过创建一个
下面是我创建的类:
/*
* Helper class to generate formatted toString() methods for pojos
*/
public class CustomToStringBuilder {
private StringBuilder builder;
private Object o;
public CustomToStringBuilder(Object o) {
builder = new StringBuilder();
this.o = o;
}
public CustomToStringBuilder appendItem(String s, Object o) {
builder.append(" ").append(s).append(": ").append(toIndentedString(o)).append("\n");
return this;
}
public String getString() {
return "class " + o.getClass().getSimpleName() + "{ \n" + builder.toString() + "}";
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private static String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
然后我可以使用Alt
+Shift
+S
->“生成toString()…”和“选择自定义toString()生成器””并选择我的自定义toString builder
。有了它,Eclipse为歌曲
和专辑
生成以下代码:
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("class Album {\n ");
if (songs != null)
builder.append("songs: ").append(Arrays.toString(songs)).append(",\n ");
builder.append("songCount: ").append(songCount).append("\n}");
return builder.toString();
}
//Song
@Override
public String toString() {
CustomToStringBuilder builder = new CustomToStringBuilder(this);
builder.appendItem("title", title).appendItem("lengthInSeconds", lengthInSeconds);
return builder.getString();
}
//Album
@Override
public String toString() {
CustomToStringBuilder builder = new CustomToStringBuilder(this);
builder.appendItem("songs", songs).appendItem("songCount", songCount);
return builder.getString();
}
将所有这些放在一起并再次运行我的测试可以得到我想要的结果:
class Album{
songs: [class Song{
title: We Will Rock You
lengthInSeconds: 200
}, class Song{
title: Beat it
lengthInSeconds: 150
}, class Song{
title: Piano Man
lengthInSeconds: 400
}]
songCount: 3
}
但是,如果可能的话,我仍然希望使用一种不需要向源代码中添加新类的方法,因此我将把这个问题保留一两天,看看是否有人可以找到一种不同的方法来更轻松地完成它。在toString()
中缩进相册中的缩进:数组.toString(歌曲)。替换(“\n”,“\n”)
(而不是Arrays.toString(songs)
)。@howlger但我如何在eclipse生成器模板中做到这一点?事实上,我现在有了一个解决方案,我会在今晚晚些时候有机会的时候发布。它需要创建一个自定义toString()生成器。我更愿意单独使用模板,但我不确定这是否可行。