[jackson-jaxrs-providers] 67/162: Trying to fix #11, problems with caching of ObjectReaders/-Writers
Timo Aaltonen
tjaalton at moszumanska.debian.org
Mon Sep 8 22:16:29 UTC 2014
This is an automated email from the git hooks/post-receive script.
tjaalton pushed a commit to branch master
in repository jackson-jaxrs-providers.
commit c6ef21d20f00d363f5b70cd99e36671fed51a690
Author: Tatu Saloranta <tsaloranta at gmail.com>
Date: Tue May 14 20:36:09 2013 -0700
Trying to fix #11, problems with caching of ObjectReaders/-Writers
---
.../fasterxml/jackson/jaxrs/base/ProviderBase.java | 4 +-
.../jackson/jaxrs/cfg/AnnotationBundleKey.java | 43 +++++++++++++++++-----
.../jackson/jaxrs/cfg/EndpointConfigBase.java | 4 --
.../jaxrs/json/util/TestAnnotationBundleKey.java | 6 +--
4 files changed, 39 insertions(+), 18 deletions(-)
diff --git a/base/src/main/java/com/fasterxml/jackson/jaxrs/base/ProviderBase.java b/base/src/main/java/com/fasterxml/jackson/jaxrs/base/ProviderBase.java
index 003faa6..34775e5 100644
--- a/base/src/main/java/com/fasterxml/jackson/jaxrs/base/ProviderBase.java
+++ b/base/src/main/java/com/fasterxml/jackson/jaxrs/base/ProviderBase.java
@@ -399,7 +399,7 @@ public abstract class ProviderBase<
MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream)
throws IOException
{
- AnnotationBundleKey key = new AnnotationBundleKey(annotations);
+ AnnotationBundleKey key = new AnnotationBundleKey(annotations, type);
EP_CONFIG endpoint;
synchronized (_writers) {
endpoint = _writers.get(key);
@@ -536,7 +536,7 @@ public abstract class ProviderBase<
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders, InputStream entityStream)
throws IOException
{
- AnnotationBundleKey key = new AnnotationBundleKey(annotations);
+ AnnotationBundleKey key = new AnnotationBundleKey(annotations, type);
EP_CONFIG endpoint;
synchronized (_readers) {
endpoint = _readers.get(key);
diff --git a/base/src/main/java/com/fasterxml/jackson/jaxrs/cfg/AnnotationBundleKey.java b/base/src/main/java/com/fasterxml/jackson/jaxrs/cfg/AnnotationBundleKey.java
index 83c5f56..09337a2 100644
--- a/base/src/main/java/com/fasterxml/jackson/jaxrs/cfg/AnnotationBundleKey.java
+++ b/base/src/main/java/com/fasterxml/jackson/jaxrs/cfg/AnnotationBundleKey.java
@@ -15,6 +15,14 @@ public final class AnnotationBundleKey
private final static Annotation[] NO_ANNOTATIONS = new Annotation[0];
private final Annotation[] _annotations;
+
+ /**
+ * We also seem to need the type as part of the key (as per [Issue#11]);
+ * hopefully that and annotations are enough (if not, may need to reconsider
+ * the way caching is done, and possibly only cache derivation of annotations,
+ * not mapper or reader/writer).
+ */
+ private final Class<?> _type;
private final boolean _annotationsCopied;
@@ -25,24 +33,38 @@ public final class AnnotationBundleKey
/* Construction
/**********************************************************
*/
-
+
+ /**
+ * @deprecated Since 2.2.2: use variant that takes explicit Class
+ */
+ @Deprecated
public AnnotationBundleKey(Annotation[] annotations)
{
+ // could use a private inner class, but this'll do for now; no one should call anyway
+ this(annotations, AnnotationBundleKey.class);
+ }
+
+ public AnnotationBundleKey(Annotation[] annotations, Class<?> type)
+ {
+ _type = type;
+ // getting hash of name is faster than Class.hashCode() just because latter uses system identity hash:
+ final int typeHash = type.getName().hashCode();
if (annotations == null || annotations.length == 0) {
annotations = NO_ANNOTATIONS;
_annotationsCopied = true;
- _hashCode = -1;
+ _hashCode = typeHash;
} else {
_annotationsCopied = false;
- _hashCode = calcHash(annotations);
+ _hashCode = calcHash(annotations) ^ typeHash;
}
- _annotations = annotations;
+ _annotations = annotations;
}
- private AnnotationBundleKey(Annotation[] annotations, int hashCode)
+ private AnnotationBundleKey(Annotation[] annotations, Class<?> type, int hashCode)
{
- _annotations = annotations;
+ _annotations = annotations;
_annotationsCopied = true;
+ _type = type;
_hashCode = hashCode;
}
@@ -70,7 +92,7 @@ public final class AnnotationBundleKey
int len = _annotations.length;
Annotation[] newAnnotations = new Annotation[len];
System.arraycopy(_annotations, 0, newAnnotations, 0, len);
- return new AnnotationBundleKey(newAnnotations, _hashCode);
+ return new AnnotationBundleKey(newAnnotations, _type, _hashCode);
}
/*
@@ -86,7 +108,8 @@ public final class AnnotationBundleKey
@Override
public String toString() {
- return "[Annotations: "+_annotations.length+", hash 0x"+Integer.toHexString(_hashCode)
+ return "[Annotations: "+_annotations.length+", type: "
+ +_type.getName()+", hash 0x"+Integer.toHexString(_hashCode)
+", copied: "+_annotationsCopied+"]";
}
@@ -97,7 +120,9 @@ public final class AnnotationBundleKey
if (o == null) return false;
if (o.getClass() != getClass()) return false;
AnnotationBundleKey other = (AnnotationBundleKey) o;
- if (other._hashCode != _hashCode) return false;
+ if ((other._hashCode != _hashCode) || (other._type != _type)) {
+ return false;
+ }
return _equals(other._annotations);
}
diff --git a/base/src/main/java/com/fasterxml/jackson/jaxrs/cfg/EndpointConfigBase.java b/base/src/main/java/com/fasterxml/jackson/jaxrs/cfg/EndpointConfigBase.java
index 043e5b9..41496f0 100644
--- a/base/src/main/java/com/fasterxml/jackson/jaxrs/cfg/EndpointConfigBase.java
+++ b/base/src/main/java/com/fasterxml/jackson/jaxrs/cfg/EndpointConfigBase.java
@@ -133,10 +133,6 @@ public abstract class EndpointConfigBase<THIS extends EndpointConfigBase<THIS>>
if (_serDisable != null) {
_writer = _writer.withoutFeatures(_serDisable);
}
- // then others
-
- // Finally: couple of features we always set
-
/* Important: we are NOT to close the underlying stream after
* mapping, so we need to instruct parser:
*/
diff --git a/json/src/test/java/com/fasterxml/jackson/jaxrs/json/util/TestAnnotationBundleKey.java b/json/src/test/java/com/fasterxml/jackson/jaxrs/json/util/TestAnnotationBundleKey.java
index 1039a22..488a6ba 100644
--- a/json/src/test/java/com/fasterxml/jackson/jaxrs/json/util/TestAnnotationBundleKey.java
+++ b/json/src/test/java/com/fasterxml/jackson/jaxrs/json/util/TestAnnotationBundleKey.java
@@ -34,9 +34,9 @@ public class TestAnnotationBundleKey extends JaxrsTestBase
Annotation[] ann2 = m2.getAnnotations();
assertEquals(1, ann2.length);
- AnnotationBundleKey key1 = new AnnotationBundleKey(ann1);
- AnnotationBundleKey key2 = new AnnotationBundleKey(ann2);
- AnnotationBundleKey key1dup = new AnnotationBundleKey(ann1);
+ AnnotationBundleKey key1 = new AnnotationBundleKey(ann1, Object.class);
+ AnnotationBundleKey key2 = new AnnotationBundleKey(ann2, Object.class);
+ AnnotationBundleKey key1dup = new AnnotationBundleKey(ann1, Object.class);
AnnotationBundleKey key1immutable = key1.immutableKey();
// identity checks first
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jackson-jaxrs-providers.git
More information about the pkg-java-commits
mailing list