Bound mismatch with Java Generics "nesting" - java

I'm having difficulty using generics for a redesign/refactoring I'm doing on an existing design.
public interface DataDto {
// some data here
}
public interface SetDto<MyDataDto extends DataDto> {
List<MyDataDto> getData();
}
public interface Results<MySetDto extends SetDto<DataDto>> {
MySetDto getResults();
}
public interface MyProblemInterface<MyDataDto extends DataDto,
MySetDto extends SetDto<MyDataDto>,
MyResults extends Results<MySetDto>> {
// some stuff here
}
My problem is that I get the following error for MyProblemInterface:
Bound mismatch: The type MySetDto is not a valid substitute for the
bounded parameter <MySetDto extends SetDto<DataDto>> of the type
Results<MySetDto>
I admit my experience with generics is somewhat limited, but basically I'm trying to enforce that all three of the types in MyProblemInterface are the same "type". For example, if I have ADataDto, BDataDto, ASetDto<ADataDto>, BSetDto<BDataDto>, AResults<ASetDto>, BResults<BSetDto>, I want to ensure a class can't implement MyProblemInterface in a manner like AMyProblemInterface<ADataDto, ASetDto, BResults>. I would think that since MySetDto extends SetDto<MyDataDto> just fine, I could continue to take that further, but I'm apparently wrong.
Thank you for any help.

You want too much from Java generics.
It would be simpler to declare your interface as following:
public interface MyProblemInterface<MyDataDto extends DataDto>
And then force method to use SetDto<MyDataDto> and Results<MySetDto>.
By using generics in class/interface declaration you specify some kind of variety which is determined later in definition. But in your case you said that SetDto and Results will always have MyDataDto as parameter, so there is no variety.

Shouldn't it be something like this instead, and you add the actual classes only when implementing the interfaces.
Updated the code, because I forgot to add the right Results definition. This should work.
public interface DataDto {
// some data here
}
public interface SetDto<T extends DataDto> {
List<T> getData();
}
public interface Results<T extends SetDto<? extends DataDto>> {
T getResults();
}
public interface MyProblemInterface<T extends DataDto, E extends SetDto<T>, K extends Results<E>> {
// some stuff here
}

Related

Restrict Java Generic to a Class

In C#, this is possible:
public interface IMyCoolInterface<TEntity> where TEntity : class
{
}
So the generic is confined / restricted to a class.
Is this possible with Java?
Generic parameters are always classes in Java. There's no other option. The language doesn't allow primitives to be parameters, and there's no equivalent to C#'s struct.
You can restrict it even further if you want the interface to be of a certain base type by adding the extends keyword within your generic
public interface MyGenericInterface<T extends List<?>> {
T convert(String value);
}
Now every class that implements MyGenericInterface must use an object that extends List in its implementation
public class MyClass implements MyGenericInterface<ArrayList<String>> {
#Override
public ArrayList<String> convert(String value) {
return new ArrayList<>();
}
}

Annotation Processor appears to break Java generics

Background
I was trying to use Annotation Processors, to generate implementations of specific Factory interfaces. Those interfaces look the following:
public interface ViewFactory<T extends View> {
<S extends Presenter<T>> T create(S presenter);
}
and
public interface PresenterFactory<T extends View> {
<S extends Presenter<T>> S create();
}
The Annotation Processor is doing the correct thing and generates a factory for each matching class, that is annotated with an corresponding annotation.
The Problem
The output of the Annotation Processor is the following:
public final class TestViewImplFactory implements ViewFactory {
public final TestView create(TestPresenter presenter) {
return new TestViewImpl(presenter);
}
}
and the corresponding other class:
public final class TestPresenterImplFactory implements PresenterFactory {
public final TestPresenter create() {
return new TestPresenterImpl();
}
}
The TestViewImplFactory however cannot be compiled. The error message is:
"Class 'TestViewImplFactory' must be declared abstract or implement
abstract method create(S) in 'ViewFactory'"
Java says, the following is correct:
#Override
public View create(Presenter presenter) {
return new TestViewImpl(presenter);
}
which would not work at all, considering that the user wants to know, which View will be returned and which Presenter is required. I would have expected that:
either both of the autogenerated files are wrong
or both are correct
because they both are really similar. I expected the first to be true.
What am I missing here?
If I add the Generic type to the TestViewImplFactory like this:
public final class TestViewImplFactory implements ViewFactory<TestView> {
#Override
public <S extends Presenter<TestView>> TestView create(S presenter) {
return new TestViewImpl(presenter);
}
}
The problem arises, that the constructor Parameter (which is of the Type TestPresenter) is incorrect. Changing the S to a concrete TestPresenter will, again, make the class not compilable for the same reason as above.
So, I stumbled across an "solution" that can be compiled.
What basically has to be done, is to change the ViewFactory interface to the following:
public interface ViewFactory<T extends View, S extends Presenter<T>> {
T create(S presenter);
}
So the class definition has the same Generic type, as the method in the Question above.
After compilation (this time with generic type specification), the output looks like this:
public final class TestViewImplFactory implements ViewFactory<TestView, TestPresenter> {
public TestViewImplFactory() {
}
public final TestView create(TestPresenter presenter) {
return new TestViewImpl(presenter);
}
}
This can be compiled and runs successfully.
This however does not answer the original question. Why is the generic explicitly stated in the type definition correct, but inherited and specified in the method declaration wrong and not compilable?
To be concrete: Why can Java inherit one Generic automatically (within the PresenterFactory) and the other ones not (within the ViewFactory, at the method and at the type declaration)?
Why it is not working:
public interface PresenterFactory<T extends View> {
<S extends Presenter<T>> S create();
}
This signature causes the compiler to infer S at the location where create() is called. S will be what ever you assign create() to as in:
FancyPresenter fp = presenterFactory.create();
SomeOtherPresenter sop = presenterFactory.create();
This implies that:
public TestPresenter create(){...}
is not an implementation of:
<S extends Presenter<T>> S create();
but a method override. There is no implementation of the interface' method. It's not even possible to provide any implementation with a concrete S. It's similar with:
public interface ViewFactory<T extends View> {
<S extends Presenter<T>> T create(S presenter);
}
Here the generic is again inferred on method invocation. So an implementation must accept every subtype of Presenter<T>. The only valid implementation for this is:
public interface ViewFactory<T extends View> {
T create(Presenter<T> presenter);
}
But the return type is dependent on the parameter presenter. This might work if presenter provides you with a method to create an instance of T only.
Why does the other solution work:
Binding the method's generic via the type means that an implementation of the interface provides the concrete type. So for one object you don't need to provide multiple different bindings. No matter where you call the create() method of PresenterFactory<TestView, TestPresenter<TestView>> the return type's generic is bound to TestPresenter<TestView>. So there is a possible implementation for each subtype of PresenterFactory<...>.
I think the very first part of your problem statement should be addressed as I notice your annotation processor is implementing the raw ViewFactory type. I guess with type erasure, since it's generated code, it doesn't make a real difference in practice. But if the processor could generate implementations using the parameterized type, it would at least be easier to reason about the problem.
So, given a message signature <S extends Presenter<T>> T create(S presenter), you could have it generate:
public class TestViewImplFactory implements ViewFactory<TestView> {
#Override
public <S extends Presenter<TestView>> TestView create(S presenter) { ... }
}
Or more minimally:
public class TestViewImplFactory implements ViewFactory<TestView> {
#Override
public TestView create(Presenter presenter) { ... }
}
But then, with either of those, you cannot restrict the parameter to TestPresenter. You would have to change ViewFactory to something like
public interface ViewFactory<T extends View, U extends Presenter<T>>
and them implement ViewFactory<TestView, TestPresenter>. You kind of have to use the type parameters in the implementation to achieve the type restrictions you want.

Intertwined java generic interfaces and classes

I have a very specific problem with java generics. The follwowing classes and interfaces have been predefined:
public interface IFirst<R, T> {...}
public abstract class AbstractFirst<T extends AbstractFirst, L extends IFirst<String, T>> {...}
public interface ISecond extends IFirst<String, AbstractSecond> {...}
public abstract class AbstractSecond extends AbstractFirst<AbstractSecond, ISecond> {...}
Now I've created a following repo definition which seems to be valid:
public abstract class AbstractRepo<T extends AbstractFirst<T, IFirst<String,T>>> {...}
But now that i want to extend it:
public class RepoFirst extends AbstractRepo<AbstractSecond> {...}
I get the following error:
Bound mismatch: The type AbstractSecond is not a valid substitute for the bounded parameter
<T extends AbstractFirst<T,IFirst<String,T>>> of the type AbstractRepo<T>
I cannot change the first four (at least not radically) beacuse they are too heavily ingrained with the rest of the application, but the second two are new and up for change if need be.
Also intrestingly it allows the following (with raw type warnings):
public class RepoFirst extends AbstractRepo {
...
#Override
AbstractFirst someAbstractMethod() {
return new AbstractSecond() {...};
}
...
}
But for code clarity I would like to implement it with clearly defining AbstractSecond as the generic type for Abstract Repo.
What am I missing?
Your AbstractRepo expects an instance of IFirst and not a subtype of IFirst. But your AbstractSecond is clearly not IFirst. (I mean it is, from a OO standpoint but for generics, List<Number> is not the same as List<Integer>). It's ISecond. It might work if you could change your AbstractRepo from IFirst to ? extends IFirst as you did for AbstractFirst.

Nesting generics into generics

Lets say I have 2 interfaces which define some kind of container format holding a specific type of data.
public interface Content {
}
public interface Holder1<T extends Content> {
}
public interface Holder2<T extends Content> {
}
Now I want some converter which defines objects that can transform an object of type Holder1 into an Holder2.
This converter should keep information about the kind of objects stored within the original object:
public interface ConverterPrototype1 {
public <U extends Content> Holder2<U> convert(Holder1<U> source);
}
But I also want to be able to restrict the type of Holder1 that some converter can work on:
interface ConverterPrototype2<U extends Content, V extends Holder1<U>> {
public Holder2<U> convert(V source);
}
Is there a way to combine the semantics of these 2 interfaces into a single one? Something like
//INVALID CODE!
interface CombinedConvertor<V extends Holder1> {
public <U extends Content> Holder2<U> convert(V<U> source);
}
I'm not sure if my title is suited for this problem, but I couldn't find a better description... Similar problems posted here always seemed to talk about different things.
Edit:
After stumbling upon this link, I came up with following code. It is still invalid, but closer to actual java code.
//INVALID CODE!
interface CombinedConvertor<X extends Source<?>> {
public <U extends Content, V extends X & Source<U>> Target<U> convert(V source);
}
It is possible to create a CombinedConvertor interface, as long as you define another generic type parameter to represent the single generic type parameter of Holder1, as you already have done to ConverterProptotype2. This removes the generic type parameter from the convert method and adds it to the interface itself.
interface CombinedConvertor<U extends Content, V extends Holder1<U>> {
public Holder2<U> convert(V source);
}
If I'm understanding what you're asking, and from your comment to the other answer ... you can infer both.
public interface Converter
{
public <U extends Content, V extends Holder1<U>> Holder2<U> convert(V source);
}
After finding the format of code posted in my edit, I found many related items talking about this problem. It appears the java language simply does not support this case. I can recommend this post to learn more about the details of this limitation.

Extend generic List

I am a C# programmer and have agreed to help a fried doing Java homework.
In one example I want to create a class that extends a generic List. In C# this looks like
public class MyListClass : List<MyCustomType>
I have tried
public class MyListClass extends List<MyCustomType>
and get the error "no interface expected here". Well, I am not trying to use an interface... Any hints?
java.util.List is a interface. You need to implement it not extend it.
public class MyListClass implements List<MyCustomType>{
}
You can't extend interface. You must implement it.
But you can extend one of implementations (LinkedList for example):
public class MyListClass extends LinkedList<MyCustomType> {
Java ain't C++, so forget all about standard templates.
What you probably want is just a typed List:
List<MyCustomType> myList = new ArrayList<MyCustomType>();
and that's all.
It would be unusual to have "extending a generic class" as a goal for an assignment. It is unusual in the real world too.
You need to declare a generic class / interface
public class MyListClass<T> implements List<T> { }
Or
public interface MyListInterface<T> extends List<T> { }
Or best of all
public class MyListClass<T> extends AbstractList<T> implements List<T> { }

Categories

Resources