您可以使用
Class.forName(className);
如果您使用的是短类名,则需要实现类似的路径
转换字符串最接近的方法是假设一些约定,例如 valueOf 或采用 String 的构造函数。
public static <T> T parseAs(String text, String className) {
Class clazz;
FOUND:
{
ClassNotFoundException e = null;
for (String pkg : "java.lang.,java.util.,java.math.,".split(",",-1)) {
try {
clazz = Class.forName(pkg + className);
break FOUND;
} catch (ClassNotFoundException e2) {
e = e2;
}
}
throw new IllegalArgumentException(e);
}
try {
Method valueOf = clazz.getMethod("valueOf", String.class);
return (T) valueOf.invoke(null, text);
} catch (Exception e) {
try {
Constructor<T> valueOf = clazz.getConstructor(String.class);
return (T) valueOf.newInstance(text);
} catch (Exception e2) {
throw new IllegalArgumentException(e);
}
}
}
enum BuySell {
BUY, SELL
}
public static void main(String... args) {
for (String className : "String,Long,Double,BigDecimal".split(",")) {
Object o = parseAs("1", className);
System.out.println(className + " => " + o.getClass() + " as " + o);
}
Object o = parseAs("BUY", BuySell.class.getName());
System.out.println("BuySell => " + o.getClass() + " as " + o);
}
印刷
String => class java.lang.String as 1
Long => class java.lang.Long as 1
Double => class java.lang.Double as 1.0
BigDecimal => class java.math.BigDecimal as 1
BuySell => class Main$BuySell as BUY