I have a string com.bar.Baz
. I want to get Class<T>
, where T is anything that extends an interface com.bar.Foo
from the string.
Here is what I have tried but, it allows any class type, which is wrong.
Updated:(Based on the comment)
@SuppressWarnings("unchecked")
public static <T extends Foo> Class<T> getClassOfTypeT(String clazz) {
Class<T> ruleType = null;
try {
ruleType = (Class<T>) Class.forName(clazz);
} catch (ClassNotFoundException e) {
logger.error("Can't cast " + clazz, e);
}
return ruleType;
}
public static void main(String[] args) {
System.out.println(getClassOfTypeT("java.lang.String"));//Should not be allowed
}
How to get Class<T>
from a string?