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

Custom getters inside builder #464

Closed
stijnb1234 opened this issue Jul 15, 2023 · 5 comments
Closed

Custom getters inside builder #464

stijnb1234 opened this issue Jul 15, 2023 · 5 comments

Comments

@stijnb1234
Copy link

I have a class like this:

public abstract class Vehicle {
  public abstract String getTypeId();
  
  public Type getType() {
    //TODO Call stuff to get the Type
  }

  public abstract Builder toBuilder();
  public static class Builder extends Vehicle_Builder {}
}

How can I get the getType() function inside the generated builder too? I won't like to define it twice.

@alicederyn
Copy link
Collaborator

This is not currently supported. FWIW, I don't think this is the right ownership for this behaviour. The typeId to Type conversion belongs to the Type class; Vehicle should just store a Type object, and not expose its conversion functions. It's too tightly coupled.

@stijnb1234
Copy link
Author

Ah right, I see. And I have a List with a getter and a getter with a class parameter (generics) to get the parts with that class. Sadly I can't create getters with a parameter. How can I do that?

@alicederyn
Copy link
Collaborator

alicederyn commented Jul 15, 2023

That's also not supported I'm afraid! I'm not sure how that would work tbh. How would the value depend on the type? Wouldn't it be the other way around? Perhaps you could paste a code snippet of how you would implement it by hand.

@stijnb1234
Copy link
Author

That's also not supported I'm afraid! I'm not sure how that would work tbh. How would the value depend on the type? Wouldn't it be the other way around? Perhaps you could paste a code snippet of how you would implement it by hand.

abstract class Part {}

class Wheel extends Part {}
class Seat extends Part {}

And then:

class Vehicle {
  List<Part> parts;

  public <V> V getPart(@NotNull Class<V> partClass) {
        return parts.stream().filter(partClass::isInstance).map(partClass::cast).findFirst().orElse(null);
  }
}

@alicederyn
Copy link
Collaborator

That's very bespoke, I can't imagine this being used enough to put into a code generator. It might be a use-case for issue #14 if that ever gets implemented.

@stijnb1234 stijnb1234 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 23, 2023
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

2 participants