source: java/main/src/main/java/com/framsticks/params/ParamCandidate.java @ 99

Last change on this file since 99 was 99, checked in by psniegowski, 11 years ago

HIGHLIGTS:

  • complete events implementation
  • add CLI in Java Framsticks server
  • add automatic registration for events in GUI
  • improve objects fetching (object are never overwritten with new instances)
  • properly react for ListChange? events
  • add ListPanel? with table view
    • columns to be shown may be statically specified in configuration
    • currently modyfying data through tables is not available
  • improve maven configuration
    • configuration file may be specified without touching pom.xml

CHANGELOG:
Extract constants from Flags into ParamFlags? and SetStateFlags?.

Extract flags I/O to FlagsUtils? class.

Configured maven to exec given resource configuration.

For example:
mvn exec:exec -Dframsticks.config=/configs/managed-console.xml

Cleanup pom.xml

Rename ObjectTree? to LocalTree? (also make LocalTree? and RemoteTree? final).

Minor change.

Add maximum number of columns in ListPanelProvider?.

Improve ColumnsConfig? interpretation.

Automatically fill FramsClass?.name if trying to construct empty.

Improve identitifer case mangling in XmlLoader?.

Introduce configurable ColumnsConfig?.

Draft working version of ListPanel?.

Table is being shown (although empty).

More improvements to table building.

Move some functionality from Frame to TreeModel?.

Move tree classes in gui to separate package.

Remove old table related classes.

Add draft implementation of TableModel?.

Redirect ParamBuilder?.forAccess to AccessInterface?.

Optimize ParamBuilder?.forAccess()

Do not clear list when loading.

Do not load fetched values directly.

Implement different AccessInterface? copying policy.

Optimize fetching values routine.

Remove Mode enum (work out get semantics).

Some improvements to ListChange? handling.

Improve UniqueListAccess?.

Add reaction for ListChanges? in the TreeNode?.

EventListeners? are being added in the TreeNode?.

Listeners for ListParams? are now very naive (they download
whole list).

Automatially register on events in GUI.

Events are working in RemoteTree? and Server.

Move listeners to the ClientSideManagedConnection?.

Remove old classes responsible for event subscriptions.

Improve event reading.

Improve events handling at server side.

Add register attribute in FramsClassAnnotation?
to automatically also register other classes.

Registering events works.

Setup for remote listeners registration.

More improvements.

Minor changes.

Add rootTree to the ClientAtServer?.

Moving CLI to the ClientAtServer?.

Fix bug: use Void.TYPE instead of Void.class

More development around CLI.

  • Improve Path resolving.

Add synthetic root to ObjectTree?.

It is needed to allow sybling for the original root
that would containg CLI.

Some work with registering events in RemoteTree?.

Draft implementation of listener registering in RemoteTree?.

Support events registration in the ObjectTree?.

Add events support to ReflectionAccess?.

EventParam? is recognized by ParamCandidate?.

Prepare interface for Events across project.

Add EventListener? and API for listeners in Tree.

File size: 10.0 KB
Line 
1package com.framsticks.params;
2
3import java.lang.reflect.AnnotatedElement;
4import java.lang.reflect.Array;
5import java.lang.reflect.Field;
6import java.lang.reflect.GenericArrayType;
7import java.lang.reflect.Member;
8import java.lang.reflect.Method;
9import java.lang.reflect.Modifier;
10import java.lang.reflect.ParameterizedType;
11import java.lang.reflect.Type;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.Collections;
15import java.util.Comparator;
16import java.util.HashMap;
17import java.util.LinkedList;
18import java.util.List;
19import java.util.Map;
20
21import com.framsticks.params.annotations.FramsClassAnnotation;
22import com.framsticks.params.annotations.ParamAnnotation;
23import com.framsticks.params.types.ProcedureParam;
24
25public class ParamCandidate {
26
27        public class OneTime<T> {
28                protected final String name;
29                T value;
30
31                /**
32                 * @param name
33                 */
34                public OneTime(String name) {
35                        this.name = name;
36                }
37
38                final void set(T value) {
39                        if (this.value == null) {
40                                this.value = value;
41                                return;
42                        }
43                        if (!this.value.equals(value)) {
44                                throw new ConstructionException().msg("already set")
45                                        .arg("name", name)
46                                        .arg("in", ParamCandidate.this)
47                                        .arg("already", this.value)
48                                        .arg("now", value);
49                        }
50                }
51
52                public final T get() {
53                        return value;
54                }
55
56                public final boolean has() {
57                        return value != null;
58                }
59
60                @Override
61                public String toString() {
62                        return value == null ? "<null>" : value.toString();
63                }
64
65
66        }
67
68        protected final String id;
69        protected final OneTime<String> name = new OneTime<>("name");
70        protected final OneTime<Type> type = new OneTime<>("type");
71        protected final OneTime<Field> field = new OneTime<>("field");
72        protected final OneTime<Method> setter = new OneTime<>("setter");
73        protected final OneTime<Method> getter = new OneTime<>("getter");
74        protected final OneTime<Method> caller = new OneTime<>("caller");
75        protected final OneTime<Method> adder = new OneTime<>("adder");
76        protected final OneTime<Method> remover = new OneTime<>("remover");
77
78        protected final List<ParamAnnotation> annotations = new LinkedList<>();
79
80        /**
81         * @param id
82         */
83        public ParamCandidate(String id) {
84                this.id = id;
85        }
86
87        /**
88         * @return the id
89         */
90        public String getId() {
91                return id;
92        }
93
94        /**
95         * @return the name
96         */
97        public String getName() {
98                return name.get();
99        }
100
101        /**
102         * @return the type
103         */
104        public Type getType() {
105                return type.get();
106        }
107
108        public Class<?> getRawType() {
109                return getRawClass(type.get());
110        }
111
112        void setType(Type type) {
113                this.type.set(type);
114        }
115
116
117        /**
118         * @return the field
119         */
120        public Field getField() {
121                return field.get();
122        }
123
124        /**
125         * @return the setter
126         */
127        public Method getSetter() {
128                return setter.get();
129        }
130
131        /**
132         * @return the getter
133         */
134        public Method getGetter() {
135                return getter.get();
136        }
137
138        /**
139         * @return the getter
140         */
141        public Method getCaller() {
142                return caller.get();
143        }
144
145        /**
146         * @return the getter
147         */
148        public Method getAdder() {
149                return adder.get();
150        }
151
152        /**
153         * @return the getter
154         */
155        public Method getRemover() {
156                return remover.get();
157        }
158
159        /**
160         * @return the annotations
161         */
162        public List<ParamAnnotation> getAnnotations() {
163                return Collections.unmodifiableList(annotations);
164        }
165
166        void validate() throws ConstructionException {
167                try {
168                        if (adder.has() != remover.has()) {
169                                throw new ConstructionException().msg("only one of event manipulator methods is defined");
170                        }
171                        if (adder.has() && remover.has()) {
172                                return;
173                        }
174                        if (caller.has()) {
175                                if (!isPublic(caller)) {
176                                        throw new ConstructionException().msg("method is not public");
177                                }
178                                if (getter.has() || setter.has()) {
179                                        throw new ConstructionException().msg("getter or setter coexist with caller");
180                                }
181                                return;
182                        }
183                        if (isPublic(field)) {
184                                if (getter.has()) {
185                                        throw new ConstructionException().msg("getter and public field coexist");
186                                }
187                                return;
188                        }
189                        if (isPublic(field)) {
190                                if (setter.has()) {
191                                        throw new ConstructionException().msg("setter and field coexist");
192                                }
193                        }
194
195                        if (!getter.has() && !field.has()) {
196                                throw new ConstructionException().msg("missing getter or field");
197                        }
198                        if (getter.has() || field.has() || setter.has()) {
199                                if (type.get().equals(Void.TYPE)) {
200                                        throw new ConstructionException().msg("type of field is void");
201                                }
202                        }
203                } catch (ConstructionException e) {
204                        throw e.arg("in", this);
205                }
206        }
207
208        boolean isFinal() {
209                if (caller.has()) {
210                        return false;
211                }
212                if (adder.has() || remover.has()) {
213                        return false;
214                }
215                if (Collection.class.isAssignableFrom(getRawType())) {
216                        return false;
217                }
218                if (setter.has()) {
219                        return false;
220                }
221                if (field.has()) {
222                        return Modifier.isFinal(field.get().getModifiers());
223                }
224                return true;
225        }
226
227        boolean isReadOnly() {
228                if (caller.has()) {
229                        return false;
230                }
231                if (adder.has() || remover.has()) {
232                        return false;
233                }
234                if (Collection.class.isAssignableFrom(getRawType())) {
235                        return false;
236                }
237                if (isPublic(setter)) {
238                        return false;
239                }
240                if (isPublic(field)) {
241                        return Modifier.isFinal(field.get().getModifiers());
242                }
243                return true;
244        }
245
246        void add(ParamAnnotation paramAnnotation, Member member, String name) {
247                this.name.set(name);
248                annotations.add(paramAnnotation);
249                if (member instanceof Field) {
250                        field.set((Field) member);
251                        setType(field.get().getGenericType());
252                        return;
253                }
254                if (member instanceof Method) {
255                        Method m = (Method) member;
256                        if (!paramAnnotation.paramType().equals(Param.class)) {
257                                if (paramAnnotation.paramType().equals(ProcedureParam.class)) {
258                                        caller.set(m);
259                                        return;
260                                }
261                                throw new ConstructionException().msg("explicit set of paramType different than ProcedureParam is not yet supported").arg("name", name).arg("method", m).arg("in", this);
262                        }
263                        Type[] ps = m.getGenericParameterTypes();
264                        Class<?>[] pts = m.getParameterTypes();
265                        if (ps.length == 0) {
266                                if (m.getReturnType().equals(Void.TYPE)) {
267                                        throw new ConstructionException().msg("failed to add getter of void return type");
268                                }
269                                getter.set(m);
270                                setType(m.getGenericReturnType());
271                                return;
272                        }
273                        if (ps.length == 1) {
274                                if (pts[0].equals(EventListener.class)) {
275                                        if (member.getName().startsWith("add")) {
276                                                adder.set(m);
277                                                setType(ps[0]);
278                                                return;
279                                        }
280                                        if (member.getName().startsWith("remove")) {
281                                                remover.set(m);
282                                                setType(ps[0]);
283                                                return;
284                                        }
285                                        throw new ConstructionException().msg("invalid name of event manipulator").arg("method", m).arg("in", this);
286                                }
287                                setter.set(m);
288                                setType(ps[0]);
289                                return;
290                        }
291                        throw new ConstructionException().msg("invalid number of arguments").arg("method", m).arg("in", this);
292                }
293                throw new ConstructionException().msg("invalid kind of member").arg("member", member).arg("in", this);
294        }
295
296        public boolean isPrimitive() {
297                return getRawType().isPrimitive();
298        }
299
300        public int getFlags() {
301                int f = 0;
302                if (isReadOnly()) {
303                        f |= ParamFlags.READONLY;
304                }
305                return f;
306        }
307
308        @Override
309        public String toString() {
310                return id + "(" + type.toString() + ")";
311        }
312
313        public static boolean isPublic(Member member) {
314                return Modifier.isPublic(member.getModifiers());
315        }
316
317        public static boolean isPublic(OneTime<? extends Member> v) {
318                return v.has() ? isPublic(v.get()) : false;
319        }
320
321        public static <M extends Member & AnnotatedElement> void filterParamsCandidates(Set set, M[] members) {
322                for (M m : members) {
323                        ParamAnnotation pa = m.getAnnotation(ParamAnnotation.class);
324                        if (pa == null) {
325                                continue;
326                        }
327                        String id = FramsClassBuilder.getId(pa, m);
328                        ParamCandidate pc = null;
329                        if (set.getCandidates().containsKey(id)) {
330                                pc = set.getCandidates().get(id);
331                        } else {
332                                pc = new ParamCandidate(id);
333                                set.getCandidates().put(id, pc);
334                                set.getOrder().add(pc);
335                        }
336                        pc.add(pa, m, FramsClassBuilder.getName(pa, m));
337                }
338        }
339
340        public static class Set {
341                protected final Map<String, ParamCandidate> candidates;
342                protected final List<ParamCandidate> order;
343
344                /**
345                 * @param candidates
346                 * @param order
347                 */
348                public Set(Map<String, ParamCandidate> candidates, List<ParamCandidate> order) {
349                        this.candidates = candidates;
350                        this.order = order;
351                }
352
353                /**
354                 * @return the candidates
355                 */
356                public Map<String, ParamCandidate> getCandidates() {
357                        return candidates;
358                }
359
360                /**
361                 * @return the order
362                 */
363                public List<ParamCandidate> getOrder() {
364                        return order;
365                }
366        }
367
368        public static Set getAllCandidates(Class<?> javaClass) throws ConstructionException {
369
370                List<Class<?>> javaClasses = new LinkedList<>();
371                while (javaClass != null) {
372                        javaClasses.add(0, javaClass);
373                        javaClass = javaClass.getSuperclass();
374                }
375
376                Set resultSet = new Set(new HashMap<String, ParamCandidate>(), new LinkedList<ParamCandidate>());
377
378                for (Class<?> j : javaClasses) {
379                        Set set = new Set(resultSet.getCandidates(), new LinkedList<ParamCandidate>());
380                        filterParamsCandidates(set, j.getDeclaredFields());
381                        filterParamsCandidates(set, j.getDeclaredMethods());
382
383                        FramsClassAnnotation fa = j.getAnnotation(FramsClassAnnotation.class);
384                        if (fa != null) {
385                                final List<String> order = Arrays.asList(fa.order());
386                                Collections.sort(set.getOrder(), new Comparator<ParamCandidate>() {
387                                        @Override
388                                        public int compare(ParamCandidate pc0, ParamCandidate pc1) {
389                                                int u0 = order.indexOf(pc0.getId());
390                                                int u1 = order.indexOf(pc1.getId());
391                                                if (u0 == -1 || u1 == -1) {
392                                                        return 0;
393                                                }
394                                                return u0 - u1;
395                                        }
396                                });
397                        }
398                        resultSet.getOrder().addAll(0, set.getOrder());
399                }
400
401                for (ParamCandidate pc : resultSet.getOrder()) {
402                        pc.validate();
403                }
404
405                return resultSet;
406        }
407
408        public static Class<?> getRawClass(final Type type) {
409                if (type == null) {
410                        throw new IllegalArgumentException();
411                }
412                if (Class.class.isInstance(type)) {
413                        return Class.class.cast(type);
414                }
415                if (ParameterizedType.class.isInstance(type)) {
416                        final ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
417                        return getRawClass(parameterizedType.getRawType());
418                } else if (GenericArrayType.class.isInstance(type)) {
419                        GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
420                        Class<?> c = getRawClass(genericArrayType.getGenericComponentType());
421                        return Array.newInstance(c, 0).getClass();
422                } else {
423                        return null;
424                }
425        }
426
427};
Note: See TracBrowser for help on using the repository browser.