I am trying to create a function that takes in a generic Class
, does some work with a function of that Class
, then return an instance of said Class
.
I'm getting a compiler error: error: cannot select from a type variable - [ERROR] T.getManagers(), T.class);
. Here is the function:
public static <T extends Bar> T getFoo(Class<T> fooClass , String person)
{
T foo = null;
try
{
Configuration config = Configuration.getConfiguration(person);
foo = (T) Bar.getInstance(config,T.getManagers(), T.class);
}
catch (Exception e)
{
logger.error("Error", e);
}
return foo;
}
EDIT: Is there a way I can get around calling Bar.getManagers()
? What I'm trying to do with generics is that I have say 5 classes very similar to Bar but not exactly the same. They all have getManagers()
. By using a generic like T.getManagers()
I'm trying to get around calling the explicit names in the code and using a generic one instead.