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

Avoid generate @Delegate method body NPE #3811

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

eeoun
Copy link

@eeoun eeoun commented Jan 17, 2025

Avoid generate @DeleGate method body NPE when deletegate target is null on javac's HandleDelegate

Add a null check before delegate call,now is only for javac

before:

    @Generated
    public void asd() {
        this.getSd().asd();
    }

    @Generated
    public String getExt() {
        return this.getSd().getExt();
    }

    @Generated
    public void setExt(final String ext) {
        this.getSd().setExt(ext);
    }

after

    @Generated
    public void asd() {
        if (this.getSd() != null) {
            this.getSd().asd();
        }
    }

    @Generated
    public String getExt() {
        return this.getSd() == null ? null : this.getSd().getExt();
    }

    @Generated
    public void setExt(final String ext) {
        if (this.getSd() != null) {
            this.getSd().setExt(ext);
        }
    }

@eeoun
Copy link
Author

eeoun commented Jan 17, 2025

There is also e ecj's implement version. But i've not ever test event compile it. But I think it's ok. I'll add in PR if you think is ok.

commit 4d4e70b8c4b570740dac35cd45d2ef5aab821eeb

@victorwss
Copy link
Contributor

Not sure if this is a good idea. Let's see this case:

public interface Foo {
    /**
     * Do a foo thing. Never, ever returns {@code null}.
     * It is very important that in no circunstances {@code null} is ever returned.
     * @return Anything, everything or something, except {@code null}.
     */
    public String foo();
}
public class FooDecorator implements Foo {
    @Delegate(types = Foo.class)
    private Foo myFooField = null;
}

@eeoun
Copy link
Author

eeoun commented Feb 8, 2025

Not sure if this is a good idea. Let's see this case:

public interface Foo {
    /**
     * Do a foo thing. Never, ever returns {@code null}.
     * It is very important that in no circunstances {@code null} is ever returned.
     * @return Anything, everything or something, except {@code null}.
     */
    public String foo();
}
public class FooDecorator implements Foo {
    @Delegate(types = Foo.class)
    private Foo myFooField = null;
}

Well in this case, the code runs and stops immediately throwing a NPE. It May useful for some scenario want expose bug earlier in test time.

In the samethime in some use case like DTO case, need expose additioal field but don't want add it to core model

class RestControllerShopItemReturnDTO{
    
    private String someAdditionalShitFieldFromShitService;

    @Delegate
    private ShopItemDTO item = null;

    @Delegate
    private PriceDTO price = null;
}

class ShopItemDTO{

}

according to query condition it may contains price or not.

May be add a method in annoation @Delegate defination makes developer control it is a good idea. And it won't change the default behavier compare with old versions.

boolean skipWhenFieldNull() default false;

finally it looks like this

class RestControllerShopItemReturnDTO{
    
    private String someAdditionalShitFieldFromShitService;

    @Delegate(skipWhenFieldNull=true)
    private ShopItemDTO item = null;

    @Delegate(skipWhenFieldNull=true)
    private PriceDTO price = null;
}

class ShopItemDTO{

}

@Rawi01 Rawi01 marked this pull request as draft February 9, 2025 18:33
@Rawi01
Copy link
Collaborator

Rawi01 commented Feb 9, 2025

I converted this to a draft because:

  • I'm not sure if @rzwitserloot and @rspilker approve of the idea.
  • It doesn't handle primitive values.
  • There are no tests.
  • It introduces a breaking change.

While the actual modification is straightforward and could be adjusted to avoid being a breaking change, making it work with primitives might be a bit more complex. I recommend waiting for Reinier or Roel to approve the idea before investing time in implementing it.

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

Successfully merging this pull request may close these issues.

3 participants