mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
improved handling of default values and boolean constants in annotations
This commit is contained in:
parent
e8d846c17d
commit
aeef332601
@ -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;
|
||||
|
||||
28
src/test/java/com/mysema/codegen/AnnotationTest.java
Normal file
28
src/test/java/com/mysema/codegen/AnnotationTest.java
Normal 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());
|
||||
}
|
||||
|
||||
}
|
||||
19
src/test/java/com/mysema/codegen/TestAnnotation.java
Normal file
19
src/test/java/com/mysema/codegen/TestAnnotation.java
Normal 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();
|
||||
|
||||
}
|
||||
@ -6,7 +6,7 @@ import java.io.*;
|
||||
@Entity
|
||||
public class JavaWriterTest {
|
||||
|
||||
@org.junit.Test(timeout=0)
|
||||
@org.junit.Test
|
||||
public void test() {
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user