diff --git a/src/main/java/com/mysema/codegen/JavaWriter.java b/src/main/java/com/mysema/codegen/JavaWriter.java index 1d59a6dd0..5f833af0d 100644 --- a/src/main/java/com/mysema/codegen/JavaWriter.java +++ b/src/main/java/com/mysema/codegen/JavaWriter.java @@ -85,12 +85,14 @@ public final class JavaWriter implements Appendable, CodeWriter{ @Override public JavaWriter annotation(Annotation annotation) throws IOException { - append(indent).append("@").appendType(annotation.annotationType()).append("("); + append(indent).append("@").appendType(annotation.annotationType()); Method[] methods = annotation.annotationType().getDeclaredMethods(); if (methods.length == 1 && methods[0].getName().equals("value")){ try { Object value = methods[0].invoke(annotation); + append("("); annotationConstant(value); + append(")"); } catch (IllegalArgumentException e) { throw new CodegenException(e); } catch (IllegalAccessException e) { @@ -103,10 +105,12 @@ public final class JavaWriter implements Appendable, CodeWriter{ for (Method method : methods){ try { Object value = method.invoke(annotation); - if (value == null){ + if (value == null || value.equals(method.getDefaultValue())){ continue; }else if (!first){ append(COMMA); + }else{ + append("("); } append(method.getName()+"="); annotationConstant(value); @@ -118,9 +122,12 @@ public final class JavaWriter implements Appendable, CodeWriter{ throw new CodegenException(e); } first = false; - } - } - return append(")").nl(); + } + if (!first){ + append(")"); + } + } + return nl(); } @@ -133,7 +140,7 @@ public final class JavaWriter implements Appendable, CodeWriter{ private void annotationConstant(Object value) throws IOException{ if (value instanceof Class){ appendType((Class)value).append(".class"); - }else if (value instanceof Number){ + }else if (value instanceof Number || value instanceof Boolean){ append(value.toString()); }else if (value instanceof Enum){ Enum enumValue = (Enum)value; diff --git a/src/test/java/com/mysema/codegen/AnnotationTest.java b/src/test/java/com/mysema/codegen/AnnotationTest.java new file mode 100644 index 000000000..75570e9cc --- /dev/null +++ b/src/test/java/com/mysema/codegen/AnnotationTest.java @@ -0,0 +1,28 @@ +package com.mysema.codegen; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.io.StringWriter; + +import org.junit.Test; + +@TestAnnotation(prop2 = false) +public class AnnotationTest { + + private StringWriter w = new StringWriter(); + private CodeWriter writer = new JavaWriter(w); + + @Test + public void testClassAnnotation() throws IOException{ + writer.annotation(getClass().getAnnotation(TestAnnotation.class)); + assertEquals("@com.mysema.codegen.TestAnnotation(prop2=false)", w.toString().trim()); + } + + @Test + public void testMethodAnnotation() throws IOException, SecurityException, NoSuchMethodException { + writer.annotation(getClass().getMethod("testMethodAnnotation").getAnnotation(Test.class)); + assertEquals("@org.junit.Test", w.toString().trim()); + } + +} diff --git a/src/test/java/com/mysema/codegen/TestAnnotation.java b/src/test/java/com/mysema/codegen/TestAnnotation.java new file mode 100644 index 000000000..5641a62b6 --- /dev/null +++ b/src/test/java/com/mysema/codegen/TestAnnotation.java @@ -0,0 +1,19 @@ +package com.mysema.codegen; + +import static java.lang.annotation.ElementType.TYPE; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Documented +@Target( { TYPE }) +@Retention(RetentionPolicy.RUNTIME) +public @interface TestAnnotation { + + boolean prop1() default false; + + boolean prop2(); + +} diff --git a/src/test/resources/testAnnotations2 b/src/test/resources/testAnnotations2 index 1e5ecc785..c430e6aea 100644 --- a/src/test/resources/testAnnotations2 +++ b/src/test/resources/testAnnotations2 @@ -6,7 +6,7 @@ import java.io.*; @Entity public class JavaWriterTest { - @org.junit.Test(timeout=0) + @org.junit.Test public void test() { }