This commit is contained in:
Timo Westkämper 2009-01-06 19:50:56 +00:00
parent a36c2a6492
commit 6a41c2fa19
5 changed files with 37 additions and 24 deletions

View File

@ -31,7 +31,7 @@ public class AliasAwareExprFactory extends SimpleExprFactory{
public EBoolean create(Boolean arg){
try{
return aliasFactory.isBound() ? aliasFactory.<PBoolean>getCurrent() : super.create(arg);
return aliasFactory.hasCurrent() ? aliasFactory.<PBoolean>getCurrent() : super.create(arg);
}finally{
aliasFactory.setCurrent(null);
}
@ -39,7 +39,7 @@ public class AliasAwareExprFactory extends SimpleExprFactory{
public PBooleanArray create(Boolean[] args){
try{
return aliasFactory.isBound() ? aliasFactory.<PBooleanArray>getCurrent() : super.create(args);
return aliasFactory.hasCurrent() ? aliasFactory.<PBooleanArray>getCurrent() : super.create(args);
}finally{
aliasFactory.setCurrent(null);
}
@ -47,7 +47,7 @@ public class AliasAwareExprFactory extends SimpleExprFactory{
public <D> PComponentCollection<D> create(Collection<D> arg) {
try{
return aliasFactory.isBound() ? aliasFactory.<PComponentCollection<D>>getCurrent() : super.create(arg);
return aliasFactory.hasCurrent() ? aliasFactory.<PComponentCollection<D>>getCurrent() : super.create(arg);
}finally{
aliasFactory.setCurrent(null);
}
@ -55,7 +55,7 @@ public class AliasAwareExprFactory extends SimpleExprFactory{
public <D extends Comparable<D>> EComparable<D> create(D arg){
try{
return aliasFactory.isBound() ? aliasFactory.<EComparable<D>>getCurrent() : super.create(arg);
return aliasFactory.hasCurrent() ? aliasFactory.<EComparable<D>>getCurrent() : super.create(arg);
}finally{
aliasFactory.setCurrent(null);
}
@ -77,7 +77,7 @@ public class AliasAwareExprFactory extends SimpleExprFactory{
public <D extends Comparable<D>> PComparableArray<D> create(D[] args){
try{
return aliasFactory.isBound() ? aliasFactory.<PComparableArray<D>>getCurrent() : super.create(args);
return aliasFactory.hasCurrent() ? aliasFactory.<PComparableArray<D>>getCurrent() : super.create(args);
}finally{
aliasFactory.setCurrent(null);
}
@ -85,7 +85,7 @@ public class AliasAwareExprFactory extends SimpleExprFactory{
public <D> PComponentList<D> create(List<D> arg) {
try{
return aliasFactory.isBound() ? aliasFactory.<PComponentList<D>>getCurrent() : super.create(arg);
return aliasFactory.hasCurrent() ? aliasFactory.<PComponentList<D>>getCurrent() : super.create(arg);
}finally{
aliasFactory.setCurrent(null);
}
@ -93,7 +93,7 @@ public class AliasAwareExprFactory extends SimpleExprFactory{
public ExtString create(String arg){
try{
return aliasFactory.isBound() ? aliasFactory.<ExtString>getCurrent() : super.create(arg);
return aliasFactory.hasCurrent() ? aliasFactory.<ExtString>getCurrent() : super.create(arg);
}finally{
aliasFactory.setCurrent(null);
}
@ -101,7 +101,7 @@ public class AliasAwareExprFactory extends SimpleExprFactory{
public PStringArray create(String[] args){
try{
return aliasFactory.isBound() ? aliasFactory.<PStringArray>getCurrent() : super.create(args);
return aliasFactory.hasCurrent() ? aliasFactory.<PStringArray>getCurrent() : super.create(args);
}finally{
aliasFactory.setCurrent(null);
}

View File

@ -66,7 +66,7 @@ public class AliasFactory {
return (A) current.get();
}
public boolean isBound() {
public boolean hasCurrent() {
return current.get() != null;
}

View File

@ -10,5 +10,9 @@ package com.mysema.query.collections.alias;
public interface ManagedObject {
void setElementType(Class<?> type);
void setKeyType(Class<?> type);
void setValueType(Class<?> type);
}

View File

@ -36,7 +36,7 @@ class PropertyAccessInvocationHandler implements MethodInterceptor{
private AliasFactory aliasFactory;
private Class<?> elementType;
private Class<?> elementType, keyType, valueType;
private final Map<String,Expr<?>> propToExpr = new HashMap<String,Expr<?>>();
@ -68,7 +68,7 @@ class PropertyAccessInvocationHandler implements MethodInterceptor{
}else{
if (parent == null) throw new IllegalArgumentException("No path for " + proxy);
PathMetadata<String> pm = PathMetadata.forProperty((Path<?>) parent, ptyName);
rv = makeNew(ptyClass, proxy, ptyName, pm);
rv = newInstance(ptyClass, proxy, ptyName, pm);
if (Collection.class.isAssignableFrom(ptyClass)){
((ManagedObject)rv).setElementType(getFirstTypeParameter(method.getGenericReturnType()));
}
@ -82,7 +82,7 @@ class PropertyAccessInvocationHandler implements MethodInterceptor{
rv = propToObj.get(ptyName);
}else{
PathMetadata<Integer> pm = PathMetadata.forSize((PCollection<?>) parent);
rv = makeNew(Integer.class, proxy, ptyName, pm);
rv = newInstance(Integer.class, proxy, ptyName, pm);
}
aliasFactory.setCurrent(propToExpr.get(ptyName));
@ -93,9 +93,9 @@ class PropertyAccessInvocationHandler implements MethodInterceptor{
}else{
PathMetadata<Integer> pm = PathMetadata.forListAccess((PList<?>)parent, (Integer)args[0]);
if (elementType != null){
rv = makeNew(elementType, proxy, ptyName, pm);
rv = newInstance(elementType, proxy, ptyName, pm);
}else{
rv = makeNew(method.getReturnType(), proxy, ptyName, pm);
rv = newInstance(method.getReturnType(), proxy, ptyName, pm);
}
}
aliasFactory.setCurrent(propToExpr.get(ptyName));
@ -110,6 +110,12 @@ class PropertyAccessInvocationHandler implements MethodInterceptor{
}else if (method.getName().equals("setElementType")){
elementType = (Class<?>) args[0];
}else if (method.getName().equals("setKeyType")){
keyType = (Class<?>) args[0];
}else if (method.getName().equals("setValueType")){
valueType = (Class<?>) args[0];
}else{
rv = methodProxy.invokeSuper(proxy, args);
@ -148,7 +154,7 @@ class PropertyAccessInvocationHandler implements MethodInterceptor{
}
@SuppressWarnings("unchecked")
private <T> T makeNew(Class<T> type, Object parent, String prop, PathMetadata<?> pm) {
private <T> T newInstance(Class<T> type, Object parent, String prop, PathMetadata<?> pm) {
Expr<?> path;
T rv;
@ -197,19 +203,19 @@ class PropertyAccessInvocationHandler implements MethodInterceptor{
// Collection API types
} else if (List.class.isAssignableFrom(type)) {
path = new Path.PComponentList(null,pm);
path = new Path.PComponentList(elementType,pm);
rv = (T) aliasFactory.createAliasForProp(type, parent, path);
} else if (Set.class.isAssignableFrom(type)) {
path = new Path.PComponentCollection(null,pm);
path = new Path.PComponentCollection(elementType,pm);
rv = (T) aliasFactory.createAliasForProp(type, parent, path);
} else if (Collection.class.isAssignableFrom(type)) {
path = new Path.PComponentCollection(null,pm);
path = new Path.PComponentCollection(elementType,pm);
rv = (T) aliasFactory.createAliasForProp(type, parent, path);
} else if (Map.class.isAssignableFrom(type)) {
path = new Path.PComponentMap(null,null,pm);
path = new Path.PComponentMap(keyType,valueType,pm);
rv = (T) aliasFactory.createAliasForProp(type, parent, path);
// enums

View File

@ -96,12 +96,17 @@ public class MiniApiTest {
System.out.println(name);
}
// 2nd - variation
// 2nd - variation 1
for (String name : from($(c),cats).where($(c.getKittens().size()).gt(0))
.iterate($(c.getName()))){
System.out.println(name);
}
// 2nd - variation 2
for (String name : from($(c),cats).where($(c.getKittens().size()).gt(0))
.iterate($(c.getName()))){
System.out.println(name);
}
}
@Test
@ -129,9 +134,7 @@ public class MiniApiTest {
from($(c),cats)
.where($(c.getMate().getBirthdate()).after(new Date()))
.iterate($(c)).iterator();
.iterate($(c)).iterator();
}
@Test