Customize the eclipse "Generate toString()..." to print the path of a class

Asked

Viewed 399 times

4

How do I customize the function Generate toString() eclipse (source > Generate toString()) to print the path of a class?

For example I have the following entity that prints:

package com.etc.model;

@Entity
@Table(name="CLIENTE")
public class Cliente implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ID")
    private int id;

@Override
    public String toString() {
        return "Cliente [id=" + id + "]";
    }

How do I configure generate toString() to exit with the full path? In this case:

@Override
    public String toString() {
        return "com.etc.model.Cliente [id=" + id + "]";
    }

I use the Neon.3 version


Thanks personally for the feedback, but I’ll express myself better. I need to set up Generate Tostring for generate code with full path (what is visualized in package) in the java class.

In the example shown above it generates showing the name of the Client class:

@Override
    public String toString() {
        return "Cliente [id=" + id + "]";
    }

How to customize it to generate code with the full path (the path is shown in the package line):

@Override
    public String toString() {
        return "com.etc.model.Cliente [id=" + id + "]";
    }

See that the full path with the name: with.etc.model.Client It seems that I have to create a new format template, I analyzed it through the documentation but I couldn’t get to what I need.

  • None of the answers served you? Mine I tested and worked normally.

  • Thanks @Articuno . They helped me.

2 answers

2

The simplest way to do something close to this is to change the default template in the IDE settings by going to Source-> Generate toString()...

In String.format, click on the button Edit... and change the default template that should look like this:

${object.className} [${member.name()}=${member.value}, ${otherMembers}]

for:

${object.getClassName} [${member.name()}=${member.value}, ${otherMembers}]

That one ${object.getClassName} shall be replaced by getClass().getName(), that will return the full name of the class(recommended reading), including the package hierarchy. Other arguments will display members and methods that the class has.

See an example below with the toString() method generated in this way being executed:

inserir a descrição da imagem aqui

If you want more customization than this, I recommend you take a look at Generate toString() dialog(Eclipse documentation), because it seems to be something deeper in the IDE’s settings.

  • Thanks for the answer, I noticed that in the Generatetostring() default template the ${Object.classname} variable results in the generation of code showing only the class name, as if it were a getSimpleName(). I need it to generate with the full name (with the class hierarchy, in the case of your example Generatetostring should generate:@Override
 public String toString() {
 return "br.com.exemplo.TestClass [id=" + id + "]";
 }

1

Simple, just use the method getName(). Behold:

@Override
public String toString() {
    return this.getClass().getName()+" [id=" + id + "]";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.