如何获得<! - ?超级T - >来自T.

时间:2013-07-03 11:16:42

标签: java generics

我的方法看起来像这样。

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

}

还有一种方法可以为T或Object找到合适的超类。

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(Object,ContentHandler)
  2. marshal(对象,档案)
  3. 等等。
  4. 而且我认为我可以使用这样的反射制作一个通用的实用方法。

    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);
    }
    

    还有更好的方法吗?

4 个答案:

答案 0 :(得分:2)

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

但是要调用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);
    }

答案 1 :(得分:1)

带有“<? super E>”的泛型总是有点棘手,我总能找到解决方法。也许你可以做到。

我不完全是你想要做的,但如果你试着打电话会怎么样?

doSomething(String.class, "foo");

使用<? super T>您也可以:

doSomething(Object.class, "foo");

仅使用Class就不可能。

答案 2 :(得分:1)

这会让你的痛苦消失

//a target method to call
static <T> void doSomething(final Class<? super T> type, final T instance) {
}

//returns a super of T as you pointed out
static <T> Class<? super T> getType(final T instance) {
}

static <T> void doSomething(final T instance) {
    final Class<? super T> type= getType(instance);
    //call to doSomething with a type and an instance
    doSomething(type, instance);
}

答案 3 :(得分:1)

实际上你不能没有警告。你需要使用不安全的演员。

static <T> void doSomething(final Class<T> type, final T instance) 
{
}

//here the unchecked cast warning can be suppressed. 
//It could be somewhere else though, the unchecked cast I mean. This is one solution only.
@SuppressWarnings("unchecked")
static <T> Class<T> getType(final T instance) 
{
    return (Class<T>) instance.getClass();
}

static <T> void doSomething(final T instance) 
{
    final Class<T> type= getType(instance);
    //call to doSomething with a type and an instance
    doSomething(type, instance);
}