我从 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()));
}