1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.blossom.dialog; |
35 |
|
|
36 |
|
import java.lang.annotation.Annotation; |
37 |
|
import java.lang.reflect.Method; |
38 |
|
import java.lang.reflect.Modifier; |
39 |
|
import java.util.ArrayList; |
40 |
|
import java.util.Collections; |
41 |
|
import java.util.List; |
42 |
|
|
43 |
|
import org.slf4j.Logger; |
44 |
|
import org.slf4j.LoggerFactory; |
45 |
|
import org.springframework.aop.support.AopUtils; |
46 |
|
import org.springframework.util.ClassUtils; |
47 |
|
import org.springframework.util.ReflectionUtils; |
48 |
|
|
49 |
|
import info.magnolia.module.blossom.annotation.DialogFactory; |
50 |
|
import info.magnolia.module.blossom.annotation.I18nBasename; |
51 |
|
import info.magnolia.module.blossom.annotation.PostCreate; |
52 |
|
import info.magnolia.module.blossom.annotation.TabFactory; |
53 |
|
import info.magnolia.module.blossom.annotation.TabOrder; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@since |
59 |
|
|
|
|
| 95% |
Uncovered Elements: 6 (119) |
Complexity: 32 |
Complexity Density: 0.44 |
|
60 |
|
public class DialogDescriptionBuilder { |
61 |
|
|
62 |
|
private final Logger logger = LoggerFactory.getLogger(getClass()); |
63 |
|
|
64 |
|
private DialogCreator dialogCreator; |
65 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
66 |
|
public void setDialogCreator(DialogCreator dialogCreator) {... |
67 |
|
this.dialogCreator = dialogCreator; |
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
70 |
31 |
public DialogCreator getDialogCreator() {... |
71 |
31 |
if (dialogCreator == null) { |
72 |
23 |
dialogCreator = new DefaultDialogCreator(); |
73 |
|
} |
74 |
31 |
return dialogCreator; |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
77 |
7 |
public BlossomDialogDescription buildDescription(Object factoryObject) {... |
78 |
7 |
return buildDescription(factoryObject, null); |
79 |
|
} |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
81 |
11 |
public List<BlossomDialogDescription> buildDescriptions(final Object handler) {... |
82 |
|
|
83 |
11 |
final List<Method> factoryMethods = new ArrayList<Method>(); |
84 |
|
|
85 |
11 |
final Class<?> handlerClass = AopUtils.getTargetClass(handler); |
86 |
11 |
ReflectionUtils.doWithMethods(handlerClass, new ReflectionUtils.MethodCallback() { |
87 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
88 |
156 |
@Override... |
89 |
|
public void doWith(Method method) { |
90 |
156 |
DialogFactory dialogFactory = method.getAnnotation(DialogFactory.class); |
91 |
156 |
if (dialogFactory != null && method.equals(ClassUtils.getMostSpecificMethod(method, handlerClass))) { |
92 |
4 |
if (Modifier.isStatic(method.getModifiers())) { |
93 |
0 |
throw new IllegalStateException("DialogFactory annotation is not supported on static methods"); |
94 |
|
} |
95 |
4 |
factoryMethods.add(method); |
96 |
|
} |
97 |
|
} |
98 |
|
}); |
99 |
|
|
100 |
11 |
List<BlossomDialogDescription> descriptions = new ArrayList<BlossomDialogDescription>(); |
101 |
|
|
102 |
11 |
for (Method method : factoryMethods) { |
103 |
4 |
descriptions.add(buildDescription(handler, method)); |
104 |
|
} |
105 |
|
|
106 |
11 |
return descriptions; |
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
109 |
14 |
public BlossomDialogDescription buildDescription(Object factoryObject, Method factoryMethod) {... |
110 |
14 |
DialogFactory annotation = findAnnotation(DialogFactory.class, factoryObject, factoryMethod); |
111 |
14 |
return buildDescription(annotation.value(), annotation.label(), factoryObject, factoryMethod); |
112 |
|
} |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
19 |
public BlossomDialogDescription buildDescription(String id, String label, Object factoryObject) {... |
115 |
19 |
return buildDescription(id, label, factoryObject, null); |
116 |
|
} |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
118 |
33 |
protected BlossomDialogDescription buildDescription(String id, String label, Object factoryObject, Method factoryMethod) {... |
119 |
33 |
BlossomDialogDescription dialogDescription = new BlossomDialogDescription(); |
120 |
33 |
dialogDescription.setId(id); |
121 |
33 |
dialogDescription.setFactoryMetaData(buildFactoryMetaData(label, factoryObject, factoryMethod)); |
122 |
31 |
dialogDescription.setDialogCreator(getDialogCreator()); |
123 |
31 |
return dialogDescription; |
124 |
|
} |
125 |
|
|
|
|
| 93.8% |
Uncovered Elements: 4 (65) |
Complexity: 17 |
Complexity Density: 0.44 |
|
126 |
33 |
protected DialogFactoryMetaData buildFactoryMetaData(String label, Object factoryObject, Method factoryMethod) {... |
127 |
|
|
128 |
33 |
DialogFactoryMetaData factoryMetaData = new DialogFactoryMetaData(); |
129 |
33 |
factoryMetaData.setLabel(label); |
130 |
33 |
factoryMetaData.setFactoryObject(factoryObject); |
131 |
33 |
factoryMetaData.setFactoryMethod(factoryMethod); |
132 |
|
|
133 |
33 |
TabOrder tabOrder = findAnnotation(TabOrder.class, factoryObject, factoryMethod); |
134 |
33 |
I18nBasename i18nBasename = findAnnotation(I18nBasename.class, factoryObject, factoryMethod); |
135 |
|
|
136 |
33 |
factoryMetaData.setTabOrder(tabOrder != null ? tabOrder.value() : null); |
137 |
33 |
factoryMetaData.setI18nBasename(i18nBasename != null ? i18nBasename.value() : null); |
138 |
|
|
139 |
33 |
if (factoryMethod == null) { |
140 |
|
|
141 |
29 |
Class<?> factoryClass = AopUtils.getTargetClass(factoryObject); |
142 |
|
|
143 |
29 |
List<Class<?>> classHierarchy = getClassHierarchyInTopToBottomOrder(factoryClass); |
144 |
|
|
145 |
29 |
List<Method> methodsAdded = new ArrayList<Method>(); |
146 |
|
|
147 |
29 |
for (Class<?> clazz : classHierarchy) { |
148 |
|
|
149 |
66 |
DialogFactoryClassMetaData classMetaData = new DialogFactoryClassMetaData(); |
150 |
66 |
classMetaData.setClazz(clazz); |
151 |
|
|
152 |
66 |
for (Method method : clazz.getDeclaredMethods()) { |
153 |
|
|
154 |
414 |
if (method.isAnnotationPresent(TabFactory.class) && method.isAnnotationPresent(PostCreate.class)) { |
155 |
0 |
throw new IllegalStateException("TabFactory and PostCreate annotations cannot both be used on the same method"); |
156 |
|
} |
157 |
414 |
if (method.isAnnotationPresent(TabFactory.class)) { |
158 |
18 |
if (Modifier.isStatic(method.getModifiers())) { |
159 |
1 |
throw new IllegalStateException("TabFactory annotation is not supported on static methods"); |
160 |
|
} |
161 |
17 |
Method mostSpecificMethod = ClassUtils.getMostSpecificMethod(method, factoryClass); |
162 |
17 |
if (mostSpecificMethod.isAnnotationPresent(TabFactory.class) && !methodsAdded.contains(mostSpecificMethod)) { |
163 |
16 |
classMetaData.addTabFactory(mostSpecificMethod); |
164 |
16 |
methodsAdded.add(mostSpecificMethod); |
165 |
|
} |
166 |
|
} |
167 |
413 |
if (method.isAnnotationPresent(PostCreate.class)) { |
168 |
13 |
if (Modifier.isStatic(method.getModifiers())) { |
169 |
1 |
throw new IllegalStateException("PostCreate annotation is not supported on static methods"); |
170 |
|
} |
171 |
12 |
Method mostSpecificMethod = ClassUtils.getMostSpecificMethod(method, factoryClass); |
172 |
12 |
if (mostSpecificMethod.isAnnotationPresent(PostCreate.class) && !methodsAdded.contains(mostSpecificMethod)) { |
173 |
9 |
PostCreate annotation = mostSpecificMethod.getAnnotation(PostCreate.class); |
174 |
9 |
if (annotation.value() == PostCreate.Phase.AFTER_TAB_FACTORIES_IN_SAME_CLASS) { |
175 |
7 |
classMetaData.addPostCreateCallback(mostSpecificMethod); |
176 |
2 |
} else if (annotation.value() == PostCreate.Phase.AFTER_SUB_CLASSES) { |
177 |
2 |
factoryMetaData.addPostCreateCallback(mostSpecificMethod); |
178 |
|
} |
179 |
9 |
methodsAdded.add(mostSpecificMethod); |
180 |
|
} |
181 |
|
} |
182 |
|
} |
183 |
|
|
184 |
64 |
if (!classMetaData.isEmpty()) { |
185 |
16 |
factoryMetaData.addClassMetaData(classMetaData); |
186 |
|
} |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
31 |
return factoryMetaData; |
191 |
|
} |
192 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
193 |
80 |
protected <T extends Annotation> T findAnnotation(Class<T> annotationClass, Object factoryObject, Method factoryMethod) {... |
194 |
80 |
if (factoryMethod != null) { |
195 |
12 |
return factoryMethod.getAnnotation(annotationClass); |
196 |
|
} |
197 |
68 |
Class<?> factoryClass = AopUtils.getTargetClass(factoryObject); |
198 |
68 |
return factoryClass.getAnnotation(annotationClass); |
199 |
|
} |
200 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
201 |
29 |
private List<Class<?>> getClassHierarchyInTopToBottomOrder(Class<?> factoryClass) {... |
202 |
29 |
List<Class<?>> hierarchy = new ArrayList<Class<?>>(); |
203 |
95 |
while (factoryClass != null) { |
204 |
66 |
hierarchy.add(factoryClass); |
205 |
66 |
factoryClass = factoryClass.getSuperclass(); |
206 |
|
} |
207 |
29 |
Collections.reverse(hierarchy); |
208 |
29 |
return hierarchy; |
209 |
|
} |
210 |
|
} |