Package: ModuleCopy

ModuleCopy

nameinstructionbranchcomplexitylinemethod
ModuleCopy(Class)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
copyStaticFields(Class)
M: 52 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
deepCopy(Object)
M: 27 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getAllFields(Class)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getAllFields(List, Class)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getValue()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isFinal(Field)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isJavaLangClass(Class)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isStatic(Field)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
reset()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
resetStaticFields()
M: 35 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
unfinal(Field)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package org.overture.codegen.runtime.traces;
2:
3: import java.lang.reflect.Field;
4: import java.lang.reflect.Modifier;
5: import java.util.Arrays;
6: import java.util.HashMap;
7: import java.util.LinkedList;
8: import java.util.List;
9: import java.util.Map;
10:
11: import org.overture.codegen.runtime.ValueType;
12: import org.overture.codegen.runtime.copying.DeepCopy;
13:
14: public class ModuleCopy
15: {
16:         private static final String MODIFIERS_FIELD = "modifiers";
17:         private static final String JAVA_LANG = "java.lang";
18:
19:         protected Map<Field, Object> staticFields;
20:
21:         public ModuleCopy(Class<?> clazz)
22:         {
23:                 super();
24:
25:•                if (!isJavaLangClass(clazz))
26:                 {
27:                         copyStaticFields(clazz);
28:                 }
29:         }
30:
31:         private boolean isJavaLangClass(Class<?> clazz)
32:         {
33:                 return clazz.getName().startsWith(JAVA_LANG);
34:         }
35:
36:         public void reset()
37:         {
38:                 resetStaticFields();
39:         }
40:
41:         public static List<Field> getAllFields(Class<?> type)
42:         {
43:                 return getAllFields(new LinkedList<Field>(), type);
44:         }
45:
46:         public void resetStaticFields()
47:         {
48:•                if (staticFields == null)
49:                 {
50:                         return;
51:                 }
52:
53:•                for (Field f : staticFields.keySet())
54:                 {
55:                         f.setAccessible(true);
56:
57:                         Object v = deepCopy(staticFields.get(f));
58:
59:                         try
60:                         {
61:                                 f.set(null, v);
62:                         } catch (IllegalArgumentException | IllegalAccessException e)
63:                         {
64:                                 e.printStackTrace();
65:                         }
66:                 }
67:         }
68:
69:         public void copyStaticFields(Class<?> clazz)
70:         {
71:                 this.staticFields = new HashMap<>();
72:
73:•                for (Field f : getAllFields(clazz))
74:                 {
75:                         try
76:                         {
77:                                 /**
78:                                  * The field may be 'private'. Make it accessible so we can set it later
79:                                  */
80:                                 f.setAccessible(true);
81:•                                if (isFinal(f))
82:                                 {
83:                                         /**
84:                                          * Remove the 'final' modifier so we can set it later
85:                                          */
86:                                         unfinal(f);
87:
88:                                         /**
89:                                          * The Java code generator also makes 'final' fields 'static'
90:                                          */
91:                                         staticFields.put(f, deepCopy(f.get(null)));
92:•                                } else if (isStatic(f))
93:                                 {
94:                                         staticFields.put(f, deepCopy(f.get(null)));
95:                                 }
96:                         } catch (NoSuchFieldException | SecurityException
97:                                         | IllegalArgumentException | IllegalAccessException e)
98:                         {
99:                                 e.printStackTrace();
100:                         }
101:                 }
102:         }
103:
104:         private static void unfinal(Field f)
105:                         throws NoSuchFieldException, IllegalAccessException
106:         {
107:                 f.setAccessible(true);
108:                 Field modifiersField = Field.class.getDeclaredField(MODIFIERS_FIELD);
109:                 modifiersField.setAccessible(true);
110:                 modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
111:         }
112:
113:         public static boolean isFinal(Field f)
114:         {
115:                 return Modifier.isFinal(f.getModifiers());
116:         }
117:
118:         public static boolean isStatic(Field f)
119:         {
120:                 return Modifier.isStatic(f.getModifiers());
121:         }
122:
123:         public static List<Field> getAllFields(List<Field> fields, Class<?> type)
124:         {
125:                 fields.addAll(Arrays.asList(type.getDeclaredFields()));
126:
127:•                if (type.getSuperclass() != null)
128:                 {
129:                         fields = getAllFields(fields, type.getSuperclass());
130:                 }
131:
132:                 return fields;
133:         }
134:
135:         public static Object deepCopy(Object orig)
136:         {
137:•                if (orig == null)
138:                 {
139:                         return null;
140:•                } else if (orig instanceof ValueType)
141:                 {
142:                         ValueType vt = (ValueType) orig;
143:
144:                         return vt.copy();
145:•                } else if (orig instanceof Number || orig instanceof Character
146:                                 || orig instanceof Boolean)
147:                 {
148:                         return orig;
149:                 } else
150:                 {
151:                         return DeepCopy.copy(orig);
152:                 }
153:         }
154:
155:         public Object getValue()
156:         {
157:                 // A module cannot be instantiated so it has no value
158:                 return null;
159:         }
160: }