From 209f89ea994ad029e554b021a000db28e96f32d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Thu, 2 Sep 2010 14:08:34 +0000 Subject: [PATCH] #628838 : made sure that entity annotated enum Q-types extends PEnum --- .../com/mysema/query/apt/APTTypeFactory.java | 45 +++++++++++++------ .../mysema/query/domain/ComparableTest.java | 37 +++++++++++++++ .../com/mysema/query/domain/EnumTest.java | 36 +++++++++++++++ .../query/codegen/EmbeddableSerializer.java | 24 +++++----- .../query/codegen/EntitySerializer.java | 25 ++++++----- 5 files changed, 133 insertions(+), 34 deletions(-) create mode 100644 querydsl-apt/src/test/java/com/mysema/query/domain/ComparableTest.java create mode 100644 querydsl-apt/src/test/java/com/mysema/query/domain/EnumTest.java diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/APTTypeFactory.java b/querydsl-apt/src/main/java/com/mysema/query/apt/APTTypeFactory.java index 8c2d6ae01..e86fcbc6d 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/APTTypeFactory.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/APTTypeFactory.java @@ -155,13 +155,6 @@ public final class APTTypeFactory { } private Type createClassType(DeclaredType t, TypeElement typeElement) { - // entity type - for (Class entityAnn : entityAnnotations){ - if (typeElement.getAnnotation(entityAnn) != null){ - return create(typeElement, TypeCategory.ENTITY, t.getTypeArguments()); - } - } - // other String name = typeElement.getQualifiedName().toString(); TypeCategory typeCategory = TypeCategory.get(name); @@ -175,7 +168,25 @@ public final class APTTypeFactory { && isImplemented(typeElement, comparableType)){ typeCategory = TypeCategory.COMPARABLE; } - return create(typeElement, typeCategory, t.getTypeArguments()); + + if (typeCategory == TypeCategory.SIMPLE){ + for (Class entityAnn : entityAnnotations){ + if (typeElement.getAnnotation(entityAnn) != null){ + typeCategory = TypeCategory.ENTITY; + } + } + } + + Type type = create(typeElement, typeCategory, t.getTypeArguments()); + + // entity type + for (Class entityAnn : entityAnnotations){ + if (typeElement.getAnnotation(entityAnn) != null){ + return new EntityType(configuration.getNamePrefix(), type); + } + } + + return type; } private Type createCollectionType(String simpleName, @@ -196,7 +207,12 @@ public final class APTTypeFactory { entityTypeCache.put(key, null); Type value = handle(type); if (value != null){ - EntityType entityModel = new EntityType(configuration.getNamePrefix(), value); + EntityType entityModel = null; + if (value instanceof EntityType){ + entityModel = (EntityType)value; + }else{ + entityModel = new EntityType(configuration.getNamePrefix(), value); + } entityTypeCache.put(key, entityModel); if (key.size() > 1 && key.get(0).equals(entityModel.getFullName()) && doubleIndexEntities){ @@ -221,14 +237,15 @@ public final class APTTypeFactory { } private Type createEnumType(DeclaredType t, TypeElement typeElement) { + // fallback + Type enumType = create(typeElement, TypeCategory.ENUM, t.getTypeArguments()); + for (Class entityAnn : entityAnnotations){ if (typeElement.getAnnotation(entityAnn) != null){ - return create(typeElement, TypeCategory.ENTITY, t.getTypeArguments()); + return new EntityType(configuration.getNamePrefix(), enumType); } - } - - // fallback - return create(typeElement, TypeCategory.ENUM, t.getTypeArguments()); + } + return enumType; } private Type createInterfaceType(DeclaredType t, TypeElement typeElement) { diff --git a/querydsl-apt/src/test/java/com/mysema/query/domain/ComparableTest.java b/querydsl-apt/src/test/java/com/mysema/query/domain/ComparableTest.java new file mode 100644 index 000000000..647189cf1 --- /dev/null +++ b/querydsl-apt/src/test/java/com/mysema/query/domain/ComparableTest.java @@ -0,0 +1,37 @@ +package com.mysema.query.domain; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.mysema.query.annotations.QueryEmbeddable; +import com.mysema.query.annotations.QueryEntity; + +public class ComparableTest { + + @QueryEntity + public class CustomComparable implements Comparable{ + + @Override + public int compareTo(CustomComparable o) { + return 0; + } + + } + + @QueryEmbeddable + public class CustomComparable2 implements Comparable{ + + @Override + public int compareTo(CustomComparable2 o) { + return 0; + } + + } + + @Test + public void test(){ + assertNotNull(QComparableTest_CustomComparable.customComparable.asc()); + } + +} diff --git a/querydsl-apt/src/test/java/com/mysema/query/domain/EnumTest.java b/querydsl-apt/src/test/java/com/mysema/query/domain/EnumTest.java new file mode 100644 index 000000000..5f3d27a15 --- /dev/null +++ b/querydsl-apt/src/test/java/com/mysema/query/domain/EnumTest.java @@ -0,0 +1,36 @@ +package com.mysema.query.domain; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.mysema.query.annotations.QueryEmbeddable; +import com.mysema.query.annotations.QueryEntity; + +public class EnumTest { + + @QueryEntity + public enum Gender { + MALE, + FEMALE + } + + @QueryEmbeddable + public enum Gender2 { + MALE, + FEMALE + } + + @QueryEntity + public class Bean { + Gender gender; + } + + @Test + public void test(){ + assertNotNull(QEnumTest_Gender.gender.asc()); + assertNotNull(QEnumTest_Gender.gender.ordinal().asc()); + + } + +} diff --git a/querydsl-core/src/main/java/com/mysema/query/codegen/EmbeddableSerializer.java b/querydsl-core/src/main/java/com/mysema/query/codegen/EmbeddableSerializer.java index 3ac45b47d..545a99f1e 100644 --- a/querydsl-core/src/main/java/com/mysema/query/codegen/EmbeddableSerializer.java +++ b/querydsl-core/src/main/java/com/mysema/query/codegen/EmbeddableSerializer.java @@ -43,16 +43,20 @@ public final class EmbeddableSerializer extends EntitySerializer{ TypeCategory category = model.getOriginalCategory(); Class pathType; - switch(category){ - case COMPARABLE : pathType = PComparable.class; break; - case ENUM: pathType = PEnum.class; break; - case DATE: pathType = PDate.class; break; - case DATETIME: pathType = PDateTime.class; break; - case TIME: pathType = PTime.class; break; - case NUMERIC: pathType = PNumber.class; break; - case STRING: pathType = PString.class; break; - case BOOLEAN: pathType = PBoolean.class; break; - default : pathType = BeanPath.class; + if (model.getProperties().isEmpty()){ + switch(category){ + case COMPARABLE : pathType = PComparable.class; break; + case ENUM: pathType = PEnum.class; break; + case DATE: pathType = PDate.class; break; + case DATETIME: pathType = PDateTime.class; break; + case TIME: pathType = PTime.class; break; + case NUMERIC: pathType = PNumber.class; break; + case STRING: pathType = PString.class; break; + case BOOLEAN: pathType = PBoolean.class; break; + default : pathType = BeanPath.class; + } + }else{ + pathType = BeanPath.class; } for (Annotation annotation : model.getAnnotations()){ diff --git a/querydsl-core/src/main/java/com/mysema/query/codegen/EntitySerializer.java b/querydsl-core/src/main/java/com/mysema/query/codegen/EntitySerializer.java index 4c7fc08e9..4d740b4b7 100644 --- a/querydsl-core/src/main/java/com/mysema/query/codegen/EntitySerializer.java +++ b/querydsl-core/src/main/java/com/mysema/query/codegen/EntitySerializer.java @@ -238,16 +238,21 @@ public class EntitySerializer implements Serializer{ TypeCategory category = model.getOriginalCategory(); Class pathType; - switch(category){ - case COMPARABLE : pathType = PComparable.class; break; - case ENUM: pathType = PEnum.class; break; - case DATE: pathType = PDate.class; break; - case DATETIME: pathType = PDateTime.class; break; - case TIME: pathType = PTime.class; break; - case NUMERIC: pathType = PNumber.class; break; - case STRING: pathType = PString.class; break; - case BOOLEAN: pathType = PBoolean.class; break; - default : pathType = EntityPathBase.class; + + if (model.getProperties().isEmpty()){ + switch(category){ + case COMPARABLE : pathType = PComparable.class; break; + case ENUM: pathType = PEnum.class; break; + case DATE: pathType = PDate.class; break; + case DATETIME: pathType = PDateTime.class; break; + case TIME: pathType = PTime.class; break; + case NUMERIC: pathType = PNumber.class; break; + case STRING: pathType = PString.class; break; + case BOOLEAN: pathType = PBoolean.class; break; + default : pathType = EntityPathBase.class; + } + }else{ + pathType = EntityPathBase.class; } for (Annotation annotation : model.getAnnotations()){