DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3

Trending

  • When Incentives Sabotage Product Strategy
  • Modernize Your IAM Into Identity Fabric Powered by Connectors
  • Parallel Data Conflict Resolution in Enterprise Workflows: Pessimistic vs. Optimistic Locking at Scale
  • Rust: The Must-Adopt Language for Modern Software Development
  1. DZone
  2. Coding
  3. Frameworks
  4. Replacing commons-lang ToStringBuilder With Eclipse

Replacing commons-lang ToStringBuilder With Eclipse

Want to get the benefit of Apache's commons-lang ToStringBuilder class, but don't want to add the dependency? Here's how to do it with Eclipse.

By 
Gabriel Belingueres user avatar
Gabriel Belingueres
·
Mar. 12, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.1K Views

Join the DZone community and get the full member experience.

Join For Free

Recently, I was modifying a common library and I wanted to create a toString method with some (but not all) of the fields of the object.

This can be easily done using the Apache commons-lang ToStringBuilder class. A quick way to generate a toString() method is using the reflectionToString method (though it will include all the object's fields in the result):

@Override
public String toString() {
  return ToStringBuilder.reflectionToString(this);
}


You can customize the fields to include by using something like this:

@Override
public String toString() {
    return new ToStringBuilder(this)
        .append("field1", field1)
        .append("field2", field2)
        .append("field3", field3)
            .toString();
}


This is straightforward. However, as simple a solution as it is, it requires us to include Apache commons-lang as a dependency for your library, a price that you may not want to pay for some util components.

Luckily, Eclipse has a useful functionality to generate a toString() method for your class. The default key binding is Alt-Shift-S + S, which will open a dialog box:


This will generate a toString() method according to the <Default template>:

@Override
public String toString() {
    return "MyClass [sessionID=" + sessionID + ", usuario=" + usuario + ", nombre=" + nombre + "]";
}


This is good, but not enough for my particular requirements, as I had unit tests that check that the "roles" field is not included in the toString() method, which matched the toString() method produced by commons-lang ToStringBuilder:

@Test
public void testToStringWithoutRoles() {
  MyClass object = new MyClass();
  object.setNombre("Gabriel Belingueres");
  object.setSessionID("abcdefg");
  object.setUsuario("gbe");

  HashSet<String> roles = new HashSet<String>();
  roles.add("rol1");
  roles.add("rol2");
  roles.add("rol3");
  object.setRoles(roles);

  String expected = "com.hexaid.myapp.MyClass@" + Integer.toHexString(object.hashCode())
        + "[sessionID=abcdefg,usuario=gbe,nombre=Gabriel Belingueres]";

  assertEquals(expected, object.toString());
}


Lickily, Eclipse allowed me to easily change the String format template to create one that matched that of ToStringBuilder, pressing the "New..." button and entering:

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


When regenerating the toString() method, tests turned green again and allowed me to remove the commons-lang dependency from the project.

Eclipse

Opinions expressed by DZone contributors are their own.

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: