Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有嵌套缩进的Eclipse toString()生成器_Java_Eclipse_Tostring_Spring Tool Suite - Fatal编程技术网

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

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;

Eclipse有一个方便的模板,可以自动为类生成
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()生成器。我更愿意单独使用模板,但我不确定这是否可行。