Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default message label to all annotations based on @NotNull template. #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -269,46 +276,46 @@ 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)) {
Integer maxLength = simpleType.getFacet("maxLength") == null ? null : Utils.parseInt(simpleType.getFacet(
"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");
if (maxInclusive != null && Utils.isNumber(field) && isValidValue(maxInclusive)
&& !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);
Expand All @@ -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);
Expand All @@ -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);
}
}
/**
Expand All @@ -359,7 +366,7 @@ public void processType(XSSimpleType simpleType, JFieldVar field, String propert
List<XSFacet> 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())) {
Expand All @@ -378,15 +385,15 @@ 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));
}
}
}
} else if ("String".equals(field.type().name())) {
final List<XSFacet> 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;
Expand All @@ -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));
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/sun/tools/xjc/addon/krasa/Utils.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <? extends Annotation> 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
Expand Down