通过反射检查 Class<?> 是否与其他类即时

Raf*_*ima 1 java generics reflection typeof instanceof

我正在尝试为压力测试目的创建任何类的随机生成器。

我的生成器目标是使用默认构造函数创建任何类并填充其原始字段和字符串字段(通过反射)

我还想填充数字类型(整数、长、双....)的字段,因为它们很容易转换为基元。

但是我instanceof在类型之间找不到简单的方法 o 。

这是方法

public static <V> Collection<V> createMassiveCollection(Class<V> clazz, int amount, Class<?> collectionType) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {

        Random r = new Random();
        RandomGenerator rg = new RandomGenerator();
        @SuppressWarnings("unchecked")
        Collection<V> collection = (Collection<V>) collectionType.newInstance();
        for (int i = 0; i < amount; i++) {
            V item = clazz.newInstance();

            for (Field field : item.getClass().getDeclaredFields()) {

                if (java.lang.reflect.Modifier.isStatic(field.getModifiers()) || java.lang.reflect.Modifier.isFinal(field.getModifiers()))
                    continue;

                field.setAccessible(true);

                if (field.getType().equals(String.class))
                    field.set(item, rg.nextCompliment());

                else if (field.getType().isPrimitive() && (field.getType() != boolean.class && field.getType() != byte.class && field.getType() != char.class))
                    field.set(item, r.nextInt(1000000));

                else {
                    Constructor<?> c = null;

                    try {
                        c = field.getType().getConstructor(String.class);
                    }
                    catch (Exception e) {}
                    if (c != null && Number.class.isInstance(c.newInstance("1")))
                        field.set(item, field.getType().getConstructor(String.class).newInstance(r.nextInt(1000000) + ""));
                }

            }

            collection.add(item);
        }
        return collection;
    }
Run Code Online (Sandbox Code Playgroud)

第三个 if 是导致问题的一个我想要的东西:“ifType<C>是”的子类型,Type<V>但我发现所有方法都需要一个有效的对象实例来检查。

前任。 Number.class.isInstance(field.getType())field.getType() instanceof Number不起作用,因为field.getType()是 Type 的实例。

有谁知道这是否可能?

我想要的方法签名中的另一个问题(不太重要)

Class<? extends Collection<V>> collectionType
Run Code Online (Sandbox Code Playgroud)

但是如果我在尝试调用该方法时这样做

createMassiveCollection(User.class, 100000, new ArrayList<User>().getClass());
Run Code Online (Sandbox Code Playgroud)

编译器失败!因此,为了能够编译,我必须相信用户正在传递某种扩展 Collection 作为参数的类型

Ste*_*ski 5

要检查一个类是否是另一个类的子类,您必须使用isAssignableFrom

if (Number.class.isAssignableFrom(field.getType()) { // ...
Run Code Online (Sandbox Code Playgroud)

对于您的第二个问题,您需要第二个通用:

public static <V, C extends Collection<?>> Collection<V> createMassiveCollection(Class<V> clazz, int amount, Class<C> collectionType)
Run Code Online (Sandbox Code Playgroud)

您也可以将此泛型定义为,C extends Collection<V>但这会创建编译器警告并且不会阻止像createMassiveCollection(String.class, 10, new ArrayList<Number>().getClass()).

顺便说一下,我的最后一个参数是ArrayList.class.