我正面临gwt问题5794:http://code.google.com/p/google-web-toolkit/issues/detail?id=5794
我已经看到有一个8个月大的补丁,但它没有被包含在gwt 2.5 RC1中 http://gwt-code-reviews.appspot.com/1620804/
有谁知道这个补丁是否会包含在gwt 2.5 rc2或最终版本中?
如果没有,有人可以解释一下这个问题的最佳解决方法。
提前致谢。
答案 0 :(得分:3)
我从AbstractRequestContext的copyBeanAndCollections方法中得到了这个。它似乎完成了我想要将代理克隆到具有新上下文的另一个代理的工作。这实际上要求您首先创建新代理或使用新上下文编辑代理,但我不再需要使用复制构造函数来迭代地将属性复制到新代理。我还在测试,但我创建了一个简单的GWTTestCase,它似乎正常工作。
快速更新
我解决了请求工厂实体定位器无法解析类型的问题,因此我添加了stableid和version标签以进行克隆。这可能是个坏主意,当我弄清楚稳定id是否用于原始对象的实际引用的类类型时,我会更新。我的猜测是它用于解析服务器端的类型。
public class ProxyUtils {
public static <T extends BaseProxy> AutoBean<T> cloneBeanProperties(final T original, final T clone, final RequestContext toContext) {
AutoBean<T> originalBean = AutoBeanUtils.getAutoBean(original);
AutoBean<T> cloneBean = AutoBeanUtils.getAutoBean(clone);
return cloneBeanProperties(originalBean, cloneBean, toContext);
}
/**
* Shallow-clones an autobean and makes duplicates of the collection types.
* A regular {@link AutoBean#clone} won't duplicate reference properties.
*/
public static <T extends BaseProxy> AutoBean<T> cloneBeanProperties(final AutoBean<T> toClone, final AutoBean<T> clone, final RequestContext toContext) {
// NOTE: This may be a bad idea, i don't know if the STABLE_ID is the type or if it is the
// actual server side reference to this object. Don't want it to update my original object.
// I added this back in because I was getting an InstantionException in the locator I am
// pretty sure this is because request factory server side could not resolve the class type.
// Maybe someone could shed some light on this one, if you know what the stable id is really
// used for.
clone.setTag(STABLE_ID, toClone.getTag(STABLE_ID));
clone.setTag(Constants.VERSION_PROPERTY_B64, toClone.getTag(Constants.VERSION_PROPERTY_B64));
clone.accept(new AutoBeanVisitor() {
final Map<String, Object> values = AutoBeanUtils.getAllProperties(toClone);
@Override
public boolean visitCollectionProperty(String propertyName, AutoBean<Collection<?>> value, CollectionPropertyContext ctx) {
// javac generics bug
value = AutoBeanUtils.<Collection<?>, Collection<?>> getAutoBean((Collection<?>) values.get(propertyName));
if (value != null) {
Collection<Object> collection;
if (List.class == ctx.getType()) {
collection = new ArrayList<Object>();
} else if (Set.class == ctx.getType()) {
collection = new HashSet<Object>();
} else {
// Should not get here if the validator works correctly
throw new IllegalArgumentException(ctx.getType().getName());
}
if (isValueType(ctx.getElementType()) || isEntityType(ctx.getElementType())) {
/*
* Proxies must be edited up-front so that the elements
* in the collection have stable identity.
*/
for (Object o : value.as()) {
if (o == null) {
collection.add(null);
} else {
collection.add(editProxy(toContext, (Class<T>) ctx.getType(), (T) o));
}
}
} else {
// For simple values, just copy the values
collection.addAll(value.as());
}
ctx.set(collection);
}
return false;
}
@Override
public boolean visitReferenceProperty(String propertyName, AutoBean<?> value, PropertyContext ctx) {
value = AutoBeanUtils.getAutoBean(values.get(propertyName));
if (value != null) {
if (isValueType(ctx.getType()) || isEntityType(ctx.getType())) {
/*
* Value proxies must be cloned upfront, since the value
* is replaced outright.
*/
@SuppressWarnings("unchecked")
AutoBean<BaseProxy> valueBean = (AutoBean<BaseProxy>) value;
ctx.set(editProxy(toContext, (Class<T>) ctx.getType(), (T) valueBean.as()));
} else {
ctx.set(value.as());
}
}
return false;
}
@Override
public boolean visitValueProperty(String propertyName, Object value, PropertyContext ctx) {
ctx.set(values.get(propertyName));
return false;
}
});
return clone;
}
/**
* Take ownership of a proxy instance and make it editable.
*/
private static <T extends BaseProxy> T editProxy(RequestContext ctx, Class<T> clazz, T object) {
AutoBean<T> toClone = AutoBeanUtils.getAutoBean(object);
// Create editable copies
AutoBean<T> parent = toClone;
AutoBean<T> clone = (AutoBean<T>) ctx.create(clazz);
AutoBean<T> cloned = cloneBeanProperties(toClone, clone, ctx);
cloned.setTag(Constants.PARENT_OBJECT, parent);
return cloned.as();
}
private static boolean isEntityType(Class<?> clazz) {
return isAssignableTo(clazz, EntityProxy.class);
}
private static boolean isValueType(Class<?> clazz) {
return isAssignableTo(clazz, ValueProxy.class);
}
public static boolean isAssignableTo(Class<?> thisClass, Class<?> assignableTo ) {
if(thisClass == null || assignableTo == null) {
return false;
}
if(thisClass.equals(assignableTo)) {
return true;
}
Class<?> currentSuperClass = thisClass.getSuperclass();
while(currentSuperClass != null) {
if(currentSuperClass.equals(assignableTo)) {
return true;
}
currentSuperClass = thisClass.getSuperclass();
}
return false;
}
}
这是我成功完成的简单单元测试。我确实有一些错误,我将不得不在isAssignableFrom方法中进行调查。
public void testCloneProxy() {
DaoRequestFactory requestFactory = GWT.create(DaoRequestFactory.class);
RequestContext fromContext = requestFactory.analyticsTaskRequest();
AnalyticsOperationInputProxy from = fromContext.create(AnalyticsOperationInputProxy.class);
from.setDisplayName("DISPLAY 1");
from.setInputName("INPUT 1");
RequestContext toContext = requestFactory.analyticsTaskRequest();
AnalyticsOperationInputProxy to = toContext.create(AnalyticsOperationInputProxy.class);
ProxyUtils.cloneBeanProperties(from, to, toContext);
System.out.println("Cloned output " + to.getDisplayName());
System.out.println("Cloned output " + to.getInputName());
Assert.assertTrue("Display name not equal" , from.getDisplayName().equals(to.getDisplayName()));
Assert.assertTrue("Input name not equal" , from.getInputName().equals(to.getInputName()));
}
答案 1 :(得分:2)
我认为Chris Hinshaw已经制作了一个很棒的代码来克隆一个代理,但我会改变一行只是为了让它更好,避免在第129行无限循环
public class ProxyUtils {
public static <T extends BaseProxy> AutoBean<T> cloneBeanProperties(final T original, final T clone, final RequestContext toContext) {
AutoBean<T> originalBean = AutoBeanUtils.getAutoBean(original);
AutoBean<T> cloneBean = AutoBeanUtils.getAutoBean(clone);
return cloneBeanProperties(originalBean, cloneBean, toContext);
}
/**
* Shallow-clones an autobean and makes duplicates of the collection types.
* A regular {@link AutoBean#clone} won't duplicate reference properties.
*/
public static <T extends BaseProxy> AutoBean<T> cloneBeanProperties(final AutoBean<T> toClone, final AutoBean<T> clone, final RequestContext toContext) {
// NOTE: This may be a bad idea, i don't know if the STABLE_ID is the type or if it is the
// actual server side reference to this object. Don't want it to update my original object.
// I added this back in because I was getting an InstantionException in the locator I am
// pretty sure this is because request factory server side could not resolve the class type.
// Maybe someone could shed some light on this one, if you know what the stable id is really
// used for.
clone.setTag(STABLE_ID, clone.getTag(STABLE_ID));
clone.setTag(Constants.VERSION_PROPERTY_B64, toClone.getTag(Constants.VERSION_PROPERTY_B64));
clone.accept(new AutoBeanVisitor() {
final Map<String, Object> values = AutoBeanUtils.getAllProperties(toClone);
@Override
public boolean visitCollectionProperty(String propertyName, AutoBean<Collection<?>> value, CollectionPropertyContext ctx) {
// javac generics bug
value = AutoBeanUtils.<Collection<?>, Collection<?>> getAutoBean((Collection<?>) values.get(propertyName));
if (value != null) {
Collection<Object> collection;
if (List.class == ctx.getType()) {
collection = new ArrayList<Object>();
} else if (Set.class == ctx.getType()) {
collection = new HashSet<Object>();
} else {
// Should not get here if the validator works correctly
throw new IllegalArgumentException(ctx.getType().getName());
}
if (isValueType(ctx.getElementType()) || isEntityType(ctx.getElementType())) {
/*
* Proxies must be edited up-front so that the elements
* in the collection have stable identity.
*/
for (Object o : value.as()) {
if (o == null) {
collection.add(null);
} else {
collection.add(editProxy(toContext, (Class<T>) ctx.getType(), (T) o));
}
}
} else {
// For simple values, just copy the values
collection.addAll(value.as());
}
ctx.set(collection);
}
return false;
}
@Override
public boolean visitReferenceProperty(String propertyName, AutoBean<?> value, PropertyContext ctx) {
value = AutoBeanUtils.getAutoBean(values.get(propertyName));
if (value != null) {
if (isValueType(ctx.getType()) || isEntityType(ctx.getType())) {
/*
* Value proxies must be cloned upfront, since the value
* is replaced outright.
*/
@SuppressWarnings("unchecked")
AutoBean<BaseProxy> valueBean = (AutoBean<BaseProxy>) value;
ctx.set(editProxy(toContext, (Class<T>) ctx.getType(), (T) valueBean.as()));
} else {
ctx.set(value.as());
}
}
return false;
}
@Override
public boolean visitValueProperty(String propertyName, Object value, PropertyContext ctx) {
ctx.set(values.get(propertyName));
return false;
}
});
return clone;
}
/**
* Take ownership of a proxy instance and make it editable.
*/
private static <T extends BaseProxy> T editProxy(RequestContext ctx, Class<T> clazz, T object) {
AutoBean<T> toClone = AutoBeanUtils.getAutoBean(object);
// Create editable copies
AutoBean<T> parent = toClone;
AutoBean<T> clone = (AutoBean<T>) ctx.create(clazz);
AutoBean<T> cloned = cloneBeanProperties(toClone, clone, ctx);
cloned.setTag(Constants.PARENT_OBJECT, parent);
return cloned.as();
}
private static boolean isEntityType(Class<?> clazz) {
return isAssignableTo(clazz, EntityProxy.class);
}
private static boolean isValueType(Class<?> clazz) {
return isAssignableTo(clazz, ValueProxy.class);
}
public static boolean isAssignableTo(Class<?> thisClass, Class<?> assignableTo ) {
if(thisClass == null || assignableTo == null) {
return false;
}
if(thisClass.equals(assignableTo)) {
return true;
}
Class<?> currentSuperClass = thisClass.getSuperclass();
if(currentSuperClass != null) {
if(currentSuperClass.equals(assignableTo)) {
return true;
}
}
return false;
}
}
答案 2 :(得分:1)
不会。
从第一次审查(恰好是我自己):
简而言之:这个补丁是不够的,它打破了很多规则(“克服了” 溪流“),测试被打破了。
如果您无法使用问题中建议的序列化/反序列化解决方法,我相信有一种方法可以使用AutoBeanVisitor
来克隆内容。
当前“只有一个RequestContext可以编辑一个给定的代理 - 在给定时间由其stableId-识别”真的很烦人,并不是真正合理的(不再是至少;它是在Request Factory的早期迭代中)。这是我将来想要摆脱的东西,但我们还有其他一些大事要做。