Get rid of Unchecked overriding: return type requires unchecked conversion

The name of the picture


Get rid of Unchecked overriding: return type requires unchecked conversion



Example code similar to my code.


public interface Builder<B, A> {
B build(A a);
}

public class ClientBuilder implements Builder<String, Integer> {
@Override
public String build(Integer i) {
return i.toString();
}
}

public abstract class Client<B> {
protected abstract <A> Builder<B, A> getBuilder();
}

public class ClientClient extends Client<String> {

@Override
protected Builder<String, Integer> getBuilder() {
return null;
}
}



In my ClientClient class I get a warning that says the following:


ClientClient



Unchecked overriding: return type requires unchecked conversion.Found 'Builder<java.lang.String,java.lang.Integer>', required 'Builder<java.lang.String,A>


Unchecked overriding: return type requires unchecked conversion.Found 'Builder<java.lang.String,java.lang.Integer>', required 'Builder<java.lang.String,A>



Is there something I can do to get rid of this error?
I could use @SuppressWarnings("unchecked"), but that doesn't feel right.


@SuppressWarnings("unchecked")



In my example A is always an Integer but in my real code A can be one of two objects.


A


Integer


A



I guess I could also have two methods in my interface like this:


public interface Builder<B> {
B build(Integer i);
B build(String s);
}





Note that it is recommended to use the existing JDK interfaces, so in your case you might use Function<A,B>.
– daniu
6 hours ago


Function<A,B>





The compiler does not lie here, <A>is not the type from Builder<B, A>, but any type passed to the method.
– Glains
6 hours ago




<A>


Builder<B, A>




1 Answer
1



This isn't overriding the method correctly:


protected Builder<String, Integer> getBuilder() {
return null;
}



You're missing the type variable.



Either declare it correctly:


protected <A> Builder<String, A> getBuilder() {
return null;
}



or change the class declaration to allow you to provide the second type parameter:


public abstract class Client<B, A> {
protected abstract Builder<B, A> getBuilder();
}





I have tried that but the issue that occurs is that I cant access the getters from A
– idkwat2do
6 hours ago





There are no getters from A, aside from those on Object. If you want your getBuilder() method to access methods specific to Integer, you would need to add the extra type parameter to the class.
– Andy Turner
5 hours ago


Object


getBuilder()


Integer





I can not seem to get around it. I've tried both ways but I end up having to access getters of A.. Would using @SuppressWarnings be "ok" at this point or should I add another method to the interface which leads to the affected classes having to implement a method that will do nothing expect for the one class where it will be used and vice versa.
– idkwat2do
5 hours ago


@SuppressWarnings





No, it would not be OK, other than in the code given, where you are returning null instead of an actual Builder. See this code, it compiles with no warning.
– Andy Turner
4 hours ago


null


Builder






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS