泛型类型声明反射 [英] generics type declaration for reflection

查看:101
本文介绍了泛型类型声明反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我询问了有关
$ b之间差异的问题

$ b

  public static< T> void work(Class< T> type,T instance); 

  public static< T,S extends T> void work(Class< T> type,S instance); 

我想我应该解释一下我想知道的内容。我认为更新原始问题是不适当的,所以我在这里问另一个问题。



说一下,我想做一个反映方法调用 marshal 方法。 htmlrel =nofollow noreferrer> Marshaller ,例如
$ b

  void marshal Object元素,ContentHandler处理函数)
void marshal(Object element,File output)
void marshal(Object element,Node node)

等等。



我正在处理的一种方法是

  void marshal(Object jaxbElement,Class <?> targetType,Object target)
pre>

实现很简单


  1. 找到一个类似于 marshal(Ljava / lang / Object; Ljava / lang / Class;)V with Object.class targetType

  2. 元素目标

测试代码可以像

  marshal(element,ContentHandler.class,handler); 
marshal(element,File.class,new File(text.xml));

在这种情况下,我应该如何定义元帅方法?

 < T> marshal(Object element,Class< T> targetType,T target); 

 < T,S extends T> marshal(Object element,Class< T> targetType,S target)



每个答案的进一步评论

我想我需要 targetType

如果没有 targetType ,我必须迭代所有的方法如方法method:Marshaller.class.getMethods()){



  
$ b //检查修饰符,名称,返回类型等等。如果(!method.getParameterTypes()[1] .isAssignableFrom(target.getClass())){
}
}

>

我认为为这个添加另一个版本会更好。 :)

解决方案

很难知道为什么你需要类对象,因为如果你有一个 T 您可以在此方法中执行此操作:

  Class< T> targetType = target.getClass(); // class可能来自实例

然而,如果你确实需要通过一个类,我认为你想要这样做:

 < T> void marshal(Object element,Class< ;? super T> targetType,T target){

}

通过使用 Class <?超级T> 您可以灵活地传递一个实例的超类的Class,而不需要额外的通用方法参数。


I asked a question about differences between

public static <T> void work(Class<T> type, T instance);

and

public static <T, S extends T> void work(Class<T> type, S instance);

I think I should explain what exactly I want to know. Updating the original question is not proper for this time, I think, so I'm asking an another question here.

Say, I want to make a single reflected method for invoking those marshal methods defined in Marshaller such as

void marshal(Object element, ContentHandler handler)
void marshal(Object element, File output)
void marshal(Object element, Node node)

and so on.

One of method that I'm working on is

void marshal(Object jaxbElement, Class<?> targetType, Object target)

The implementation is simple

  1. Find a method looks like marshal(Ljava/lang/Object;Ljava/lang/Class;)V with Object.class and targetType
  2. invoke the method with element and target.

So any unit testing code can invoke like

marshal(element, ContentHandler.class, handler);
marshal(element, File.class, new File("text.xml"));

In this case how should I define the marshal method? Is there any difference between

<T> marshal(Object element, Class<T> targetType, T target);

and

<T, S extends T> marshal(Object element, Class<T> targetType, S target)

?

Further Comments per Answers

I think I need the targetType for fast and direct method looking the right method.

Without the targetType I have to iterate all methods like

for (Method method : Marshaller.class.getMethods()) {

    // check modifiers, name, return type, and so on.

    if (!method.getParameterTypes()[1].isAssignableFrom(target.getClass())) {
    }
}

Adding another version for this will be better, I think. :)

解决方案

It's hard to know why you want the class object, because if you have an instance of T you can do this in the method:

Class<T> targetType = target.getClass(); // class may be derived from the instance    

However, if you really need to pass a class, I think you want this:

<T> void marshal(Object element, Class<? super T> targetType, T target) {

}

By using Class<? super T> you achieve the flexibility of passing a Class that is a super class of the instance without the extra generic method parameter.

这篇关于泛型类型声明反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆