Method return type using enclosed Generic class?

amol anerao :

I am trying to write a generic request generator method which will return different objects like below:

GenricClass<Object1> genCls1 = getNewRequest(Object1.class);
GenricClass<Object2> genCls2 = getNewRequest(Object2.class);
GenricClass<Object3> genCls3 = getNewRequest(Object3.class);

I want this getNewRequest to return an object which has enclosing generic class object.

How should the signature of my getNewRequest method ?

public `?` getNewRequest(Class classtype) {...}
Eran :

You can either declare a generic type parameter in the class that contains the method getNewRequest, or directly in the method.

For example, declare generic parameter T in the method getNewRequest:

public <T> GenricClass<T> getNewRequest(Class<T> classtype)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Delegate for generic class and method with generic return type

Return class of generic type in default interface method

Using type variable as a return type in a generic class

How to return a generic type from a nested class using a method in the main class

How to return instance of concrete class from method with generic return type

Method with generic return type

Generic type method return

Generic method return generic type

Why is this method with generic return not properly using the generic type?

How to invoke a generic method with generic return type using reflection

Single method to return an instance of different generic type of a class

Java generics: get class of generic method's return type

Return nested type parameter from method of generic class?

mypy: How to declare the return type of a method returning self in a generic class?

Javaparser: get generic class of a method return type like List<MyClass>

Method in generic class does not return type specified. Why?

Casting type twice necessary in a generic class method return call

How can you enforce that a generic type is enclosed in a specific class?

Using class name as method parameter and return type

How to return class type of a generic class with generic?

Generic return type when using reflection to invoke a method

Typescript Return the enum values in parameter using a generic enum type method

Zeroary method with generic return type

Overriding method with generic return type

Static method with generic return type

Return type of generic method (Java)

java generic method return type

Using a Generic to return correct class extracted from a Type

How to return the Class of a generic type