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 |
|
|
|
|
| 94.3% |
Uncovered Elements: 7 (123) |
Complexity: 33 |
Complexity Density: 0.43 |
|
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 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
70 |
35 |
public DialogCreator getDialogCreator() {... |
71 |
35 |
if (dialogCreator == null) { |
72 |
27 |
DefaultDialogCreator defaultDialogCreator = new DefaultDialogCreator(); |
73 |
27 |
try { |
74 |
27 |
defaultDialogCreator.afterPropertiesSet(); |
75 |
|
} catch (Exception e) { |
76 |
0 |
throw new RuntimeException("Could not create DefaultDialogCreator", e); |
77 |
|
} |
78 |
27 |
dialogCreator = defaultDialogCreator; |
79 |
|
} |
80 |
35 |
return dialogCreator; |
81 |
|
} |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
7 |
public BlossomDialogDescription buildDescription(Object factoryObject) {... |
84 |
7 |
return buildDescription(factoryObject, null); |
85 |
|
} |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
87 |
11 |
public List<BlossomDialogDescription> buildDescriptions(final Object handler) {... |
88 |
|
|
89 |
11 |
final List<Method> factoryMethods = new ArrayList<Method>(); |
90 |
|
|
91 |
11 |
final Class<?> handlerClass = AopUtils.getTargetClass(handler); |
92 |
11 |
ReflectionUtils.doWithMethods(handlerClass, new ReflectionUtils.MethodCallback() { |
93 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
94 |
156 |
@Override... |
95 |
|
public void doWith(Method method) { |
96 |
156 |
DialogFactory dialogFactory = method.getAnnotation(DialogFactory.class); |
97 |
156 |
if (dialogFactory != null && method.equals(ClassUtils.getMostSpecificMethod(method, handlerClass))) { |
98 |
4 |
if (Modifier.isStatic(method.getModifiers())) { |
99 |
0 |
throw new IllegalStateException("DialogFactory annotation is not supported on static methods"); |
100 |
|
} |
101 |
4 |
factoryMethods.add(method); |
102 |
|
} |
103 |
|
} |
104 |
|
}); |
105 |
|
|
106 |
11 |
List<BlossomDialogDescription> descriptions = new ArrayList<BlossomDialogDescription>(); |
107 |
|
|
108 |
11 |
for (Method method : factoryMethods) { |
109 |
4 |
descriptions.add(buildDescription(handler, method)); |
110 |
|
} |
111 |
|
|
112 |
11 |
return descriptions; |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
115 |
18 |
public BlossomDialogDescription buildDescription(Object factoryObject, Method factoryMethod) {... |
116 |
18 |
DialogFactory annotation = findAnnotation(DialogFactory.class, factoryObject, factoryMethod); |
117 |
18 |
return buildDescription(annotation.value(), annotation.label(), factoryObject, factoryMethod); |
118 |
|
} |
119 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
120 |
19 |
public BlossomDialogDescription buildDescription(String id, String label, Object factoryObject) {... |
121 |
19 |
return buildDescription(id, label, factoryObject, null); |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
124 |
37 |
protected BlossomDialogDescription buildDescription(String id, String label, Object factoryObject, Method factoryMethod) {... |
125 |
37 |
BlossomDialogDescription dialogDescription = new BlossomDialogDescription(); |
126 |
37 |
dialogDescription.setId(id); |
127 |
37 |
dialogDescription.setFactoryMetaData(buildFactoryMetaData(label, factoryObject, factoryMethod)); |
128 |
35 |
dialogDescription.setDialogCreator(getDialogCreator()); |
129 |
35 |
return dialogDescription; |
130 |
|
} |
131 |
|
|
|
|
| 93.8% |
Uncovered Elements: 4 (65) |
Complexity: 17 |
Complexity Density: 0.44 |
|
132 |
37 |
protected DialogFactoryMetaData buildFactoryMetaData(String label, Object factoryObject, Method factoryMethod) {... |
133 |
|
|
134 |
37 |
DialogFactoryMetaData factoryMetaData = new DialogFactoryMetaData(); |
135 |
37 |
factoryMetaData.setLabel(label); |
136 |
37 |
factoryMetaData.setFactoryObject(factoryObject); |
137 |
37 |
factoryMetaData.setFactoryMethod(factoryMethod); |
138 |
|
|
139 |
37 |
TabOrder tabOrder = findAnnotation(TabOrder.class, factoryObject, factoryMethod); |
140 |
37 |
I18nBasename i18nBasename = findAnnotation(I18nBasename.class, factoryObject, factoryMethod); |
141 |
|
|
142 |
37 |
factoryMetaData.setTabOrder(tabOrder != null ? tabOrder.value() : null); |
143 |
37 |
factoryMetaData.setI18nBasename(i18nBasename != null ? i18nBasename.value() : null); |
144 |
|
|
145 |
37 |
if (factoryMethod == null) { |
146 |
|
|
147 |
33 |
Class<?> factoryClass = AopUtils.getTargetClass(factoryObject); |
148 |
|
|
149 |
33 |
List<Class<?>> classHierarchy = getClassHierarchyInTopToBottomOrder(factoryClass); |
150 |
|
|
151 |
33 |
List<Method> methodsAdded = new ArrayList<Method>(); |
152 |
|
|
153 |
33 |
for (Class<?> clazz : classHierarchy) { |
154 |
|
|
155 |
74 |
DialogFactoryClassMetaData classMetaData = new DialogFactoryClassMetaData(); |
156 |
74 |
classMetaData.setClazz(clazz); |
157 |
|
|
158 |
74 |
for (Method method : clazz.getDeclaredMethods()) { |
159 |
|
|
160 |
474 |
if (method.isAnnotationPresent(TabFactory.class) && method.isAnnotationPresent(PostCreate.class)) { |
161 |
0 |
throw new IllegalStateException("TabFactory and PostCreate annotations cannot both be used on the same method"); |
162 |
|
} |
163 |
474 |
if (method.isAnnotationPresent(TabFactory.class)) { |
164 |
28 |
if (Modifier.isStatic(method.getModifiers())) { |
165 |
1 |
throw new IllegalStateException("TabFactory annotation is not supported on static methods"); |
166 |
|
} |
167 |
27 |
Method mostSpecificMethod = ClassUtils.getMostSpecificMethod(method, factoryClass); |
168 |
27 |
if (mostSpecificMethod.isAnnotationPresent(TabFactory.class) && !methodsAdded.contains(mostSpecificMethod)) { |
169 |
26 |
classMetaData.addTabFactory(mostSpecificMethod); |
170 |
26 |
methodsAdded.add(mostSpecificMethod); |
171 |
|
} |
172 |
|
} |
173 |
473 |
if (method.isAnnotationPresent(PostCreate.class)) { |
174 |
13 |
if (Modifier.isStatic(method.getModifiers())) { |
175 |
1 |
throw new IllegalStateException("PostCreate annotation is not supported on static methods"); |
176 |
|
} |
177 |
12 |
Method mostSpecificMethod = ClassUtils.getMostSpecificMethod(method, factoryClass); |
178 |
12 |
if (mostSpecificMethod.isAnnotationPresent(PostCreate.class) && !methodsAdded.contains(mostSpecificMethod)) { |
179 |
9 |
PostCreate annotation = mostSpecificMethod.getAnnotation(PostCreate.class); |
180 |
9 |
if (annotation.value() == PostCreate.Phase.AFTER_TAB_FACTORIES_IN_SAME_CLASS) { |
181 |
7 |
classMetaData.addPostCreateCallback(mostSpecificMethod); |
182 |
2 |
} else if (annotation.value() == PostCreate.Phase.AFTER_SUB_CLASSES) { |
183 |
2 |
factoryMetaData.addPostCreateCallback(mostSpecificMethod); |
184 |
|
} |
185 |
9 |
methodsAdded.add(mostSpecificMethod); |
186 |
|
} |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
72 |
if (!classMetaData.isEmpty()) { |
191 |
20 |
factoryMetaData.addClassMetaData(classMetaData); |
192 |
|
} |
193 |
|
} |
194 |
|
} |
195 |
|
|
196 |
35 |
return factoryMetaData; |
197 |
|
} |
198 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
199 |
92 |
protected <T extends Annotation> T findAnnotation(Class<T> annotationClass, Object factoryObject, Method factoryMethod) {... |
200 |
92 |
if (factoryMethod != null) { |
201 |
12 |
return factoryMethod.getAnnotation(annotationClass); |
202 |
|
} |
203 |
80 |
Class<?> factoryClass = AopUtils.getTargetClass(factoryObject); |
204 |
80 |
return factoryClass.getAnnotation(annotationClass); |
205 |
|
} |
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
207 |
33 |
private List<Class<?>> getClassHierarchyInTopToBottomOrder(Class<?> factoryClass) {... |
208 |
33 |
List<Class<?>> hierarchy = new ArrayList<Class<?>>(); |
209 |
107 |
while (factoryClass != null) { |
210 |
74 |
hierarchy.add(factoryClass); |
211 |
74 |
factoryClass = factoryClass.getSuperclass(); |
212 |
|
} |
213 |
33 |
Collections.reverse(hierarchy); |
214 |
33 |
return hierarchy; |
215 |
|
} |
216 |
|
} |