-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
131fda0
commit d4f3f22
Showing
7 changed files
with
178 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
paranamer/src/main/java/com/fasterxml/jackson/module/paranamer/SerializableParanamer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.fasterxml.jackson.module.paranamer; | ||
|
||
import java.lang.reflect.AccessibleObject; | ||
import java.lang.reflect.AnnotatedElement; | ||
|
||
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter; | ||
import com.thoughtworks.paranamer.BytecodeReadingParanamer; | ||
import com.thoughtworks.paranamer.CachingParanamer; | ||
import com.thoughtworks.paranamer.Paranamer; | ||
|
||
/** | ||
* Simple wrapper used to hide the fact that paranamer accessor itself if not JDK serializable | ||
* in a way to keep actual <code>ObjectMapper</code> / <code>ObjectReader</code> serializable. | ||
*/ | ||
public class SerializableParanamer | ||
implements java.io.Serializable | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
protected transient Paranamer _paranamer; | ||
|
||
public SerializableParanamer() { | ||
this(null); | ||
} | ||
|
||
public SerializableParanamer(Paranamer paranamer) { | ||
if (paranamer == null) { | ||
paranamer = defaultParanamer(); | ||
} | ||
_paranamer = paranamer; | ||
} | ||
|
||
/** | ||
* Overridable method in case someone really wants to sub-class this implementation. | ||
*/ | ||
protected Paranamer defaultParanamer() { | ||
return new CachingParanamer(new BytecodeReadingParanamer()); | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Public API | ||
/********************************************************** | ||
*/ | ||
|
||
public String findParameterName(AnnotatedParameter param) | ||
{ | ||
int index = param.getIndex(); | ||
AnnotatedElement ctor = param.getOwner().getAnnotated(); | ||
String[] names = _paranamer.lookupParameterNames((AccessibleObject) ctor, false); | ||
if (names != null && index < names.length) { | ||
return names[index]; | ||
} | ||
return null; | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* JDK serialization handling | ||
/********************************************************** | ||
*/ | ||
|
||
Object readResolve() { | ||
_paranamer = defaultParanamer(); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
paranamer/src/test/java/com/fasterxml/jackson/module/paranamer/TestJDKSerializability.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.fasterxml.jackson.module.paranamer; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class TestJDKSerializability extends ParanamerTestBase | ||
{ | ||
static class Point { | ||
public int x, y; | ||
} | ||
|
||
public void testMapper() throws Exception | ||
{ | ||
ObjectMapper mapper = new ObjectMapper().registerModule(new ParanamerModule()); | ||
// first: serialize as is | ||
byte[] ser = jdkSerialize(mapper); | ||
ObjectMapper m2 = jdkDeserialize(ser); | ||
|
||
// then use lightly, repeat | ||
byte[] rawPoint = m2.writeValueAsBytes(new Point()); | ||
Point result = m2.readValue(rawPoint, Point.class); | ||
assertNotNull(result); | ||
|
||
ser = jdkSerialize(m2); | ||
ObjectMapper m3 = jdkDeserialize(ser); | ||
assertNotNull(m3); | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Helper methods | ||
/********************************************************** | ||
*/ | ||
|
||
protected byte[] jdkSerialize(Object o) throws IOException | ||
{ | ||
ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000); | ||
ObjectOutputStream obOut = new ObjectOutputStream(bytes); | ||
obOut.writeObject(o); | ||
obOut.close(); | ||
return bytes.toByteArray(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected <T> T jdkDeserialize(byte[] raw) throws IOException | ||
{ | ||
ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(raw)); | ||
try { | ||
return (T) objIn.readObject(); | ||
} catch (ClassNotFoundException e) { | ||
fail("Missing class: "+e.getMessage()); | ||
return null; | ||
} finally { | ||
objIn.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters