带有嵌套缩进的Eclipse toString()生成器



Eclipse有一个方便的模板,可以自动为类生成toString()方法。您可以通过点击Alt+Shift+S并点击"Generate toString()..."来访问它

在那里,您可以选择要包含在派生toString()中的字段,并设置其他选项来确定应该如何生成它。

我想用它为大量的类快速生成toString()方法。

举个例子,这里有一个Song类:

public class Song {
private String title;
private int lengthInSeconds;
public Song(String title, int lengthInSeconds) {
this.title = title;
this.lengthInSeconds = lengthInSeconds;
}
// getters and setters...
}

这里有一个Album类,它包含Songs:的数组

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"选项):

class ${object.className} {
${member.name}: ${member.value},
${otherMembers}
}

我现在可以使用它为Song:生成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();
}

对于Album

@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();
}

现在,假设我创建了一张专辑,并想像这样测试它的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
}

如果有意义的话,在每个对象中有更多类的情况下继续这样做。

我试图制作一个方法,在调用其toString():之前,可以用4个空格和一个换行符替换一个换行字符

private String indentString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("n", "n    ");
}

这个想法是,它可以将append中的"n "转换为"n "等等,但我不确定是否可以在eclipse模板中调用函数。

有人知道怎么做吗?我已经检查了文档,但它相当稀疏。看起来也很好,但我没有看到任何类似的问题。

作为参考,我特别使用了SpringToolSuite版本4.0.1RELEASE.

这不是我希望的方式,但我确实有一个解决方案。我能够通过创建一个自定义的toString()构建器类来实现我想要的

这是我创建的类:

/*
* 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->"生成字符串()…"one_answers">选择自定义字符串()生成器",然后选择我的CustomToStringBuilder。这样,Eclipse为SongAlbum生成以下代码:

//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
}

然而,如果可能的话,我仍然更喜欢一种不需要在源代码中添加新类的方法,所以我会把这个问题留一两天,看看是否有人能找到一种更容易的不同方法。

最新更新