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

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

HIGHLIGHTS:

  • add <include/> to configuration
  • add side notes to tree
    • used to store arbitrary information alongside the tree structure
  • migrate to log4j2
    • supports lazy string evaluation of passed arguments
  • improve GUI tree
    • it stays in synchronization with actual state (even in high load test scenario)
  • improve panel management in GUI
  • make loading objects in GUI more lazy
  • offload parsing to connection receiver thread
    • info parsing
    • first step of objects parsing
  • fix connection parsing bug (eof in long values)
  • support zero-arguments procedure in table view

CHANGELOG:
Implement procedure calls from table view.

Refactorization around procedures in tables.

Add table editor for buttons.

Render buttons in the the list view.

Further improve Columns.

Add Column class for TableModel?.

Accept also non-arguments ProcedureParams? in tableView.

Increase maximal TextAreaControl? size.

Add tooltip to ProcedureControl?.

Fix bug of interpreting eofs in long values by connection reader.

Further rework connection parsing.

Simplify client connection processing.

Test ListChange? modification.

Test ListChange? events with java server.

Add TestChild?.

Fix bug with fast deregistering when connecting to running server.

Another minor refactorization in TreeOperations?.

Fix bug in SimpleAbstractAccess? loading routine.

Another minor improvement.

Minor change.

Make reading of List objects two-phase.

Another minor change.

Dispatch parsing into receiver thread.

Another step.

Enclose passing value in ObjectParam? case in closure.

Minor step.

Minor change on way to offload parsing.

Temporarily comment out single ValueParam? get.

It will be generalized to multi ValueParam?.

Process info in receiver thread.

Add DispatchingExceptionHandler?.

Make waits in browser test longer.

Use FETCHED_MARK.

It is honored in GUI, where it used to decide whether to get values

after user action.

It is set in standard algorithm for processing fetched values.

Add remove operation to side notes.

Make loading more lazy.

Improve loading policy.

On node choose load itself, on node expansion, load children.

Minor improvement.

Fix bug with panel interleaving.

Minor improvements.

Improve panel management.

More cleaning around panels.

Reorganize panels.

Further improve tree.

Fix bug in TreeModel?.

Remove children from TreeNode?.

Implement TreeNode? hashCode and equals.

Make TreeNode? delegate equals and hashcode to internal reference.

Move listeners from TreeNode? to side notes.

Store path.textual as a side note.

Side note params instead of accesses for objects.

More refactorizations.

In TreeNode? bindAccess based on side notes.

Minor step.

Hide createAccess.

Rename AccessInterface? to Access.

Minor changes.

Several improvements in high load scenarios.

Change semantics of ArrayListAccess?.set(index, null);

It now removes the element, making list shorter
(it was set to null before).

Add path remove handler.

Handle exceptions in Connection.

Update .gitignore

Configure logging to file.

Move registration to TreeModel?.

Further refactorization.

Minor refactorization.

Minor improvements.

Use specialized event also for Modify action of ListChange?.

Use remove events.

Use the insertion events for tree.

Further improve tree refreshing.

Further improve reacting on events in GUI.

Fix problem with not adding objects on addition list change.

Migrate to log4j lazy String construction interface.

Migrate imports to log4j2.

Drop dependency on adapter to version 1.2.

Switch log4j implementation to log4j2.

Add dirty mark to the NodeAtFrame?.

Make selecting in AccessInterfaces? type safe.

Ignore containers size settings in Model and Genotype.

Use tree side notes to remember local changes and panels.

Add sideNotes to tree.

They will be used to store various accompanying information
right in the tree.

Use ReferenceIdentityMap? from apache in TreeNode?.

It suits the need perfectly (weak semantics on both key and value).

Make ArrayListParam? do not react size changes.

Guard in TableModel? before not yet loaded objects.

Add <include/> clause and AutoInjector?.

Extract common columns configuration to separate xml,
that can be included by other configurations.

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 (field.has()) {
216                        return Modifier.isFinal(field.get().getModifiers());
217                }
218                if (setter.has()) {
219                        return false;
220                }
221                if (Collection.class.isAssignableFrom(getRawType())) {
222                        return false;
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.