From 6539b9e732b9a68f651f8ff0718764980a8414c8 Mon Sep 17 00:00:00 2001 From: fjuaneda Date: Mon, 15 Aug 2016 17:00:58 +0200 Subject: [PATCH] Add default message label to all annotations based on @NotNull template. --- .../addon/krasa/JaxbValidationsPlugins.java | 43 +++++++++++-------- .../com/sun/tools/xjc/addon/krasa/Utils.java | 10 ++++- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/sun/tools/xjc/addon/krasa/JaxbValidationsPlugins.java b/src/main/java/com/sun/tools/xjc/addon/krasa/JaxbValidationsPlugins.java index 3df4521..27362ee 100644 --- a/src/main/java/com/sun/tools/xjc/addon/krasa/JaxbValidationsPlugins.java +++ b/src/main/java/com/sun/tools/xjc/addon/krasa/JaxbValidationsPlugins.java @@ -167,8 +167,10 @@ public boolean run(Outline model, Options opt, ErrorHandler errorHandler) { /** * XS:Element + * @throws IllegalAccessException + * @throws InstantiationException */ - public void processElement(CElementPropertyInfo property, ClassOutline classOutline, Outline model) { + public void processElement(CElementPropertyInfo property, ClassOutline classOutline, Outline model) throws InstantiationException, IllegalAccessException { XSComponent schemaComponent = property.getSchemaComponent(); ParticleImpl particle = (ParticleImpl) schemaComponent; // must be reflection because of cxf-codegen @@ -189,14 +191,18 @@ public void processElement(CElementPropertyInfo property, ClassOutline classOutl log("@Size (" + minOccurs + "," + maxOccurs + ") " + propertyName(property) + " added to class " + classOutline.implClass.name()); - field.annotate(Size.class).param("min", minOccurs).param("max", maxOccurs); + Utils.fieldAnnotate(field, Size.class, propertyName(property)) + .param("min", minOccurs) + .param("max", maxOccurs); } } if (maxOccurs == -1 && minOccurs > 0) { // maxOccurs="unbounded" if (!hasAnnotation(field, Size.class)) { log("@Size (" + minOccurs + ") " + propertyName(property) + " added to class " + classOutline.implClass.name()); - field.annotate(Size.class).param("min", minOccurs); + + Utils.fieldAnnotate(field, Size.class, propertyName(property)) + .param("min", minOccurs); } } @@ -235,6 +241,7 @@ private void processNotNull(ClassOutline co, JFieldVar field) { if (notNullAnnotations) { log("@NotNull: " + field.name() + " added to class " + co.implClass.name()); JAnnotationUse annotation = field.annotate(NotNull.class); + if (notNullPrefixClassName) { annotation.param("message", String.format("%s.%s {%s.message}", co.implClass.name(), field.name(), NotNull.class.getName())); } else if (notNullPrefixFieldName) { @@ -269,17 +276,17 @@ public void processType(XSSimpleType simpleType, JFieldVar field, String propert if (maxLength != null && minLength != null) { log("@Size(" + minLength + "," + maxLength + "): " + propertyName + " added to class " + className); - field.annotate(Size.class).param("min", minLength).param("max", maxLength); + Utils.fieldAnnotate(field, Size.class, propertyName).param("min", minLength).param("max", maxLength); } else if (minLength != null) { log("@Size(" + minLength + ", null): " + propertyName + " added to class " + className); - field.annotate(Size.class).param("min", minLength); + Utils.fieldAnnotate(field, Size.class, propertyName).param("min", minLength); } else if (maxLength != null) { log("@Size(null, " + maxLength + "): " + propertyName + " added to class " + className); - field.annotate(Size.class).param("max", maxLength); + Utils.fieldAnnotate(field, Size.class, propertyName).param("max", maxLength); } else if (length != null) { log("@Size(" + length + "," + length + "): " + propertyName + " added to class " + className); - field.annotate(Size.class).param("min", length).param("max", length); + Utils.fieldAnnotate(field, Size.class, propertyName).param("min", length).param("max", length); } } if (jpaAnnotations && isSizeAnnotationApplicable(field)) { @@ -287,7 +294,7 @@ public void processType(XSSimpleType simpleType, JFieldVar field, String propert "maxLength").getValue().value); if (maxLength != null) { log("@Column(null, " + maxLength + "): " + propertyName + " added to class " + className); - field.annotate(Column.class).param("length", maxLength); + Utils.fieldAnnotate(field, Column.class, propertyName).param("length", maxLength); } } XSFacet maxInclusive = simpleType.getFacet("maxInclusive"); @@ -295,20 +302,20 @@ public void processType(XSSimpleType simpleType, JFieldVar field, String propert && !hasAnnotation(field, DecimalMax.class)) { log("@DecimalMax(" + maxInclusive.getValue().value + "): " + propertyName + " added to class " + className); - field.annotate(DecimalMax.class).param("value", maxInclusive.getValue().value); + Utils.fieldAnnotate(field, DecimalMax.class, propertyName).param("value", maxInclusive.getValue().value); } XSFacet minInclusive = simpleType.getFacet("minInclusive"); if (minInclusive != null && Utils.isNumber(field) && isValidValue(minInclusive) && !hasAnnotation(field, DecimalMin.class)) { log("@DecimalMin(" + minInclusive.getValue().value + "): " + propertyName + " added to class " + className); - field.annotate(DecimalMin.class).param("value", minInclusive.getValue().value); + Utils.fieldAnnotate(field, DecimalMin.class, propertyName).param("value", minInclusive.getValue().value); } XSFacet maxExclusive = simpleType.getFacet("maxExclusive"); if (maxExclusive != null && Utils.isNumber(field) && isValidValue(maxExclusive) && !hasAnnotation(field, DecimalMax.class)) { - JAnnotationUse annotate = field.annotate(DecimalMax.class); + JAnnotationUse annotate = Utils.fieldAnnotate(field, DecimalMax.class, propertyName); if (jsr349) { log("@DecimalMax(value = " + maxExclusive.getValue().value + ", inclusive = false): " + propertyName + " added to class " + className); @@ -323,7 +330,7 @@ public void processType(XSSimpleType simpleType, JFieldVar field, String propert XSFacet minExclusive = simpleType.getFacet("minExclusive"); if (minExclusive != null && Utils.isNumber(field) && isValidValue(minExclusive) && !hasAnnotation(field, DecimalMin.class)) { - JAnnotationUse annotate = field.annotate(DecimalMin.class); + JAnnotationUse annotate = Utils.fieldAnnotate(field, DecimalMin.class, propertyName); if (jsr349) { log("@DecimalMin(value = " + minExclusive.getValue().value + ", inclusive = false): " + propertyName + " added to class " + className); @@ -344,11 +351,11 @@ public void processType(XSSimpleType simpleType, JFieldVar field, String propert if (!hasAnnotation(field, Digits.class)) { log("@Digits(" + totalDigits + "," + fractionDigits + "): " + propertyName + " added to class " + className); - JAnnotationUse annox = field.annotate(Digits.class).param("integer", totalDigits); + JAnnotationUse annox = Utils.fieldAnnotate(field, Digits.class, propertyName).param("integer", totalDigits); annox.param("fraction", fractionDigits); } if (jpaAnnotations) { - field.annotate(Column.class).param("precision", totalDigits).param("scale", fractionDigits); + Utils.fieldAnnotate(field, Column.class, propertyName).param("precision", totalDigits).param("scale", fractionDigits); } } /** @@ -359,7 +366,7 @@ public void processType(XSSimpleType simpleType, JFieldVar field, String propert List patternList = simpleType.getFacets("pattern"); if (patternList.size() > 1) { // More than one pattern log("@Pattern.List: " + propertyName + " added to class " + className); - JAnnotationUse patternListAnnotation = field.annotate(Pattern.List.class); + JAnnotationUse patternListAnnotation = Utils.fieldAnnotate(field, Pattern.List.class, propertyName); JAnnotationArrayMember listValue = patternListAnnotation.paramArray("value"); if ("String".equals(field.type().name())) { @@ -378,7 +385,7 @@ public void processType(XSSimpleType simpleType, JFieldVar field, String propert if (!"\\c+".equals(pattern)) { log("@Pattern(" + pattern + "): " + propertyName + " added to class " + className); if (!hasAnnotation(field, Pattern.class)) { - field.annotate(Pattern.class).param("regexp", replaceXmlProprietals(pattern)); + Utils.fieldAnnotate(field, Pattern.class, propertyName).param("regexp", replaceXmlProprietals(pattern)); } } } @@ -386,7 +393,7 @@ public void processType(XSSimpleType simpleType, JFieldVar field, String propert final List enumerationList = simpleType.getFacets("enumeration"); if (enumerationList.size() > 1) { // More than one pattern log("@Pattern: " + propertyName + " added to class " + className); - final JAnnotationUse patternListAnnotation = field.annotate(Pattern.class); + final JAnnotationUse patternListAnnotation = Utils.fieldAnnotate(field, Pattern.class, propertyName); StringBuilder sb=new StringBuilder(); for (XSFacet xsFacet : enumerationList) { final String value = xsFacet.getValue().value; @@ -402,7 +409,7 @@ public void processType(XSSimpleType simpleType, JFieldVar field, String propert // cxf-codegen fix if (!"\\c+".equals(pattern)) { log("@Pattern(" + pattern + "): " + propertyName + " added to class " + className); - field.annotate(Pattern.class).param("regexp", replaceXmlProprietals(pattern)); + Utils.fieldAnnotate(field, Pattern.class, propertyName).param("regexp", replaceXmlProprietals(pattern)); } } } diff --git a/src/main/java/com/sun/tools/xjc/addon/krasa/Utils.java b/src/main/java/com/sun/tools/xjc/addon/krasa/Utils.java index c400710..876b827 100644 --- a/src/main/java/com/sun/tools/xjc/addon/krasa/Utils.java +++ b/src/main/java/com/sun/tools/xjc/addon/krasa/Utils.java @@ -1,10 +1,12 @@ package com.sun.tools.xjc.addon.krasa; +import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.math.BigInteger; +import com.sun.codemodel.JAnnotationUse; +import com.sun.codemodel.JClass; import com.sun.codemodel.JFieldVar; -import com.sun.codemodel.JType; /** * @author Vojtech Krasa @@ -13,6 +15,12 @@ public class Utils { public static final String[] NUMBERS = new String[]{"BigDecimal", "BigInteger", "String", "byte", "short", "int", "long"}; + public static JAnnotationUse fieldAnnotate(final JFieldVar jfv, final Class c, final String propName) { + return + jfv.annotate(c) + .param("message", propName + ": {javax.validation.constraints." + c.getSimpleName() + ".message}"); + } + public static int toInt(Object maxOccurs) { if (maxOccurs instanceof BigInteger) { // xjc