java 获取组件类型的数组Class

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4901128/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:32:56  来源:igfitidea点击:

Obtaining the array Class of a component type

javaarrays

提问by Tom Castle

If I have an instance of Class, is there a way of obtaining a Classinstance for its array type? What I'm essentially asking for is the equivalent of a method getArrayTypewhich is the inverse of the getComponentType()method, such that:

如果我有一个 的实例Class,有没有办法获得Class它的数组类型的实例?我本质上要求的是与方法getArrayType相反的getComponentType()方法的等价物,例如:

array.getClass().getComponentType().getArrayType() == array.getClass()

回答by Bozho

One thing that comes to mind is:

想到的一件事是:

java.lang.reflect.Array.newInstance(componentType, 0).getClass();

But it creates an unnecessary instance.

但它创建了一个不必要的实例。

Btw, this appears to work:

顺便说一句,这似乎有效:

Class clazz = Class.forName("[L" + componentType.getName() + ";");

Here is test. It prints true:

这里是测试。它打印true

Integer[] ar = new Integer[1];
Class componentType = ar.getClass().getComponentType();
Class clazz = Class.forName("[L" + componentType.getName() + ";");

System.out.println(clazz == ar.getClass());

The documentation of Class#getName()defines strictly the format of array class names:

的文档Class#getName()严格定义了数组类名的格式:

If this class object represents a class of arrays, then the internal form of the name consists of the name of the element type preceded by one or more '[' characters representing the depth of the array nesting.

如果此类对象表示一类数组,则名称的内部形式由元素类型的名称组成,前面有一个或多个代表数组嵌套深度的“[”字符。

The Class.forName(..)approach won't directly work for primitives though - for them you'd have to create a mapping between the name (int) and the array shorthand - (I)

不过,该Class.forName(..)方法不会直接适用于原语 - 对于它们,您必须在名称 ( int) 和数组速记之间创建映射- ( I)

回答by FroMage

Actually due to ClassLoader, primitives and multi-dimensional arrays, the answer is a little more complex:

实际上由于ClassLoader, 基元和多维数组,答案有点复杂:

public static Class<?> getArrayClass(Class<?> componentType) throws ClassNotFoundException{
    ClassLoader classLoader = componentType.getClassLoader();
    String name;
    if(componentType.isArray()){
        // just add a leading "["
        name = "["+componentType.getName();
    }else if(componentType == boolean.class){
        name = "[Z";
    }else if(componentType == byte.class){
        name = "[B";
    }else if(componentType == char.class){
        name = "[C";
    }else if(componentType == double.class){
        name = "[D";
    }else if(componentType == float.class){
        name = "[F";
    }else if(componentType == int.class){
        name = "[I";
    }else if(componentType == long.class){
        name = "[J";
    }else if(componentType == short.class){
        name = "[S";
    }else{
        // must be an object non-array class
        name = "[L"+componentType.getName()+";";
    }
    return classLoader != null ? classLoader.loadClass(name) : Class.forName(name);
}

回答by Peter Lawrey

You can do the following

您可以执行以下操作

array.getClass() == 
    Array.newInstance(array.getClass().getComponentType(), 0).getClass()

Usually, you don't need to know the type, you just want to create the array.

通常,您不需要知道类型,您只想创建数组。

回答by Leigh Klotz

Another possible refactoring is to use a generic superclass and pass in two class objects to the constructor.

另一种可能的重构是使用通用超类并将两个类对象传递给构造函数。

protected AbstractMetaProperty(Class<T> valueClass, Class<T[]> valueArrayClass) {
  this.valueClass = valueClass;
  this.valueArrayClass = valueArrayClass;
}

Then in subclasses:

然后在子类中:

public IntegerClass() {
  super(Integer.class, Integer[].class);
}

Then in the abstract class you can use valueClass.cast(x), valueArrayClass.isInstance(x)etc.

然后在抽象类,你可以使用valueClass.cast(x)valueArrayClass.isInstance(x)等等。