java 如何为原始类型实例化 Class 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5032898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to instantiate Class class for a primitive type?
提问by yegor256
I'm trying to do this, but doesn't work:
我正在尝试这样做,但不起作用:
public static Class loadIt(String name) throws Throwable {
return Class.forName(name);
}
assert foo.loadIt("int") == int.class; // exception here
How should I do this properly?
我应该如何正确地做到这一点?
采纳答案by Bozho
You can't, because primitives are not objects.
你不能,因为基元不是对象。
What you are trying currently though is not yet instantiation - it is loading a class. But you can't do that for primitives. int
is indeed the name that is used for int
types, whenever their Class
object is obtained (via reflection, for example method.getReturnType()
), but you can't load it with forName()
.
您目前正在尝试的还不是实例化 - 它正在加载一个类。但是对于基元,你不能这样做。int
确实是用于int
类型的名称,无论何时Class
获得它们的对象(例如通过反射method.getReturnType()
),但您不能使用forName()
.
Reference: Reflection tutorial:
参考:反射教程:
If the fully-qualified name of a class is available, it is possible to get the corresponding Class using the static method Class.forName(). This cannot be used for primitive types
如果类的完全限定名称可用,则可以使用静态方法 Class.forName() 获取相应的类。这不能用于原始类型
A solution to instantiate a primitive is to use commons-langClassUtils
, which can get the wrapper class corresponding to a given primitive:
实例化原语的一种解决方案是使用commons-langClassUtils
,它可以获得与给定原语对应的包装类:
if (clazz.isPrimitive() {
clazz = ClassUtils.primitiveToWrapper(clazz);
}
clazz.newInstance();
Note that this assumes you have the Class
representing the int type - either via reflection, or via the literal (int.class
). But it is beyond me what would be the usecase of having a string representation of that. You can use forName("java.lang.Integer")
instead.
请注意,这假设您具有Class
表示 int 类型的信息 - 通过反射或通过文字 ( int.class
)。但是我无法理解使用字符串表示的用例是什么。你可以forName("java.lang.Integer")
改用。
回答by rmuller
We use or own simple method (Java 7+):
我们使用或拥有简单的方法(Java 7+):
/**
* Return the java {@link java.lang.Class} object with the specified class name.
*
* This is an "extended" {@link java.lang.Class#forName(java.lang.String) } operation.
*
* + It is able to return Class objects for primitive types
* + Classes in name space `java.lang` do not need the fully qualified name
* + It does not throw a checked Exception
*
* @param className The class name, never `null`
*
* @throws IllegalArgumentException if no class can be loaded
*/
public static Class<?> parseType(final String className) {
switch (className) {
case "boolean":
return boolean.class;
case "byte":
return byte.class;
case "short":
return short.class;
case "int":
return int.class;
case "long":
return long.class;
case "float":
return float.class;
case "double":
return double.class;
case "char":
return char.class;
case "void":
return void.class;
default:
String fqn = className.contains(".") ? className : "java.lang.".concat(className);
try {
return Class.forName(fqn);
} catch (ClassNotFoundException ex) {
throw new IllegalArgumentException("Class not found: " + fqn);
}
}
}
回答by Andrew White
In this case, the best you can hope for is to create a map of primitives to their Autoboxed equivalent and return a class of that type.
在这种情况下,您所能希望的最好结果是创建一个原始类型映射到它们的 Autoboxed 等效项并返回该类型的类。
回答by brafdlog
Try ApacheCommons ClassUtils like this:
像这样尝试 ApacheCommons ClassUtils:
ClassUtils.getClass(className)
Works for primitives nicely.
很好地适用于原语。
The full name for the class is org.apache.commons.lang3.ClassUtils
该类的全名是 org.apache.commons.lang3.ClassUtils