Skip to content

Commit

Permalink
Bit more rearranging; trying to reduce use of inner classes for share…
Browse files Browse the repository at this point in the history
…d things (only leave inner classes for private use)
  • Loading branch information
cowtowncoder committed Feb 5, 2014
1 parent dcdc5e4 commit 5e21d2e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
* @author pgelinas
*/
public final class UnresolvedForwardReference extends JsonMappingException {

private static final long serialVersionUID = -5097969645059502061L;
private static final long serialVersionUID = 1L;
private ReadableObjectId _roid;
private List<UnresolvedId> _unresolvedIds;

Expand All @@ -36,52 +35,15 @@ public UnresolvedForwardReference(String msg)
// ****** Accessor methods ******
// ******************************

public ReadableObjectId getRoid()
{
public ReadableObjectId getRoid() {
return _roid;
}

public Object getUnresolvedId()
{
public Object getUnresolvedId() {
return _roid.id;
}

/**
* Helper class
*
* @author pgelinas
*/
public static class UnresolvedId {
private Object _id;
private JsonLocation _location;
private Class<?> _type;

public UnresolvedId(Object id, Class<?> type, JsonLocation where)
{
_id = id;
_type = type;
_location = where;
}

/**
* The id which is unresolved.
*/
public Object getId() { return _id; }
/**
* The type of object which was expected.
*/
public Class<?> getType() { return _type; }
public JsonLocation getLocation() { return _location; }

@Override
public String toString()
{
return String.format("Object id [%s] (for %s) at %s", _id, _type, _location);
}
}

public void addUnresolvedId(Object id, Class<?> type, JsonLocation where)
{
public void addUnresolvedId(Object id, Class<?> type, JsonLocation where) {
_unresolvedIds.add(new UnresolvedId(id, type, where));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.fasterxml.jackson.databind.deser;

import com.fasterxml.jackson.core.JsonLocation;

/**
* Helper class for {@link UnresolvedForwardReference}, to contain information about unresolved ids.
*
* @author pgelinas
*/
public class UnresolvedId {
private Object _id;
private JsonLocation _location;
private Class<?> _type;

public UnresolvedId(Object id, Class<?> type, JsonLocation where)
{
_id = id;
_type = type;
_location = where;
}

/**
* The id which is unresolved.
*/
public Object getId() { return _id; }

/**
* The type of object which was expected.
*/
public Class<?> getType() { return _type; }
public JsonLocation getLocation() { return _location; }

@Override
public String toString() {
return String.format("Object id [%s] (for %s) at %s", _id, _type, _location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

/**
* Wrapper property that is used to handle managed (forward) properties
* (see [JACKSON-235] for more information). Basically just need to
* delegate first to actual forward property, and
* Basically just needs to delegate first to actual forward property, and
* then to back property.
*/
public final class ManagedReferenceProperty
extends SettableBeanProperty
Expand All @@ -36,9 +36,8 @@ public final class ManagedReferenceProperty

protected final SettableBeanProperty _backProperty;

public ManagedReferenceProperty(SettableBeanProperty forward,
String refName, SettableBeanProperty backward,
Annotations contextAnnotations, boolean isContainer)
public ManagedReferenceProperty(SettableBeanProperty forward, String refName,
SettableBeanProperty backward, Annotations contextAnnotations, boolean isContainer)
{
super(forward.getFullName(), forward.getType(), forward.getWrapperName(),
forward.getValueTypeDeserializer(), contextAnnotations,
Expand Down Expand Up @@ -96,32 +95,31 @@ public <A extends Annotation> A getAnnotation(Class<A> acls) {
*/

@Override
public void deserializeAndSet(JsonParser jp, DeserializationContext ctxt,
Object instance)
throws IOException, JsonProcessingException
{
public void deserializeAndSet(JsonParser jp, DeserializationContext ctxt, Object instance)
throws IOException, JsonProcessingException {
set(instance, _managedProperty.deserialize(jp, ctxt));
}

@Override
public Object deserializeSetAndReturn(JsonParser jp,
DeserializationContext ctxt, Object instance)
throws IOException, JsonProcessingException
{
public Object deserializeSetAndReturn(JsonParser jp, DeserializationContext ctxt, Object instance)
throws IOException, JsonProcessingException {
return setAndReturn(instance, deserialize(jp, ctxt));
}

@Override
public final void set(Object instance, Object value)
throws IOException
{
public final void set(Object instance, Object value) throws IOException {
setAndReturn(instance, value);
}

@Override
public Object setAndReturn(Object instance, Object value)
throws IOException
{
public Object setAndReturn(Object instance, Object value) throws IOException
{
/* 04-Feb-2014, tatu: As per [#390], it may be necessary to switch the
* ordering of forward/backward references, and start with back ref.
* But before doing that, need a unit test.
*/

// Start with forward property
Object result = _managedProperty.setAndReturn(instance, value);
/* And then back reference, if (and only if!) we actually have a non-null
* reference
Expand All @@ -130,21 +128,15 @@ public Object setAndReturn(Object instance, Object value)
if (_isContainer) { // ok, this gets ugly... but has to do for now
if (value instanceof Object[]) {
for (Object ob : (Object[]) value) {
if (ob != null) {
_backProperty.set(ob, instance);
}
if (ob != null) { _backProperty.set(ob, instance); }
}
} else if (value instanceof Collection<?>) {
for (Object ob : (Collection<?>) value) {
if (ob != null) {
_backProperty.set(ob, instance);
}
if (ob != null) { _backProperty.set(ob, instance); }
}
} else if (value instanceof Map<?,?>) {
for (Object ob : ((Map<?,?>) value).values()) {
if (ob != null) {
_backProperty.set(ob, instance);
}
if (ob != null) { _backProperty.set(ob, instance); }
}
} else {
throw new IllegalStateException("Unsupported container type ("+value.getClass().getName()
Expand All @@ -156,4 +148,4 @@ public Object setAndReturn(Object instance, Object value)
}
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public ReadableObjectId(Object id)
this.id = id;
}

public void appendReferring(Referring currentReferring)
{
public void appendReferring(Referring currentReferring) {
if (_referringProperties == null) {
_referringProperties = new LinkedList<Referring>();
}
Expand All @@ -36,8 +35,7 @@ public void appendReferring(Referring currentReferring)
* Method called to assign actual POJO to which ObjectId refers to: will
* also handle referring properties, if any, by assigning POJO.
*/
public void bindItem(Object ob)
throws IOException
public void bindItem(Object ob) throws IOException
{
if (item != null) {
throw new IllegalStateException("Already had POJO for id (" + id.getClass().getName() + ") [" + id + "]");
Expand All @@ -47,19 +45,16 @@ public void bindItem(Object ob)
Iterator<Referring> it = _referringProperties.iterator();
_referringProperties = null;
while (it.hasNext()) {
Referring ref = it.next();
ref.handleResolvedForwardReference(id, ob);
it.next().handleResolvedForwardReference(id, ob);
}
}
}

public boolean hasReferringProperties()
{
public boolean hasReferringProperties() {
return (_referringProperties != null) && !_referringProperties.isEmpty();
}

public Iterator<Referring> referringProperties()
{
public Iterator<Referring> referringProperties() {
if (_referringProperties == null) {
return Collections.<Referring> emptyList().iterator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,11 @@ public Collection<Object> deserialize(JsonParser jp, DeserializationContext ctxt
}

JsonDeserializer<Object> valueDes = _valueDeserializer;
JsonToken t;
final TypeDeserializer typeDeser = _valueTypeDeserializer;
CollectionReferringAccumulator referringAccumulator = null;
if(valueDes.getObjectIdReader() != null){
referringAccumulator = new CollectionReferringAccumulator(result);
}
CollectionReferringAccumulator referringAccumulator =
(valueDes.getObjectIdReader() == null) ? null : new CollectionReferringAccumulator(result);

JsonToken t;
while ((t = jp.nextToken()) != JsonToken.END_ARRAY) {
try {
Object value;
Expand Down Expand Up @@ -318,8 +317,7 @@ public Referring handleUnresolvedReference(UnresolvedForwardReference reference)
return id;
}

public void resolveForwardReference(Object id, Object value)
throws IOException
public void resolveForwardReference(Object id, Object value) throws IOException
{
Iterator<UnresolvedId> iterator = _accumulator.iterator();
// Resolve ordering after resolution of an id. This mean either:
Expand Down Expand Up @@ -357,9 +355,7 @@ private UnresolvedId(Object id, JsonLocation location)
}

@Override
public void handleResolvedForwardReference(Object id, Object value)
throws IOException
{
public void handleResolvedForwardReference(Object id, Object value) throws IOException {
resolveForwardReference(id, value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.UnresolvedForwardReference;
import com.fasterxml.jackson.databind.deser.UnresolvedForwardReference.UnresolvedId;
import com.fasterxml.jackson.databind.deser.UnresolvedId;
import com.fasterxml.jackson.databind.struct.TestObjectId.Company;
import com.fasterxml.jackson.databind.struct.TestObjectId.Employee;

Expand Down

0 comments on commit 5e21d2e

Please sign in to comment.