The following code does exactly what I want it to, but I am curious if there is a better way of going about it. This would be so much easier if Interfaces allowed static methods, or if Java methods could be generalized/parameterized to the extent they can in C#.
I would much rather substitute the parameter "Class<TParsedClass> c
" for "Class<AbstractClass> c
". To me "Class<AbstractClass>
" means a class that extends a certain abstract class, but apparently that is wrong because when I use that parameter and use it as I descibed above, I get compiler errors.
public <TData, TParsedClass> TParsedClass convert(TData data, Class<TParsedClass> c)
{
try
{
return (TParsedClass)c.getMethod("parse", data.getClass()).invoke(c, data);
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}