diff --git a/pom.xml b/pom.xml index df2f9aaf3..1ec314524 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.mysema.codegen codegen - 0.2.6-SNAPSHOT + 0.2.7 Codegen Code generation and compilation for Java diff --git a/src/main/java/com/mysema/codegen/ScalaWriter.java b/src/main/java/com/mysema/codegen/ScalaWriter.java index 58e10de64..c15d37a38 100644 --- a/src/main/java/com/mysema/codegen/ScalaWriter.java +++ b/src/main/java/com/mysema/codegen/ScalaWriter.java @@ -10,6 +10,7 @@ import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Set; @@ -102,7 +103,10 @@ public class ScalaWriter extends AbstractCodeWriter{ for (Method method : methods){ try { Object value = method.invoke(annotation); - if (value == null || value.equals(method.getDefaultValue())){ + if (value == null + || value.equals(method.getDefaultValue()) + || (value.getClass().isArray() + && Arrays.equals((Object[])value, (Object[])method.getDefaultValue()))){ continue; }else if (!first){ append(COMMA); @@ -134,19 +138,32 @@ public class ScalaWriter extends AbstractCodeWriter{ @SuppressWarnings("unchecked") private void annotationConstant(Object value) throws IOException{ - if (value instanceof Class){ - appendType((Class)value).append(".class"); - }else if (value instanceof Number || value instanceof Boolean){ - append(value.toString()); - }else if (value instanceof Enum){ - Enum enumValue = (Enum)value; - append(enumValue.getDeclaringClass().getName()+DOT+enumValue.name()); - }else if (value instanceof String){ - append(QUOTE + StringEscapeUtils.escapeJava(value.toString()) + QUOTE); - }else{ - throw new IllegalArgumentException("Unsupported annotation value : " + value); - } - } + if (value.getClass().isArray()){ + append("Array("); + boolean first = true; + for (Object o : (Object[])value){ + if (!first){ + append(", "); + } + annotationConstant(o); + first = false; + } + append(")"); + }else if (value instanceof Class){ + append("classOf["); + appendType((Class)value); + append("]"); + }else if (value instanceof Number || value instanceof Boolean){ + append(value.toString()); + }else if (value instanceof Enum){ + Enum enumValue = (Enum)value; + append(enumValue.getDeclaringClass().getName()+DOT+enumValue.name()); + }else if (value instanceof String){ + append(QUOTE + StringEscapeUtils.escapeJava(value.toString()) + QUOTE); + }else{ + throw new IllegalArgumentException("Unsupported annotation value : " + value); + } + } private ScalaWriter appendType(Class type) throws IOException{ if (classes.contains(type.getName()) || packages.contains(type.getPackage().getName())){ diff --git a/src/test/java/com/mysema/codegen/ScalaWriterTest.java b/src/test/java/com/mysema/codegen/ScalaWriterTest.java index 49b1097cc..a351bbe93 100644 --- a/src/test/java/com/mysema/codegen/ScalaWriterTest.java +++ b/src/test/java/com/mysema/codegen/ScalaWriterTest.java @@ -9,6 +9,8 @@ import java.lang.annotation.Annotation; import java.util.Arrays; import java.util.List; +import javax.validation.constraints.Max; + import org.apache.commons.collections15.Transformer; import org.junit.Before; import org.junit.Test; @@ -172,6 +174,18 @@ public class ScalaWriterTest { System.out.println(w); } + @Test + public void AnnotationConstant() throws IOException{ + Max annotation = new MaxImpl(0l){ + @Override + public Class[] groups() { + return new Class[]{Object.class, String.class}; + } + }; + writer.annotation(annotation); + + System.out.println(w); + } @Test public void Annotations() throws IOException{