@Override public JsonInclude.Include findSerializationInclusion(Annotated a, JsonInclude.Include defValue) { JsonInclude inc = _findAnnotation(a, JsonInclude.class); if (inc != null) { return inc.value(); } JsonSerialize ann = _findAnnotation(a, JsonSerialize.class); if (ann != null) { @SuppressWarnings("deprecation") JsonSerialize.Inclusion i2 = ann.include(); switch (i2) { case ALWAYS: return JsonInclude.Include.ALWAYS; case NON_NULL: return JsonInclude.Include.NON_NULL; case NON_DEFAULT: return JsonInclude.Include.NON_DEFAULT; case NON_EMPTY: return JsonInclude.Include.NON_EMPTY; case DEFAULT_INCLUSION: // since 2.3 -- fall through, use default break; } } return defValue; }
@Override public Object findNullSerializer(Annotated a) { JsonSerialize ann = _findAnnotation(a, JsonSerialize.class); if (ann != null) { Class<? extends JsonSerializer<?>> serClass = ann.nullsUsing(); if (serClass != JsonSerializer.None.class) { return serClass; } } return null; }
@Override public Object findSerializer(Annotated a) { JsonSerialize ann = _findAnnotation(a, JsonSerialize.class); if (ann != null) { Class<? extends JsonSerializer<?>> serClass = ann.using(); if (serClass != JsonSerializer.None.class) { return serClass; } } /* 18-Oct-2010, tatu: [JACKSON-351] @JsonRawValue handled just here, for now; * if we need to get raw indicator from other sources need to add * separate accessor within {@link AnnotationIntrospector} interface. */ JsonRawValue annRaw = _findAnnotation(a, JsonRawValue.class); if ((annRaw != null) && annRaw.value()) { // let's construct instance with nominal type: Class<?> cls = a.getRawType(); return new RawSerializer<Object>(cls); } return null; }
@SuppressWarnings({"resource", "rawtypes", "unchecked"}) public static void main(String[] args) throws Exception { Configuration cfg = new Configuration(); // 设置FreeMarker的模版文件位置 cfg.setClassForTemplateLoading( SourceCodeFrameworkBuilder.class, "/lab/s2jh/tool/builder/freemarker"); cfg.setDefaultEncoding("UTF-8"); String rootPath = args[0]; Set<String> entityNames = new HashSet<String>(); String entityListFile = rootPath + "entity_list.properties"; BufferedReader reader = new BufferedReader(new FileReader(entityListFile)); String line; while ((line = reader.readLine()) != null) { if (StringUtils.isNotBlank(line) && !line.startsWith("#")) { entityNames.add(line); } } new File(rootPath + "\\codes").mkdir(); new File(rootPath + "\\codes\\integrate").mkdir(); new File(rootPath + "\\codes\\standalone").mkdir(); for (String entityName : entityNames) { String integrateRootPath = rootPath + "\\codes\\integrate\\"; String standaloneRootPath = rootPath + "\\codes\\standalone\\"; String rootPackage = StringUtils.substringBetween(entityName, "[", "]"); String rootPackagePath = StringUtils.replace(rootPackage, ".", "\\"); String className = StringUtils.substringAfterLast(entityName, "."); String classFullName = StringUtils.replaceEach(entityName, new String[] {"[", "]"}, new String[] {"", ""}); String modelName = StringUtils.substringBetween(entityName, "].", ".entity"); String modelPath = StringUtils.replace(modelName, ".", "/"); modelPath = "/" + modelPath; String modelPackagePath = StringUtils.replace(modelName, ".", "\\"); modelPackagePath = "\\" + modelPackagePath; Map<String, Object> root = new HashMap<String, Object>(); String nameField = propertyToField(StringUtils.uncapitalize(className)).toLowerCase(); root.put("model_name", modelName); root.put("model_path", modelPath); root.put("entity_name", className); root.put("entity_name_uncapitalize", StringUtils.uncapitalize(className)); root.put("entity_name_field", nameField); root.put("root_package", rootPackage + "." + modelName); root.put("action_package", rootPackage); root.put("table_name", "T_TODO_" + className.toUpperCase()); root.put("base", "${base}"); Class entityClass = Class.forName(classFullName); root.put("id_type", entityClass.getMethod("getId").getReturnType().getSimpleName()); MetaData classEntityComment = (MetaData) entityClass.getAnnotation(MetaData.class); if (classEntityComment != null) { root.put("model_title", classEntityComment.value()); } else { root.put("model_title", entityName); } debug("Entity Data Map=" + root); Set<Field> fields = new HashSet<Field>(); Field[] curfields = entityClass.getDeclaredFields(); for (Field field : curfields) { fields.add(field); } Class superClass = entityClass.getSuperclass(); while (superClass != null && !superClass.equals(BaseEntity.class)) { Field[] superfields = superClass.getDeclaredFields(); for (Field field : superfields) { fields.add(field); } superClass = superClass.getSuperclass(); } // 定义用于OneToOne关联对象的Fetch参数 Map<String, String> fetchJoinFields = Maps.newHashMap(); List<EntityCodeField> entityFields = new ArrayList<EntityCodeField>(); int cnt = 1; for (Field field : fields) { if ((field.getModifiers() & Modifier.FINAL) != 0 || "id".equals(field.getName())) { continue; } debug(" - Field=" + field); Class fieldType = field.getType(); EntityCodeField entityCodeField = null; if (fieldType.isEnum()) { entityCodeField = new EntityCodeField(); entityCodeField.setListFixed(true); entityCodeField.setListWidth(80); entityCodeField.setListAlign("center"); } else if (fieldType == Boolean.class) { entityCodeField = new EntityCodeField(); entityCodeField.setListFixed(true); entityCodeField.setListWidth(60); entityCodeField.setListAlign("center"); } else if (PersistableEntity.class.isAssignableFrom(fieldType)) { entityCodeField = new EntityCodeField(); entityCodeField.setFieldType("Entity"); } else if (Number.class.isAssignableFrom(fieldType)) { entityCodeField = new EntityCodeField(); entityCodeField.setListFixed(true); entityCodeField.setListWidth(60); entityCodeField.setListAlign("right"); } else if (fieldType == String.class) { entityCodeField = new EntityCodeField(); // 根据Hibernate注解的字符串类型和长度设定是否列表显示 Method getMethod = entityClass.getMethod("get" + StringUtils.capitalize(field.getName())); Column fieldColumn = getMethod.getAnnotation(Column.class); if (fieldColumn != null) { int length = fieldColumn.length(); if (length > 255) { entityCodeField.setList(false); entityCodeField.setListWidth(length); } } Lob fieldLob = getMethod.getAnnotation(Lob.class); if (fieldLob != null) { entityCodeField.setList(false); entityCodeField.setListWidth(Integer.MAX_VALUE); } } else if (fieldType == Date.class) { entityCodeField = new EntityCodeField(); entityCodeField.setListFixed(true); // 根据Json注解设定合理的列宽 entityCodeField.setListWidth(120); Method getMethod = entityClass.getMethod("get" + StringUtils.capitalize(field.getName())); JsonSerialize fieldJsonSerialize = getMethod.getAnnotation(JsonSerialize.class); if (fieldJsonSerialize != null) { if (DateJsonSerializer.class.equals(fieldJsonSerialize.using())) { entityCodeField.setListWidth(80); } } entityCodeField.setListAlign("center"); } if (entityCodeField != null) { if (fieldType.isEnum()) { entityCodeField.setEnumField(true); } if (StringUtils.isBlank(entityCodeField.getFieldType())) { entityCodeField.setFieldType(fieldType.getSimpleName()); } entityCodeField.setFieldName(field.getName()); EntityAutoCode entityAutoCode = field.getAnnotation(EntityAutoCode.class); if (entityAutoCode != null) { entityCodeField.setListHidden(entityAutoCode.listHidden()); entityCodeField.setEdit(entityAutoCode.edit()); entityCodeField.setList(entityAutoCode.listHidden() || entityAutoCode.listShow()); entityCodeField.setOrder(entityAutoCode.order()); } else { entityCodeField.setTitle(field.getName()); entityCodeField.setOrder(cnt++); } MetaData entityMetaData = field.getAnnotation(MetaData.class); if (entityMetaData != null) { entityCodeField.setTitle(entityMetaData.value()); } Method getMethod = entityClass.getMethod("get" + StringUtils.capitalize(field.getName())); JsonProperty fieldJsonProperty = getMethod.getAnnotation(JsonProperty.class); if (fieldJsonProperty != null) { entityCodeField.setList(true); } if (entityCodeField.getList() || entityCodeField.getListHidden()) { JoinColumn fieldJoinColumn = getMethod.getAnnotation(JoinColumn.class); if (fieldJoinColumn != null) { if (fieldJoinColumn.nullable() == false) { fetchJoinFields.put(field.getName(), "INNER"); } else { fetchJoinFields.put(field.getName(), "LEFT"); } } } entityFields.add(entityCodeField); } } Collections.sort(entityFields); root.put("entityFields", entityFields); if (fetchJoinFields.size() > 0) { root.put("fetchJoinFields", fetchJoinFields); } integrateRootPath = integrateRootPath + rootPackagePath + modelPackagePath; // process(cfg.getTemplate("Entity.ftl"), root, integrateRootPath + "\\entity\\", className + // ".java"); process( cfg.getTemplate("Dao.ftl"), root, integrateRootPath + "\\dao\\", className + "Dao.java"); process( cfg.getTemplate("Service.ftl"), root, integrateRootPath + "\\service\\", className + "Service.java"); process( cfg.getTemplate("Controller.ftl"), root, integrateRootPath + "\\web\\action\\", className + "Controller.java"); process( cfg.getTemplate("Test.ftl"), root, integrateRootPath + "\\test\\service\\", className + "ServiceTest.java"); process( cfg.getTemplate("JSP_Index.ftl"), root, integrateRootPath + "\\jsp\\", nameField + "-index.jsp"); process( cfg.getTemplate("JSP_Input_Tabs.ftl"), root, integrateRootPath + "\\jsp\\", nameField + "-inputTabs.jsp"); process( cfg.getTemplate("JSP_Input_Basic.ftl"), root, integrateRootPath + "\\jsp\\", nameField + "-inputBasic.jsp"); process( cfg.getTemplate("JSP_View_Tabs.ftl"), root, integrateRootPath + "\\jsp\\", nameField + "-viewTabs.jsp"); process( cfg.getTemplate("JSP_View_Basic.ftl"), root, integrateRootPath + "\\jsp\\", nameField + "-viewBasic.jsp"); standaloneRootPath = standaloneRootPath + rootPackagePath + modelPackagePath + "\\" + className; // process(cfg.getTemplate("Entity.ftl"), root, standaloneRootPath + "\\entity\\", className + // ".java"); process( cfg.getTemplate("Dao.ftl"), root, standaloneRootPath + "\\dao\\", className + "Dao.java"); process( cfg.getTemplate("Service.ftl"), root, standaloneRootPath + "\\service\\", className + "Service.java"); process( cfg.getTemplate("Controller.ftl"), root, standaloneRootPath + "\\web\\action\\", className + "Controller.java"); process( cfg.getTemplate("Test.ftl"), root, standaloneRootPath + "\\test\\service\\", className + "ServiceTest.java"); process( cfg.getTemplate("JSP_Index.ftl"), root, standaloneRootPath + "\\jsp\\", nameField + "-index.jsp"); process( cfg.getTemplate("JSP_Input_Tabs.ftl"), root, standaloneRootPath + "\\jsp\\", nameField + "-inputTabs.jsp"); process( cfg.getTemplate("JSP_Input_Basic.ftl"), root, standaloneRootPath + "\\jsp\\", nameField + "-inputBasic.jsp"); process( cfg.getTemplate("JSP_View_Tabs.ftl"), root, standaloneRootPath + "\\jsp\\", nameField + "-viewTabs.jsp"); process( cfg.getTemplate("JSP_View_Basic.ftl"), root, standaloneRootPath + "\\jsp\\", nameField + "-viewBasic.jsp"); } }
@Override public Object findSerializationContentConverter(AnnotatedMember a) { JsonSerialize ann = _findAnnotation(a, JsonSerialize.class); return (ann == null) ? null : _classIfExplicit(ann.contentConverter(), Converter.None.class); }
@Override public JsonSerialize.Typing findSerializationTyping(Annotated a) { JsonSerialize ann = _findAnnotation(a, JsonSerialize.class); return (ann == null) ? null : ann.typing(); }
@Override public Class<?> findSerializationContentType(Annotated am, JavaType baseType) { JsonSerialize ann = _findAnnotation(am, JsonSerialize.class); return (ann == null) ? null : _classIfExplicit(ann.contentAs()); }
@Override public Class<?> findSerializationType(Annotated am) { JsonSerialize ann = _findAnnotation(am, JsonSerialize.class); return (ann == null) ? null : _classIfExplicit(ann.as()); }
public static void buildValidatorAttribute( String validatorAttrValue, AbstractUITag tag, ValueStack stack, HttpServletRequest req, UIBean uiBean) { Map<String, Object> dynamicAttributes = new HashMap<String, Object>(); if (validatorAttrValue == null) { Map<String, Object> validator = new HashMap<String, Object>(); Map<String, String> messages = new HashMap<String, String>(); CompoundRoot rootList = stack.getRoot(); Object entity = null; Object controller = null; for (Object obj : rootList) { if (obj instanceof Persistable) { entity = obj; } else if (obj instanceof ActionSupport) { controller = obj; } } if (entity != null) { try { String tagName = tag.name; Method method = OgnlRuntime.getGetMethod( (OgnlContext) stack.getContext(), entity.getClass(), tagName); if (method == null) { String[] tagNameSplits = StringUtils.split(tagName, "."); if (tagNameSplits.length >= 2) { Class<?> retClass = entity.getClass(); for (String tagNameSplit : tagNameSplits) { method = OgnlRuntime.getGetMethod( (OgnlContext) stack.getContext(), retClass, tagNameSplit); if (method != null) { retClass = method.getReturnType(); } } if (method == null) { retClass = controller.getClass(); for (String tagNameSplit : tagNameSplits) { method = OgnlRuntime.getGetMethod( (OgnlContext) stack.getContext(), retClass, tagNameSplit); if (method != null) { retClass = method.getReturnType(); } } } } } if (method != null) { Column column = method.getAnnotation(Column.class); if (column != null) { if (column.nullable() == false) { if (tag.requiredLabel == null) { uiBean.setRequiredLabel("true"); } } if (column.unique() == true) { validator.put("unique", "true"); } } Class<?> retType = method.getReturnType(); if (retType == LocalDate.class) { validator.put("date", true); validator.put("dateISO", true); } else if (retType == LocalDateTime.class) { validator.put("timestamp", true); } else if (retType == DateTime.class || retType == Date.class) { JsonSerialize jsonSerialize = method.getAnnotation(JsonSerialize.class); if (jsonSerialize != null) { if (JodaDateJsonSerializer.class == jsonSerialize.using() || DateJsonSerializer.class == jsonSerialize.using()) { validator.put("date", true); validator.put("dateISO", true); } else if (JodaDateTimeJsonSerializer.class == jsonSerialize.using() || DateTimeJsonSerializer.class == jsonSerialize.using()) { validator.put("timestamp", true); } } } else if (retType == BigDecimal.class) { validator.put("number", true); } else if (retType == Integer.class) { validator.put("number", true); validator.put("digits", true); } else if (retType == Long.class) { validator.put("number", true); validator.put("digits", true); } Size size = method.getAnnotation(Size.class); if (size != null) { if (size.min() > 0) { validator.put("minlength", size.min()); } if (size.max() < Integer.MAX_VALUE) { validator.put("maxlength", size.max()); } } Email email = method.getAnnotation(Email.class); if (email != null) { validator.put("email", true); } Pattern pattern = method.getAnnotation(Pattern.class); if (pattern != null) { validator.put("regex", pattern.regexp()); String message = pattern.message(); if (!"{javax.validation.constraints.Pattern.message}".equals(message)) { messages.put("regex", message); } } } } catch (IntrospectionException e) { e.printStackTrace(); } catch (OgnlException e) { e.printStackTrace(); } } if (validator.size() > 0) { try { if (messages.size() > 0) { validator.put("messages", messages); } String json = mapper.writeValueAsString(validator); json = json.replaceAll("\\\"", "'"); dynamicAttributes.put("validator", json); uiBean.setDynamicAttributes(dynamicAttributes); } catch (JsonProcessingException e) { e.printStackTrace(); } } } else { dynamicAttributes.put("validator", validatorAttrValue); uiBean.setDynamicAttributes(dynamicAttributes); } }