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.i18nsystem.tools;
35
36 import info.magnolia.i18nsystem.I18nable;
37 import info.magnolia.i18nsystem.I18nText;
38 import info.magnolia.i18nsystem.proxytoys.MethodsAnnotatedWith;
39 import info.magnolia.i18nsystem.proxytoys.ProxytoysI18nizer;
40 import info.magnolia.i18nsystem.proxytoys.ReturnsAnnotatedTypeArgument;
41
42 import java.lang.reflect.InvocationTargetException;
43 import java.lang.reflect.Method;
44 import java.util.Collection;
45 import java.util.Locale;
46 import java.util.Map;
47 import java.util.Set;
48
49 import net.sf.cglib.proxy.Enhancer;
50
51 import org.reflections.ReflectionUtils;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class GenerateTranslationFilesImpl {
70 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(GenerateTranslationFilesImpl.class);
71
72
73
74
75
76
77 public GeneratorResults generateFor(Locale locale, Object o) throws InvocationTargetException, IllegalAccessException {
78 final GeneratorResults genRes = new GeneratorResults(locale);
79
80
81 final ProxytoysI18nizer i18nizer = new ToolingProxytoysI18nizer(genRes, locale, new NullTranslationService());
82 final Object root = i18nizer.decorate(o);
83 inspect(root);
84
85
86
87 return genRes;
88 }
89
90 private void inspect(Collection c) throws InvocationTargetException, IllegalAccessException {
91 for (Object o : c) {
92 inspect(o);
93 }
94 }
95
96 private void inspect(Object obj) throws InvocationTargetException, IllegalAccessException {
97 Class clazz = obj.getClass();
98 while (Enhancer.isEnhanced(clazz)) {
99 clazz = clazz.getSuperclass();
100 }
101
102 final Set<Method> textMethods = ReflectionUtils.getMethods(clazz, new MethodsAnnotatedWith(I18nText.class));
103 final Set<Method> childMethods = ReflectionUtils.getMethods(clazz, new ReturnsAnnotatedTypeArgument(I18nable.class));
104 for (Method textMethod : textMethods) {
105 log.info("Visiting textMethod: " + textMethod.getDeclaringClass().getSimpleName() + "." + textMethod.getName());
106 textMethod.invoke(obj);
107 }
108
109 for (Method childMethod : childMethods) {
110
111
112 final Class<?> returnType = childMethod.getReturnType();
113 if (Map.class.isAssignableFrom(returnType)) {
114 log.info("Visiting map values of child method " + childMethod.getDeclaringClass().getSimpleName() + "." + childMethod.getName() + "()");
115 inspect(((Map) childMethod.invoke(obj)).values());
116 } else if (Collection.class.isAssignableFrom(returnType)) {
117 log.info("Visiting items of child method " + childMethod.getDeclaringClass().getSimpleName() + "." + childMethod.getName() + "()");
118 inspect((Collection) childMethod.invoke(obj));
119 } else {
120 log.info("Visiting child method " + childMethod.getDeclaringClass().getSimpleName() + "." + childMethod.getName() + "()");
121 final Object child = childMethod.invoke(obj);
122 if (child == null) {
123 log.info("# null returned for " + childMethod);
124 } else {
125 inspect(child);
126 }
127 }
128 }
129 }
130 }