File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\xstream\ImmutableListConverter.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, Inc.
5 * Copyright (c) 2011, Richard Mortimer
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25 package hudson.util.xstream;
26
27 import com.google.common.collect.ImmutableList;
28 import com.thoughtworks.xstream.XStream;
29 import com.thoughtworks.xstream.converters.UnmarshallingContext;
30 import com.thoughtworks.xstream.converters.collections.CollectionConverter;
31 import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
32 import com.thoughtworks.xstream.converters.reflection.SerializableConverter;
33 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
34 import com.thoughtworks.xstream.mapper.CannotResolveClassException;
35 import com.thoughtworks.xstream.mapper.Mapper;
36
37 import hudson.util.RobustReflectionConverter;
38
39 import java.util.ArrayList;
40 import java.util.List;
41
42 /**
43 * {@link ImmutableList} should convert like a list, instead of via serialization.
44 *
45 * @author Kohsuke Kawaguchi
46 * @author Richard Mortimer
47 */
48 public class ImmutableListConverter extends CollectionConverter {
49 private final SerializableConverter sc;
50
51 public ImmutableListConverter(XStream xs) {
52 this(xs.getMapper(),xs.getReflectionProvider());
53 }
54
55 public ImmutableListConverter(Mapper mapper, ReflectionProvider reflectionProvider) {
56 super(mapper);
57 sc = new SerializableConverter(mapper,reflectionProvider);
58 }
59
60 @Override
61 public boolean canConvert(Class type) {
62 return ImmutableList.class.isAssignableFrom(type);
63 }
64
65 @Override
66 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
67 String resolvesTo = reader.getAttribute("resolves-to");
68 if ("com.google.common.collect.ImmutableList$SerializedForm".equals(resolvesTo)) {
69 // Skip into the elements element. This has the real children.
70 List items = new ArrayList();
71 if (reader.hasMoreChildren()) {
72 reader.moveDown();
73 // read the individual items from xml into a list
74 while (reader.hasMoreChildren()) {
75 reader.moveDown();
76 try {
77 Object item = readItem(reader, context, items);
78 items.add(item);
79 } catch (CannotResolveClassException e) {
80 RobustReflectionConverter.addErrorInContext(context, e);
81 } catch (LinkageError e) {
82 RobustReflectionConverter.addErrorInContext(context, e);
83 }
84 reader.moveUp();
85 }
86
87 // move back up past the elements element.
88 reader.moveUp();
89 }
90 return ImmutableList.copyOf(items);
91 } else {
92 return ImmutableList.copyOf((List)super.unmarshal(reader, context));
93 }
94 }
95
96 @Override
97 protected Object createCollection(Class type) {
98 return new ArrayList();
99 }
100 }
101