Using Google Guava's Objects.ToStringHelper

前端 未结 6 520
生来不讨喜
生来不讨喜 2021-01-30 10:36

I used ToStringBuilder.reflectionToString(class) in commons-lang, to implement toString() for simple DTOs. Now I\'m trying to use Google Guava instead

相关标签:
6条回答
  • 2021-01-30 10:59

    Of the available Eclipse plugins, guavaeclipse is still using MoreObjects.toStringHelper, but Jenerate uses MoreObjects.toStringHelper and works like a charm.

    Guava docs

    0 讨论(0)
  • 2021-01-30 11:00

    I have a little trick for Guava's com.google.common.base.MoreObjects.toStringHelper(). I configured IntelliJ IDEA to use it when auto-generating toString() methods. I assume you can do the same in Eclipse. Here's how to do it in Intellij:

    • go inside a class
    • hit Alt + Insert to popup the "Generate" menu
    • choose toString()
    • click the "Settings" button
    • go to the "Templates" tab
    • create a new template named "Guava's MoreObjects.toStringHelper()" (I did it by copying the "ToStringBuilder" template)
    • change the template to:

      public String toString() {
      #set ($autoImportPackages = "com.google.common.base.MoreObjects")
          return MoreObjects.toStringHelper(this)
      #foreach ($member in $members)
          .add("$member.name", $member.accessor)
      #end
          .toString();
      }
      
    • save the template, close the "Settings" and "Generate toString()" windows

    • you can now choose the Guava's MoreObjects.toStringHelper() template when generating toString() methods

    When you add a new field to the class, simply re-generate the toString() method (IDEA will ask you to confirm that you want to replace the existing toString() method).

    0 讨论(0)
  • 2021-01-30 11:01

    It is worth noting that Objects.toStringHelper has been deprecated (to be removed completely in June 2016) in favor of MoreObjects.toStringHelper. I have copied the default Guava template in my Intellij IDE into a new one that uses the MoreObjects instead. Cheers.

    Guava docs

    0 讨论(0)
  • 2021-01-30 11:02

    MoreObjects.toStringHelper is intended to help you write toString() methods with a consistent format easily, but it gives you control over what fields you include in toString() and should have performance comparable to writing it out manually. reflectionToString is shorter to type, but it doesn't give you explicit control over the included fields and, well, it uses reflection. I don't see it as a better alternative.

    As a side note, I think using toStringHelper looks a lot cleaner if you put one add call per line.

    Guava docs

    0 讨论(0)
  • 2021-01-30 11:08

    There is a plugin http://sourceforge.net/projects/guavaeclipse/ (really small one) which can generate toString methods (and equals hashcode as well) using Guava classes. This is a nice solution because generated methods are really small and do not clutter the class.

    0 讨论(0)
  • 2021-01-30 11:14

    In eclipse you can create a template (not as powerfull as IntelliJ https://stackoverflow.com/a/9445402/1301197 ). It will not loop across all member fields for you but you get at least the surrounding code

    windows > preferences > Java > Editor > Templates

    ${:import(com.google.common.base.MoreObjects)}
    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
        .add("${field}",${field})
        .toString();
    }
    

    This will add the import and you will get something like this if you enter id as the field. Then up to you to add the remaining fields.

    public String toString()
    {
        return MoreObjects.toStringHelper(this).add("id", id).toString();
    }
    

    Note that there is probably a better solution by using eclipse toString() generator and creating a custom toString() builder. But this is too much work for a lazy man like me.

    Right click then source > generate toString() and select Custom toString() Builder inside code style.

    0 讨论(0)
提交回复
热议问题