Java - Generic Static Methods - java

I have been trying to understand whether it is possible to make a method which infers a generic type based on the return class and calls a static method of that generic type.
i.e. Below I create 2 classes both of which implement the getInstances and getAllInstances methods. I then attempt to create use the methods from a generic wrapper. It appears that the super class method is always being run regardless of the return type.
For example,
public class ParentClass {
public ParentClass(){}
public static <T extends ParentClass> T getInstance(){
return (T) new ParentClass();
}
public static <T extends ParentClass> List<T> getAllInstances(){
ArrayList<ParentClass> parents = new ArrayList<ParentClass>();
for(int i=0;i<5;i++){
parents.add(new ParentClass());
}
return (List<T>) parents;
}
}
SubclassA
public class SubclassA extends ParentClass{
public SubclassA(){}
#SuppressWarnings("unchecked")
public static SubclassA getInstance(){
return new SubclassA();
}
#SuppressWarnings("unchecked")
public static List<SubclassA> getAllInstances(){
ArrayList<SubclassA> parents = new ArrayList<SubclassA>();
for(int i=0;i<5;i++){
parents.add(new SubclassA());
}
return parents;
}
}
Wrapper - Shows the problem
public class Wrapper {
public Wrapper(){
// ... some other stuff
}
public <T extends ParentClass> T getInstance(){
return T.getInstance();
}
public <T extends ParentClass> List<T> getAllInstances(){
return T.getAllInstances();
}
public static void main(String... args){
Wrapper wrapper = new Wrapper();
SubclassA subclassA = wrapper.getInstance();
ParentClass parentClass = wrapper.getInstance();
System.out.println(subclassA.getClass().getName());
System.out.println(parentClass.getClass().getName());
}
}
When running Wrapper I get the following error:
Exception in thread "main" java.lang.ClassCastException: ParentClass cannot be cast to SubclassA
at Wrapper.main(Wrapper.java:20)
Can I do this in Java?

static methods will not be override.This static methods will belongs to Class level

Your approach is incorrect, there is no notion of static Inheritance in Java as inheritance always is in context of Object level. Only member methods (non-static) can be inherited by sub classes having appropriate access modifiers.
Update:
Further to add, in your scenario Factory pattern seems more suitable than adjusting generics for getting/constructing specific class objects.

No, you can't do it like this. To make it work you can pass the Class object:
public class ParentClassUtils
public static <T extends ParentClass> T getInstance(Class<T> clazz) {
return clazz.newInstance();
}
public static <T extends ParentClass> List<T> getAllInstances(Class<T> clazz) {
List<T> list = new ArrayList<T>();
for (int i = 0; i < 5; i++) {
list.add(getInstance(clazz));
}
return list;
}
}
Also, in your example you have equivalent static methods in parent classes and subclasses. The subclass methods don't override the parent class methods, they merely hide them, and this almost certainly isn't what you want. This utility class approach above gets around this.

Related

Why cast is needed when returning this from self-bound generic in Java

In this case my IDE shows compilation error in return statement.
public class Base<T extends Base>{
public T get(){
return this;
}
}
When I add a typecast as in code bellow everything works fine, however I don't get why typecast is needed.
public class Base<T extends Base>{
public T get(){
return (T) this;
}
}
Doesn't Java replace all bounded generic occurrences with bounded type? Can someone explain what is going on under the hood and why typecast is needed?
Edit 1.
Thanks to Lothars and algrid answers it is now clear that this standalone case can cause ClassCastException. This is not safe so Base should probably be abstract.
The intent of this is to create a base class for Builder classes so that extended methods would return the type of the extending class. This is needed for method chaining. In the example bellow the return type of child.setParamOne(1) will be Child despite the fact that it is defined above in the inheritance hierarchy.
Is this code safe? Do you have any suggestions or alternatives for approaching this problem?
public abstract class Base<T extends Base>{
int paramOne;
public T setParamOne(int param){
this.paramOne = param;
return (T) this;
}
}
public final class Child extends Base<Child> {
int paramTwo;
public Child setParamTwo(int param){
this.paramTwo = param;
return this;
}
}
public static void main(String[] args) {
Child c = new Child()
.setParamOne(1)
.setParamTwo(1);
}
Why do you think that your this is of the type T? It's of the type Base<T>.
Try to run the following code and you'll get ClassCastException:
public class Main {
public static void main(String[] args) {
Base<Child> b = new Base<>();
// b.get() returns an instance of Base, not Child (however it's mistakenly cast to Child)
Child1 c = b.get();
}
public static class Base<T extends Base>{
public T get(){
return (T) this;
}
}
public static class Child extends Base {
}
}
The reason for this error is the same as the error being created for code like this:
public void myMethod(InputStream is) {
ByteArrayInputStream bais = is;
}
Just with generics. To get rid of the complier error you can do the cast as you did in your code:
public void myMethod(InputStream is) {
ByteArrayInputStream bais = (ByteArrayInputStream) is;
}
But this will fail during runtime if the passed inputstream is not a ByteArrayInputStream or a class derived from it. The same will happen with your code. Unless you only create instances of Base<Base> the cast will lead to an error when calling get.
In your example:
public class Base<T extends Base>{
public T get(){
return this;
}
}
the return statement is incorrect, because this is an instance of Base<T> and not T.
If your aim is to return the instance itself (by the way, I'm not sure why you would be doing this), the code should look like this:
public class Base<T extends Base>{
public Base<T> get(){
return this;
}
}
If your aim is to return the parameterized type, then you will probably not be able to do that. The parameterized type itself is not an instance within the Base class, but, again, just the parameterized type. If that is what you need, you can get the parameterized type class using reflection.
The conversion is unsafe because this (which has type Base<T>) may not be a T. We only know that T is a Base, but not the other way around.
There is no way to represent a "self type" in Java. So what you want to do is impossible. Instead, you can make an abstract method that forces implementing subclasses to provide a way to return a T:
public class Base<T> {
public abstract T get();
}
public final class Child extends Base<Child> {
public Child get() {
return this;
}
}

Java class information is lost when extend class with more generics

This seems to be a compiler issue, or maybe this is there by design.
ClassA is a class with two generics. ClassB will extend ClassA by providing one solid generic type, but still expose another one.
In the following example, E will be passed in type that will extend ClassA, so when any method is called, then returned type will still be the subtype which enables to call the subtype method if needed. The motivation behinds this is to do a builder pattern, e.g.
ClassB b = new ClassB<String>().m1().m2().m3().m4()......
public class ClassA<E, T> {
public final E e;
public final T t;
public ClassA(T t) {
this.e = (E)this;
this.t = t;
}
public E printA() {
System.out.println("AAAAAA");
return e;
}
}
public class ClassB<T> extends ClassA<ClassB, T> {
public ClassB(T t) {
super(t);
}
public ClassB printB() {
System.out.println("BBBBBB");
return this;
}
public static void main(String[] args) {
ClassB<String> classB = new ClassB<>("Hello World");
// classB.printA().printA().printA(); // This will fail, after the second printA() return Object type instance instead of ClassB.
System.out.println(classB.printA().printA().getClass()); // This will print "class ClassB", so the class information it still there.
((ClassB)classB.printA().printA()).printA(); // Casting the instance to ClassB again will make it work again.
}
}
The problem is that when you call the method two times, the return instance type will be lost, so it will be an Object type, and cannot call any ClassA/B method without casting it. This is super weird.
Any idea?
Your class ClassB is a generic one, but you are opting out of generics when not providing a type parameter.
And you are doing exactly that here
public class ClassB<T> extends ClassA<ClassB, T>
^^^^^^
and here
public ClassB printB()
^^^^^^
So simply change these lines to
public class ClassB<T> extends ClassA<ClassB<T>, T>
^^^
and
public ClassB<T> printB()
^^^
Then it will work.

instantiate object of abstract class with generic type

I am creating a function with a generic type and that generic type is an abstract type which I need to instantiate. This code will explain it more clearly:
public <T extends AbstractRow> foo(){//no I did not actually name the function foo
ArrayList<T> arr=new ArrayList<T>();
arr.add(new T(...)); //cant instantiate objects of abstract class
}
Basically I want to enforce "T extends AbstractRow but is not Abstract itself".
I realize you can't instantiate abstract classes, so I'd like a suggestion on a workaround or some other method that would allow me to mimic the behavior of creating an object of a generic type.
As far as I know, there's two ways to do this:
Add a Class<T> type field to your abstract generic class, and set it through the constructor (or another method). You can then invoke type.newInstance() to create the object.
Create a factory interface Factory<T> with a T create() method and set that as a field on your abstract class through the constructor (or another method). Upon creating a concrete instance of your generic class, you also have to pass a concrete implementation of said factory.
import java.util.ArrayList;
import java.util.List;
public class Generic<T> {
private Factory<T> factory;
public Generic(Factory<T> factory) {
this.factory = factory;
}
public void foo() {
List<T> list = new ArrayList<>();
list.add(factory.create());
}
}
interface Factory<T> {
T create();
}
Usage:
Generic<Bar> concrete = new Generic<>(new Factory<Bar>() {
#Override
public Bar create() {
return new Bar();
}
});
concrete.foo();
Your main issue isn't that you're working with an abstract class - in which case the suggestions posted in the comments would be useful. The bigger problem is that you're trying to instantiate a generic type directly (read: new T()) - which, simply put, you can't do in Java because of type erasure.
That said, there's always a workaround:
/**#param clazz the subclass you want to instantiate */
public <T extends AbstractRow> foo(Class<T> clazz) {
ArrayList<T> arr = new ArrayList<T>();
arr.add(clazz.newInstance); //instantiate new instance of given subclass
}
Usage:
abstract class Test1 {}
class Test2 extends Test1{ }
class Test<T> {
public static <T extends Test1> T foo(Class<T> clazz) {
T toReturn = null;
try{ toReturn = clazz.newInstance(); } catch (Exception e) { };
return toReturn ;
}
public static void main(String[] args) {
Test1 t1 = t.foo(test2.class);
System.out.println(t1.getClass()); //prints "class pkgname.test2"
}
}

Java generics static type inference

I do have an abstract class with an delegation interface defined:
public abstract class MyAbstractClass extends AsyncLoader {
public interface MyAbstractClassDelegate<M> {
//The parameter in this method should be the concrete subtype of MyAbstractClass
public M performThisCall(MyAbstractClass concreteSubclassOfAbstractClass);
}
private MyAbstractClassLoaderDelegate delegate;
...
}
The Problem is, I do not want the delegate parameter to be MyAbstractClass, instead it should be the concrete subclass. Why? Because the implementation of the delegate needs the concrete subclass for further handling and I don't want to cast it...
I know I could define an Interface in each subclass, but it'll look the same in every subclass except for the parameter type
EDIT
Here is the perfect solution solving exactly what I wanted. Great thanks!
public abstract class MyAbstractClass {
public interface MyAbstractClassDelegate<M, Subtype extends MyAbstractClass> {
public M myMethod(Subtype t);
}
}
Is this possible with java 6 and if yes - how?
My solution would be:
public final class Example<T extends Example<T>> {
public interface Interface<M, Subtype extends Interface<M, Subtype>> {
public M myMethod(Subtype t);
}
}
You have no access to the generic from the outer class inside the interface (because the interface is static) so you have to declare it again.
If you use your interface you get something like this:
private static class Impl1 implements Interface<String, Impl1> {
#Override
public String myMethod(final Impl1 t) {
return null;
}
}
I don't know if it will help but here is my complete example:
public final class Example<M, T extends Example.Delegate<M, T>> {
public interface Delegate<M, Subtype extends Delegate<M, Subtype>> {
public M myMethod(Subtype t);
}
private T delegate;
private static class Impl1 implements Delegate<String, Impl1> {
#Override
public String myMethod(final Impl1 t) {
return null;
}
}
public static void main(String[] args) {
Example<String, Impl1> example = new Example<>();
example.delegate = new Impl1();
example.delegate.myMethod(example.delegate); //works but whout?
}
}
What you could do is to give your abstract class a type parameter with the concrete subclass, similar to the way Java's Enum does it.
Something along the lines of this:
public abstract class MyAbstractClass<S extends MyAbstractClass<S>> extends AsyncLoader {
public interface MyAbstractClassDelegate<M, S> {
public M performThisCall(S concreteSubclassOfAbstractClass);
}
...

Using a generic type of a subclass within it's abstract superclass?

Within my code a have the following abstract superclass
public abstract class AbstractClass<Type extends A> {...}
and some child classes like
public class ChildClassA extends AbstractClass<GenericTypeA> {...}
public class ChildClassB extends AbstractClass<GenericTypeB> {...}
I'm searching for an elegant way how I can use the generic type of the child classes (GenericTypeA, GenericTypeB, ...) inside the abstract class in a generic way.
To solve this problem I currently defined the method
protected abstract Class<Type> getGenericTypeClass();
in my abstract class and implemented the method
#Override
protected Class<GenericType> getGenericTypeClass() {
return GenericType.class;
}
in every child class.
Is it possible to get the generic type of the child classes in my abstract class without implementing this helper method?
BR,
Markus
I think its possible. I saw this was being used in the DAO patterns along with generics. e.g.
Consider classes:
public class A {}
public class B extends A {}
And your generic class:
import java.lang.reflect.ParameterizedType;
public abstract class Test<T extends A> {
private Class<T> theType;
public Test() {
theType = (Class<T>) (
(ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
}
// this method will always return the type that extends class "A"
public Class<T> getTheType() {
return theType;
}
public void printType() {
Class<T> clazz = getTheType();
System.out.println(clazz);
}
}
You can have a class Test1 that extends Test with class B (it extends A)
public class Test1 extends Test<B> {
public static void main(String[] args) {
Test1 t = new Test1();
Class<B> clazz = t.getTheType();
System.out.println(clazz); // will print 'class B'
System.out.println(printType()); // will print 'class B'
}
}
I'm not sure I fully understand your question - <Type> is the generic type of the subclass, even when it's being expressed in the abstract class. For example, if your abstract superclass defines a method:
public void augment(Type entity) {
...
}
and you instantiate a ChildClassA, you'll only be able to call augment with an instance of GenericTypeA.
Now if you want a class literal, then you'll need to provide the method as you indicated. But if you just want the generic parameter, you don't need to do anything special.

Categories