/** 通过ResultSetMetaData的方式得到一个结果集的列的列表 */ public static List<Field> list(ResultSet resultSet) { List<Field> fields = new ArrayList<Field>(); try { ResultSetMetaData meta = (null == resultSet ? null : resultSet.getMetaData()); for (int columnCount = (null == meta ? -1 : meta.getColumnCount()), i = 1; i <= columnCount; i++) { Field attribute = new Field(); attribute.name = attribute.column = meta.getColumnLabel(i); fields.add(attribute); } } catch (Exception e) { throw new RuntimeException("Exception in li.model.Field.list(ResultSet)", e); } return fields; }
/** 解析SourceFloder下搜索到的文件名以config.xml结尾的文件,将其中配置的Bean返回 */ public List<Bean> getBeans() { List<String> fileList = Files.list(Files.root(), "^.*config.xml$", true); // 搜索以config.xml结尾的文件 log.info("Found " + fileList.size() + " Xml config files at " + Files.root()); List<Bean> beans = new ArrayList<Bean>(); for (String filePath : fileList) { NodeList beanNodes = (NodeList) Files.xpath(Files.build(filePath), "//bean", XPathConstants.NODESET); for (int length = (null == beanNodes ? -1 : beanNodes.getLength()), i = 0; i < length; i++) { Bean iocBean = new Bean(); // 一个新的Bean iocBean.name = Files.xpath(beanNodes.item(i), "@name", XPathConstants.STRING).toString(); String type = Files.xpath(beanNodes.item(i), "@class", XPathConstants.STRING).toString(); try { iocBean.type = Class.forName(type); } catch (ClassNotFoundException e) { // 配置文件中把类名写错了 throw new RuntimeException( "Class " + type + " not found , which is configured in " + filePath, e); } NodeList propertyNodes = (NodeList) Files.xpath(beanNodes.item(i), "property", XPathConstants.NODESET); for (int len = (null == propertyNodes ? -1 : propertyNodes.getLength()), m = 0; m < len; m++) { Field field = new Field(); // 一个新的Field field.name = (String) Files.xpath(propertyNodes.item(m), "@name", XPathConstants.STRING); field.type = Reflect.fieldType(iocBean.type, field.name); field.value = (String) Files.xpath(propertyNodes.item(m), "@value", XPathConstants.STRING); iocBean.fields.add(field); } beans.add(iocBean); log.debug("ADD BEAN: Xml " + iocBean.type.getName() + " " + iocBean.name); } } return beans; }
/** * 通过扫描对象结构及注解的方式得到一个类型的属性列表 List<Field>,根据类名缓存 * * @param targetType 目标对象 * @param annotated 是否只列出有Field注解的字段 */ public static List<Field> list(Class<?> targetType, Boolean annotated) { List<Field> fields = FIELDS_MAP.get("class#" + targetType.getName() + "#annotated#" + annotated); if (null == fields) { // 如果缓存中没有 log.info("Field.list() by type " + targetType.getName()); fields = new ArrayList<Field>(); java.lang.reflect.Field[] declaredFields = targetType.getDeclaredFields(); for (java.lang.reflect.Field field : declaredFields) { li.annotation.Field column = field.getAnnotation(li.annotation.Field.class); if (!annotated || null != column) { // 如果不需要Field注解或者Field注解不为空 li.model.Field attribute = new li.model.Field(); attribute.name = field.getName(); attribute.column = (null == column || Verify.isEmpty(column.value())) ? field.getName() : column.value(); fields.add(attribute); } } if (Object.class != targetType.getSuperclass()) { // 扫描超类的Field fields.addAll(list(targetType.getSuperclass(), annotated)); } FIELDS_MAP.put("class#" + targetType.getName() + "#annotated#" + annotated, fields); // 加入缓存 } return fields; }