Skip to content

Commit a87f01c

Browse files
committed
Fix #79 (non-working minor optimization)
1 parent fcee651 commit a87f01c

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueWriterLocator.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class ValueWriterLocator extends ValueLocatorBase
7373
/**
7474
* Reusable lookup key; only used by per-thread instances.
7575
*/
76-
private ClassKey _key;
76+
private final ClassKey _key;
7777

7878
private Class<?> _prevClass;
7979

@@ -97,17 +97,21 @@ protected ValueWriterLocator(int features,
9797
_writeContext = null;
9898
_writerProvider = rwp;
9999
_writerModifier = rwm;
100+
// should not be needed for blueprint instance
101+
_key = null;
100102
}
101103

102104
// for per-call instances
103105
protected ValueWriterLocator(ValueWriterLocator base,
104-
int features, JSONWriter w) {
106+
int features, JSONWriter w)
107+
{
105108
_features = features;
106109
_writeContext = w;
107110
_knownSerTypes = base._knownSerTypes;
108111
_knownWriters = base._knownWriters;
109112
_writerProvider = base._writerProvider;
110113
_writerModifier = base._writerModifier;
114+
_key = new ClassKey();
111115
}
112116

113117
public final static ValueWriterLocator blueprint(ReaderWriterProvider rwp, ReaderWriterModifier rwm) {
@@ -160,9 +164,8 @@ public final int findSerializationType(Class<?> raw)
160164
if (raw == String.class && (_writerModifier == null)) {
161165
return SER_STRING;
162166
}
163-
ClassKey k = (_key == null) ? new ClassKey(raw, _features) : _key.with(raw, _features);
167+
final ClassKey k = _key.with(raw, _features);
164168
int type;
165-
166169
Integer I = _knownSerTypes.get(k);
167170

168171
if (I == null) {

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/impl/ValueWriterLocatorTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public void setValue(Long v) {
4141
*/
4242

4343
public void testBasicTypeDetectionForSer() {
44-
ValueWriterLocator td = ValueWriterLocator.blueprint(null, null);
44+
// note: must create non-blue-print instance to avoid NPE
45+
ValueWriterLocator td = ValueWriterLocator.blueprint(null, null)
46+
.perOperationInstance(null, 0);
4547
assertEquals(ValueWriterLocator.SER_STRING, td.findSerializationType(String.class));
4648
assertEquals(ValueWriterLocator.SER_CHAR_ARRAY, td.findSerializationType(char[].class));
4749
assertEquals(ValueWriterLocator.SER_INT_ARRAY, td.findSerializationType(int[].class));

release-notes/VERSION-2.x

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Modules:
1313

1414
2.13.0 (not yet released)
1515

16-
No changes since 2.12
16+
#79: Reuse of ClassKey in ValueWriterLocator not working
1717

1818
2.12.2 (03-Mar-2021)
1919

0 commit comments

Comments
 (0)