Skip to content

Commit 9069158

Browse files
committed
Make AnnotationMap more (or, completely?) immutable
1 parent 4fb6978 commit 9069158

File tree

3 files changed

+23
-54
lines changed

3 files changed

+23
-54
lines changed

src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedWithParams.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fasterxml.jackson.databind.introspect;
22

3-
import java.lang.annotation.Annotation;
43
import java.lang.reflect.Modifier;
54
import java.lang.reflect.Parameter;
65

@@ -43,7 +42,8 @@ protected AnnotatedWithParams(AnnotatedWithParams base, AnnotationMap[] paramAnn
4342
* usually due to a mix-in
4443
* annotation masking or overriding an annotation 'real' method
4544
* has.
46-
*/
45+
*
46+
@Deprecated // since 3.0
4747
public final void addOrOverrideParam(int paramIndex, Annotation a)
4848
{
4949
AnnotationMap old = _paramAnnotations[paramIndex];
@@ -53,6 +53,7 @@ public final void addOrOverrideParam(int paramIndex, Annotation a)
5353
}
5454
old.add(a);
5555
}
56+
*/
5657

5758
/**
5859
* Method called by parameter object when an augmented instance is created;

src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationCollector.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,7 @@ public Annotations asAnnotations() {
149149

150150
@Override
151151
public AnnotationMap asAnnotationMap() {
152-
AnnotationMap result = new AnnotationMap();
153-
for (Annotation ann : _annotations.values()) {
154-
result.add(ann);
155-
}
156-
return result;
152+
return AnnotationMap.of(_annotations.values());
157153
}
158154

159155
@Override

src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationMap.java

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,32 @@
1313
*/
1414
public final class AnnotationMap implements Annotations
1515
{
16-
protected HashMap<Class<?>,Annotation> _annotations;
16+
protected Map<Class<?>,Annotation> _annotations;
1717

18+
/*
19+
/**********************************************************
20+
/* Construction
21+
/**********************************************************
22+
*/
23+
1824
public AnnotationMap() { }
1925

26+
public AnnotationMap(Map<Class<?>,Annotation> a) {
27+
_annotations = a;
28+
}
29+
2030
public static AnnotationMap of(Class<?> type, Annotation value) {
21-
HashMap<Class<?>,Annotation> ann = new HashMap<>(4);
31+
Map<Class<?>,Annotation> ann = new HashMap<>(4);
2232
ann.put(type, value);
2333
return new AnnotationMap(ann);
2434
}
2535

26-
AnnotationMap(HashMap<Class<?>,Annotation> a) {
27-
_annotations = a;
36+
public static AnnotationMap of(Collection<Annotation> rawAnnotations) {
37+
Map<Class<?>,Annotation> map = new HashMap<>(rawAnnotations.size());
38+
for (Annotation raw : rawAnnotations) {
39+
map.put(raw.annotationType(), raw);
40+
}
41+
return new AnnotationMap(map);
2842
}
2943

3044
/*
@@ -55,8 +69,6 @@ public boolean has(Class<?> cls)
5569
/**
5670
* Helper method that can be used for a "bulk" check to see if at least
5771
* one of given annotation types is included within this map.
58-
*
59-
* @since 2.7
6072
*/
6173
@Override
6274
public boolean hasOneOf(Class<? extends Annotation>[] annoClasses) {
@@ -75,10 +87,7 @@ public boolean hasOneOf(Class<? extends Annotation>[] annoClasses) {
7587
/* Other API
7688
/**********************************************************
7789
*/
78-
79-
/**
80-
* @since 2.3
81-
*/
90+
8291
public Iterable<Annotation> annotations() {
8392
if (_annotations == null || _annotations.size() == 0) {
8493
return Collections.emptyList();
@@ -111,48 +120,11 @@ public int size() {
111120
return (_annotations == null) ? 0 : _annotations.size();
112121
}
113122

114-
/**
115-
* Method called to add specified annotation in the Map, but
116-
* only if it didn't yet exist.
117-
*/
118-
public boolean addIfNotPresent(Annotation ann)
119-
{
120-
if (_annotations == null || !_annotations.containsKey(ann.annotationType())) {
121-
_add(ann);
122-
return true;
123-
}
124-
return false;
125-
}
126-
127-
/**
128-
* Method called to add specified annotation in the Map.
129-
*
130-
* @return True if the addition changed the contents, that is, this map did not
131-
* already have specified annotation
132-
*/
133-
public boolean add(Annotation ann) {
134-
return _add(ann);
135-
}
136-
137123
@Override
138124
public String toString() {
139125
if (_annotations == null) {
140126
return "[null]";
141127
}
142128
return _annotations.toString();
143129
}
144-
145-
/*
146-
/**********************************************************
147-
/* Helper methods
148-
/**********************************************************
149-
*/
150-
151-
protected final boolean _add(Annotation ann) {
152-
if (_annotations == null) {
153-
_annotations = new HashMap<Class<?>,Annotation>();
154-
}
155-
Annotation previous = _annotations.put(ann.annotationType(), ann);
156-
return (previous == null) || !previous.equals(ann);
157-
}
158130
}

0 commit comments

Comments
 (0)