source: java/main/src/main/java/com/framsticks/params/ReflectionAccess.java @ 84

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

HIGHLIGHTS:

  • simplification of entities management model
  • cleanup around params (improve hierarchy)
  • migrate from JUnit to TestNG
  • introduce FEST to automatically test GUI
  • improve slider control
  • loosen synchronization between gui tree and backend representation
  • and many other bug fixes

NOTICE:

  • a great many of lines is changed only because of substituting spaces with tabs

CHANGELOG (oldest changes at the bottom):

Some cleaning after fix found.

Fix bug with tree.

More changes with TreeNodes?.

Finally fix issue with tree.

Improve gui tree management.

Decouple update of values from fetch request in gui.

Minor changes.

Minor changes.

Minor change.

Change Path construction wording.

More fixes to SliderControl?.

Fix SliderControl?.

Fix SliderControl?.

Minor improvement.

Several changes.

Make NumberParam? a generic class.

Add robot to the gui test.

Setup common testing logging configuration.

Remove Parameters class.

Remove entityOwner from Parameters.

Move name out from Parameters class.

Move configuration to after the construction.

Simplify observers and endpoints.

Remove superfluous configureEntity overrides.

Add dependency on fest-swing-testng.

Use FEST for final print test.

Use FEST for more concise and readable assertions.

Divide test of F0Parser into multiple methods.

Migrate to TestNG

Minor change.

Change convention from LOGGER to log.

Fix reporting of errors during controls filling.

Bound maximal height of SliderControl?.

Minor improvements.

Improve tooltips for controls.

Also use Delimeted in more places.

Move static control utilities to Gui.

Rename package gui.components to controls.

Some cleaning in controls.

Improve Param classes placing.

Move ValueParam?, PrimitiveParam? and CompositeParam? one package up.

Improve ParamBuilder?.

Move getDef to ValueParam? and PrimitiveParam?.

Move getMax and getDef to ValueParam?.

Move getMin to ValueParam?.

Upgrade to laters apache commons versions.

Use filterInstanceof extensively.

Add instanceof filters.

Make ValueParam? in many places of Param.

Place assertions about ValueParam?.

Add ValueParam?

Rename ValueParam? to PrimitiveParam?

Minor changes.

Several improvements to params types.

Add NumberParam?.

Add TextControl? component.

Add .swp files to .gitignore

Greatly improved slider component.

Some improvements.

Make Param.reassign return also a state.

Add IterableIterator?.

Several changes.

  • Move util classes to better packages.
  • Remove warnings from eclim.

Several improvements.

Fix bug with BooleanParam?.

Some experiments with visualization.

Another fix to panel management.

Improve panel management.

Some refactorization around panels.

Add root class for panel.

File size: 4.1 KB
Line 
1package com.framsticks.params;
2
3import java.lang.reflect.Field;
4import java.lang.reflect.InvocationTargetException;
5import java.lang.reflect.Modifier;
6
7import org.apache.log4j.Logger;
8
9import com.framsticks.util.lang.Containers;
10
11
12/**
13 * The Class ReflectionAccess. Stores data in provided object using reflection.
14 *
15 * @author Mateusz Jarus <name.surname@gmail.com> (please replace name and
16 *         surname with my personal data)
17 *
18 * @author Piotr Sniegowski
19 */
20public class ReflectionAccess extends SimpleAbstractAccess {
21        private final static Logger log = Logger.getLogger(ReflectionAccess.class.getName());
22
23        protected final Class<?> reflectedClass;
24        private Object object;
25
26        public ReflectionAccess(Class<?> reflectedClass, FramsClass framsClass) {
27                this.reflectedClass = reflectedClass;
28                setFramsClass(framsClass);
29        }
30
31        private static String accessorName(boolean get, String id) {
32                return (get ? "get"  : "set") + id.substring(0, 1).toUpperCase() + id.substring(1);
33        }
34
35        @Override
36        public <T> T get(ValueParam param, Class<T> type) {
37                if (object == null) {
38                        return null;
39                }
40                try {
41                        //TODO: use internal id, if present
42                        String id = param.getId();
43                        try {
44                                return type.cast(reflectedClass.getField(id).get(object));
45                        } catch (NoSuchFieldException ignored) {
46                        }
47                        try {
48                                return type.cast(reflectedClass.getMethod(accessorName(true, id)).invoke(object));
49                        } catch (NoSuchMethodException ex) {
50                                //ex.printStackTrace();
51                        } catch (InvocationTargetException e) {
52                                //e.printStackTrace();
53                        }
54
55                } catch (IllegalAccessException ex) {
56                        log.warn("illegal access error occurred while trying to access returnedObject");
57                        ex.printStackTrace();
58                } catch (ClassCastException ignored) {
59
60                }
61                return null;
62        }
63
64        @Override
65        protected <T> void internalSet(ValueParam param, T value) {
66                setValue(param, value);
67        }
68
69        private <T> void setValue(ValueParam param, T value) {
70                if (object == null) {
71                        return;
72                }
73                try {
74                        String id = param.getId();
75                        try {
76                                Field f = reflectedClass.getField(id);
77                                Class<?> t = f.getType();
78                                if (Modifier.isFinal(f.getModifiers())) {
79                                        return;
80                                }
81                                if (value != null || (!t.isPrimitive())) {
82                                        f.set(object, value);
83                                }
84                                return;
85                        } catch (NoSuchFieldException ignored) {
86                        }
87                        try {
88                                reflectedClass.getMethod(accessorName(false, id), new Class[]{param.getStorageType()}).invoke(object, value);
89                        } catch (InvocationTargetException ignored) {
90                        } catch (NoSuchMethodException ignored) {
91                        }
92                } catch (Exception ex) {
93                        ex.printStackTrace();
94                }
95        }
96
97        void resetErrors() {
98                //TODO this replaces returnedObject.resetErrors();
99        }
100
101        @Override
102        public void clearValues() {
103                if (object == null) {
104                        return;
105                }
106
107                resetErrors();
108
109                try {
110                        for (ValueParam p : Containers.filterInstanceof(framsClass.getParamEntries(), ValueParam.class)) {
111                                setValue(p, p.getDef(Object.class));
112                        }
113                } catch (IllegalArgumentException ex) {
114                        ex.printStackTrace();
115                }
116        }
117
118        /**
119         * Sets the new object to operate on.
120         *
121         * @param object
122         *            new object to operate on
123         */
124        @Override
125        public ReflectionAccess select(Object object) {
126                assert object == null || reflectedClass.isInstance(object);
127                this.object = object;
128                return this;
129        }
130
131        @Override
132        public Object getSelected() {
133                return object;
134        }
135
136        // TODO: find a better place for it
137        public static String objectToString(Object object) {
138                StringBuilder b = new StringBuilder();
139                for (Field f : object.getClass().getFields()) {
140                        b.append(f.getName());
141                        b.append(":");
142                        try {
143                                Object value = f.get(object);
144                                b.append((value != null) ? value.toString() : "<null>");
145                        } catch (IllegalAccessException e) {
146                                e.printStackTrace();
147                        }
148                        b.append("\n");
149                }
150                return b.toString();
151        }
152
153
154        @Override
155        public ReflectionAccess cloneAccess() {
156                return new ReflectionAccess(reflectedClass, framsClass);
157        }
158
159        @Override
160        public Object createAccessee() {
161                try {
162                        return reflectedClass.newInstance();
163                } catch (InstantiationException e) {
164                        e.printStackTrace();
165                } catch (IllegalAccessException e) {
166                        e.printStackTrace();
167                }
168                log.fatal("failed to create reflected object of class " + reflectedClass.getCanonicalName() + " for frams type " + framsClass.getId());
169                return null;
170        }
171}
Note: See TracBrowser for help on using the repository browser.