java - 如何获得<? super T> 来自 T

标签 java generics

我有一个类似这样的方法。

static <T> void doSomethind(final Class<? super T> type, final T instance) {

}

还有一种方法可以为 T 或 Object 找到合适的父类(super class)。

static Class<?> getType(final Object instance) {

    // returns a Class which is parent of given instance.
}

我什至不确定 <? super T>部分是否必要。可以吗<T>

static void doSomething(final Object instance) {

    final Class<?> type = getType(instance);

   // how can I call <T>doSomething(Class<T>, t)?
}

或者

static <T> void doSomething(final T instance) {

    final Class<?> type = getType(instance);

    // how can I call <T>doSomething(Class<T>, t)?
}

问题是如何调用 doSomething(Class, Object)方法?

更新

我很抱歉。但我什至不知道我在问什么。所以我决定讲述这个愚蠢的(完整)故事。

javax.xml.bind.Marshaller ,有类似 marshal(Object, XXX) 的方法比如

  1. marshal(对象,ContentHandler)
  2. marshal(对象,文件)
  3. 等等。

我想我可以使用像这样的反射来创建一个通用的实用方法。

public static <T> marshal(Marshaller marshaller, Object element,
                          Class<? super T> targetType, T target)
    throws VariousNastyExceptions {

    // like a boss, huh?
    Marshaller.class.getMethod("marshal", Object.class, targetType)
        .invoke(marshaller, element, target);
}

// still not sure about <? super T>

所以任何人都可以像这样调用。

marshal(marshaller, element, OutputStream.class, output);
// say output is an instance of ByteArrayOutputStream

marshal(marshaller, element, Result.class, result);
// say the result is an instance of StreamResult

然后我想要一个没有 targetType 的新版本.

为此,我首先收集了 targetType 的候选者。 .

// InputStream.class, File.class, XMLEventWriter.class, and so on.
static final List<Class<?>> TARGET_TYPES;
static {
    final List<Class<?>> targetTypes = new ArrayList<Class<?>>();
    for (Method method : Marshaller.class.getMethods()) {
        // if method is for marshal(Object, T) // code skipped
        targetTypes.add(method.getParameterTypes()[0]);
    }
    TARGET_TYPES = Collections.unmodifiableList(targetTypes);
}

现在我可以获取 targetType从给定 Object .

static Class<?> getTargetType(Object target) {

    for (Class<?> targetType : TARGET_TYPES) {
        if (targetType.isAssignableFrom(target.getClass())) {
            return targetType;
        }
    }

    return null; // don't count this for now
}

我终于尝试了

// do I need a <T> here?
static void marshal(Marshaller marshaller, Object element, Object target) {

    final Class<?> targetType = getTargetType(target);

    // compiler hates this statement
    marshal(marshaller, element, targetType, target);
}

我只想要一个Cipher用于解密以下消息。

method Marshallers.<T> marshal(Marshaller,Object,Class<? super T>, T) is not applicable
 (actual argument Class <CAP#1> cannot be converted to Class<? super Object> by method
  invocation conversion)

我想我可以这样做。

static <T> void marshal(Marshaller marshaller, Object element, T target) {

    // No ClassCastException guaranteed
    @SuppressWarnings("unchecked")
    final Class<? super T> targetType =
        (Class<? super T>) getTargetType(target);

    marshal(marshaller, element, targetType, target);
}

还有什么更好的办法吗?

最佳答案

你能解释一下为什么你需要这样的东西吗?

但是要调用 doSomething(Class, Object) 方法,您可以像这样更改 getType 方法;

static <T> Class<? super T> getType(final T instance) {
// returns a Class which is parent of given instance.
}

static void invokeDoSomething(){
        A instance = new A();
        Class<? super A> type = getType(instance);
        doSomethind(type, instance);
    }

关于java - 如何获得<? super T> 来自 T,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17446524/

相关文章:

java - Axis 自定义处理程序未针对 wsdl 运行

java - 将 SoftReferences 添加到 "Java Concurrency in Practice"中高效且可扩展的结果缓存

swift - "subclassing"通用结构

java - 如何使用带通配符的集合和泛型?

java - 没有集合的泛型

来自不同类的 JavaFX 事件处理

Java正则表达式删除虚拟时间戳

java - 使用 LoopJ AndroidAsyncHttp 多次上传照片

c# - 如何修剪 List<string> 以便删除前后的空行?

java - Spring:通用注入(inject)不起作用