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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Implementing a default toString method with AspectJ

Implementing a default toString method with AspectJ

Thibault Delor user avatar by
Thibault Delor
·
Jul. 05, 12 · Interview
Like (0)
Save
Tweet
Share
9.66K Views

Join the DZone community and get the full member experience.

Join For Free

Overview

AspectJ has some real powerful usages. One is to inject a method inside a set of classes. Most of the time, I implement the toString() method using the useful reflectionToString method of Apache common. So, I have decided to define an Aspect that will add the toString method to all my classes.

 

The Aspect

public aspect ToString {
    private interface ReflectiveToString {}
    declare parents : com.invalidcodeexception.experiment.aspectj.* implements ReflectiveToString;

    public String ReflectiveToString.toString(){
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}

With AspectJ you cannot define a method implemtation for a set of classes directly, but you can do it for an interface, and so all inherited classes! Therefore, I first declare the ReflectiveToString interface; Then, I make all classes that I want to modify inheriting this interface; Finally, I add the toString implementation to all classes implementing ReflectiveToString. That's it! Now, all classes inside the package com.invalidcodeexception.experiment.aspectj and its sub-package will have a nice implementation of toString!

 

 Test

package com.invalidcodeexception.experiment.aspectj;

public class MyEntity {
    private String name;

    public MyEntity(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        MyEntity e = new MyEntity("hello");
        System.out.println("print entity : " + e.toString());
    }
}
 print entity : MyEntity[name=hello] 

Inheritance and overriding

The interesting point is that, you can override the toString methods in your classes. So the aspect acts like a default implementation. The only limitation is when one of your classes extends a class not concerned by your aspects which already define toString. You are facing a kind of multiple inheritance problem. In this case you will end with a "inter-type declaration" compilation error. Your best option is to simply define the toString method in the concerned class and calling super.toString()
AspectJ

Published at DZone with permission of Thibault Delor. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Choose the Right Streaming Database
  • 5 Software Developer Competencies: How To Recognize a Good Programmer
  • 11 Observability Tools You Should Know
  • Solving the Kubernetes Security Puzzle

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: