Fix [Issue#57] for 1.9.10
diff --git a/release-notes/VERSION b/release-notes/VERSION index 7a7a648..0e1de69 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION
@@ -7,7 +7,8 @@ Another patch release for 1.9. Fixes: - * [JACKSON-855] add StackOverflowError as root cause + * [JACKSON-855]: add StackOverflowError as root cause + * [Issue#57]: Allow serialization of JDK proxy types ------------------------------------------------------------------------ === History: ===
diff --git a/src/mapper/java/org/codehaus/jackson/map/util/ClassUtil.java b/src/mapper/java/org/codehaus/jackson/map/util/ClassUtil.java index 7b04299..b2ac0ab 100644 --- a/src/mapper/java/org/codehaus/jackson/map/util/ClassUtil.java +++ b/src/mapper/java/org/codehaus/jackson/map/util/ClassUtil.java
@@ -150,10 +150,12 @@ */ public static boolean isProxyType(Class<?> type) { - // Then: well-known proxy (etc) classes + // As per [Issue#57], should NOT disqualify JDK proxy: + /* if (Proxy.isProxyClass(type)) { return true; } + */ String name = type.getName(); // Hibernate uses proxies heavily as well: if (name.startsWith("net.sf.cglib.proxy.")
diff --git a/src/test/org/codehaus/jackson/map/interop/TestJDKProxy.java b/src/test/org/codehaus/jackson/map/interop/TestJDKProxy.java new file mode 100644 index 0000000..ffec85f --- /dev/null +++ b/src/test/org/codehaus/jackson/map/interop/TestJDKProxy.java
@@ -0,0 +1,70 @@ +package org.codehaus.jackson.map.interop; + +import java.lang.reflect.*; + +import org.codehaus.jackson.map.*; + +// mostly for [Issue#57] +public class TestJDKProxy extends BaseMapTest +{ + final ObjectMapper MAPPER = new ObjectMapper(); + + public interface IPlanet { + String getName(); + String setName(String s); + } + + // bit silly example; usually wouldn't implement interface (no need to proxy if it did) + static class Planet implements IPlanet { + private String name; + + public Planet() { } + public Planet(String s) { name = s; } + + public String getName(){return name;} + public String setName(String iName) {name = iName; + return name; + } + } + + /* + /******************************************************** + /* Test methods + /******************************************************** + */ + + public void testSimple() throws Exception + { + IPlanet input = getProxy(IPlanet.class, new Planet("Foo")); + String json = MAPPER.writeValueAsString(input); + assertEquals("{\"name\":\"Foo\"}", json); + + // and just for good measure + Planet output = MAPPER.readValue(json, Planet.class); + assertEquals("Foo", output.getName()); + } + + /* + /******************************************************** + /* Helper methods + /******************************************************** + */ + + public static <T> T getProxy(Class<T> type, Object toProxy) { + class ProxyUtil implements InvocationHandler { + Object obj; + public ProxyUtil(Object o) { + obj = o; + } + public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { + Object result = null; + result = m.invoke(obj, args); + return result; + } + } + @SuppressWarnings("unchecked") + T proxy = (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type }, + new ProxyUtil(toProxy)); + return proxy; + } +}