#104 fixed QueryEmbedded handling

This commit is contained in:
Timo Westkämper 2012-02-27 20:51:03 +02:00
parent ea01eb9183
commit d3163bfa7a
8 changed files with 220 additions and 24 deletions

View File

@ -27,6 +27,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
@ -222,6 +223,41 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor {
// from annotation less supertypes
elements.addAll(getAnnotationlessSupertypes(elements));
// register possible embedded types of non-tracked supertypes
if (conf.getEmbeddedAnnotation() != null) {
Class<? extends Annotation> embedded = conf.getEmbeddedAnnotation();
Set<Element> embeddedElements = new HashSet<Element>();
for (Element element : elements) {
TypeMirror superTypeMirror = ((TypeElement)element).getSuperclass();
while (superTypeMirror != null) {
TypeElement superTypeElement = (TypeElement) processingEnv.getTypeUtils().asElement(superTypeMirror);
if (superTypeElement != null) {
List<? extends Element> enclosed = superTypeElement.getEnclosedElements();
for (Element child : enclosed) {
if (child.getAnnotation(embedded) != null) {
handleEmbeddedType(child, embeddedElements);
}
}
superTypeMirror = superTypeElement.getSuperclass();
if (superTypeMirror instanceof NoType) {
superTypeMirror = null;
}
} else {
superTypeMirror = null;
}
}
}
// register found elements
for (Element element : embeddedElements) {
if (!elements.contains(element)) {
elementHandler.handleEntityType((TypeElement)element);
}
}
}
return elements;
}
@ -274,31 +310,35 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor {
// only creation
for (Element element : getElements(conf.getEmbeddedAnnotation())) {
TypeMirror type = element.asType();
if (element.getKind() == ElementKind.METHOD){
type = ((ExecutableElement)element).getReturnType();
}
String typeName = type.toString();
if (typeName.startsWith(Collection.class.getName())
|| typeName.startsWith(List.class.getName())
|| typeName.startsWith(Set.class.getName())) {
type = ((DeclaredType)type).getTypeArguments().get(0);
} else if (typeName.startsWith(Map.class.getName())){
type = ((DeclaredType)type).getTypeArguments().get(1);
}
TypeElement typeElement = typeExtractor.visit(type);
if (typeElement != null && !TypeUtils.hasAnnotationOfType(typeElement, conf.getEntityAnnotations())) {
if (!typeElement.getQualifiedName().toString().startsWith("java.")) {
elements.add(typeElement);
}
}
handleEmbeddedType(element, elements);
}
return elements;
}
private void handleEmbeddedType(Element element, Set<Element> elements) {
TypeMirror type = element.asType();
if (element.getKind() == ElementKind.METHOD){
type = ((ExecutableElement)element).getReturnType();
}
String typeName = type.toString();
if (typeName.startsWith(Collection.class.getName())
|| typeName.startsWith(List.class.getName())
|| typeName.startsWith(Set.class.getName())) {
type = ((DeclaredType)type).getTypeArguments().get(0);
} else if (typeName.startsWith(Map.class.getName())){
type = ((DeclaredType)type).getTypeArguments().get(1);
}
TypeElement typeElement = typeExtractor.visit(type);
if (typeElement != null && !TypeUtils.hasAnnotationOfType(typeElement, conf.getEntityAnnotations())) {
if (!typeElement.getQualifiedName().toString().startsWith("java.")) {
elements.add(typeElement);
}
}
}
private Set<TypeElement> getTypeFromProperties(Set<Element> parents) {

View File

@ -135,6 +135,7 @@ public final class TypeElementHandler {
return entityType;
}
private Property toProperty(EntityType entityType, String name, TypeMirror type,
Annotations annotations) {
@ -146,7 +147,7 @@ public final class TypeElementHandler {
return null;
}
propertyType = propertyType.as(typeCategory);
}
}
// inits
String[] inits = new String[0];

View File

@ -0,0 +1,19 @@
package com.mysema.query.domain;
import com.mysema.query.annotations.QueryEntity;
@QueryEntity
public class Subclass extends com.mysema.query.domain.Superclass {
private int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}

View File

@ -22,6 +22,8 @@ import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import com.mysema.query.domain.Cat;
public class GenericExporterTest {
private GenericExporter exporter;
@ -39,7 +41,7 @@ public class GenericExporterTest {
String str = FileUtils.readFileToString(new File("target/gen1-jpa/com/mysema/query/codegen/QGroup.java"));
assertTrue(str.contains("QGroup group = new QGroup(\"group1\");"));
}
@Test
public void Export() {
exporter.setTargetFolder(new File("target/gen1"));
@ -98,5 +100,12 @@ public class GenericExporterTest {
exporter.export(getClass().getPackage());
assertTrue(new File("target/gen5/com/mysema/query/codegen/QExampleEmbeddable.java").exists());
}
@Test
public void Export_Domain_Package() {
exporter.setTargetFolder(new File("target/gen6"));
exporter.export(Cat.class.getPackage());
}
}

View File

@ -0,0 +1,27 @@
package com.mysema.query.domain;
public class IdNamePair<Type> {
private String id;
private Type name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Type getName() {
return name;
}
public void setName(Type name) {
this.name = name;
}
}

View File

@ -0,0 +1,40 @@
package com.mysema.query.domain;
import static com.mysema.query.types.PathMetadataFactory.*;
import com.mysema.query.types.*;
import com.mysema.query.types.path.*;
import javax.annotation.Generated;
/**
* QIdNamePair is a Querydsl query type for IdNamePair
*/
@Generated("com.mysema.query.codegen.EmbeddableSerializer")
public class QIdNamePair extends BeanPath<IdNamePair<?>> {
private static final long serialVersionUID = -1491444395;
public static final QIdNamePair idNamePair = new QIdNamePair("idNamePair");
public final StringPath id = createString("id");
public final SimplePath<Object> name = createSimple("name", Object.class);
@SuppressWarnings("unchecked")
public QIdNamePair(String variable) {
super((Class)IdNamePair.class, forVariable(variable));
}
public QIdNamePair(Path<? extends IdNamePair<?>> entity) {
super(entity.getType(), entity.getMetadata());
}
@SuppressWarnings("unchecked")
public QIdNamePair(PathMetadata<?> metadata) {
super((Class)IdNamePair.class, metadata);
}
}

View File

@ -0,0 +1,36 @@
package com.mysema.query.domain;
import static com.mysema.query.types.PathMetadataFactory.*;
import com.mysema.query.types.*;
import com.mysema.query.types.path.*;
import javax.annotation.Generated;
/**
* QSuperclass is a Querydsl query type for Superclass
*/
@Generated("com.mysema.query.codegen.EntitySerializer")
public class QSuperclass extends EntityPathBase<Superclass> {
private static final long serialVersionUID = -1300377102;
public static final QSuperclass superclass = new QSuperclass("superclass");
public final ListPath<IdNamePair<String>, QIdNamePair> fooOfSuperclass = this.<IdNamePair<String>, QIdNamePair>createList("fooOfSuperclass", IdNamePair.class, QIdNamePair.class);
public QSuperclass(String variable) {
super(Superclass.class, forVariable(variable));
}
public QSuperclass(Path<? extends Superclass> entity) {
super(entity.getType(), entity.getMetadata());
}
public QSuperclass(PathMetadata<?> metadata) {
super(Superclass.class, metadata);
}
}

View File

@ -0,0 +1,24 @@
package com.mysema.query.domain;
import java.util.ArrayList;
import java.util.List;
import com.mysema.query.annotations.QueryEmbedded;
import com.mysema.query.annotations.QueryEntity;
@QueryEntity
public class Superclass {
@QueryEmbedded
private List<IdNamePair<String>> fooOfSuperclass = new ArrayList<IdNamePair<String>>();
public List<IdNamePair<String>> getFooOfSuperclass() {
return fooOfSuperclass;
}
public void setFooOfSuperclass(List<IdNamePair<String>> fooOfSuperclass) {
this.fooOfSuperclass = fooOfSuperclass;
}
}