Skip to content

Commit

Permalink
Fix #13
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 22, 2016
1 parent 131fda0 commit d4f3f22
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 59 deletions.
2 changes: 1 addition & 1 deletion paranamer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ to introspect names of constructor (and factory method) parameters.
</plugin>

<plugin>
<!-- We will shade ASM, to simplify deployment, avoid version conflicts -->
<!-- We will shade Paranamer, to simplify deployment, avoid version conflicts -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${version.plugin.shade}</version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.fasterxml.jackson.module.paranamer;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.AnnotatedElement;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
Expand All @@ -21,16 +18,27 @@ public class ParanamerAnnotationIntrospector
{
private static final long serialVersionUID = 1;

protected final Paranamer _paranamer;
/**
* Wrapper used to encapsulate actual Paranamer call, to allow serialization
* of this introspector
*/
protected final SerializableParanamer _paranamer;

public ParanamerAnnotationIntrospector() {
this(new CachingParanamer(new BytecodeReadingParanamer()));
this(new SerializableParanamer());
}

public ParanamerAnnotationIntrospector(Paranamer pn) {
/**
* @since 2.7.6
*/
public ParanamerAnnotationIntrospector(SerializableParanamer pn) {
_paranamer = pn;
}

public ParanamerAnnotationIntrospector(Paranamer pn) {
this(new SerializableParanamer(pn));
}

@Override
public PropertyName findNameForDeserialization(Annotated a)
{
Expand All @@ -39,10 +47,13 @@ public PropertyName findNameForDeserialization(Annotated a)
* in {@link #findParameterSourceName(AnnotatedParameter)}.
*/
/*
if (a instanceof AnnotatedParameter) {
String rawName = _findParaName((AnnotatedParameter) a);
if (rawName != null) {
return new PropertyName(rawName);
PropertyName name = super.findNameForDeserialization(a);
if (name == null) {
if (a instanceof AnnotatedParameter) {
String rawName _paranamer.findParameterName((AnnotatedParameter) a);
if (rawName != null) {
return new PropertyName(rawName);
}
}
}
*/
Expand All @@ -53,26 +64,7 @@ public PropertyName findNameForDeserialization(Annotated a)
@Override
public String findImplicitPropertyName(AnnotatedMember param) {
if (param instanceof AnnotatedParameter) {
return _findParaName((AnnotatedParameter) param);
}
return null;
}

/*
/**********************************************************
/* Internal methods
/**********************************************************
*/

protected String _findParaName(AnnotatedParameter param)
{
int index = param.getIndex();
AnnotatedElement ctor = param.getOwner().getAnnotated();
String[] names = _paranamer.lookupParameterNames((AccessibleObject) ctor, false);
if (names != null) {
if (index < names.length) {
return names[index];
}
return _paranamer.findParameterName((AnnotatedParameter) param);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.fasterxml.jackson.module.paranamer;

import java.lang.reflect.*;

import com.thoughtworks.paranamer.BytecodeReadingParanamer;
import com.thoughtworks.paranamer.CachingParanamer;
import com.thoughtworks.paranamer.Paranamer;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.PropertyName;
Expand All @@ -21,16 +17,31 @@ public class ParanamerOnJacksonAnnotationIntrospector
{
private static final long serialVersionUID = 1;

protected final Paranamer _paranamer;
/**
* Wrapper used to encapsulate actual Paranamer call, to allow serialization
* of this introspector
*/
protected final SerializableParanamer _paranamer;

public ParanamerOnJacksonAnnotationIntrospector() {
this(new CachingParanamer(new BytecodeReadingParanamer()));
this(new SerializableParanamer());
}

public ParanamerOnJacksonAnnotationIntrospector(Paranamer pn) {
/**
* @since 2.7.6
*/
public ParanamerOnJacksonAnnotationIntrospector(SerializableParanamer pn) {
_paranamer = pn;
}

/**
* @deprecated since 2.7.6
*/
@Deprecated
public ParanamerOnJacksonAnnotationIntrospector(Paranamer pn) {
this(new SerializableParanamer(pn));
}

@Override
public PropertyName findNameForDeserialization(Annotated a)
{
Expand All @@ -42,7 +53,7 @@ public PropertyName findNameForDeserialization(Annotated a)
PropertyName name = super.findNameForDeserialization(a);
if (name == null) {
if (a instanceof AnnotatedParameter) {
String rawName = _findParaName((AnnotatedParameter) a);
String rawName _paranamer.findParameterName((AnnotatedParameter) a);
if (rawName != null) {
return new PropertyName(rawName);
}
Expand All @@ -52,28 +63,10 @@ public PropertyName findNameForDeserialization(Annotated a)
return null;
}

// since 2.4
@Override
public String findImplicitPropertyName(AnnotatedMember param) {
if (param instanceof AnnotatedParameter) {
return _findParaName((AnnotatedParameter) param);
}
return null;
}

/*
/**********************************************************
/* Internal methods
/**********************************************************
*/

protected String _findParaName(AnnotatedParameter param)
{
int index = param.getIndex();
AnnotatedElement ctor = param.getOwner().getAnnotated();
String[] names = _paranamer.lookupParameterNames((AccessibleObject) ctor);
if (names != null && index < names.length) {
return names[index];
return _paranamer.findParameterName((AnnotatedParameter) param);
}
return null;
}
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public void testSimple() throws Exception
assertEquals(40, bean.age);
}

// As per [Issue#3]
// Let's test handling of case where parameter names are not found; for example when
// trying to access things for JDK types
public void testWrapper() throws Exception
{
ObjectMapper mapper = new ObjectMapper().registerModule(new ParanamerModule());
Expand Down
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();
}
}
}
4 changes: 4 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Modules:
=== Releases ===
------------------------------------------------------------------------

2.7.6 (not yet released)

#13: (paranamer) Make `ParanamerAnnotationIntrospector` serializable

2.7.5 (11-Jun-2016)

No changes since 2.7.4
Expand Down

0 comments on commit d4f3f22

Please sign in to comment.