added support for array annotation values

updated version to 0.2.4
This commit is contained in:
Timo Westkämper 2010-09-02 12:21:19 +00:00
parent e1a7e93d3b
commit 21a6d7df23
6 changed files with 168 additions and 2 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.mysema.codegen</groupId>
<artifactId>codegen</artifactId>
<version>0.2.3-SNAPSHOT</version>
<version>0.2.4</version>
<name>Codegen</name>
<description>Code generation and compilation for Java</description>
<parent>
@ -58,6 +58,13 @@
<version>1.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.CR3</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -16,6 +16,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,6 +103,8 @@ public final class JavaWriter extends AbstractCodeWriter<JavaWriter>{
Object value = method.invoke(annotation);
if (value == null || value.equals(method.getDefaultValue())){
continue;
}else if (value.getClass().isArray() && Arrays.equals((Object[])value, (Object[])method.getDefaultValue())){
continue;
}else if (!first){
append(COMMA);
}else{
@ -133,7 +136,18 @@ public final class JavaWriter extends AbstractCodeWriter<JavaWriter>{
@SuppressWarnings("unchecked")
private void annotationConstant(Object value) throws IOException{
if (value instanceof Class){
if (value.getClass().isArray()){
append("{");
boolean first = true;
for (Object o : (Object[])value){
if (!first){
append(", ");
}
annotationConstant(o);
first = false;
}
append("}");
}else if (value instanceof Class){
appendType((Class)value).append(".class");
}else if (value instanceof Number || value instanceof Boolean){
append(value.toString());

View File

@ -46,5 +46,23 @@ public class AnnotationTest {
writer.annotation(getClass().getMethod("testMethodAnnotation").getAnnotation(Test.class));
assertEquals("@org.junit.Test", w.toString().trim());
}
@Test
public void testMin() throws IOException{
writer.annotation(new MinImpl(10));
assertEquals("@javax.validation.constraints.Min(value=10)", w.toString().trim());
}
@Test
public void testMax() throws IOException{
writer.annotation(new MaxImpl(10));
assertEquals("@javax.validation.constraints.Max(value=10)", w.toString().trim());
}
@Test
public void testNotNull() throws IOException{
writer.annotation(new NotNullImpl());
assertEquals("@javax.validation.constraints.NotNull", w.toString().trim());
}
}

View File

@ -0,0 +1,46 @@
package com.mysema.codegen;
import java.lang.annotation.Annotation;
import javax.validation.ConstraintPayload;
import javax.validation.constraints.*;
/**
* @author tiwe
*
*/
@SuppressWarnings("all")
public class MaxImpl implements Max{
private final long value;
public MaxImpl(long value) {
this.value = value;
}
@Override
public Class<?>[] groups() {
return new Class[0];
}
@Override
public String message() {
return "{javax.validation.constraints.Max.message}";
}
@Override
public Class<? extends ConstraintPayload>[] payload() {
return new Class[0];
}
@Override
public long value() {
return value;
}
@Override
public Class<? extends Annotation> annotationType() {
return Max.class;
}
}

View File

@ -0,0 +1,46 @@
package com.mysema.codegen;
import java.lang.annotation.Annotation;
import javax.validation.ConstraintPayload;
import javax.validation.constraints.*;
/**
* @author tiwe
*
*/
@SuppressWarnings("all")
public class MinImpl implements Min{
private final long value;
public MinImpl(long value) {
this.value = value;
}
@Override
public Class<?>[] groups() {
return new Class[0];
}
@Override
public String message() {
return "{javax.validation.constraints.Min.message}";
}
@Override
public Class<? extends ConstraintPayload>[] payload() {
return new Class[0];
}
@Override
public long value() {
return value;
}
@Override
public Class<? extends Annotation> annotationType() {
return Min.class;
}
}

View File

@ -0,0 +1,35 @@
package com.mysema.codegen;
import java.lang.annotation.Annotation;
import javax.validation.ConstraintPayload;
import javax.validation.constraints.*;
/**
* @author tiwe
*
*/
@SuppressWarnings("all")
public class NotNullImpl implements NotNull{
@Override
public Class<?>[] groups() {
return new Class[0];
}
@Override
public String message() {
return "{javax.validation.constraints.NotNull.message}";
}
@Override
public Class<? extends ConstraintPayload>[] payload() {
return new Class[0];
}
@Override
public Class<? extends Annotation> annotationType() {
return NotNull.class;
}
}