#82 improved handling of ManyToOne relations

This commit is contained in:
Timo Westkämper 2012-01-23 22:23:56 +02:00
parent 2326c7c3b7
commit c144266b85
7 changed files with 72 additions and 6 deletions

View File

@ -23,6 +23,7 @@ import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import com.mysema.query.codegen.EntityType;
import com.mysema.query.codegen.QueryTypeFactory;
@ -203,4 +204,16 @@ public interface Configuration {
*/
boolean isExcludedClass(String className);
/**
* @param method
* @return
*/
TypeMirror getRealType(ExecutableElement method);
/**
* @param field
* @return
*/
TypeMirror getRealType(VariableElement field);
}

View File

@ -41,6 +41,7 @@ import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import org.apache.commons.lang3.StringUtils;
@ -412,6 +413,14 @@ public class DefaultConfiguration implements Configuration {
this.unknownAsEmbedded = unknownAsEmbedded;
}
@Override
public TypeMirror getRealType(ExecutableElement method) {
return null;
}
@Override
public TypeMirror getRealType(VariableElement field) {
return null;
}
}

View File

@ -75,8 +75,9 @@ public final class TypeElementHandler {
VisitorConfig config = configuration.getConfig(e, elements);
Set<String> blockedProperties = new HashSet<String>();
Map<String, TypeMirror> propertyTypes = new HashMap<String, TypeMirror>();
Map<String, TypeMirror> fixedTypes = new HashMap<String, TypeMirror>();
Map<String, Annotations> propertyAnnotations = new HashMap<String, Annotations>();
// constructors
if (config.visitConstructors()) {
handleConstructors(entityType, elements);
@ -94,6 +95,10 @@ public final class TypeElementHandler {
annotations.addAnnotation(field.getAnnotation(QueryInit.class));
propertyAnnotations.put(name, annotations);
propertyTypes.put(name, field.asType());
TypeMirror fixedType = configuration.getRealType(field);
if (fixedType != null) {
fixedTypes.put(name, fixedType);
}
}
}
}
@ -121,10 +126,16 @@ public final class TypeElementHandler {
annotations.addAnnotation(method.getAnnotation(QueryType.class));
annotations.addAnnotation(method.getAnnotation(QueryInit.class));
propertyTypes.put(name, method.getReturnType());
TypeMirror fixedType = configuration.getRealType(method);
if (fixedType != null) {
fixedTypes.put(name, fixedType);
}
}
}
}
// fixed types override property types
propertyTypes.putAll(fixedTypes);
for (Map.Entry<String, Annotations> entry : propertyAnnotations.entrySet()) {
Property property = toProperty(entityType, entry.getKey(), propertyTypes.get(entry.getKey()), entry.getValue());
if (property != null) {

View File

@ -66,7 +66,7 @@ public final class TypeUtils {
public static Set<Element> getAnnotationValuesAsElements(AnnotationMirror mirror, String method) {
Set<Element> elements = new HashSet<Element>();
for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : mirror.getElementValues().entrySet()) {
if (entry.getKey().getSimpleName().toString().equals("value")) {
if (entry.getKey().getSimpleName().toString().equals(method)) {
List<AnnotationValue> values = ((List) entry.getValue().getValue());
for (AnnotationValue value : values) {
DeclaredType type = (DeclaredType) value.getValue();
@ -79,7 +79,7 @@ public final class TypeUtils {
public static TypeMirror getAnnotationValueAsTypeMirror(AnnotationMirror mirror, String method) {
for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : mirror.getElementValues().entrySet()) {
if (entry.getKey().getSimpleName().toString().equals("value")) {
if (entry.getKey().getSimpleName().toString().equals(method)) {
return (TypeMirror) entry.getValue().getValue();
}
}

View File

@ -19,9 +19,13 @@ import java.util.List;
import java.util.Map;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
@ -36,6 +40,7 @@ import javax.persistence.Version;
import com.mysema.query.annotations.QueryTransient;
import com.mysema.query.annotations.QueryType;
import com.mysema.query.apt.DefaultConfiguration;
import com.mysema.query.apt.TypeUtils;
import com.mysema.query.apt.VisitorConfig;
/**
@ -54,7 +59,8 @@ public class JPAConfiguration extends DefaultConfiguration {
Class<? extends Annotation> embeddableAnn,
Class<? extends Annotation> embeddedAnn,
Class<? extends Annotation> skipAnn) {
super(roundEnv, options, Keywords.keywords, null, entityAnn, superTypeAnn, embeddableAnn, embeddedAnn, skipAnn);
super(roundEnv, options, Keywords.keywords, null, entityAnn, superTypeAnn,
embeddableAnn, embeddedAnn, skipAnn);
this.annotations = getAnnotations();
}
@ -76,6 +82,27 @@ public class JPAConfiguration extends DefaultConfiguration {
}
return VisitorConfig.get(fields, methods);
}
@Override
public TypeMirror getRealType(ExecutableElement method) {
return getManyToOneType(method);
}
@Override
public TypeMirror getRealType(VariableElement field) {
return getManyToOneType(field);
}
private TypeMirror getManyToOneType(Element element) {
AnnotationMirror mirror = TypeUtils.getAnnotationMirrorOfType(element, ManyToOne.class);
if (mirror != null) {
return TypeUtils.getAnnotationValueAsTypeMirror(mirror, "targetEntity");
} else {
return null;
}
}
private boolean hasRelevantAnnotation(Element element){
for (Class<? extends Annotation> annotation : annotations) {

View File

@ -1,5 +1,7 @@
package com.mysema.query.domain;
import static org.junit.Assert.*;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
@ -86,7 +88,9 @@ public class InterfaceType2Test {
@Test
public void test() {
assertEquals(
QInterfaceType2Test_PartyImpl.class,
QInterfaceType2Test_UserImpl.userImpl.party.getClass());
}

View File

@ -16,4 +16,6 @@ Import-Template:
org.apache.commons.lang3.*;version="${commons.lang.version}"
Excluded-Imports:
edu.umd.cs.findbugs.annotations.*,
net.jcip.annotations.*
net.jcip.annotations.*,
javax.persistence.*;version="[2.0.0,2.1.0)",
javax.jdo.*;version="[2.0.0,3.0.0)"