improved handling of default values and boolean constants in annotations

This commit is contained in:
Timo Westkämper 2010-07-31 21:46:13 +00:00
parent e8d846c17d
commit aeef332601
4 changed files with 61 additions and 7 deletions

View File

@ -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;

View File

@ -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());
}
}

View File

@ -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();
}

View File

@ -6,7 +6,7 @@ import java.io.*;
@Entity
public class JavaWriterTest {
@org.junit.Test(timeout=0)
@org.junit.Test
public void test() {
}