gpt4 book ai didi

gwt - Requestfactory 克隆代理到新上下文

转载 作者:行者123 更新时间:2023-12-04 21:14:00 24 4
gpt4 key购买 nike

我正面临 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 或最终版本中?

如果没有,有人可以向我解释什么是该问题的最佳解决方法。

提前致谢。

最佳答案

我从 AbstractRequestContext 的 copyBeanAndCollections 方法中想到了这个。它似乎完成了我想要使用新上下文将代理克隆到另一个代理的工作。这本质上要求您首先创建一个新代理或使用新上下文编辑代理,但我不再需要使用复制构造函数将属性迭代复制到新代理。我仍在测试,但我创建了一个简单的 GWTTestCase,它似乎可以正常工作。

快速更新

我解决了请求工厂实体定位器无法解析类型的问题,因此我重新添加了 stableid 和版本标签以进行克隆。这可能是个坏主意,当我弄清楚稳定 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()));

}

关于gwt - Requestfactory 克隆代理到新上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12462174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com