java 允许我将此对象分配给此通用字段吗?

Would java allow me to assign this object to this generic field?

在我的 POJO 编辑器中,我需要检查用户提供的 class 名称是否可以实例化并分配给通用 属性。 Guava 反射几乎提供了我需要的一切:我解析类型然后调用 proptype.isSupertypeOf(implClazz),但这并没有按预期工作。

例如,我有类型标记:java.util.Collection<java.sql.Timestamp>,我希望我的支票接受扩展原始 ArrayList 或 [=17] 的 ArrayList 和 classes =] 或它们自己被参数化,但不接受扩展 ArrayList<Integer>.

的 class

但是,isSupertypeOf() 仅适用于完全解析的类型,而不完整和原始 classes 不被视为子类型。

    package com.common.jsp.beans;

    import java.lang.reflect.Field;
    import java.lang.reflect.Type;
    import java.sql.Timestamp;
    import java.util.ArrayList;
    import java.util.Collection;

    import com.google.common.reflect.TypeToken;

    public class TestTypeInf
    {
        public static class MyCollection1 extends ArrayList<Integer> {
        }
        public static class MyCollection2 extends ArrayList<Timestamp> {
        }
        public static class MyCollection3<T> extends ArrayList<T> {
        }
        public static class MyCollection4 extends ArrayList {
        }

        public static Collection<Timestamp> fld;

        public static void main( String[] args )
                        throws Exception
        {
            // bad: fld = new MyCollection1();
            fld = new MyCollection2();
            fld = new MyCollection3(); // just a warning
            fld = new MyCollection4(); // just a warning

            Field getter = TestTypeInf.class.getField( "fld" );
            Type memberType = getter.getGenericType();

            TypeToken<?> resolved = TypeToken.of( TestTypeInf.class ).resolveType( memberType );
            System.out.println( "type of fld: " + resolved );

            checkAssignable(resolved, MyCollection1.class);
            checkAssignable(resolved, MyCollection2.class);
            checkAssignable(resolved, MyCollection3.class); // This should be totally valid
            checkAssignable(resolved, MyCollection4.class); // why no?
        }

        private static void checkAssignable(TypeToken<?> resolved, Class<?> implClass) {
            System.out.println( "fld = new " + implClass.getSimpleName() + "()" );
            System.out.println( resolved.isSupertypeOf( implClass ) ? "yes" : "no" );
        }
    }

_

type of fld: java.util.Collection<java.sql.Timestamp>
fld = new MyCollection1()
no
fld = new MyCollection2()
yes
fld = new MyCollection3()
no
fld = new MyCollection4()
no
fld = new ArrayList()
no

这很老套,但由于您尝试做的事情也很老套(即试图反思地确定在编译器中引发 未检查警告 的分配),我认为这是合理的:

private static boolean isAssignable(TypeToken<?> resolved, Class<?> implClass) {
    return resolved.isSupertypeOf(implClass) || isAnySupertypeAssignable(resolved, implClass);
}

private static boolean isAnySupertypeAssignable(TypeToken<?> resolved, Class<?> implClass) {
    return TypeToken.of(implClass).getTypes().stream()
            .anyMatch(supertype -> isUncheckedSupertype(resolved, supertype));
}

private static boolean isUncheckedSupertype(TypeToken<?> resolved, TypeToken<?> implTypeToken) {
    if (implTypeToken.getType() instanceof ParameterizedType) {
        return false; // this prevents assignments of Collection<Integer> to Collection<Timestamp>
    }
    try {
        resolved.getSubtype(implTypeToken.getRawType());
        return true;
    } catch (IllegalArgumentException ex) {
        return false;
    }
}

我在你的例子中检查过,returns:

fld = new MyCollection1() -> no
fld = new MyCollection2() -> yes
fld = new MyCollection3() -> yes
fld = new MyCollection4() -> yes
fld = new ArrayList() -> yes