updated version to 0.2.7

This commit is contained in:
Timo Westkämper 2010-09-21 13:09:40 +00:00
parent a638e01173
commit fa81781584
3 changed files with 46 additions and 15 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.mysema.codegen</groupId>
<artifactId>codegen</artifactId>
<version>0.2.6-SNAPSHOT</version>
<version>0.2.7</version>
<name>Codegen</name>
<description>Code generation and compilation for Java</description>
<parent>

View File

@ -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<ScalaWriter>{
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<ScalaWriter>{
@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())){

View File

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