1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.job.internal.xstream

File XStreamUtils.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

16
24
4
1
122
65
12
0.5
6
4
3

Classes

Class Line # Actions
XStreamUtils 42 24 0% 12 2
0.9545454495.5%
 

Contributing tests

This file is covered by 32 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.job.internal.xstream;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import javax.inject.Provider;
26   
27    import org.slf4j.Logger;
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.component.manager.ComponentManager;
30    import org.xwiki.job.annotation.Serializable;
31   
32    import com.thoughtworks.xstream.converters.MarshallingContext;
33    import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
34    import com.thoughtworks.xstream.mapper.Mapper;
35   
36    /**
37    * Various XStream related utilities.
38    *
39    * @version $Id: 5227fa72ccc59b0928305a7a64f6a129a91b3442 $
40    * @since 5.4M1
41    */
 
42    public final class XStreamUtils
43    {
44    /**
45    * Some famous unserializable classes. Fields with this classes are supposed to be made <code>transient</code> in
46    * placed that may end up serialized but never too careful...
47    */
48    private final static List<Class<?>> UNSERIALIZABLE_CLASSES =
49    Arrays.<Class<?>>asList(Logger.class, Provider.class, ComponentManager.class);
50   
 
51  0 toggle private XStreamUtils()
52    {
53   
54    }
55   
56    /**
57    * @param obj the value to check
58    * @return true if the type serialization cannot fail
59    */
 
60  6 toggle public static boolean isSafeType(Object obj)
61    {
62  6 return obj == null || obj instanceof String || obj instanceof Number || obj.getClass().isArray()
63    || obj instanceof Enum;
64    }
65   
66    /**
67    * @param item the item to serialize
68    * @return true of the item is serializable
69    */
 
70  63972 toggle public static boolean isSerializable(Object item)
71    {
72  63972 if (item != null) {
73  63859 Serializable serializable = item.getClass().getAnnotation(Serializable.class);
74  63859 if (serializable != null) {
75  157 return serializable.value();
76    } else {
77  63703 if (item instanceof java.io.Serializable) {
78  61502 return true;
79    }
80    }
81   
82  2198 if (!item.getClass().isAnnotationPresent(Component.class)) {
83  2169 for (Class<?> clazz : UNSERIALIZABLE_CLASSES) {
84  6504 if (clazz.isInstance(item)) {
85  3 return false;
86    }
87    }
88   
89  2166 return true;
90    }
91    } else {
92  114 return true;
93    }
94   
95  29 return false;
96    }
97   
 
98  3175 toggle public static void serializeField(String name, Class<?> defaultType, Object value, HierarchicalStreamWriter writer,
99    MarshallingContext context, Mapper mapper)
100    {
101  3175 if (value != null) {
102    // Start node
103  2035 writer.startNode(name);
104   
105  2035 Class<?> actualType = value.getClass();
106   
107  2035 if (actualType != defaultType) {
108  716 String serializedClassName = mapper.serializedClass(actualType);
109  716 String attributeName = mapper.aliasForSystemAttribute("class");
110  716 if (attributeName != null) {
111  716 writer.addAttribute(attributeName, serializedClassName);
112    }
113    }
114   
115    // Value
116  2035 context.convertAnother(value);
117   
118    // End node
119  2035 writer.endNode();
120    }
121    }
122    }