@NonNullByDefault breaks the build

Sometimes when I add the required @NonNullByDefault annotation I get lot of build errors like: “Illegal redefinition of parameter xxx, inherited method from xxx does not constrain this parameter”.

I don’t know how to fix this. If I remove the annotation build is not happy either.

Example:

@NonNullByDefault
public class EnergyUnitConverter implements SingleValueConverter {
    /**
     * Marshals an Object into a single value representation.
     *
     * @param obj the Object to be converted
     * @return a String with the single value of the Object or <code>null</code>
     */
    @Override
    public String toString(Object obj) {
        return obj.toString();
    }
    /* ... */
}

And the build error is: Illegal redefinition of parameter obj, inherited method from com.thoughtworks.xstream.converters.SingleValueConverter does not constrain this parameter

I checked triple times and the class contain that parameter!

public interface SingleValueConverter extends ConverterMatcher {

    /**
     * Marshals an Object into a single value representation.
     * @param obj the Object to be converted
     * @return a String with the single value of the Object or <code>null</code>
     */
    public String toString(Object obj);

    /* ... */
}

Can we disable this “stupid” rule? Use Kotlin to avoid NPE or someting. I’m weeery frustrated.

And return obj == null ? null : obj.toString(); says that: Condition 'obj == null' is always 'false'

Now I found workaround: I have to add DTO class name suffix or move under dto package to skip this check.

It is indeed a redefinition. Because the interface you implement has no annotations, it allows null to be passed in as an argument. You cannot change that in your implementation and you need to make it explicitl that it is Nullable. The compiler will then make sure you properly take care of this.

Okay now I understand :+1:

I can’t modify the interface, but I can annotate the implementation. Thanks! You can close this issue :slight_smile: