Java How to avoid passing Class as parameter of generic object

Axel Carré :

I have written the following:

public class DataContainer<Data>{

    public DataContainer(Class<Data> clazz, String method) throws NoSuchMethodException, SecurityException{

        clazz.getMethod(method);

    }

}

And so I create my objects this way:

new DataContainer<SomeClass>(SomeClass.class, "get");

But I wanted it to look more like:

public class DataContainer<Data>{

    public DataContainer(String method) throws NoSuchMethodException, SecurityException{

        Data.getMethod(method);

    }

}

And a construct call should look like this:

new DataContainer<SomeClass>("get");

How can I avoid passing the Data class when I construct A DataContainer object? I know Data can't be manipulated at runtime (new DataContainer<>("get"); -> what is Data then?) but I've heard there are solutions to work around, unfortunately it seems like I haven't the vocab yet to google it.

Also it's a simplified version of my problem, we assume method is valid, public and hasn't arguments.

Thomas :

It's not really possible in the way you want to use your code, due to type erasure.

However, some generic information is preserved at runtime, i.e. when it is accessible to reflection. One such situation would be generics on the class hierarchy, i.e. you could do something like this (which we do quite frequently):

//Note that I used T instead of Data to reduce confusion
//Data looks a lot like an actual class name
public abstract class DataContainer<T>{
  public DataContainer(String method) throws NoSuchMethodException, SecurityException {
    Class<?> actualClass = getActualTypeForT();
    //use reflection to get the method from actualClass and call it
  }

  protected Class<?> getActualTypeForT() {
    //get the generic boundary here, for details check http://www.artima.com/weblogs/viewpost.jsp?thread=208860
  } 
} 

//A concrete subclass to provide the actual type of T for reflection, can be mostly empty
public class SomeClassContainer extends DataContainer<SomeClass> {
  //constructor etc.
}

Something similar should be possible for class fields or parameters, although I didn't test that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Avoid passing generic type parameter of class extending generic class

Passing a class with type parameter as type parameter for generic method in Java

I want to implement generic java method passing argument as class object and defining method parameter as Class<T> or T type

Typescript passing generic class as parameter

Passing a generic class to a java function?

How to pass generic Object as a Generic parameter on other method in java?

C# Passing a Generic Class As a Method Parameter

Error with passing parameter to Typescript generic class with React

java generic function parameter type mismatch for "Object" class

Java: Is it possible to compare a Class<?> object with a generic type parameter?

Get Class-object representation of generic parameter in Java

Java class - how to pass Generic Object to a function

Passing Interface Class as a Parameter in Java

Passing parameter to anonymous class in Java

JAVA - Passing a generic enum as a method parameter for a constructor

Type Parameter Class with generic java

How to avoid using a raw type as a generic parameter when Class<T> is needed

How can I pass a Class as parameter and return a generic collection in Java?

How to generic pass class as parameter

How to pass generic class as a generic parameter of method

Passing generic subtype class information to superclass in Java

Passing the Class<T> in java of a generic list?

Java - Passing generic lists into class via constructor

Adapter pattern without passing class object as parameter

Passing Scanner object as a constructor parameter to the UserInterface class

Passing A Form Object As A Class Parameter In VBA

Java, passing an object to a different class

sending a generic class object to a generic method (java)

Java Generic Type and Object Class