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

Covariant builders - copy builder methods from superclass, with narrow return type #455

Closed
blizznets opened this issue Nov 30, 2015 · 8 comments
Milestone

Comments

@blizznets
Copy link
Contributor

In case I have, for example, super class for all my responses with such schema (Response.json):

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "error": {
      "type": "integer"
    }
  }
}

and some response that extends it (SomeResponse.json):

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "type": "object",
  "additionalProperties": false,
  "extens": {
    "$ref": "Response.json"
  }
  "properties": {
    "whatEver": {
      "type": "string"
    }
  }
}

builder method "withError" will be generated for Response only.
So I've got compilation error when tried to do something like this:

new SomeResponse().withError(0).withWhatEver("somethnig");

But if withError override in each subclass - it will work.

public class SomeResponse extends Response{
    ...

    public SomeResponse withError(Integer error) {
        super.setError(error);
        return this;
    }

}

Is it possible to fix it?
Thanks a lot so helpfull tool!

@blizznets blizznets changed the title Builder methods not overrides form super type Sub type not override builder methods Nov 30, 2015
@blizznets blizznets changed the title Sub type not override builder methods Subtype not override builder method Nov 30, 2015
@joelittlejohn joelittlejohn changed the title Subtype not override builder method Covariant builders - copy builder methods from superclass, with narrow return type Nov 30, 2015
@joelittlejohn
Copy link
Owner

This sounds possible. I'm interested to know if this is something people regularly run in to, as no-one has mentioned this in the past.

Rather than trying to find the setters, the simplest implementation would probably be:

  • For each withXxx method in the superclass, introduce a withXxx method on the subclass
  • The implementation in the subclass would:
    • call super.withXxx, passing the arg straight through
    • cast the returned value to narrow the type

@blizznets
Copy link
Contributor Author

@joelittlejohn Thanks for reply.
Just one question: m.b. better just return "this", and ignore result of calling super.withXxx(), with out type casting?

@joelittlejohn
Copy link
Owner

Even better, I like it!

On 1 December 2015 at 09:48, blizznets notifications@github.com wrote:

@joelittlejohn https://github.com/joelittlejohn Thanks for reply.
Just one question: m.b. better just return "this", and ignore result of
calling super.withXxx(), with out type casting?


Reply to this email directly or view it on GitHub
#455 (comment)
.

@blizznets
Copy link
Contributor Author

@joelittlejohn great! )
I'll waiting for new version with fix!

@joelittlejohn
Copy link
Owner

@blizznets I'm happy to accept a pull request if you want to fix this.

@blizznets
Copy link
Contributor Author

@joelittlejohn ok )
Could you please assist me and help to find entry point to fixing it?

@joelittlejohn
Copy link
Owner

Which part are you having trouble with?

blizznets added a commit to blizznets/jsonschema2pojo that referenced this issue Dec 2, 2015
blizznets added a commit to blizznets/jsonschema2pojo that referenced this issue Dec 2, 2015
blizznets added a commit to blizznets/jsonschema2pojo that referenced this issue Dec 2, 2015
@joelittlejohn joelittlejohn added this to the 0.4.17 milestone Dec 2, 2015
blizznets added a commit to blizznets/jsonschema2pojo that referenced this issue Dec 3, 2015
joelittlejohn added a commit that referenced this issue Dec 3, 2015
#455 Override builder methods from parent class
@joelittlejohn
Copy link
Owner

Closed by #458.

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

No branches or pull requests

2 participants