1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.script.internal.safe; |
21 |
|
|
22 |
|
import java.lang.reflect.ParameterizedType; |
23 |
|
import java.lang.reflect.Type; |
24 |
|
import java.util.Arrays; |
25 |
|
import java.util.List; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.xwiki.component.annotation.Component; |
32 |
|
import org.xwiki.component.manager.ComponentLookupException; |
33 |
|
import org.xwiki.component.manager.ComponentManager; |
34 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
35 |
|
import org.xwiki.component.util.ReflectionUtils; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
43 |
|
@Component |
44 |
|
@Singleton |
45 |
|
@SuppressWarnings("rawtypes") |
|
|
| 87.5% |
Uncovered Elements: 6 (48) |
Complexity: 12 |
Complexity Density: 0.39 |
|
46 |
|
public class DefaultScriptSafeProvider implements ScriptSafeProvider |
47 |
|
{ |
48 |
|
|
49 |
|
@link |
50 |
|
|
51 |
|
@Inject |
52 |
|
private ComponentManager component; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@Inject |
58 |
|
private Logger logger; |
59 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
60 |
8689 |
@Override... |
61 |
|
public Object get(Object unsafe) |
62 |
|
{ |
63 |
8689 |
if (unsafe == null) { |
64 |
287 |
return null; |
65 |
|
} |
66 |
|
|
67 |
8402 |
Object safe = get(unsafe, Arrays.<Type>asList(unsafe.getClass())); |
68 |
|
|
69 |
8402 |
if (safe == null) { |
70 |
82 |
safe = unsafe; |
71 |
|
} |
72 |
|
|
73 |
8402 |
return safe; |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
@param |
78 |
|
@param |
79 |
|
@return |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
81 |
26943 |
private Object get(Object unsafe, List<Type> types)... |
82 |
|
{ |
83 |
26944 |
for (Type type : types) { |
84 |
36768 |
Object safe = get(unsafe, type); |
85 |
|
|
86 |
36766 |
if (safe != null) { |
87 |
8319 |
return safe; |
88 |
|
} |
89 |
|
} |
90 |
|
|
91 |
18623 |
for (Type type : types) { |
92 |
18542 |
Object safe = get(unsafe, ReflectionUtils.getDirectTypes(type)); |
93 |
|
|
94 |
18539 |
if (safe != null) { |
95 |
17870 |
return safe; |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
751 |
return null; |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
@param |
104 |
|
@param |
105 |
|
@return |
106 |
|
|
|
|
| 72.7% |
Uncovered Elements: 6 (22) |
Complexity: 6 |
Complexity Density: 0.38 |
|
107 |
36768 |
private Object get(Object unsafe, Type type)... |
108 |
|
{ |
109 |
36768 |
Type completeRole = new DefaultParameterizedType(null, ScriptSafeProvider.class, type); |
110 |
|
|
111 |
36765 |
if (this.component.hasComponent(completeRole)) { |
112 |
8320 |
try { |
113 |
8319 |
ScriptSafeProvider<Object> provider = this.component.getInstance(completeRole); |
114 |
|
|
115 |
8320 |
return provider.get(unsafe); |
116 |
|
} catch (ComponentLookupException e) { |
117 |
0 |
this.logger.error("Failed to load safe provider for complete type [{}]", type, e); |
118 |
|
} |
119 |
28446 |
} else if (type instanceof ParameterizedType) { |
120 |
404 |
Type rawType = ((ParameterizedType) type).getRawType(); |
121 |
404 |
Type rawRole = new DefaultParameterizedType(null, ScriptSafeProvider.class, rawType); |
122 |
|
|
123 |
404 |
this.logger.debug("Could not find any safe provider for type [{}]. Trying with [{}]", type, rawType); |
124 |
|
|
125 |
404 |
if (this.component.hasComponent(rawRole)) { |
126 |
0 |
try { |
127 |
0 |
ScriptSafeProvider<Object> provider = this.component.getInstance(rawRole); |
128 |
|
|
129 |
0 |
return provider.get(unsafe); |
130 |
|
} catch (ComponentLookupException e) { |
131 |
0 |
this.logger.error("Failed to load safe provider for raw type [{}]", rawType, e); |
132 |
|
} |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
28445 |
return null; |
137 |
|
} |
138 |
|
} |