Permalink
Browse files

Merge branch 'wicket-1.4' into wicket-1.5

  • Loading branch information...
2 parents a5cb720 + 2e38526 commit fd5a940424a5dcb51b40d7b6a8af3d11dec4e428 @duesenklipper committed Sep 12, 2011
@@ -133,7 +133,7 @@ public Object invoke(final Invocation invocation) throws Throwable {
private final Class<T> type;
private TypeAwareLDM(final Class<T> type) {
- this.type = unproxy(type);
+ this.type = (Class<T>) unproxy(type)[0];
}
public Class<T> __type() {
@@ -142,19 +142,27 @@ private TypeAwareLDM(final Class<T> type) {
}
@SuppressWarnings("unchecked")
- private static <T> Class<T> unproxy(final Class<T> type) {
+ private static Class<?>[] unproxy(final Class<?> type) {
if (type == null) {
return null;
}
- Class<T> result = type;
- while (isProxy(result)) {
- result = (Class<T>) result.getSuperclass();
+ Class<?> result = type;
+ // unwrap any cglib proxy...
+ while (isCglibProxy(result)) {
+ result = result.getSuperclass();
+ }
+ // ...unwrap any JDK proxy...
+ if (Proxy.isProxyClass(result)) {
+ // we proxy all these interfaces
+ return result.getInterfaces();
+ } else {
+ // no jdk proxy -> we just subclass whatever this is
+ return new Class<?>[] { result };
}
- return result;
}
- public static <T> boolean isProxy(Class<T> result) {
- return Proxy.isProxyClass(result) || result.getName().contains(CGLIB_NAME_MARKER);
+ public static <T> boolean isCglibProxy(Class<T> result) {
+ return result.getName().contains(CGLIB_NAME_MARKER);
}
@SuppressWarnings("unchecked")
@@ -210,7 +218,7 @@ protected T load() {
private TypeAwarePropModel(final Class<T> type, final Object target, final String expression) {
super(target, expression);
- this.type = unproxy(type);
+ this.type = (Class<T>) unproxy(type)[0];
}
public Class<T> __type() {
@@ -242,8 +250,7 @@ private static void clear() {
root.set(target);
currentTarget.set(target);
mode.set(Mode.PROPERTY);
- return (U) ClassImposteriser.INSTANCE.imposterise(PropertyFinderImpl.INSTANCE, unproxy(target.getClass()),
- PropertyFinder.class);
+ return (U) imposterise(target.getClass(), PropertyFinderImpl.INSTANCE, PropertyFinder.class);
}
/**
@@ -274,8 +281,16 @@ private static void clear() {
classToImposterize = reflectModelObjectType(target);
}
}
- return ClassImposteriser.INSTANCE.imposterise(PropertyFinderImpl.INSTANCE, unproxy(classToImposterize),
- PropertyFinder.class);
+ return imposterise(classToImposterize, PropertyFinderImpl.INSTANCE, PropertyFinder.class);
+ }
+
+ public static <U> U imposterise(final Class<U> classToImposterize, Invokable handler, Class<?> handlerInterface) {
+ final Class<?>[] classOrInterfaces = unproxy(classToImposterize);
+ if (classOrInterfaces.length == 1) {
+ return (U) ClassImposteriser.INSTANCE.imposterise(handler, classOrInterfaces[0], handlerInterface);
+ } else {
+ return (U) ClassImposteriser.INSTANCE.imposterise(handler, handlerInterface, classOrInterfaces);
+ }
}
@SuppressWarnings("unchecked")
@@ -297,12 +312,15 @@ private static void clear() {
}
@SuppressWarnings("unchecked")
+ /**
+ * @param target may be a proxy. If it is a JDK proxy, it is assumed that the first interface
+ * @return
+ */
public static <U> U fromService(final U target) {
clear();
root.set(target);
mode.set(Mode.SERVICE);
- return (U) ClassImposteriser.INSTANCE.imposterise(ServiceFinderImpl.INSTANCE, unproxy(target.getClass()),
- ServiceFinder.class);
+ return (U) imposterise(target.getClass(), ServiceFinderImpl.INSTANCE, ServiceFinder.class);
}
public static interface ServiceFinder {
@@ -20,6 +20,10 @@
import static de.wicketbuch.safemodel.SafeModel.*;
import static org.junit.Assert.*;
+import java.io.Serializable;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -298,7 +302,7 @@ public void nestedServiceModel() throws Exception {
}
@Test
- public void proxiedService() throws Exception {
+ public void cgProxiedService() throws Exception {
final Middle expected = new Middle();
MidService mockedService = ClassImposteriser.INSTANCE.imposterise(new Invokable() {
@@ -309,4 +313,18 @@ public Object invoke(Invocation invocation) throws Throwable {
IModel<Middle> model = model(fromService(mockedService).loadMid(42));
assertSame(expected, model.getObject());
}
+
+ @Test
+ public void javaProxiedService() throws Exception {
+ final Middle expected = new Middle();
+ MidService mockedService = (MidService) Proxy.newProxyInstance(MidService.class.getClassLoader(),
+ new Class<?>[] { Serializable.class, MidService.class }, new InvocationHandler() {
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ return expected;
+ }
+ });
+ IModel<Middle> model = model(fromService(mockedService).loadMid(42));
+ assertSame(expected, model.getObject());
+ }
}

0 comments on commit fd5a940

Please sign in to comment.