I have a Generic Class Factory class that has two methods that use the common value of class T, and the other uses only its own definitions of the general methods of the method.
public class GenericClassFactory<T extends ClassMatchable> { public <E, K> E newObject(ClassMatcher<E, K> matcher, K key, String packageName){...} public <K> T newObject(K key, String packageName){...} }
A method that uses T generic works fine, but when I want to use another method that does not care that T generic will not use Generic E, it just returns the object, and then I have to type cast it.
Data data = new GenericClassFactory().newObject(new ClassMatcher<Data, String>(){...}, "key1", "my.package.name.impl");
This one has compilation errors because it wants me to attribute it (Data). If I pass the GenericClassFactory a valid Generic class, it will work. Its like he does not recognize the general methods of the method, if you have Generic Generic, but not used.
Data data = new GenericClassFactory<ClassMatchable>().newObject(new ClassMatcher<Data, String>(){...}, "key1", "my.package.name.impl");
It works great. But it is stupid that I would have to define a class like the one when it is not needed for my purposes. I could do this:
public class GenericClassFactory { public <E, K> E newObject(ClassMatcher<E, K> matcher, K key, String packageName){...} public <T extends ClassMatchable, K> T newObject(K key, String packageName){...} }
But now my second method seems too broad or something ... maybe not. I mean, it will still give a compilation error if the object you assign to the return type does not implement ClassMatchable. So should I go? So I do not need to resort to type?