1
1
/*
2
- * Copyright (C) 2011, 2013, 2014, 2015, 2016 XStream Committers.
2
+ * Copyright (C) 2011, 2013, 2014, 2015, 2016, 2024 XStream Committers.
3
3
* All rights reserved.
4
4
*
5
5
* The software in this package is published under the terms of the BSD
6
6
* style license a copy of which has been included with this distribution in
7
7
* the LICENSE.txt file.
8
- *
8
+ *
9
9
* Created on 30. July 2011 by Joerg Schaible
10
10
*/
11
11
28
28
import com .thoughtworks .xstream .converters .UnmarshallingContext ;
29
29
import com .thoughtworks .xstream .converters .reflection .AbstractReflectionConverter .DuplicateFieldException ;
30
30
import com .thoughtworks .xstream .converters .reflection .ReflectionProvider ;
31
- import com .thoughtworks .xstream .core .util .FastField ;
32
31
import com .thoughtworks .xstream .core .util .HierarchicalStreams ;
32
+ import com .thoughtworks .xstream .core .util .MemberDictionary ;
33
33
import com .thoughtworks .xstream .core .util .Primitives ;
34
34
import com .thoughtworks .xstream .io .HierarchicalStreamReader ;
35
35
import com .thoughtworks .xstream .io .HierarchicalStreamWriter ;
45
45
* field name itself. Therefore it is possible to define an inherited field as value. It is also possible to provide no
46
46
* value field at all, so that all fields are written as attributes.
47
47
* </p>
48
- *
48
+ *
49
49
* @author Jörg Schaible
50
50
* @since 1.4
51
51
*/
@@ -59,10 +59,9 @@ public class ToAttributedValueConverter implements Converter {
59
59
private final Field valueField ;
60
60
61
61
/**
62
- * Creates a new ToAttributedValueConverter instance.
63
- *
64
- * All field elements will be attributes, the element itself will have no value.
65
- *
62
+ * Creates a new ToAttributedValueConverter instance. All field elements will be attributes, the element itself will
63
+ * have no value.
64
+ *
66
65
* @param type the type that is handled by this converter instance
67
66
* @param mapper the mapper in use
68
67
* @param reflectionProvider the reflection provider in use
@@ -77,7 +76,7 @@ public ToAttributedValueConverter(
77
76
78
77
/**
79
78
* Creates a new ToAttributedValueConverter instance.
80
- *
79
+ *
81
80
* @param type the type that is handled by this converter instance
82
81
* @param mapper the mapper in use
83
82
* @param reflectionProvider the reflection provider in use
@@ -92,7 +91,7 @@ public ToAttributedValueConverter(
92
91
93
92
/**
94
93
* Creates a new ToAttributedValueConverter instance.
95
- *
94
+ *
96
95
* @param type the type that is handled by this converter instance
97
96
* @param mapper the mapper in use
98
97
* @param reflectionProvider the reflection provider in use
@@ -140,33 +139,33 @@ public void marshal(final Object source, final HierarchicalStreamWriter writer,
140
139
final Class <?>[] definingType = new Class [1 ];
141
140
reflectionProvider .visitSerializableFields (source , new ReflectionProvider .Visitor () {
142
141
@ Override
143
- public void visit (final String fieldName , final Class <?> type , final Class <?> definedIn , final Object value ) {
142
+ public void visit (final String fieldName , final Class <?> type , final Class <?> definedIn ,
143
+ final Object value ) {
144
144
if (!mapper .shouldSerializeMember (definedIn , fieldName )) {
145
145
return ;
146
146
}
147
147
148
- final FastField field = new FastField (definedIn , fieldName );
149
148
final String alias = mapper .serializedMember (definedIn , fieldName );
150
149
if (!defaultFieldDefinition .containsKey (alias )) {
151
150
final Class <?> lookupType = sourceType ;
152
151
defaultFieldDefinition .put (alias , reflectionProvider .getField (lookupType , fieldName ));
153
- } else if (!fieldIsEqual (field )) {
152
+ } else if (!fieldIsEqual (definedIn , fieldName )) {
154
153
final ConversionException exception = new ConversionException (
155
154
"Cannot write attribute twice for object" );
156
155
exception .add ("alias" , alias );
157
156
exception .add ("type" , sourceType .getName ());
158
157
throw exception ;
159
158
}
160
159
161
- ConverterMatcher converter = Enum .class .isAssignableFrom (type ) ? ( ConverterMatcher ) enumMapper
162
- .getConverterFromItemType (null , type , null ) : ( ConverterMatcher ) mapper . getLocalConverter ( definedIn ,
163
- fieldName );
160
+ ConverterMatcher converter = Enum .class .isAssignableFrom (type )
161
+ ? ( ConverterMatcher ) enumMapper .getConverterFromItemType (null , type , null )
162
+ : ( ConverterMatcher ) mapper . getLocalConverter ( definedIn , fieldName );
164
163
if (converter == null ) {
165
164
converter = lookup .lookupConverterForType (type );
166
165
}
167
166
168
167
if (value != null ) {
169
- final boolean isValueField = valueField != null && fieldIsEqual (field );
168
+ final boolean isValueField = valueField != null && fieldIsEqual (definedIn , fieldName );
170
169
if (isValueField ) {
171
170
definingType [0 ] = definedIn ;
172
171
fieldType [0 ] = type ;
@@ -222,7 +221,7 @@ public Object unmarshal(final HierarchicalStreamReader reader, final Unmarshalli
222
221
final Object result = reflectionProvider .newInstance (context .getRequiredType ());
223
222
final Class <?> resultType = result .getClass ();
224
223
225
- final Set < FastField > seenFields = new HashSet <> ();
224
+ final MemberDictionary seenFields = new MemberDictionary ();
226
225
final Iterator <String > it = reader .getAttributeNames ();
227
226
228
227
final Set <String > systemAttributes = new HashSet <>();
@@ -244,9 +243,9 @@ public Object unmarshal(final HierarchicalStreamReader reader, final Unmarshalli
244
243
245
244
Class <?> type = field .getType ();
246
245
final Class <?> declaringClass = field .getDeclaringClass ();
247
- ConverterMatcher converter = Enum .class .isAssignableFrom (type ) ? ( ConverterMatcher ) enumMapper
248
- .getConverterFromItemType (null , type , null ) : ( ConverterMatcher ) mapper . getLocalConverter (
249
- declaringClass , fieldName );
246
+ ConverterMatcher converter = Enum .class .isAssignableFrom (type )
247
+ ? ( ConverterMatcher ) enumMapper .getConverterFromItemType (null , type , null )
248
+ : ( ConverterMatcher ) mapper . getLocalConverter ( declaringClass , fieldName );
250
249
if (converter == null ) {
251
250
converter = lookup .lookupConverterForType (type );
252
251
}
@@ -273,7 +272,7 @@ public Object unmarshal(final HierarchicalStreamReader reader, final Unmarshalli
273
272
}
274
273
275
274
reflectionProvider .writeField (result , fieldName , value , declaringClass );
276
- if (!seenFields .add (new FastField ( declaringClass , fieldName ) )) {
275
+ if (!seenFields .add (declaringClass , fieldName )) {
277
276
throw new DuplicateFieldException (fieldName + " [" + declaringClass .getName () + "]" );
278
277
}
279
278
}
@@ -317,15 +316,15 @@ public Object unmarshal(final HierarchicalStreamReader reader, final Unmarshalli
317
316
}
318
317
319
318
reflectionProvider .writeField (result , fieldName , value , classDefiningField );
320
- if (!seenFields .add (new FastField ( classDefiningField , fieldName ) )) {
319
+ if (!seenFields .add (classDefiningField , fieldName )) {
321
320
throw new DuplicateFieldException (fieldName + " [" + classDefiningField .getName () + "]" );
322
321
}
323
322
}
324
323
return result ;
325
324
}
326
325
327
- private boolean fieldIsEqual (final FastField field ) {
328
- return valueField .getName ().equals (field . getName () )
329
- && valueField .getDeclaringClass ().getName ().equals (field . getDeclaringClass ());
326
+ private boolean fieldIsEqual (final Class <?> definedIn , final String name ) {
327
+ return valueField .getName ().equals (name )
328
+ && valueField .getDeclaringClass ().getName ().equals (definedIn . getName ());
330
329
}
331
330
}
0 commit comments