1 | package com.framsticks.params; |
---|
2 | |
---|
3 | import java.io.InputStream; |
---|
4 | import java.lang.reflect.Field; |
---|
5 | import java.lang.reflect.Member; |
---|
6 | import java.lang.reflect.Method; |
---|
7 | import java.util.ArrayList; |
---|
8 | import java.util.Collections; |
---|
9 | import java.util.IdentityHashMap; |
---|
10 | import java.util.LinkedList; |
---|
11 | import java.util.List; |
---|
12 | import java.util.Map; |
---|
13 | |
---|
14 | import org.apache.logging.log4j.Logger; |
---|
15 | import org.apache.logging.log4j.LogManager; |
---|
16 | |
---|
17 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
18 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
19 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
20 | import com.framsticks.parsers.FileSource; |
---|
21 | import com.framsticks.parsers.Loaders; |
---|
22 | import com.framsticks.util.Builder; |
---|
23 | import com.framsticks.util.Misc; |
---|
24 | import com.framsticks.util.lang.Containers; |
---|
25 | import com.framsticks.util.lang.Strings; |
---|
26 | |
---|
27 | @FramsClassAnnotation(id = "class", name = "class") |
---|
28 | public class FramsClassBuilder implements Builder<FramsClass> { |
---|
29 | private static final Logger log = |
---|
30 | LogManager.getLogger(FramsClassBuilder.class); |
---|
31 | |
---|
32 | public static String getName(FramsClassAnnotation fca, Class<?> javaClass) { |
---|
33 | return fca.name().equals("") ? javaClass.getSimpleName() : fca.name(); |
---|
34 | } |
---|
35 | |
---|
36 | public static String getId(FramsClassAnnotation fca, Class<?> javaClass) { |
---|
37 | return fca.id().equals("") ? javaClass.getSimpleName() : fca.id(); |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | public static final String GENERATE_HELP_PREFIX = "automatically generated from: "; |
---|
42 | |
---|
43 | public static FramsClass readFromStream(InputStream stream) { |
---|
44 | return readFromSource(new FileSource(stream)); |
---|
45 | } |
---|
46 | |
---|
47 | public static FramsClass readFromSource(Source source) { |
---|
48 | return Loaders.loadFramsClass(source); |
---|
49 | } |
---|
50 | |
---|
51 | // public static Class<? extends Param> getParamType(@Nonnull Class<?> c) { |
---|
52 | // if (c.equals(Integer.class)) { |
---|
53 | // return DecimalParam.class; |
---|
54 | // } |
---|
55 | // if (c.equals(Double.class)) { |
---|
56 | // return FloatParam.class; |
---|
57 | // } |
---|
58 | // if (c.equals(String.class)) { |
---|
59 | // return StringParam.class; |
---|
60 | // } |
---|
61 | // if (c.equals(Object.class)) { |
---|
62 | // return UniversalParam.class; |
---|
63 | // } |
---|
64 | // return null; |
---|
65 | // } |
---|
66 | |
---|
67 | public static String extractIdOf(Member member) { |
---|
68 | if (member instanceof Field) { |
---|
69 | return member.getName(); |
---|
70 | } |
---|
71 | if (member instanceof Method) { |
---|
72 | Method m = (Method) member; |
---|
73 | String n = m.getName(); |
---|
74 | int argsNum = m.getParameterTypes().length; |
---|
75 | if (argsNum == 0) { |
---|
76 | return n.startsWith("get") ? Strings.uncapitalize(n.substring(3)) : n; |
---|
77 | } |
---|
78 | if (argsNum == 1) { |
---|
79 | if (n.startsWith("set")) { |
---|
80 | return Strings.uncapitalize(n.substring(3)); |
---|
81 | } |
---|
82 | if (n.startsWith("add")) { |
---|
83 | return Strings.uncapitalize(n.substring(3)); |
---|
84 | } |
---|
85 | if (n.startsWith("remove")) { |
---|
86 | return Strings.uncapitalize(n.substring(6)); |
---|
87 | } |
---|
88 | return n; |
---|
89 | } |
---|
90 | log.error("invalid number of arguments"); |
---|
91 | return null; |
---|
92 | } |
---|
93 | log.error("invalid kind of member"); |
---|
94 | return null; |
---|
95 | } |
---|
96 | public static String getName(ParamAnnotation annotation, Member member) { |
---|
97 | return annotation.name().equals("") ? Strings.capitalize(extractIdOf(member)) : annotation.name(); |
---|
98 | } |
---|
99 | |
---|
100 | public static String getId(ParamAnnotation annotation, Member member) { |
---|
101 | return annotation.id().equals("") ? extractIdOf(member) : annotation.id(); |
---|
102 | } |
---|
103 | |
---|
104 | public static ParamBuilder fill(ParamBuilder builder, Member member, ParamAnnotation annotation) { |
---|
105 | return builder |
---|
106 | .id(getId(annotation, member)) |
---|
107 | .name(getName(annotation, member)); |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | public static final Map<Class<?>, FramsClass> synchronizedCacheForBasedOnForJavaClass = Collections.synchronizedMap(new IdentityHashMap<Class<?>, FramsClass>()); |
---|
112 | |
---|
113 | public FramsClass forClass(Class<?> javaClass) throws ConstructionException { |
---|
114 | FramsClass result = synchronizedCacheForBasedOnForJavaClass.get(javaClass); |
---|
115 | if (result != null) { |
---|
116 | return result; |
---|
117 | } |
---|
118 | |
---|
119 | log.debug("building for class {}", javaClass); |
---|
120 | |
---|
121 | FramsClassAnnotation fca = javaClass.getAnnotation(FramsClassAnnotation.class); |
---|
122 | if (fca == null) { |
---|
123 | throw new ConstructionException().msg("java class is not annotated with FramsClassAnnotation").arg("java", javaClass); |
---|
124 | } |
---|
125 | |
---|
126 | id(getId(fca, javaClass)); |
---|
127 | name(getName(fca, javaClass)); |
---|
128 | |
---|
129 | for (ParamCandidate pc : ParamCandidate.getAllCandidates(javaClass).getOrder()) { |
---|
130 | ParamBuilder builder = Param.build().id(pc.getId()).name(pc.getName()).flags(pc.getFlags()); |
---|
131 | |
---|
132 | pc.induceParamType(builder); |
---|
133 | |
---|
134 | for (ParamAnnotation pa : pc.getAnnotations()) { |
---|
135 | if (!"".equals(pa.def())) { |
---|
136 | builder.def(pa.def()); |
---|
137 | } |
---|
138 | if (!"".equals(pa.help())) { |
---|
139 | builder.help(pa.help()); |
---|
140 | } |
---|
141 | if (!"".equals(pa.min())) { |
---|
142 | builder.min(pa.min()); |
---|
143 | } |
---|
144 | if (!"".equals(pa.max())) { |
---|
145 | builder.max(pa.max()); |
---|
146 | } |
---|
147 | builder.extra(pa.extra()); |
---|
148 | if (!"".equals(pa.stringType())) { |
---|
149 | builder.type(pa.stringType()); |
---|
150 | } |
---|
151 | if (!pa.paramType().equals(Param.class)) { |
---|
152 | builder.type(pa.paramType()); |
---|
153 | } |
---|
154 | } |
---|
155 | param(builder); |
---|
156 | } |
---|
157 | |
---|
158 | result = finish(); |
---|
159 | |
---|
160 | synchronizedCacheForBasedOnForJavaClass.put(javaClass, result); |
---|
161 | |
---|
162 | return result; |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | protected String id; |
---|
167 | |
---|
168 | protected String name; |
---|
169 | |
---|
170 | protected String description; |
---|
171 | |
---|
172 | protected final List<Param> params = new LinkedList<>(); |
---|
173 | |
---|
174 | protected List<GroupBuilder> groupBuilders = new ArrayList<GroupBuilder>(); |
---|
175 | |
---|
176 | @ParamAnnotation |
---|
177 | public FramsClassBuilder id(String id) { |
---|
178 | this.id = id; |
---|
179 | return this; |
---|
180 | } |
---|
181 | |
---|
182 | @ParamAnnotation |
---|
183 | public FramsClassBuilder name(String name) { |
---|
184 | this.name = name; |
---|
185 | return this; |
---|
186 | } |
---|
187 | |
---|
188 | public FramsClassBuilder idAndName(String v) { |
---|
189 | this.id = v; |
---|
190 | this.name = v; |
---|
191 | return this; |
---|
192 | } |
---|
193 | |
---|
194 | @ParamAnnotation(id = "desc") |
---|
195 | public FramsClassBuilder description(String description) { |
---|
196 | this.description = description; |
---|
197 | return this; |
---|
198 | } |
---|
199 | |
---|
200 | public FramsClassBuilder() { |
---|
201 | } |
---|
202 | |
---|
203 | public FramsClass finish() { |
---|
204 | return new FramsClass(this); |
---|
205 | } |
---|
206 | |
---|
207 | @AutoAppendAnnotation |
---|
208 | public FramsClassBuilder param(ParamBuilder builder) { |
---|
209 | Param param = builder.finish(); |
---|
210 | |
---|
211 | Integer group = param.getGroup(); |
---|
212 | if (group != null) { |
---|
213 | Containers.getFromList(groupBuilders, group, "group", this).addParam(param); |
---|
214 | } |
---|
215 | |
---|
216 | params.add(param); |
---|
217 | return this; |
---|
218 | } |
---|
219 | |
---|
220 | @AutoAppendAnnotation |
---|
221 | public FramsClassBuilder group(GroupBuilder builder) { |
---|
222 | groupBuilders.add(builder); |
---|
223 | return this; |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * @return the id |
---|
228 | */ |
---|
229 | @ParamAnnotation |
---|
230 | public String getId() { |
---|
231 | return id; |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | * @return the name |
---|
236 | */ |
---|
237 | @ParamAnnotation |
---|
238 | public String getName() { |
---|
239 | return name; |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * @return the description |
---|
244 | */ |
---|
245 | @ParamAnnotation(id = "desc") |
---|
246 | public String getDescription() { |
---|
247 | return description; |
---|
248 | } |
---|
249 | |
---|
250 | public FramsClassBuilder group(String group) { |
---|
251 | return group(new GroupBuilder().name(group)); |
---|
252 | } |
---|
253 | |
---|
254 | @Override |
---|
255 | public String toString() { |
---|
256 | return "FramsClassBuilder for " + Misc.returnNotNull(id, "<not yet known>"); |
---|
257 | } |
---|
258 | |
---|
259 | |
---|
260 | |
---|
261 | } |
---|