/** * 转换字符串到相应类型. * * @param value 待转换的字符串. * @param toType 转换目标类型. */ public static Object convertStringToObject(String value, Class<?> toType) { try { return org.apache.commons.beanutils.ConvertUtils.convert(value, toType); } catch (Exception e) { throw Reflections.convertReflectionExceptionToUnchecked(e); } }
private static boolean isClassProxyable(Class<?> clazz) { if (clazz.isInterface()) { return true; } else { Constructor<?> constructor = Reflections.getDeclaredConstructor(clazz); if (constructor == null) { return false; } else if (Modifier.isPrivate(constructor.getModifiers())) { return false; } else if (Reflections.isTypeOrAnyMethodFinal(clazz)) { return false; } else if (Reflections.isPrimitive(clazz)) { return false; } else if (Reflections.isArrayType(clazz)) { return false; } else { return true; } } }
/** * 提取集合中的对象的一个属性(通过Getter函数), 组合成List. * * @param collection 来源集合. * @param propertyName 要提取的属性名. */ public static List extractToList(final Collection collection, final String propertyName) { List list = new ArrayList(collection.size()); try { for (Object obj : collection) { list.add(PropertyUtils.getProperty(obj, propertyName)); } } catch (Exception e) { throw Reflections.convertReflectionExceptionToUnchecked(e); } return list; }
/** * 提取集合中的对象的属性(通过getter函数), 组合成List. * * @param collection 来源集合. * @param propertyName 要提取的属性名. */ @SuppressWarnings({"unchecked", "rawtypes"}) public static List convertElementPropertyToList( final Collection collection, final String propertyName) { List list = new ArrayList(); try { for (Object obj : collection) { list.add(PropertyUtils.getProperty(obj, propertyName)); } } catch (Exception e) { throw Reflections.convertReflectionExceptionToUnchecked(e); } return list; }
/** * 提取集合中的对象的两个属性(通过Getter函数), 组合成Map. * * @param collection 来源集合. * @param keyPropertyName 要提取为Map中的Key值的属性名. * @param valuePropertyName 要提取为Map中的Value值的属性名. */ public static Map extractToMap( final Collection collection, final String keyPropertyName, final String valuePropertyName) { Map map = new HashMap(collection.size()); try { for (Object obj : collection) { map.put( PropertyUtils.getProperty(obj, keyPropertyName), PropertyUtils.getProperty(obj, valuePropertyName)); } } catch (Exception e) { throw Reflections.convertReflectionExceptionToUnchecked(e); } return map; }
@SuppressWarnings({"unchecked", "rawtypes"}) public static <K> K key(final Class<K> keyClass) { Class<? extends IdGenerator> genClass = Instances.fetch( () -> { Map<Class<?>, Class<? extends IdGenerator>> map = new HashMap<Class<?>, Class<? extends IdGenerator>>(); for (Class<? extends IdGenerator> subClass : Reflections.getSubClasses(IdGenerator.class)) { Class<?> pcl = Generics.resolveGenericParameter(subClass, IdGenerator.class, "K"); map.put(pcl, subClass); } return map; }) .get(keyClass); if (null == genClass) throw new SystemException("", "Could not found any key generator class."); IdGenerator<K> g = Instances.fetch(genClass); return g.generate(); }
@Test public void testGetGenericTypeArgumentClass() { assertEquals(Long.class, Reflections.getGenericTypeArgument(OtherClass.class, 0)); assertEquals(String.class, Reflections.getGenericTypeArgument(OtherClass.class, 1)); }