worked on Querydsl generics support

This commit is contained in:
Timo Westkämper 2009-11-14 16:23:01 +00:00
parent 95487c9751
commit db9abc1ca4
4 changed files with 44 additions and 105 deletions

View File

@ -63,20 +63,44 @@ public class APTModelFactory implements TypeVisitor<TypeModel,Elements> {
this.comparableType = env.getElementUtils().getTypeElement(Comparable.class.getName());
}
public TypeModel create(TypeMirror type, Elements el){
String key = type + " " + type.getKind();
private String getKey(TypeMirror type, boolean deep){
StringBuilder key = new StringBuilder(type.toString());
if (type.getKind() == TypeKind.TYPEVAR){
TypeVariable t = (TypeVariable)type;
if (t.getUpperBound() != null){
key = t.getUpperBound() + " " + t.getUpperBound().getKind();
key.append(";");
key.append(getKey(t.getUpperBound(), false));
}
if (t.getLowerBound() != null){
key.append(";");
key.append(getKey(t.getLowerBound(), false));
}
}else if (type.getKind() == TypeKind.WILDCARD){
WildcardType t = (WildcardType)type;
if (t.getExtendsBound() != null){
key = t.getExtendsBound() + " " + t.getExtendsBound().getKind();
}
key.append(";");
key.append(getKey(t.getExtendsBound(), false));
}
if (t.getSuperBound() != null){
key.append(";");
key.append(getKey(t.getSuperBound(), false));
}
}else if (type.getKind() == TypeKind.DECLARED){
DeclaredType t = (DeclaredType)type;
for (TypeMirror arg : t.getTypeArguments()){
key.append(";");
if (deep){
key.append(getKey(arg, false));
}else{
key.append(arg.toString());
}
}
}
return key.toString();
}
public TypeModel create(TypeMirror type, Elements el){
String key = getKey(type, true);
if (cache.containsKey(key)){
return cache.get(key);
}else{

View File

@ -121,7 +121,7 @@ public class ClassTypeModel implements TypeModel{
public boolean equals(Object o){
if (o instanceof TypeModel){
TypeModel t = (TypeModel)o;
return clazz.getName().equals(t.getFullName());
return clazz.getName().equals(t.getFullName()) && !t.isExtendsType();
}else{
return false;
}

View File

@ -17,7 +17,7 @@ import com.mysema.commons.lang.Assert;
*
*/
@Immutable
public final class SimpleTypeModel implements TypeModel {
public class SimpleTypeModel implements TypeModel {
private final String fullName, packageName, simpleName, localName;
@ -59,29 +59,28 @@ public final class SimpleTypeModel implements TypeModel {
@Override
public String getLocalGenericName(BeanModel context) {
if (parameters.length > 0){
StringBuilder builder = new StringBuilder();
StringBuilder builder = new StringBuilder();
if (isExtendsType()){
builder.append("? extends ");
}
if (parameters.length > 0){
if (!visible && !context.getPackageName().equals(packageName)){
builder.append(packageName).append(".");
}
builder.append(localName).append("<");
for (int i = 0; i < parameters.length; i++){
if (i > 0) builder.append(",");
if (parameters[i] != null && !parameters[i].equals(this)){
if (parameters[i].isExtendsType()){
builder.append("? extends ");
}
if (parameters[i] != null && !parameters[i].getFullName().equals(fullName)){
builder.append(parameters[i].getLocalGenericName(context));
}else{
builder.append("?");
}
}
builder.append(">");
return builder.toString();
builder.append(">");
}else{
return getLocalRawName(context);
builder.append(getLocalRawName(context));
}
return builder.toString();
}
@Override
@ -147,7 +146,7 @@ public final class SimpleTypeModel implements TypeModel {
public boolean equals(Object o){
if (o instanceof TypeModel){
TypeModel t = (TypeModel)o;
return fullName.equals(t.getFullName());
return fullName.equals(t.getFullName()) && isExtendsType() == t.isExtendsType();
}else{
return false;
}
@ -165,13 +164,9 @@ public final class SimpleTypeModel implements TypeModel {
@Override
public TypeModel asAnySubtype() {
return new TypeModelAdapter(this){
return new SimpleTypeModel(typeCategory, fullName, packageName, simpleName, finalClass, parameters){
@Override
public TypeModel asAnySubtype() {
return this;
}
@Override
public boolean isExtendsType() {
public boolean isExtendsType(){
return true;
}
};

View File

@ -1,80 +0,0 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.codegen;
public class TypeModelAdapter implements TypeModel{
private final TypeModel typeModel;
public TypeModelAdapter(TypeModel typeModel){
this.typeModel = typeModel;
}
public TypeModel as(TypeCategory category) {
return typeModel.as(category);
}
public TypeModel asAnySubtype() {
return typeModel.asAnySubtype();
}
public String getFullName() {
return typeModel.getFullName();
}
public String getLocalGenericName(BeanModel context) {
return typeModel.getLocalGenericName(context);
}
public String getLocalRawName(BeanModel context) {
return typeModel.getLocalRawName(context);
}
public String getPackageName() {
return typeModel.getPackageName();
}
public TypeModel getParameter(int i) {
return typeModel.getParameter(i);
}
public int getParameterCount() {
return typeModel.getParameterCount();
}
public String getPrimitiveName() {
return typeModel.getPrimitiveName();
}
public TypeModel getSelfOrValueType() {
return typeModel.getSelfOrValueType();
}
public String getSimpleName() {
return typeModel.getSimpleName();
}
public TypeCategory getTypeCategory() {
return typeModel.getTypeCategory();
}
public boolean isExtendsType() {
return typeModel.isExtendsType();
}
public boolean isFinal() {
return typeModel.isFinal();
}
public boolean isPrimitive() {
return typeModel.isPrimitive();
}
public String toString() {
return typeModel.toString();
}
}