Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is possible to use toString from a super class without "callSuper=true"? #1234

Closed
rn4n opened this issue Nov 14, 2016 · 3 comments
Closed

Is possible to use toString from a super class without "callSuper=true"? #1234

rn4n opened this issue Nov 14, 2016 · 3 comments

Comments

@rn4n
Copy link

rn4n commented Nov 14, 2016

@ToString
class SuperClass {
   private @Getter @Setter int id;
   private @Getter @Setter String date;
}
@ToString(callSuper = true)
class MyClass extends SuperClass {
   private @Getter @Setter String field;
}

Calling toString from MyClass results in this output:

MyClass(super=SuperClass(id=1, date=00/00/0000), field=test)

Is possible to get something like this...

MyClass(id=1, date=00/00/0000, field=test)

... directly, without manipulate the output string?


cheers!

@rzwitserloot
Copy link
Collaborator

Not really; we'd have to do resolution to pull that off, and we don't want to go there for the lombok basics, because resolution is very tricky (performance issues and far more bugs).

@rn4n rn4n closed this as completed Jun 9, 2017
@rn4n rn4n reopened this Jan 17, 2019
@rn4n rn4n closed this as completed Jan 17, 2019
@Maaartinus
Copy link
Contributor

Now, it is sort of possible using something like

@ToString(callSuper = false)
class MyClass extends SuperClass {
   @ToString.Include(name="id")
    private int dummyId() {
        return id;
    }

   @ToString.Include(name="date")
    private int dummyDate() {
        return date;
    }

   private @Getter @Setter String field;
}

This is obviously only doable for a small number of fields (untested).

A better idea might be

@ToString(callSuper = false)
class MyClass extends SuperClass {
   @ToString.Include(name=" ")
    private int dummySuper() {
        return super.toString().replace(".*?\\(((?s:.*))\\)$", "$1");
    }

   private @Getter @Setter String field;
}
  • Using " " as the name may or mayn't work.
  • In "replace", I'm assuming that super.toString() is (or looks like) lombok-generated.
  • You get MyClass( id=1, date=00/00/0000, field=test) with one blank too much.

@mxmlnglt
Copy link

mxmlnglt commented Jun 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants