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

Constructor inheritance and default values #29

Closed
andrievskya opened this issue Jun 18, 2019 · 2 comments
Closed

Constructor inheritance and default values #29

andrievskya opened this issue Jun 18, 2019 · 2 comments
Labels

Comments

@andrievskya
Copy link
Contributor

andrievskya commented Jun 18, 2019

Default no-args constructor for class that have ancestors calls constructor with arguments and passes nulls.

For example if we have two classes Foo and Bar, that extends it and have default value defined for an element of Foo:

public class Foo {
    private final String param1;

    public Foo(){
        param1="Default1";
    }
    public Foo(String param1){
        this.param1 = param1;
    }
    //..
}
public final class Bar extends Foo {
    private final String param2;

    public Bar(){
        super(null);// Foo have default value for param1, but it will not be set
        this.param2="Default2";
    }
    //..
}

If we create an instance of Bar class using no-args constructor (for example, if we parse some xml without param1 specified), we will have an object with param1==null, and not "Default1"

@sabomichal sabomichal added the bug label Jun 18, 2019
@andrievskya
Copy link
Contributor Author

andrievskya commented Jun 28, 2019

I would suggest either removing following lines

			ctor.body().invoke("super");
			JInvocation superInvocation = ctor.body().invoke("super");
			for (FieldOutline fieldOutline : superclassFields) {
				superInvocation.arg(defaultValue(getJavaType(fieldOutline), fieldOutline));
			}

from generateStandardConstructor(...) method completely
or adding a check of the constructorDefaults flag, in order not to create such a call in resulting class

@sabomichal
Copy link
Owner

Fixed in PR #32

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

No branches or pull requests

2 participants