Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1 | package com.fasterxml.jackson.databind.introspect; |
| 2 | |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 3 | import java.util.*; |
Tatu Saloranta | a00cdd8 | 2014-03-30 19:22:49 -0700 | [diff] [blame] | 4 | |
Tatu Saloranta | 80674fe | 2014-12-03 22:17:21 -0800 | [diff] [blame] | 5 | import com.fasterxml.jackson.annotation.JsonInclude; |
Cowtowncoder | 04413c7 | 2015-06-02 17:46:11 -0700 | [diff] [blame] | 6 | import com.fasterxml.jackson.annotation.JsonProperty; |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 7 | import com.fasterxml.jackson.databind.*; |
Cowtowncoder | c717c2f | 2014-09-25 16:52:30 -0700 | [diff] [blame] | 8 | import com.fasterxml.jackson.databind.util.EmptyIterator; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * Helper class used for aggregating information about a single |
| 12 | * potential POJO property. |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 13 | */ |
| 14 | public class POJOPropertyBuilder |
| 15 | extends BeanPropertyDefinition |
| 16 | implements Comparable<POJOPropertyBuilder> |
| 17 | { |
| 18 | /** |
Tatu Saloranta | e40a769 | 2012-01-25 17:36:36 -0800 | [diff] [blame] | 19 | * Whether property is being composed for serialization |
| 20 | * (true) or deserialization (false) |
| 21 | */ |
| 22 | protected final boolean _forSerialization; |
| 23 | |
| 24 | protected final AnnotationIntrospector _annotationIntrospector; |
Tatu | 97d03f6 | 2014-05-15 14:23:11 -0700 | [diff] [blame] | 25 | |
Tatu Saloranta | e40a769 | 2012-01-25 17:36:36 -0800 | [diff] [blame] | 26 | /** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 27 | * External name of logical property; may change with |
| 28 | * renaming (by new instance being constructed using |
| 29 | * a new name) |
| 30 | */ |
Tatu Saloranta | 1488cef | 2013-07-26 14:42:17 -0700 | [diff] [blame] | 31 | protected final PropertyName _name; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Original internal name, derived from accessor, of this |
| 35 | * property. Will not be changed by renaming. |
| 36 | */ |
Tatu Saloranta | 1488cef | 2013-07-26 14:42:17 -0700 | [diff] [blame] | 37 | protected final PropertyName _internalName; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 38 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 39 | protected Linked<AnnotatedField> _fields; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 40 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 41 | protected Linked<AnnotatedParameter> _ctorParameters; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 42 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 43 | protected Linked<AnnotatedMethod> _getters; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 44 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 45 | protected Linked<AnnotatedMethod> _setters; |
Tatu Saloranta | 1488cef | 2013-07-26 14:42:17 -0700 | [diff] [blame] | 46 | |
Tatu Saloranta | 80674fe | 2014-12-03 22:17:21 -0800 | [diff] [blame] | 47 | public POJOPropertyBuilder(PropertyName internalName, AnnotationIntrospector ai, |
| 48 | boolean forSerialization) { |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 49 | this(internalName, internalName, ai, forSerialization); |
| 50 | } |
| 51 | |
| 52 | protected POJOPropertyBuilder(PropertyName internalName, PropertyName name, |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 53 | AnnotationIntrospector annotationIntrospector, boolean forSerialization) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 54 | { |
| 55 | _internalName = internalName; |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 56 | _name = name; |
Tatu Saloranta | e40a769 | 2012-01-25 17:36:36 -0800 | [diff] [blame] | 57 | _annotationIntrospector = annotationIntrospector; |
| 58 | _forSerialization = forSerialization; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 59 | } |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 60 | |
Tatu Saloranta | 1488cef | 2013-07-26 14:42:17 -0700 | [diff] [blame] | 61 | public POJOPropertyBuilder(POJOPropertyBuilder src, PropertyName newName) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 62 | { |
| 63 | _internalName = src._internalName; |
| 64 | _name = newName; |
Tatu Saloranta | e40a769 | 2012-01-25 17:36:36 -0800 | [diff] [blame] | 65 | _annotationIntrospector = src._annotationIntrospector; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 66 | _fields = src._fields; |
| 67 | _ctorParameters = src._ctorParameters; |
| 68 | _getters = src._getters; |
| 69 | _setters = src._setters; |
Tatu Saloranta | e40a769 | 2012-01-25 17:36:36 -0800 | [diff] [blame] | 70 | _forSerialization = src._forSerialization; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 71 | } |
Tatu Saloranta | fbd795b | 2012-01-26 22:02:30 -0800 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | /********************************************************** |
| 75 | /* Fluent factory methods |
| 76 | /********************************************************** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 77 | */ |
Tatu Saloranta | fbd795b | 2012-01-26 22:02:30 -0800 | [diff] [blame] | 78 | |
| 79 | @Override |
Tatu Saloranta | 1488cef | 2013-07-26 14:42:17 -0700 | [diff] [blame] | 80 | public POJOPropertyBuilder withName(PropertyName newName) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 81 | return new POJOPropertyBuilder(this, newName); |
| 82 | } |
Tatu Saloranta | 1488cef | 2013-07-26 14:42:17 -0700 | [diff] [blame] | 83 | |
| 84 | @Override |
| 85 | public POJOPropertyBuilder withSimpleName(String newSimpleName) |
| 86 | { |
| 87 | PropertyName newName = _name.withSimpleName(newSimpleName); |
| 88 | return (newName == _name) ? this : new POJOPropertyBuilder(this, newName); |
| 89 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | /********************************************************** |
| 93 | /* Comparable implementation: sort alphabetically, except |
| 94 | /* that properties with constructor parameters sorted |
| 95 | /* before other properties |
| 96 | /********************************************************** |
| 97 | */ |
| 98 | |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 99 | @Override |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 100 | public int compareTo(POJOPropertyBuilder other) |
| 101 | { |
| 102 | // first, if one has ctor params, that should come first: |
| 103 | if (_ctorParameters != null) { |
| 104 | if (other._ctorParameters == null) { |
| 105 | return -1; |
| 106 | } |
| 107 | } else if (other._ctorParameters != null) { |
| 108 | return 1; |
| 109 | } |
| 110 | /* otherwise sort by external name (including sorting of |
| 111 | * ctor parameters) |
| 112 | */ |
| 113 | return getName().compareTo(other.getName()); |
| 114 | } |
Tatu Saloranta | 69d9c10 | 2013-03-13 15:20:18 -0700 | [diff] [blame] | 115 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 116 | /* |
| 117 | /********************************************************** |
Tatu Saloranta | 69d9c10 | 2013-03-13 15:20:18 -0700 | [diff] [blame] | 118 | /* BeanPropertyDefinition implementation, name/type |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 119 | /********************************************************** |
| 120 | */ |
| 121 | |
| 122 | @Override |
Tatu Saloranta | 1488cef | 2013-07-26 14:42:17 -0700 | [diff] [blame] | 123 | public String getName() { |
| 124 | return (_name == null) ? null : _name.getSimpleName(); |
| 125 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 126 | |
| 127 | @Override |
Tatu Saloranta | 1488cef | 2013-07-26 14:42:17 -0700 | [diff] [blame] | 128 | public PropertyName getFullName() { |
| 129 | return _name; |
| 130 | } |
Tatu Saloranta | a6943ec | 2015-03-15 20:00:58 -0700 | [diff] [blame] | 131 | |
| 132 | @Override |
| 133 | public boolean hasName(PropertyName name) { |
| 134 | return _name.equals(name); |
| 135 | } |
| 136 | |
Tatu Saloranta | 1488cef | 2013-07-26 14:42:17 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public String getInternalName() { return _internalName.getSimpleName(); } |
Tatu Saloranta | d8e52fa | 2012-03-12 23:17:45 -0700 | [diff] [blame] | 139 | |
| 140 | @Override |
Tatu Saloranta | 69d9c10 | 2013-03-13 15:20:18 -0700 | [diff] [blame] | 141 | public PropertyName getWrapperName() { |
Tatu Saloranta | 8d53c22 | 2013-03-13 15:56:09 -0700 | [diff] [blame] | 142 | /* 13-Mar-2013, tatu: Accessing via primary member SHOULD work, |
| 143 | * due to annotation merging. However, I have seen some problems |
Tatu Saloranta | 1488cef | 2013-07-26 14:42:17 -0700 | [diff] [blame] | 144 | * with this access (for other annotations)... so if this should |
| 145 | * occur, try commenting out full traversal code |
Tatu Saloranta | 8d53c22 | 2013-03-13 15:56:09 -0700 | [diff] [blame] | 146 | */ |
| 147 | AnnotatedMember member = getPrimaryMember(); |
| 148 | return (member == null || _annotationIntrospector == null) ? null |
| 149 | : _annotationIntrospector.findWrapperName(member); |
Tatu Saloranta | 69d9c10 | 2013-03-13 15:20:18 -0700 | [diff] [blame] | 150 | /* |
| 151 | return fromMemberAnnotations(new WithMember<PropertyName>() { |
| 152 | @Override |
| 153 | public PropertyName withMember(AnnotatedMember member) { |
| 154 | return _annotationIntrospector.findWrapperName(member); |
| 155 | } |
| 156 | }); |
| 157 | */ |
| 158 | } |
| 159 | |
| 160 | @Override |
Tatu Saloranta | d8e52fa | 2012-03-12 23:17:45 -0700 | [diff] [blame] | 161 | public boolean isExplicitlyIncluded() { |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 162 | return _anyExplicits(_fields) |
| 163 | || _anyExplicits(_getters) |
| 164 | || _anyExplicits(_setters) |
| 165 | || _anyExplicits(_ctorParameters) |
| 166 | ; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public boolean isExplicitlyNamed() { |
Tatu Saloranta | d8e52fa | 2012-03-12 23:17:45 -0700 | [diff] [blame] | 171 | return _anyExplicitNames(_fields) |
| 172 | || _anyExplicitNames(_getters) |
| 173 | || _anyExplicitNames(_setters) |
| 174 | || _anyExplicitNames(_ctorParameters) |
| 175 | ; |
| 176 | } |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 177 | |
Tatu Saloranta | 69d9c10 | 2013-03-13 15:20:18 -0700 | [diff] [blame] | 178 | /* |
| 179 | /********************************************************** |
| 180 | /* BeanPropertyDefinition implementation, accessor access |
| 181 | /********************************************************** |
| 182 | */ |
Tatu | 7003462 | 2014-05-21 14:41:45 -0700 | [diff] [blame] | 183 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 184 | @Override |
| 185 | public boolean hasGetter() { return _getters != null; } |
| 186 | |
| 187 | @Override |
| 188 | public boolean hasSetter() { return _setters != null; } |
| 189 | |
| 190 | @Override |
| 191 | public boolean hasField() { return _fields != null; } |
| 192 | |
| 193 | @Override |
| 194 | public boolean hasConstructorParameter() { return _ctorParameters != null; } |
| 195 | |
| 196 | @Override |
Tatu | 7003462 | 2014-05-21 14:41:45 -0700 | [diff] [blame] | 197 | public boolean couldDeserialize() { |
| 198 | return (_ctorParameters != null) || (_setters != null) || (_fields != null); |
| 199 | } |
| 200 | |
| 201 | @Override |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 202 | public boolean couldSerialize() { |
| 203 | return (_getters != null) || (_fields != null); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public AnnotatedMethod getGetter() |
| 208 | { |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 209 | // Easy with zero or one getters... |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 210 | Linked<AnnotatedMethod> curr = _getters; |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 211 | if (curr == null) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 212 | return null; |
| 213 | } |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 214 | Linked<AnnotatedMethod> next = curr.next; |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 215 | if (next == null) { |
| 216 | return curr.value; |
| 217 | } |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 218 | // But if multiple, verify that they do not conflict... |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 219 | for (; next != null; next = next.next) { |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 220 | /* [JACKSON-255] Allow masking, i.e. do not report exception if one |
| 221 | * is in super-class from the other |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 222 | */ |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 223 | Class<?> currClass = curr.value.getDeclaringClass(); |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 224 | Class<?> nextClass = next.value.getDeclaringClass(); |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 225 | if (currClass != nextClass) { |
| 226 | if (currClass.isAssignableFrom(nextClass)) { // next is more specific |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 227 | curr = next; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 228 | continue; |
| 229 | } |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 230 | if (nextClass.isAssignableFrom(currClass)) { // current more specific |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 231 | continue; |
| 232 | } |
| 233 | } |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 234 | /* 30-May-2014, tatu: Three levels of precedence: |
| 235 | * |
| 236 | * 1. Regular getters ("getX") |
| 237 | * 2. Is-getters ("isX") |
| 238 | * 3. Implicit, possible getters ("x") |
Tatu Saloranta | 833680a | 2013-07-10 19:37:14 -0700 | [diff] [blame] | 239 | */ |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 240 | int priNext = _getterPriority(next.value); |
| 241 | int priCurr = _getterPriority(curr.value); |
| 242 | |
| 243 | if (priNext != priCurr) { |
| 244 | if (priNext < priCurr) { |
| 245 | curr = next; |
| 246 | } |
Tatu Saloranta | 833680a | 2013-07-10 19:37:14 -0700 | [diff] [blame] | 247 | continue; |
| 248 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 249 | throw new IllegalArgumentException("Conflicting getter definitions for property \""+getName()+"\": " |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 250 | +curr.value.getFullName()+" vs "+next.value.getFullName()); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 251 | } |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 252 | // One more thing; to avoid having to do it again... |
| 253 | _getters = curr.withoutNext(); |
| 254 | return curr.value; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 255 | } |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 256 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 257 | @Override |
| 258 | public AnnotatedMethod getSetter() |
| 259 | { |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 260 | // Easy with zero or one getters... |
| 261 | Linked<AnnotatedMethod> curr = _setters; |
| 262 | if (curr == null) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 263 | return null; |
| 264 | } |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 265 | Linked<AnnotatedMethod> next = curr.next; |
| 266 | if (next == null) { |
| 267 | return curr.value; |
| 268 | } |
| 269 | // But if multiple, verify that they do not conflict... |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 270 | for (; next != null; next = next.next) { |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 271 | /* [JACKSON-255] Allow masking, i.e. do not report exception if one |
| 272 | * is in super-class from the other |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 273 | */ |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 274 | Class<?> currClass = curr.value.getDeclaringClass(); |
| 275 | Class<?> nextClass = next.value.getDeclaringClass(); |
| 276 | if (currClass != nextClass) { |
| 277 | if (currClass.isAssignableFrom(nextClass)) { // next is more specific |
| 278 | curr = next; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 279 | continue; |
| 280 | } |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 281 | if (nextClass.isAssignableFrom(currClass)) { // current more specific |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 282 | continue; |
| 283 | } |
| 284 | } |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 285 | /* 30-May-2014, tatu: Two levels of precedence: |
| 286 | * |
| 287 | * 1. Regular setters ("setX(...)") |
| 288 | * 2. Implicit, possible setters ("x(...)") |
| 289 | */ |
| 290 | int priNext = _setterPriority(next.value); |
| 291 | int priCurr = _setterPriority(curr.value); |
| 292 | |
| 293 | if (priNext != priCurr) { |
| 294 | if (priNext < priCurr) { |
| 295 | curr = next; |
| 296 | } |
| 297 | continue; |
| 298 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 299 | throw new IllegalArgumentException("Conflicting setter definitions for property \""+getName()+"\": " |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 300 | +curr.value.getFullName()+" vs "+next.value.getFullName()); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 301 | } |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 302 | // One more thing; to avoid having to do it again... |
| 303 | _setters = curr.withoutNext(); |
| 304 | return curr.value; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public AnnotatedField getField() |
| 309 | { |
| 310 | if (_fields == null) { |
| 311 | return null; |
| 312 | } |
| 313 | // If multiple, verify that they do not conflict... |
| 314 | AnnotatedField field = _fields.value; |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 315 | Linked<AnnotatedField> next = _fields.next; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 316 | for (; next != null; next = next.next) { |
| 317 | AnnotatedField nextField = next.value; |
| 318 | Class<?> fieldClass = field.getDeclaringClass(); |
| 319 | Class<?> nextClass = nextField.getDeclaringClass(); |
| 320 | if (fieldClass != nextClass) { |
| 321 | if (fieldClass.isAssignableFrom(nextClass)) { // next is more specific |
| 322 | field = nextField; |
| 323 | continue; |
| 324 | } |
| 325 | if (nextClass.isAssignableFrom(fieldClass)) { // getter more specific |
| 326 | continue; |
| 327 | } |
| 328 | } |
| 329 | throw new IllegalArgumentException("Multiple fields representing property \""+getName()+"\": " |
| 330 | +field.getFullName()+" vs "+nextField.getFullName()); |
| 331 | } |
| 332 | return field; |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public AnnotatedParameter getConstructorParameter() |
| 337 | { |
| 338 | if (_ctorParameters == null) { |
| 339 | return null; |
| 340 | } |
| 341 | /* Hmmh. Checking for constructor parameters is trickier; for one, |
| 342 | * we must allow creator and factory method annotations. |
| 343 | * If this is the case, constructor parameter has the precedence. |
| 344 | * |
| 345 | * So, for now, just try finding the first constructor parameter; |
| 346 | * if none, first factory method. And don't check for dups, if we must, |
| 347 | * can start checking for them later on. |
| 348 | */ |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 349 | Linked<AnnotatedParameter> curr = _ctorParameters; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 350 | do { |
| 351 | if (curr.value.getOwner() instanceof AnnotatedConstructor) { |
| 352 | return curr.value; |
| 353 | } |
| 354 | curr = curr.next; |
| 355 | } while (curr != null); |
| 356 | return _ctorParameters.value; |
| 357 | } |
Cowtowncoder | c717c2f | 2014-09-25 16:52:30 -0700 | [diff] [blame] | 358 | |
| 359 | @Override |
| 360 | public Iterator<AnnotatedParameter> getConstructorParameters() { |
| 361 | if (_ctorParameters == null) { |
| 362 | return EmptyIterator.instance(); |
| 363 | } |
| 364 | return new MemberIterator<AnnotatedParameter>(_ctorParameters); |
| 365 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 366 | |
Tatu Saloranta | e40a769 | 2012-01-25 17:36:36 -0800 | [diff] [blame] | 367 | @Override |
| 368 | public AnnotatedMember getAccessor() |
| 369 | { |
| 370 | AnnotatedMember m = getGetter(); |
| 371 | if (m == null) { |
| 372 | m = getField(); |
| 373 | } |
| 374 | return m; |
| 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public AnnotatedMember getMutator() |
| 379 | { |
| 380 | AnnotatedMember m = getConstructorParameter(); |
| 381 | if (m == null) { |
| 382 | m = getSetter(); |
| 383 | if (m == null) { |
| 384 | m = getField(); |
| 385 | } |
| 386 | } |
| 387 | return m; |
| 388 | } |
Tatu Saloranta | b009194 | 2013-10-18 23:52:30 -0700 | [diff] [blame] | 389 | |
| 390 | @Override |
| 391 | public AnnotatedMember getNonConstructorMutator() { |
| 392 | AnnotatedMember m = getSetter(); |
| 393 | if (m == null) { |
| 394 | m = getField(); |
| 395 | } |
| 396 | return m; |
| 397 | } |
| 398 | |
Tatu Saloranta | f412e85 | 2012-09-11 22:05:51 -0700 | [diff] [blame] | 399 | @Override |
| 400 | public AnnotatedMember getPrimaryMember() { |
| 401 | if (_forSerialization) { |
| 402 | return getAccessor(); |
| 403 | } |
| 404 | return getMutator(); |
| 405 | } |
Tatu Saloranta | e40a769 | 2012-01-25 17:36:36 -0800 | [diff] [blame] | 406 | |
Tatu Saloranta | f52059d | 2014-05-30 23:07:58 -0700 | [diff] [blame] | 407 | protected int _getterPriority(AnnotatedMethod m) |
| 408 | { |
| 409 | final String name = m.getName(); |
| 410 | // [#238]: Also, regular getters have precedence over "is-getters" |
| 411 | if (name.startsWith("get") && name.length() > 3) { |
| 412 | // should we check capitalization? |
| 413 | return 1; |
| 414 | } |
| 415 | if (name.startsWith("is") && name.length() > 2) { |
| 416 | return 2; |
| 417 | } |
| 418 | return 3; |
| 419 | } |
| 420 | |
| 421 | protected int _setterPriority(AnnotatedMethod m) |
| 422 | { |
| 423 | final String name = m.getName(); |
| 424 | if (name.startsWith("set") && name.length() > 3) { |
| 425 | // should we check capitalization? |
| 426 | return 1; |
| 427 | } |
| 428 | return 2; |
| 429 | } |
| 430 | |
Tatu | 9550e09 | 2012-02-03 17:20:13 -0800 | [diff] [blame] | 431 | /* |
Tatu Saloranta | f412e85 | 2012-09-11 22:05:51 -0700 | [diff] [blame] | 432 | /********************************************************** |
Tatu | 9550e09 | 2012-02-03 17:20:13 -0800 | [diff] [blame] | 433 | /* Implementations of refinement accessors |
Tatu Saloranta | f412e85 | 2012-09-11 22:05:51 -0700 | [diff] [blame] | 434 | /********************************************************** |
Tatu Saloranta | e40a769 | 2012-01-25 17:36:36 -0800 | [diff] [blame] | 435 | */ |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 436 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 437 | @Override |
| 438 | public Class<?>[] findViews() { |
Tatu Saloranta | f412e85 | 2012-09-11 22:05:51 -0700 | [diff] [blame] | 439 | return fromMemberAnnotations(new WithMember<Class<?>[]>() { |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 440 | @Override |
Tatu Saloranta | f1c79d4 | 2012-04-17 07:57:41 -0700 | [diff] [blame] | 441 | public Class<?>[] withMember(AnnotatedMember member) { |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 442 | return _annotationIntrospector.findViews(member); |
Tatu Saloranta | 19ab580 | 2012-01-26 22:26:18 -0800 | [diff] [blame] | 443 | } |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 444 | }); |
| 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public AnnotationIntrospector.ReferenceProperty findReferenceType() { |
Tatu Saloranta | f412e85 | 2012-09-11 22:05:51 -0700 | [diff] [blame] | 449 | return fromMemberAnnotations(new WithMember<AnnotationIntrospector.ReferenceProperty>() { |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 450 | @Override |
Tatu Saloranta | f1c79d4 | 2012-04-17 07:57:41 -0700 | [diff] [blame] | 451 | public AnnotationIntrospector.ReferenceProperty withMember(AnnotatedMember member) { |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 452 | return _annotationIntrospector.findReferenceType(member); |
Tatu Saloranta | 19ab580 | 2012-01-26 22:26:18 -0800 | [diff] [blame] | 453 | } |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 454 | }); |
Tatu Saloranta | e40a769 | 2012-01-25 17:36:36 -0800 | [diff] [blame] | 455 | } |
Tatu | 9550e09 | 2012-02-03 17:20:13 -0800 | [diff] [blame] | 456 | |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 457 | @Override |
Tatu | 9550e09 | 2012-02-03 17:20:13 -0800 | [diff] [blame] | 458 | public boolean isTypeId() { |
Tatu Saloranta | f412e85 | 2012-09-11 22:05:51 -0700 | [diff] [blame] | 459 | Boolean b = fromMemberAnnotations(new WithMember<Boolean>() { |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 460 | @Override |
Tatu Saloranta | f1c79d4 | 2012-04-17 07:57:41 -0700 | [diff] [blame] | 461 | public Boolean withMember(AnnotatedMember member) { |
Tatu | 9550e09 | 2012-02-03 17:20:13 -0800 | [diff] [blame] | 462 | return _annotationIntrospector.isTypeId(member); |
| 463 | } |
| 464 | }); |
| 465 | return (b != null) && b.booleanValue(); |
| 466 | } |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 467 | |
| 468 | @Override |
Tatu Saloranta | 8bdd49b | 2013-07-26 16:18:32 -0700 | [diff] [blame] | 469 | public PropertyMetadata getMetadata() { |
Tatu Saloranta | 105c58a | 2013-07-26 16:33:43 -0700 | [diff] [blame] | 470 | final Boolean b = _findRequired(); |
| 471 | final String desc = _findDescription(); |
Tatu Saloranta | ab53ab4 | 2014-04-12 12:57:43 -0700 | [diff] [blame] | 472 | final Integer idx = _findIndex(); |
Tatu Saloranta | 2b917fc | 2014-12-23 21:45:43 -0800 | [diff] [blame] | 473 | final String def = _findDefaultValue(); |
| 474 | if (b == null && idx == null && def == null) { |
Tatu Saloranta | 105c58a | 2013-07-26 16:33:43 -0700 | [diff] [blame] | 475 | return (desc == null) ? PropertyMetadata.STD_REQUIRED_OR_OPTIONAL |
| 476 | : PropertyMetadata.STD_REQUIRED_OR_OPTIONAL.withDescription(desc); |
| 477 | } |
Tatu Saloranta | 2b917fc | 2014-12-23 21:45:43 -0800 | [diff] [blame] | 478 | return PropertyMetadata.construct(b.booleanValue(), desc, idx, def); |
Tatu Saloranta | 8bdd49b | 2013-07-26 16:18:32 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Tatu Saloranta | 105c58a | 2013-07-26 16:33:43 -0700 | [diff] [blame] | 481 | protected Boolean _findRequired() { |
Tatu Saloranta | f412e85 | 2012-09-11 22:05:51 -0700 | [diff] [blame] | 482 | Boolean b = fromMemberAnnotations(new WithMember<Boolean>() { |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 483 | @Override |
Tatu Saloranta | f1c79d4 | 2012-04-17 07:57:41 -0700 | [diff] [blame] | 484 | public Boolean withMember(AnnotatedMember member) { |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 485 | return _annotationIntrospector.hasRequiredMarker(member); |
| 486 | } |
| 487 | }); |
Tatu Saloranta | 105c58a | 2013-07-26 16:33:43 -0700 | [diff] [blame] | 488 | return b; |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 489 | } |
Tatu Saloranta | 8bdd49b | 2013-07-26 16:18:32 -0700 | [diff] [blame] | 490 | |
| 491 | protected String _findDescription() { |
| 492 | return fromMemberAnnotations(new WithMember<String>() { |
| 493 | @Override |
| 494 | public String withMember(AnnotatedMember member) { |
| 495 | return _annotationIntrospector.findPropertyDescription(member); |
| 496 | } |
| 497 | }); |
| 498 | } |
Tatu Saloranta | ab53ab4 | 2014-04-12 12:57:43 -0700 | [diff] [blame] | 499 | |
| 500 | protected Integer _findIndex() { |
| 501 | return fromMemberAnnotations(new WithMember<Integer>() { |
| 502 | @Override |
| 503 | public Integer withMember(AnnotatedMember member) { |
| 504 | return _annotationIntrospector.findPropertyIndex(member); |
| 505 | } |
| 506 | }); |
| 507 | } |
Tatu Saloranta | 2b917fc | 2014-12-23 21:45:43 -0800 | [diff] [blame] | 508 | |
| 509 | protected String _findDefaultValue() { |
| 510 | return fromMemberAnnotations(new WithMember<String>() { |
| 511 | @Override |
| 512 | public String withMember(AnnotatedMember member) { |
| 513 | return _annotationIntrospector.findPropertyDefaultValue(member); |
| 514 | } |
| 515 | }); |
| 516 | } |
Tatu Saloranta | 8bdd49b | 2013-07-26 16:18:32 -0700 | [diff] [blame] | 517 | |
Tatu Saloranta | 2fd4ffd | 2012-02-17 22:40:13 -0800 | [diff] [blame] | 518 | @Override |
Tatu Saloranta | d453182 | 2012-02-06 22:44:40 -0800 | [diff] [blame] | 519 | public ObjectIdInfo findObjectIdInfo() { |
Tatu Saloranta | f412e85 | 2012-09-11 22:05:51 -0700 | [diff] [blame] | 520 | return fromMemberAnnotations(new WithMember<ObjectIdInfo>() { |
Tatu Saloranta | 9439a31 | 2013-03-02 13:13:09 -0800 | [diff] [blame] | 521 | @Override |
Tatu Saloranta | f1c79d4 | 2012-04-17 07:57:41 -0700 | [diff] [blame] | 522 | public ObjectIdInfo withMember(AnnotatedMember member) { |
Tatu Saloranta | 88040e2 | 2012-08-24 13:33:00 -0700 | [diff] [blame] | 523 | ObjectIdInfo info = _annotationIntrospector.findObjectIdInfo(member); |
| 524 | if (info != null) { |
| 525 | info = _annotationIntrospector.findObjectReferenceInfo(member, info); |
| 526 | } |
| 527 | return info; |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 528 | } |
| 529 | }); |
Tatu Saloranta | effb8fd | 2012-02-04 19:20:10 -0800 | [diff] [blame] | 530 | } |
Tatu Saloranta | 80674fe | 2014-12-03 22:17:21 -0800 | [diff] [blame] | 531 | |
| 532 | @Override |
| 533 | public JsonInclude.Include findInclusion() { |
| 534 | if (_annotationIntrospector == null) { |
| 535 | return null; |
| 536 | } |
| 537 | AnnotatedMember am = getAccessor(); |
| 538 | return _annotationIntrospector.findSerializationInclusion(am, null); |
| 539 | } |
| 540 | |
Cowtowncoder | 04413c7 | 2015-06-02 17:46:11 -0700 | [diff] [blame] | 541 | public JsonProperty.Access findAccess() { |
| 542 | return fromMemberAnnotationsExcept(new WithMember<JsonProperty.Access>() { |
| 543 | @Override |
| 544 | public JsonProperty.Access withMember(AnnotatedMember member) { |
| 545 | return _annotationIntrospector.findPropertyAccess(member); |
| 546 | } |
| 547 | }, JsonProperty.Access.AUTO); |
| 548 | } |
| 549 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 550 | /* |
| 551 | /********************************************************** |
| 552 | /* Data aggregation |
| 553 | /********************************************************** |
| 554 | */ |
| 555 | |
Tatu Saloranta | 26a052b | 2014-03-30 20:48:56 -0700 | [diff] [blame] | 556 | public void addField(AnnotatedField a, PropertyName name, boolean explName, boolean visible, boolean ignored) { |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 557 | _fields = new Linked<AnnotatedField>(a, _fields, name, explName, visible, ignored); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 558 | } |
| 559 | |
Tatu Saloranta | 26a052b | 2014-03-30 20:48:56 -0700 | [diff] [blame] | 560 | public void addCtor(AnnotatedParameter a, PropertyName name, boolean explName, boolean visible, boolean ignored) { |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 561 | _ctorParameters = new Linked<AnnotatedParameter>(a, _ctorParameters, name, explName, visible, ignored); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Tatu Saloranta | 26a052b | 2014-03-30 20:48:56 -0700 | [diff] [blame] | 564 | public void addGetter(AnnotatedMethod a, PropertyName name, boolean explName, boolean visible, boolean ignored) { |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 565 | _getters = new Linked<AnnotatedMethod>(a, _getters, name, explName, visible, ignored); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Tatu Saloranta | 26a052b | 2014-03-30 20:48:56 -0700 | [diff] [blame] | 568 | public void addSetter(AnnotatedMethod a, PropertyName name, boolean explName, boolean visible, boolean ignored) { |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 569 | _setters = new Linked<AnnotatedMethod>(a, _setters, name, explName, visible, ignored); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /** |
| 573 | * Method for adding all property members from specified collector into |
| 574 | * this collector. |
| 575 | */ |
| 576 | public void addAll(POJOPropertyBuilder src) |
| 577 | { |
| 578 | _fields = merge(_fields, src._fields); |
| 579 | _ctorParameters = merge(_ctorParameters, src._ctorParameters); |
| 580 | _getters= merge(_getters, src._getters); |
| 581 | _setters = merge(_setters, src._setters); |
| 582 | } |
| 583 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 584 | private static <T> Linked<T> merge(Linked<T> chain1, Linked<T> chain2) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 585 | { |
| 586 | if (chain1 == null) { |
| 587 | return chain2; |
| 588 | } |
| 589 | if (chain2 == null) { |
| 590 | return chain1; |
| 591 | } |
| 592 | return chain1.append(chain2); |
| 593 | } |
Tatu Saloranta | a00cdd8 | 2014-03-30 19:22:49 -0700 | [diff] [blame] | 594 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 595 | /* |
| 596 | /********************************************************** |
| 597 | /* Modifications |
| 598 | /********************************************************** |
| 599 | */ |
| 600 | |
| 601 | /** |
| 602 | * Method called to remove all entries that are marked as |
| 603 | * ignored. |
| 604 | */ |
| 605 | public void removeIgnored() |
| 606 | { |
| 607 | _fields = _removeIgnored(_fields); |
| 608 | _getters = _removeIgnored(_getters); |
| 609 | _setters = _removeIgnored(_setters); |
| 610 | _ctorParameters = _removeIgnored(_ctorParameters); |
| 611 | } |
| 612 | |
Tatu Saloranta | fbb1c3b | 2015-06-07 22:50:50 -0700 | [diff] [blame] | 613 | /** |
| 614 | * @param inferMutators Whether mutators can be "pulled in" by visible |
| 615 | * accessors or not. |
| 616 | */ |
| 617 | public void removeNonVisible(boolean inferMutators) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 618 | { |
Tatu Saloranta | fbb1c3b | 2015-06-07 22:50:50 -0700 | [diff] [blame] | 619 | /* 07-Jun-2015, tatu: With 2.6, we will allow optional definition |
| 620 | * of explicit access type for property; if not "AUTO", it will |
| 621 | * dictate how visibility checks are applied. |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 622 | */ |
Tatu Saloranta | fbb1c3b | 2015-06-07 22:50:50 -0700 | [diff] [blame] | 623 | JsonProperty.Access acc = findAccess(); |
| 624 | if (acc == null) { |
| 625 | acc = JsonProperty.Access.AUTO; |
| 626 | } |
| 627 | switch (acc) { |
| 628 | case READ_ONLY: |
| 629 | // Remove setters, creators for sure, but fields too if deserializing |
| 630 | _setters = null; |
| 631 | _ctorParameters = null; |
| 632 | if (!_forSerialization) { |
| 633 | _fields = null; |
| 634 | } |
| 635 | break; |
| 636 | case READ_WRITE: |
| 637 | // no trimming whatsoever? |
| 638 | break; |
| 639 | case WRITE_ONLY: |
| 640 | // remove getters, definitely, but also fields if serializing |
| 641 | _getters = null; |
| 642 | if (_forSerialization) { |
| 643 | _fields = null; |
| 644 | } |
| 645 | break; |
| 646 | default: |
| 647 | case AUTO: // the default case: base it imply on visibility |
| 648 | _getters = _removeNonVisible(_getters); |
| 649 | _ctorParameters = _removeNonVisible(_ctorParameters); |
| 650 | |
| 651 | if (!inferMutators || (_getters == null)) { |
| 652 | _fields = _removeNonVisible(_fields); |
| 653 | _setters = _removeNonVisible(_setters); |
| 654 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
| 658 | /** |
Tatu Saloranta | a56270a | 2014-11-16 23:05:18 -0800 | [diff] [blame] | 659 | * Mutator that will simply drop any constructor parameters property may have. |
| 660 | * |
| 661 | * @since 2.5 |
| 662 | */ |
| 663 | public void removeConstructors() { |
| 664 | _ctorParameters = null; |
| 665 | } |
| 666 | |
| 667 | /** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 668 | * Method called to trim unnecessary entries, such as implicit |
| 669 | * getter if there is an explict one available. This is important |
| 670 | * for later stages, to avoid unnecessary conflicts. |
| 671 | */ |
| 672 | public void trimByVisibility() |
| 673 | { |
| 674 | _fields = _trimByVisibility(_fields); |
| 675 | _getters = _trimByVisibility(_getters); |
| 676 | _setters = _trimByVisibility(_setters); |
| 677 | _ctorParameters = _trimByVisibility(_ctorParameters); |
| 678 | } |
| 679 | |
| 680 | @SuppressWarnings("unchecked") |
| 681 | public void mergeAnnotations(boolean forSerialization) |
| 682 | { |
| 683 | if (forSerialization) { |
| 684 | if (_getters != null) { |
| 685 | AnnotationMap ann = _mergeAnnotations(0, _getters, _fields, _ctorParameters, _setters); |
Christopher Currie | 6cf2bb5 | 2015-07-16 23:48:05 -0700 | [diff] [blame] | 686 | _getters = _applyAnnotations(_getters, ann); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 687 | } else if (_fields != null) { |
| 688 | AnnotationMap ann = _mergeAnnotations(0, _fields, _ctorParameters, _setters); |
Christopher Currie | 6cf2bb5 | 2015-07-16 23:48:05 -0700 | [diff] [blame] | 689 | _fields = _applyAnnotations(_fields, ann); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 690 | } |
Tatu Saloranta | b009194 | 2013-10-18 23:52:30 -0700 | [diff] [blame] | 691 | } else { // for deserialization |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 692 | if (_ctorParameters != null) { |
| 693 | AnnotationMap ann = _mergeAnnotations(0, _ctorParameters, _setters, _fields, _getters); |
Christopher Currie | 6cf2bb5 | 2015-07-16 23:48:05 -0700 | [diff] [blame] | 694 | _ctorParameters = _applyAnnotations(_ctorParameters, ann); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 695 | } else if (_setters != null) { |
| 696 | AnnotationMap ann = _mergeAnnotations(0, _setters, _fields, _getters); |
Christopher Currie | 6cf2bb5 | 2015-07-16 23:48:05 -0700 | [diff] [blame] | 697 | _setters = _applyAnnotations(_setters, ann); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 698 | } else if (_fields != null) { |
| 699 | AnnotationMap ann = _mergeAnnotations(0, _fields, _getters); |
Christopher Currie | 6cf2bb5 | 2015-07-16 23:48:05 -0700 | [diff] [blame] | 700 | _fields = _applyAnnotations(_fields, ann); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 705 | private AnnotationMap _mergeAnnotations(int index, Linked<? extends AnnotatedMember>... nodes) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 706 | { |
Christopher Currie | 6cf2bb5 | 2015-07-16 23:48:05 -0700 | [diff] [blame] | 707 | AnnotationMap ann = _getAllAnnotations(nodes[index]); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 708 | ++index; |
| 709 | for (; index < nodes.length; ++index) { |
| 710 | if (nodes[index] != null) { |
| 711 | return AnnotationMap.merge(ann, _mergeAnnotations(index, nodes)); |
| 712 | } |
| 713 | } |
| 714 | return ann; |
| 715 | } |
Christopher Currie | 6cf2bb5 | 2015-07-16 23:48:05 -0700 | [diff] [blame] | 716 | |
Cowtowncoder | a23ff9f | 2015-07-17 11:53:06 -0700 | [diff] [blame^] | 717 | /** |
| 718 | * Replacement, as per [databind#868], of simple access to annotations, which |
| 719 | * does "deep merge" if an as necessary. |
| 720 | *<pre> |
| 721 | * nodes[index].value.getAllAnnotations() |
| 722 | *</pre> |
| 723 | * |
| 724 | * @since 2.6 |
| 725 | */ |
Christopher Currie | 6cf2bb5 | 2015-07-16 23:48:05 -0700 | [diff] [blame] | 726 | private <T extends AnnotatedMember> AnnotationMap _getAllAnnotations(Linked<T> node) { |
| 727 | AnnotationMap ann = node.value.getAllAnnotations(); |
| 728 | if (node.next != null) { |
| 729 | ann = AnnotationMap.merge(ann, _getAllAnnotations(node.next)); |
| 730 | } |
| 731 | return ann; |
| 732 | } |
| 733 | |
Cowtowncoder | a23ff9f | 2015-07-17 11:53:06 -0700 | [diff] [blame^] | 734 | /** |
| 735 | * Helper method to handle recursive merging of annotations within accessor class, |
| 736 | * to ensure no annotations are accidentally dropped within chain when non-visible |
| 737 | * and secondary accessors are pruned later on. |
| 738 | *<p> |
| 739 | * See [databind#868] for more information. |
| 740 | * |
| 741 | * @since 2.6 |
| 742 | */ |
Christopher Currie | 6cf2bb5 | 2015-07-16 23:48:05 -0700 | [diff] [blame] | 743 | private <T extends AnnotatedMember> Linked<T> _applyAnnotations(Linked<T> node, AnnotationMap ann) { |
Cowtowncoder | a23ff9f | 2015-07-17 11:53:06 -0700 | [diff] [blame^] | 744 | @SuppressWarnings("unchecked") |
Christopher Currie | 6cf2bb5 | 2015-07-16 23:48:05 -0700 | [diff] [blame] | 745 | T value = (T) node.value.withAnnotations(ann); |
| 746 | if (node.next != null) { |
| 747 | node = node.withNext(_applyAnnotations(node.next, ann)); |
| 748 | } |
| 749 | return node.withValue(value); |
| 750 | } |
| 751 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 752 | private <T> Linked<T> _removeIgnored(Linked<T> node) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 753 | { |
| 754 | if (node == null) { |
| 755 | return node; |
| 756 | } |
| 757 | return node.withoutIgnored(); |
| 758 | } |
| 759 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 760 | private <T> Linked<T> _removeNonVisible(Linked<T> node) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 761 | { |
| 762 | if (node == null) { |
| 763 | return node; |
| 764 | } |
| 765 | return node.withoutNonVisible(); |
| 766 | } |
| 767 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 768 | private <T> Linked<T> _trimByVisibility(Linked<T> node) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 769 | { |
| 770 | if (node == null) { |
| 771 | return node; |
| 772 | } |
| 773 | return node.trimByVisibility(); |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | /********************************************************** |
| 778 | /* Accessors for aggregate information |
| 779 | /********************************************************** |
| 780 | */ |
| 781 | |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 782 | private <T> boolean _anyExplicits(Linked<T> n) |
| 783 | { |
| 784 | for (; n != null; n = n.next) { |
Tatu Saloranta | 26a052b | 2014-03-30 20:48:56 -0700 | [diff] [blame] | 785 | if (n.name != null && n.name.hasSimpleName()) { |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 786 | return true; |
| 787 | } |
| 788 | } |
| 789 | return false; |
| 790 | } |
| 791 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 792 | private <T> boolean _anyExplicitNames(Linked<T> n) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 793 | { |
| 794 | for (; n != null; n = n.next) { |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 795 | if (n.name != null && n.isNameExplicit) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 796 | return true; |
| 797 | } |
| 798 | } |
| 799 | return false; |
| 800 | } |
| 801 | |
| 802 | public boolean anyVisible() { |
| 803 | return _anyVisible(_fields) |
| 804 | || _anyVisible(_getters) |
| 805 | || _anyVisible(_setters) |
| 806 | || _anyVisible(_ctorParameters) |
| 807 | ; |
| 808 | } |
| 809 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 810 | private <T> boolean _anyVisible(Linked<T> n) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 811 | { |
| 812 | for (; n != null; n = n.next) { |
| 813 | if (n.isVisible) { |
| 814 | return true; |
| 815 | } |
| 816 | } |
| 817 | return false; |
| 818 | } |
| 819 | |
| 820 | public boolean anyIgnorals() { |
| 821 | return _anyIgnorals(_fields) |
| 822 | || _anyIgnorals(_getters) |
| 823 | || _anyIgnorals(_setters) |
| 824 | || _anyIgnorals(_ctorParameters) |
| 825 | ; |
| 826 | } |
| 827 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 828 | private <T> boolean _anyIgnorals(Linked<T> n) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 829 | { |
| 830 | for (; n != null; n = n.next) { |
| 831 | if (n.isMarkedIgnored) { |
| 832 | return true; |
| 833 | } |
| 834 | } |
| 835 | return false; |
| 836 | } |
| 837 | |
| 838 | /** |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 839 | * Method called to find out set of explicit names for accessors |
| 840 | * bound together due to implicit name. |
| 841 | * |
| 842 | * @since 2.4 |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 843 | */ |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 844 | public Set<PropertyName> findExplicitNames() |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 845 | { |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 846 | Set<PropertyName> renamed = null; |
| 847 | renamed = _findExplicitNames(_fields, renamed); |
| 848 | renamed = _findExplicitNames(_getters, renamed); |
| 849 | renamed = _findExplicitNames(_setters, renamed); |
| 850 | renamed = _findExplicitNames(_ctorParameters, renamed); |
| 851 | if (renamed == null) { |
| 852 | return Collections.emptySet(); |
| 853 | } |
| 854 | return renamed; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 855 | } |
| 856 | |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 857 | /** |
| 858 | * Method called when a previous call to {@link #findExplicitNames} found |
| 859 | * multiple distinct explicit names, and the property this builder represents |
| 860 | * basically needs to be broken apart and replaced by a set of more than |
| 861 | * one properties. |
| 862 | * |
| 863 | * @since 2.4 |
| 864 | */ |
| 865 | public Collection<POJOPropertyBuilder> explode(Collection<PropertyName> newNames) |
| 866 | { |
| 867 | HashMap<PropertyName,POJOPropertyBuilder> props = new HashMap<PropertyName,POJOPropertyBuilder>(); |
| 868 | _explode(newNames, props, _fields); |
| 869 | _explode(newNames, props, _getters); |
| 870 | _explode(newNames, props, _setters); |
| 871 | _explode(newNames, props, _ctorParameters); |
| 872 | return props.values(); |
| 873 | } |
| 874 | |
| 875 | @SuppressWarnings("unchecked") |
| 876 | private void _explode(Collection<PropertyName> newNames, |
| 877 | Map<PropertyName,POJOPropertyBuilder> props, |
| 878 | Linked<?> accessors) |
| 879 | { |
| 880 | final Linked<?> firstAcc = accessors; // clumsy, part 1 |
| 881 | for (Linked<?> node = accessors; node != null; node = node.next) { |
| 882 | PropertyName name = node.name; |
| 883 | if (!node.isNameExplicit || name == null) { // no explicit name -- problem! |
Cowtowncoder | f95a927 | 2014-09-16 16:40:43 -0700 | [diff] [blame] | 884 | // [Issue#541] ... but only as long as it's visible |
| 885 | if (!node.isVisible) { |
| 886 | continue; |
| 887 | } |
| 888 | |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 889 | throw new IllegalStateException("Conflicting/ambiguous property name definitions (implicit name '" |
| 890 | +_name+"'): found multiple explicit names: " |
| 891 | +newNames+", but also implicit accessor: "+node); |
| 892 | } |
| 893 | POJOPropertyBuilder prop = props.get(name); |
| 894 | if (prop == null) { |
| 895 | prop = new POJOPropertyBuilder(_internalName, name, _annotationIntrospector, _forSerialization); |
| 896 | props.put(name, prop); |
| 897 | } |
| 898 | // ultra-clumsy, part 2 -- lambdas would be nice here |
| 899 | if (firstAcc == _fields) { |
| 900 | Linked<AnnotatedField> n2 = (Linked<AnnotatedField>) node; |
| 901 | prop._fields = n2.withNext(prop._fields); |
| 902 | } else if (firstAcc == _getters) { |
| 903 | Linked<AnnotatedMethod> n2 = (Linked<AnnotatedMethod>) node; |
| 904 | prop._getters = n2.withNext(prop._getters); |
| 905 | } else if (firstAcc == _setters) { |
| 906 | Linked<AnnotatedMethod> n2 = (Linked<AnnotatedMethod>) node; |
| 907 | prop._setters = n2.withNext(prop._setters); |
| 908 | } else if (firstAcc == _ctorParameters) { |
| 909 | Linked<AnnotatedParameter> n2 = (Linked<AnnotatedParameter>) node; |
| 910 | prop._ctorParameters = n2.withNext(prop._ctorParameters); |
| 911 | } else { |
| 912 | throw new IllegalStateException("Internal error: mismatched accessors, property: "+this); |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | private Set<PropertyName> _findExplicitNames(Linked<? extends AnnotatedMember> node, |
| 918 | Set<PropertyName> renamed) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 919 | { |
| 920 | for (; node != null; node = node.next) { |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 921 | /* 30-Mar-2014, tatu: Second check should not be needed, but seems like |
Tatu Saloranta | 2bd58f5 | 2014-03-30 21:11:06 -0700 | [diff] [blame] | 922 | * removing it can cause nasty exceptions with certain version |
| 923 | * combinations (2.4 databind, an older module). |
| 924 | * So leaving it in for now until this is resolved |
| 925 | * (or version beyond 2.4) |
| 926 | */ |
| 927 | if (!node.isNameExplicit || node.name == null) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 928 | continue; |
| 929 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 930 | if (renamed == null) { |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 931 | renamed = new HashSet<PropertyName>(); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 932 | } |
Tatu Saloranta | 1962410 | 2014-04-14 20:07:31 -0700 | [diff] [blame] | 933 | renamed.add(node.name); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 934 | } |
| 935 | return renamed; |
| 936 | } |
Tatu Saloranta | a1e496f | 2012-09-11 20:25:12 -0700 | [diff] [blame] | 937 | |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 938 | // For trouble-shooting |
| 939 | @Override |
| 940 | public String toString() |
| 941 | { |
| 942 | StringBuilder sb = new StringBuilder(); |
| 943 | sb.append("[Property '").append(_name) |
| 944 | .append("'; ctors: ").append(_ctorParameters) |
| 945 | .append(", field(s): ").append(_fields) |
| 946 | .append(", getter(s): ").append(_getters) |
| 947 | .append(", setter(s): ").append(_setters) |
| 948 | ; |
| 949 | sb.append("]"); |
| 950 | return sb.toString(); |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | /********************************************************** |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 955 | /* Helper methods |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 956 | /********************************************************** |
| 957 | */ |
| 958 | |
| 959 | /** |
Tatu Saloranta | f412e85 | 2012-09-11 22:05:51 -0700 | [diff] [blame] | 960 | * Helper method used for finding annotation values, from accessors |
| 961 | * relevant to current usage (deserialization, serialization) |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 962 | */ |
Tatu Saloranta | f412e85 | 2012-09-11 22:05:51 -0700 | [diff] [blame] | 963 | protected <T> T fromMemberAnnotations(WithMember<T> func) |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 964 | { |
| 965 | T result = null; |
| 966 | if (_annotationIntrospector != null) { |
| 967 | if (_forSerialization) { |
| 968 | if (_getters != null) { |
| 969 | result = func.withMember(_getters.value); |
| 970 | } |
| 971 | } else { |
| 972 | if (_ctorParameters != null) { |
| 973 | result = func.withMember(_ctorParameters.value); |
| 974 | } |
| 975 | if (result == null && _setters != null) { |
| 976 | result = func.withMember(_setters.value); |
| 977 | } |
| 978 | } |
| 979 | if (result == null && _fields != null) { |
| 980 | result = func.withMember(_fields.value); |
| 981 | } |
| 982 | } |
| 983 | return result; |
| 984 | } |
Cowtowncoder | 04413c7 | 2015-06-02 17:46:11 -0700 | [diff] [blame] | 985 | |
| 986 | protected <T> T fromMemberAnnotationsExcept(WithMember<T> func, T defaultValue) |
| 987 | { |
Tatu Saloranta | fbb1c3b | 2015-06-07 22:50:50 -0700 | [diff] [blame] | 988 | if (_annotationIntrospector == null) { |
| 989 | return null; |
| 990 | } |
| 991 | |
| 992 | // NOTE: here we must ask ALL accessors, but the order varies between |
| 993 | // serialization, deserialization |
| 994 | if (_forSerialization) { |
| 995 | if (_getters != null) { |
| 996 | T result = func.withMember(_getters.value); |
| 997 | if ((result != null) && (result != defaultValue)) { |
| 998 | return result; |
Cowtowncoder | 04413c7 | 2015-06-02 17:46:11 -0700 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | if (_fields != null) { |
| 1002 | T result = func.withMember(_fields.value); |
| 1003 | if ((result != null) && (result != defaultValue)) { |
| 1004 | return result; |
| 1005 | } |
| 1006 | } |
Tatu Saloranta | fbb1c3b | 2015-06-07 22:50:50 -0700 | [diff] [blame] | 1007 | if (_ctorParameters != null) { |
| 1008 | T result = func.withMember(_ctorParameters.value); |
| 1009 | if ((result != null) && (result != defaultValue)) { |
| 1010 | return result; |
| 1011 | } |
| 1012 | } |
| 1013 | if (_setters != null) { |
| 1014 | T result = func.withMember(_setters.value); |
| 1015 | if ((result != null) && (result != defaultValue)) { |
| 1016 | return result; |
| 1017 | } |
| 1018 | } |
| 1019 | return null; |
| 1020 | } |
| 1021 | if (_ctorParameters != null) { |
| 1022 | T result = func.withMember(_ctorParameters.value); |
| 1023 | if ((result != null) && (result != defaultValue)) { |
| 1024 | return result; |
| 1025 | } |
| 1026 | } |
| 1027 | if (_setters != null) { |
| 1028 | T result = func.withMember(_setters.value); |
| 1029 | if ((result != null) && (result != defaultValue)) { |
| 1030 | return result; |
| 1031 | } |
| 1032 | } |
| 1033 | if (_fields != null) { |
| 1034 | T result = func.withMember(_fields.value); |
| 1035 | if ((result != null) && (result != defaultValue)) { |
| 1036 | return result; |
| 1037 | } |
| 1038 | } |
| 1039 | if (_getters != null) { |
| 1040 | T result = func.withMember(_getters.value); |
| 1041 | if ((result != null) && (result != defaultValue)) { |
| 1042 | return result; |
| 1043 | } |
Cowtowncoder | 04413c7 | 2015-06-02 17:46:11 -0700 | [diff] [blame] | 1044 | } |
| 1045 | return null; |
| 1046 | } |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 1047 | |
| 1048 | /* |
| 1049 | /********************************************************** |
| 1050 | /* Helper classes |
| 1051 | /********************************************************** |
| 1052 | */ |
| 1053 | |
Tatu Saloranta | 26a052b | 2014-03-30 20:48:56 -0700 | [diff] [blame] | 1054 | private interface WithMember<T> { |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 1055 | public T withMember(AnnotatedMember member); |
| 1056 | } |
Cowtowncoder | c717c2f | 2014-09-25 16:52:30 -0700 | [diff] [blame] | 1057 | |
| 1058 | /** |
| 1059 | * @since 2.5 |
| 1060 | */ |
| 1061 | protected static class MemberIterator<T extends AnnotatedMember> |
| 1062 | implements Iterator<T> |
| 1063 | { |
| 1064 | private Linked<T> next; |
| 1065 | |
| 1066 | public MemberIterator(Linked<T> first) { |
| 1067 | next = first; |
| 1068 | } |
| 1069 | |
| 1070 | @Override |
| 1071 | public boolean hasNext() { |
| 1072 | return (next != null); |
| 1073 | } |
| 1074 | |
| 1075 | @Override |
| 1076 | public T next() { |
| 1077 | if (next == null) throw new NoSuchElementException(); |
| 1078 | T result = next.value; |
| 1079 | next = next.next; |
| 1080 | return result; |
| 1081 | } |
| 1082 | |
| 1083 | @Override |
| 1084 | public void remove() { |
| 1085 | throw new UnsupportedOperationException(); |
| 1086 | } |
| 1087 | |
| 1088 | } |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 1089 | |
| 1090 | /** |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1091 | * Node used for creating simple linked lists to efficiently store small sets |
| 1092 | * of things. |
| 1093 | */ |
Christopher Currie | b6f3f6a | 2015-07-16 23:17:16 -0700 | [diff] [blame] | 1094 | protected final static class Linked<T> |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1095 | { |
| 1096 | public final T value; |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 1097 | public final Linked<T> next; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1098 | |
Tatu Saloranta | 26a052b | 2014-03-30 20:48:56 -0700 | [diff] [blame] | 1099 | public final PropertyName name; |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 1100 | public final boolean isNameExplicit; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1101 | public final boolean isVisible; |
| 1102 | public final boolean isMarkedIgnored; |
| 1103 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 1104 | public Linked(T v, Linked<T> n, |
Tatu Saloranta | 26a052b | 2014-03-30 20:48:56 -0700 | [diff] [blame] | 1105 | PropertyName name, boolean explName, boolean visible, boolean ignored) |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1106 | { |
| 1107 | value = v; |
| 1108 | next = n; |
| 1109 | // ensure that we'll never have missing names |
Tatu | e400d16 | 2014-04-03 10:27:43 -0700 | [diff] [blame] | 1110 | this.name = (name == null || name.isEmpty()) ? null : name; |
Tatu Saloranta | a769351 | 2014-03-30 21:05:16 -0700 | [diff] [blame] | 1111 | |
Tatu | e400d16 | 2014-04-03 10:27:43 -0700 | [diff] [blame] | 1112 | if (explName) { |
| 1113 | if (this.name == null) { // sanity check to catch internal problems |
| 1114 | throw new IllegalArgumentException("Can not pass true for 'explName' if name is null/empty"); |
| 1115 | } |
| 1116 | // 03-Apr-2014, tatu: But how about name-space only override? |
| 1117 | // Probably should not be explicit? Or, need to merge somehow? |
| 1118 | if (!name.hasSimpleName()) { |
| 1119 | explName = false; |
| 1120 | } |
Tatu Saloranta | a769351 | 2014-03-30 21:05:16 -0700 | [diff] [blame] | 1121 | } |
Tatu Saloranta | a00cdd8 | 2014-03-30 19:22:49 -0700 | [diff] [blame] | 1122 | |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 1123 | isNameExplicit = explName; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1124 | isVisible = visible; |
| 1125 | isMarkedIgnored = ignored; |
| 1126 | } |
| 1127 | |
Tatu Saloranta | 2a41afc | 2014-05-30 22:58:41 -0700 | [diff] [blame] | 1128 | public Linked<T> withoutNext() { |
| 1129 | if (next == null) { |
| 1130 | return this; |
| 1131 | } |
| 1132 | return new Linked<T>(value, null, name, isNameExplicit, isVisible, isMarkedIgnored); |
| 1133 | } |
| 1134 | |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 1135 | public Linked<T> withValue(T newValue) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1136 | if (newValue == value) { |
| 1137 | return this; |
| 1138 | } |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 1139 | return new Linked<T>(newValue, next, name, isNameExplicit, isVisible, isMarkedIgnored); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 1142 | public Linked<T> withNext(Linked<T> newNext) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1143 | if (newNext == next) { |
| 1144 | return this; |
| 1145 | } |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 1146 | return new Linked<T>(value, newNext, name, isNameExplicit, isVisible, isMarkedIgnored); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Tatu Saloranta | 26a052b | 2014-03-30 20:48:56 -0700 | [diff] [blame] | 1149 | public Linked<T> withoutIgnored() { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1150 | if (isMarkedIgnored) { |
| 1151 | return (next == null) ? null : next.withoutIgnored(); |
| 1152 | } |
| 1153 | if (next != null) { |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 1154 | Linked<T> newNext = next.withoutIgnored(); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1155 | if (newNext != next) { |
| 1156 | return withNext(newNext); |
| 1157 | } |
| 1158 | } |
| 1159 | return this; |
| 1160 | } |
| 1161 | |
Tatu Saloranta | 26a052b | 2014-03-30 20:48:56 -0700 | [diff] [blame] | 1162 | public Linked<T> withoutNonVisible() { |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 1163 | Linked<T> newNext = (next == null) ? null : next.withoutNonVisible(); |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1164 | return isVisible ? withNext(newNext) : newNext; |
| 1165 | } |
| 1166 | |
| 1167 | /** |
| 1168 | * Method called to append given node(s) at the end of this |
| 1169 | * node chain. |
| 1170 | */ |
Cowtowncoder | 72de08e | 2014-11-25 14:58:53 -0800 | [diff] [blame] | 1171 | protected Linked<T> append(Linked<T> appendable) { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1172 | if (next == null) { |
| 1173 | return withNext(appendable); |
| 1174 | } |
| 1175 | return withNext(next.append(appendable)); |
| 1176 | } |
Tatu Saloranta | 26a052b | 2014-03-30 20:48:56 -0700 | [diff] [blame] | 1177 | |
| 1178 | public Linked<T> trimByVisibility() { |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1179 | if (next == null) { |
| 1180 | return this; |
| 1181 | } |
Tatu Saloranta | 65c410c | 2012-01-27 20:44:45 -0800 | [diff] [blame] | 1182 | Linked<T> newNext = next.trimByVisibility(); |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 1183 | if (name != null) { // this already has highest; how about next one? |
| 1184 | if (newNext.name == null) { // next one not, drop it |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1185 | return withNext(null); |
| 1186 | } |
| 1187 | // both have it, keep |
| 1188 | return withNext(newNext); |
| 1189 | } |
Tatu | 365c29b | 2014-03-28 15:51:00 -0700 | [diff] [blame] | 1190 | if (newNext.name != null) { // next one has higher, return it... |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1191 | return newNext; |
| 1192 | } |
| 1193 | // neither has explicit name; how about visibility? |
| 1194 | if (isVisible == newNext.isVisible) { // same; keep both in current order |
| 1195 | return withNext(newNext); |
| 1196 | } |
| 1197 | return isVisible ? withNext(null) : newNext; |
| 1198 | } |
| 1199 | |
| 1200 | @Override |
| 1201 | public String toString() { |
Tatu Saloranta | a00cdd8 | 2014-03-30 19:22:49 -0700 | [diff] [blame] | 1202 | String msg = value.toString()+"[visible="+isVisible+",ignore="+isMarkedIgnored |
| 1203 | +",explicitName="+isNameExplicit+"]"; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1204 | if (next != null) { |
| 1205 | msg = msg + ", "+next.toString(); |
| 1206 | } |
| 1207 | return msg; |
| 1208 | } |
| 1209 | } |
| 1210 | } |