锁定老帖子 主题:提炼Java Reflection
精华帖 (1) :: 良好帖 (2) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-07-28
反射是Java语言中很重要的一个组成部分,所以就此话题讨论的资源可谓数之不尽,日常开发也会经常使用到关于反射的Reflection API。Java5.0 Tiger出现以后,更对反射API有了新的扩展,尽管讨论的话题很多,不过我还是觉得不够全面,尤其是对泛型这一块,所以就我所知,再花力气总结一番 public Long getId() { return id; }
Method getId = Order.class.getMethod("getId", new Class[0]);
Method getId = Order.class.getMethod("getId");
public class BaseOrder<M extends Object & Serializable,N extends Comparable<N>> implements IBaseOrder { public M getManufactory(){ return manufactory; } }
Field manufactoryField = BaseOrder.class.getDeclaredField("manufactory"); type = manufactoryField.getGenericType(); assertTrue("The type of field manufactory is an instance of TypeVariable", type instanceof TypeVariable); TypeVariable tType = (TypeVariable)type; assertEquals("The name of this TypeVariable is M", "M", tType.getName()); assertEquals("The TypeVariable bounds two type", 2, tType.getBounds().length); assertEquals("One type of these bounds is Object", Object.class, tType.getBounds()[0]); assertEquals("And annother si Serializable", Serializable.class, tType.getBounds()[1]);
public class Order extends BaseOrder<Customer, Long> implements IOrder, Serializable { }
Type genericSuperclass = Order.class.getGenericSuperclass(); assertTrue("Order's supper class is a type of ParameterizedType.", genericSuperclass instanceof ParameterizedType); ParameterizedType pType = (ParameterizedType)genericSuperclass; assertEquals("Order's supper class is BaseOrder.", BaseOrder.class, pType.getRawType()); Type[] arguments = pType.getActualTypeArguments(); assertEquals("getActualTypeArguments() method return 2 arguments.", 2, arguments.length); for (Type type : arguments) { Class clazz = (Class)type; if(!(clazz.equals(Customer.class)) && !(clazz.equals(Long.class))){ assertTrue(false); } }
public String[] getPayments(String[] payments, List<Product> products){ return payments; }
Method getPayments = BaseOrder.class.getMethod("getPayments", new Class[]{String[].class, List.class}); types = getPayments.getGenericParameterTypes(); assertTrue("The first parameter of this method is GenericArrayType.", types[0] instanceof GenericArrayType); GenericArrayType gType = (GenericArrayType)types[0]; assertEquals("The GenericArrayType's component is String.", String.class, gType.getGenericComponentType());
public Type[] getGenericParameterTypes() { if (getGenericSignature() != null) return getGenericInfo().getParameterTypes(); else return getParameterTypes(); }
private Comparable<? extends Customer> comparator;
Field comparatorField = BaseOrder.class.getDeclaredField("comparator"); ParameterizedType pType = (ParameterizedType)comparatorField.getGenericType(); type = pType.getActualTypeArguments()[0]; assertTrue("The type of field comparator is an instance of ParameterizedType, and the actual argument is an instance of WildcardType.", type instanceof WildcardType); WildcardType wType = (WildcardType)type; assertEquals("The upper bound of this WildcardType is Customer.", Customer.class, wType.getUpperBounds()[0]);
//error private static T customer; //error public static T getCustomer(){ return customer; } //error static { customerHolder = new HashSet<T>(); }
public static <B extends BusinessType, S extends Serializable> Customer getSpcialCustomer(List<B> types, S serial){ return new Customer(); }
Method getSpcialCustomer = SalePolicy.class.getMethod("getSpcialCustomer", new Class[]{List.class,Serializable.class}); types = getSpcialCustomer.getTypeParameters(); assertEquals("The method declared two TypeVariable.", 2, types.length); assertEquals("One of the TypeVariable is B.", "B", ((TypeVariable)types[0]).getName()); assertEquals("And another is S.", "S", ((TypeVariable)types[1]).getName());
晕,一写就那么晚了,抓紧睡觉去。如果本文写的有错误的地方请指正,以免误人子弟。
PS:附件是一个较为全面的测试代码
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-07-30
反射虽然功能强大,可以让应用程序不知情的情况下改变对象的行为和状态。不过也正因为如此,所以如果稍有疏忽可能会造成一些莫名其妙的问题。因此如果反射的目标应用于完全符合JavaBeanhttp://java.sun.com/docs/books/tutorial/javabeans/规范的对象时,那么JavaBean已经提供了一套完整的机制来处理。BeanUtils和Spring也都是这样做的,在应用时我们也不妨借鉴借鉴:idea:
|
|
返回顶楼 | |
浏览 3804 次