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

3

How do I customize the Generate toString() function of 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 case:

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

I use the Neon.3 version

Thank you guys for the feedback, but I'll express myself better. I need to configure Generate ToString to generate the full path code (which is displayed in package) in the java class.

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

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

How to customize it to generate the 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 the full path with the name: com.etc.model.Customer It looks like I have to create a new format template, I have analyzed it through #

    

asked by anonymous 30.09.2017 / 04:48

2 answers

2

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

@Override
public String toString() {
    return this.getClass().getName()+" [id=" + id + "]";
}
    
30.09.2017 / 04:56
1

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

In% w / o, click the Edit ... button and change the default template that should look like this:

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

To:

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

This String.format will be replaced in code by ${object.getClassName} , which will return the fully qualified class name ( recommended reading < a>), including the packet hierarchy. Other arguments will display members and methods that the class has.

See the following example with the toString () method being executed in this way:

Ifyouwantmorecustomizationthanthis,Irecommendthatyouread Generate toString () dialog (Eclipse documentation) , because it seems to be something deeper in IDE settings.

    
30.09.2017 / 04:57