Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Merge commit 'eede862' into wicket-1.5
Browse files Browse the repository at this point in the history
Conflicts:
	pom.xml
  • Loading branch information
duesenklipper committed Apr 3, 2012
2 parents fb62ed5 + eede862 commit 09940b8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -15,7 +15,7 @@
</license>
</licenses>
<properties>
<wicket.version>1.5.0</wicket.version>
<wicket.version>1.5.5</wicket.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.5.8</slf4j.version>
</properties>
Expand Down
38 changes: 25 additions & 13 deletions src/main/java/de/wicketbuch/safemodel/SafeModel.java
Expand Up @@ -295,19 +295,26 @@ public static <U> U imposterise(final Class<U> classToImposterize, Invokable han

@SuppressWarnings("unchecked")
private static <U> Class<U> reflectModelObjectType(final IModel<U> target) throws Error {
final Method getObject;
try {
getObject = target.getClass().getMethod("getObject");
} catch (final NoSuchMethodException e) {
throw new Error();
}
final Type type = GenericTypeReflector.getExactReturnType(getObject, target.getClass());
if (type instanceof Class) {
return (Class<U>) type;
} else if (type instanceof ParameterizedType) {
return (Class<U>) ((ParameterizedType) type).getRawType();
final U targetObject = target.getObject();
if (targetObject == null) {
final Method getObject;
try {
getObject = target.getClass().getMethod("getObject");
} catch (final NoSuchMethodException e) {
throw new Error();
}
final Type type = GenericTypeReflector.getExactReturnType(getObject, target.getClass());
final Class<U> reflectedType;
if (type instanceof Class) {
reflectedType = (Class<U>) type;
} else if (type instanceof ParameterizedType) {
reflectedType = (Class<U>) ((ParameterizedType) type).getRawType();
} else {
throw new UnsupportedOperationException("don't know how to find the type");
}
return reflectedType; // can't do anything else here
} else {
throw new UnsupportedOperationException("don't know how to find the type");
return (Class<U>) targetObject.getClass();
}
}

Expand Down Expand Up @@ -356,7 +363,12 @@ public Object invoke(final Invocation invocation) throws Throwable {
} else if (Object.class.equals(returnType)) {
return ClassImposteriser.INSTANCE.imposterise(BLOCKER, Object.class);
} else {
return ClassImposteriser.INSTANCE.imposterise(BLOCKER, returnType);
try {
return ClassImposteriser.INSTANCE.imposterise(BLOCKER, returnType);
} catch (ClassCastException e) {
// some classloading problem in an appserver... maybe we can get by with just a null:
return null;
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/de/wicketbuch/safemodel/SafeModelTest.java
Expand Up @@ -31,6 +31,7 @@

import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.tester.WicketTester;
import org.jmock.api.Invocation;
import org.jmock.api.Invokable;
Expand Down Expand Up @@ -325,4 +326,25 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
IModel<Middle> model = model(fromService(mockedService).loadMid(42));
assertSame(expected, model.getObject());
}

public static class SomethingSerializable implements Serializable {
private String foo;

public String getFoo() {
return foo;
}

public void setFoo(String foo) {
this.foo = foo;
}
}

@Test
public void nonreflectableModel() throws Exception {
final SomethingSerializable something = new SomethingSerializable();
something.setFoo("bar");
Model<SomethingSerializable> rootModel = new Model<SomethingSerializable>(something);
IModel<String> propmodel = model(from(rootModel).getFoo());
assertEquals("bar", propmodel.getObject());
}
}

0 comments on commit 09940b8

Please sign in to comment.