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

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

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

File size: 4.4 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 | InvocationTargetException ex) {
50                                //e.printStackTrace();
51                        }
52                        try {
53                                return type.cast(reflectedClass.getMethod(id).invoke(object));
54                        } catch (NoSuchMethodException | InvocationTargetException ex) {
55                                //e.printStackTrace();
56                        }
57
58                } catch (IllegalAccessException e) {
59                        log.warn("illegal access error occurred while trying to access returnedObject");
60                        e.printStackTrace();
61                } catch (ClassCastException ignored) {
62
63                }
64                return null;
65        }
66
67        @Override
68        protected <T> void internalSet(ValueParam param, T value) {
69                setValue(param, value);
70        }
71
72        private <T> void setValue(ValueParam param, T value) {
73                if (object == null) {
74                        return;
75                }
76                try {
77                        String id = param.getId();
78                        try {
79                                Field f = reflectedClass.getField(id);
80                                Class<?> t = f.getType();
81                                if (Modifier.isFinal(f.getModifiers())) {
82                                        return;
83                                }
84                                if (value != null || (!t.isPrimitive())) {
85                                        f.set(object, value);
86                                }
87                                return;
88                        } catch (NoSuchFieldException ignored) {
89                        }
90                        try {
91                                reflectedClass.getMethod(accessorName(false, id), new Class[]{param.getStorageType()}).invoke(object, value);
92                        } catch (InvocationTargetException | NoSuchMethodException ignored) {
93                        }
94                        try {
95                                reflectedClass.getMethod(id, new Class[]{param.getStorageType()}).invoke(object, value);
96                        } catch (InvocationTargetException | NoSuchMethodException ignored) {
97                        }
98                } catch (Exception ex) {
99                        ex.printStackTrace();
100                }
101        }
102
103        void resetErrors() {
104                //TODO this replaces returnedObject.resetErrors();
105        }
106
107        @Override
108        public void clearValues() {
109                if (object == null) {
110                        return;
111                }
112
113                resetErrors();
114
115                try {
116                        for (ValueParam p : Containers.filterInstanceof(framsClass.getParamEntries(), ValueParam.class)) {
117                                setValue(p, p.getDef(Object.class));
118                        }
119                } catch (IllegalArgumentException ex) {
120                        ex.printStackTrace();
121                }
122        }
123
124        /**
125         * Sets the new object to operate on.
126         *
127         * @param object
128         *            new object to operate on
129         */
130        @Override
131        public ReflectionAccess select(Object object) {
132                assert object == null || reflectedClass.isInstance(object);
133                this.object = object;
134                return this;
135        }
136
137        @Override
138        public Object getSelected() {
139                return object;
140        }
141
142        // TODO: find a better place for it
143        public static String objectToString(Object object) {
144                StringBuilder b = new StringBuilder();
145                for (Field f : object.getClass().getFields()) {
146                        b.append(f.getName());
147                        b.append(":");
148                        try {
149                                Object value = f.get(object);
150                                b.append((value != null) ? value.toString() : "<null>");
151                        } catch (IllegalAccessException e) {
152                                e.printStackTrace();
153                        }
154                        b.append("\n");
155                }
156                return b.toString();
157        }
158
159
160        @Override
161        public ReflectionAccess cloneAccess() {
162                return new ReflectionAccess(reflectedClass, framsClass);
163        }
164
165        @Override
166        public Object createAccessee() {
167                try {
168                        return reflectedClass.newInstance();
169                } catch (InstantiationException | IllegalAccessException e) {
170                        e.printStackTrace();
171                }
172                log.fatal("failed to create reflected object of class " + reflectedClass.getCanonicalName() + " for frams type " + framsClass.getId());
173                return null;
174        }
175}
Note: See TracBrowser for help on using the repository browser.