changed semantics of QueryProjection

This commit is contained in:
Timo Westkämper 2009-10-22 09:44:25 +00:00
parent 4266fdd380
commit 7a4eca3d36
13 changed files with 87 additions and 38 deletions

View File

@ -15,6 +15,7 @@ import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import com.mysema.commons.lang.Assert;
import com.mysema.query.annotations.QueryProjection;
/**
* @author tiwe
@ -24,7 +25,7 @@ public class Configuration {
private String namePrefix = "Q";
protected final Class<? extends Annotation> entityAnn, superTypeAnn, embeddableAnn, dtoAnn, skipAnn;
protected final Class<? extends Annotation> entityAnn, superTypeAnn, embeddableAnn, skipAnn;
private boolean useFields = true, useGetters = true;
@ -32,12 +33,12 @@ public class Configuration {
Class<? extends Annotation> entityAnn,
Class<? extends Annotation> superTypeAnn,
Class<? extends Annotation> embeddableAnn,
Class<? extends Annotation> dtoAnn,
// Class<? extends Annotation> dtoAnn,
Class<? extends Annotation> skipAnn) {
this.entityAnn = Assert.notNull(entityAnn);
this.superTypeAnn = superTypeAnn;
this.embeddableAnn = embeddableAnn;
this.dtoAnn = dtoAnn;
// this.dtoAnn = dtoAnn;
this.skipAnn = skipAnn;
}
@ -47,6 +48,7 @@ public class Configuration {
public boolean isValidConstructor(ExecutableElement constructor) {
return constructor.getModifiers().contains(Modifier.PUBLIC)
&& constructor.getAnnotation(QueryProjection.class) != null
&& !constructor.getParameters().isEmpty();
}
@ -75,9 +77,9 @@ public class Configuration {
return embeddableAnn;
}
public Class<? extends Annotation> getDtoAnn() {
return dtoAnn;
}
// public Class<? extends Annotation> getDtoAnn() {
// return dtoAnn;
// }
public Class<? extends Annotation> getSkipAnn() {
return skipAnn;

View File

@ -9,8 +9,10 @@ import java.io.Writer;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import javax.annotation.processing.Messager;
@ -23,6 +25,7 @@ import javax.tools.Diagnostic.Kind;
import net.jcip.annotations.Immutable;
import com.mysema.commons.lang.Assert;
import com.mysema.query.annotations.QueryProjection;
import com.mysema.query.codegen.BeanModel;
import com.mysema.query.codegen.BeanModelFactory;
import com.mysema.query.codegen.Serializer;
@ -129,18 +132,23 @@ public class Processor {
// DTOS (optional)
if (conf.getDtoAnn() != null){
DTOElementVisitor dtoVisitor = new DTOElementVisitor(env, conf, typeFactory);
Map<String, BeanModel> dtos = new HashMap<String, BeanModel>();
for (Element element : roundEnv.getElementsAnnotatedWith(conf.getDtoAnn())) {
BeanModel model = element.accept(dtoVisitor, null);
dtos.put(model.getName(), model);
}
// serialize entity types
if (!dtos.isEmpty()) {
serialize(Serializers.DTO, dtos);
}
}
DTOElementVisitor dtoVisitor = new DTOElementVisitor(env, conf, typeFactory);
Map<String, BeanModel> dtos = new HashMap<String, BeanModel>();
Set<Element> visited = new HashSet<Element>();
for (Element element : roundEnv.getElementsAnnotatedWith(QueryProjection.class)) {
Element parent = element.getEnclosingElement();
if (parent.getAnnotation(conf.getEntityAnn()) == null
&& parent.getAnnotation(conf.getEmbeddableAnn()) == null
&& !visited.contains(parent)){
BeanModel model = parent.accept(dtoVisitor, null);
dtos.put(model.getName(), model);
visited.add(parent);
}
}
// serialize entity types
if (!dtos.isEmpty()) {
serialize(Serializers.DTO, dtos);
}
}

View File

@ -31,7 +31,7 @@ import com.mysema.query.annotations.QueryTransient;
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class QuerydslAnnotationProcessor extends AbstractProcessor{
private Class<? extends Annotation> entity, superType, embeddable, dto, skip;
private Class<? extends Annotation> entity, superType, embeddable, skip;
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
@ -39,10 +39,10 @@ public class QuerydslAnnotationProcessor extends AbstractProcessor{
entity = QueryEntity.class;
superType = QuerySupertype.class;
embeddable = QueryEmbeddable.class;
dto = QueryProjection.class;
// dto = QueryProjection.class;
skip = QueryTransient.class;
Configuration configuration = new Configuration(entity, superType, embeddable, dto, skip);
Configuration configuration = new Configuration(entity, superType, embeddable, skip);
Processor processor = new Processor(processingEnv, configuration);
processor.process(roundEnv);
return true;

View File

@ -17,7 +17,7 @@ public class HibernateAnnotationProcessor extends JPAAnnotationProcessor{
@Override
protected Configuration createConfiguration() throws ClassNotFoundException {
return new HibernateConfiguration(entity, superType, embeddable, dto, skip);
return new HibernateConfiguration(entity, superType, embeddable, skip);
}
}

View File

@ -15,9 +15,8 @@ public class HibernateConfiguration extends JPAConfiguration{
public HibernateConfiguration(Class<? extends Annotation> entityAnn,
Class<? extends Annotation> superTypeAnn,
Class<? extends Annotation> embeddableAnn,
Class<? extends Annotation> dtoAnn,
Class<? extends Annotation> skipAnn) throws ClassNotFoundException {
super(entityAnn, superTypeAnn, embeddableAnn, dtoAnn, skipAnn);
super(entityAnn, superTypeAnn, embeddableAnn, skipAnn);
}
@SuppressWarnings("unchecked")

View File

@ -16,7 +16,6 @@ import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import com.mysema.query.annotations.QueryProjection;
import com.mysema.query.apt.Configuration;
import com.mysema.query.apt.Processor;
@ -28,7 +27,7 @@ import com.mysema.query.apt.Processor;
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class JDOAnnotationProcessor extends AbstractProcessor{
private Class<? extends Annotation> entity, superType, embeddable, dto, skip;
private Class<? extends Annotation> entity, superType, embeddable, skip;
@SuppressWarnings("unchecked")
@Override
@ -38,10 +37,9 @@ public class JDOAnnotationProcessor extends AbstractProcessor{
entity = (Class)Class.forName("javax.jdo.annotations.PersistenceCapable");
superType = null; // ?!?
embeddable = (Class)Class.forName("javax.jdo.annotations.EmbeddedOnly");
dto = QueryProjection.class;
skip = (Class)Class.forName("javax.jdo.annotations.NotPersistent");
Configuration configuration = new Configuration(entity, superType, embeddable, dto, skip);
Configuration configuration = new Configuration(entity, superType, embeddable, skip);
configuration.setUseGetters(false);
Processor processor = new Processor(processingEnv, configuration);
processor.process(roundEnv);

View File

@ -16,7 +16,6 @@ import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import com.mysema.query.annotations.QueryProjection;
import com.mysema.query.apt.Configuration;
import com.mysema.query.apt.Processor;
@ -28,7 +27,7 @@ import com.mysema.query.apt.Processor;
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class JPAAnnotationProcessor extends AbstractProcessor{
protected Class<? extends Annotation> entity, superType, embeddable, dto, skip;
protected Class<? extends Annotation> entity, superType, embeddable, skip;
@SuppressWarnings("unchecked")
@Override
@ -38,7 +37,6 @@ public class JPAAnnotationProcessor extends AbstractProcessor{
entity = (Class)Class.forName("javax.persistence.Entity");
superType = (Class)Class.forName("javax.persistence.MappedSuperclass");
embeddable = (Class)Class.forName("javax.persistence.Embeddable");
dto = QueryProjection.class;
skip = (Class)Class.forName("javax.persistence.Transient");
Configuration configuration = createConfiguration();
@ -52,7 +50,7 @@ public class JPAAnnotationProcessor extends AbstractProcessor{
}
protected Configuration createConfiguration() throws ClassNotFoundException {
return new JPAConfiguration(entity, superType, embeddable, dto, skip);
return new JPAConfiguration(entity, superType, embeddable, skip);
}
}

View File

@ -29,9 +29,8 @@ public class JPAConfiguration extends Configuration {
public JPAConfiguration(Class<? extends Annotation> entityAnn,
Class<? extends Annotation> superTypeAnn,
Class<? extends Annotation> embeddableAnn,
Class<? extends Annotation> dtoAnn,
Class<? extends Annotation> skipAnn) throws ClassNotFoundException {
super(entityAnn, superTypeAnn, embeddableAnn, dtoAnn, skipAnn);
super(entityAnn, superTypeAnn, embeddableAnn, skipAnn);
this.annotations = getAnnotations();
}

View File

@ -0,0 +1,41 @@
package com.mysema.query.domain;
import org.junit.Test;
import com.mysema.query.annotations.QueryEntity;
import com.mysema.query.annotations.QueryProjection;
import com.mysema.query.types.expr.EString;
public class ProjectionTest {
@QueryEntity
public static class EntityWithProjection{
public EntityWithProjection(long id){
}
@QueryProjection
public EntityWithProjection(String name){
}
}
public static class DTOWithProjection {
public DTOWithProjection(long id){
}
@QueryProjection
public DTOWithProjection(String name){
}
}
@Test
public void test() throws SecurityException, NoSuchMethodException{
QEntityWithProjection.create(EString.create(""));
new QDTOWithProjection(EString.create(""));
}
}

View File

@ -13,6 +13,7 @@ import java.util.Map;
import com.mysema.query.annotations.PropertyType;
import com.mysema.query.annotations.QueryEntity;
import com.mysema.query.annotations.QueryProjection;
import com.mysema.query.annotations.QueryType;
@QueryEntity
@ -49,6 +50,7 @@ public class Cat extends Animal {
this.name = name;
}
@QueryProjection
public Cat(String name, int id) {
this(name);
this.id = id;

View File

@ -5,15 +5,15 @@
*/
package com.mysema.query.annotations;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Documented
@Target(TYPE)
@Target(ElementType.CONSTRUCTOR)
@Retention(RUNTIME)
/**
* Annotation for APT based DTO query type generation. Annotate DTO types with this annotation.

View File

@ -10,8 +10,9 @@ import com.mysema.query.annotations.QueryProjection;
/**
* The Class Family.
*/
@QueryProjection
public class Family {
@QueryProjection
public Family(Cat mother, Cat mate, Cat offspr) {
}

View File

@ -13,7 +13,6 @@ import org.hibernate.annotations.CollectionOfElements;
import com.mysema.query.annotations.QueryProjection;
@QueryProjection
public class FooDTO {
String bar;
@Id
@ -25,9 +24,11 @@ public class FooDTO {
public FooDTO() {
}
@QueryProjection
public FooDTO(long l) {
}
@QueryProjection
public FooDTO(long l, long r) {
}
}