[77] | 1 | package com.framsticks.params; |
---|
| 2 | |
---|
| 3 | import java.lang.reflect.Field; |
---|
| 4 | import java.lang.reflect.InvocationTargetException; |
---|
[86] | 5 | import java.lang.reflect.Method; |
---|
[90] | 6 | import java.util.ArrayList; |
---|
[87] | 7 | import java.util.Collections; |
---|
[90] | 8 | import java.util.Comparator; |
---|
[86] | 9 | import java.util.HashMap; |
---|
[88] | 10 | import java.util.List; |
---|
[86] | 11 | import java.util.Map; |
---|
[77] | 12 | |
---|
[87] | 13 | import javax.annotation.concurrent.Immutable; |
---|
| 14 | |
---|
[77] | 15 | import org.apache.log4j.Logger; |
---|
| 16 | |
---|
[87] | 17 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
[90] | 18 | import com.framsticks.params.types.ProcedureParam; |
---|
[86] | 19 | import com.framsticks.util.FramsticksException; |
---|
[87] | 20 | import com.framsticks.util.lang.Pair; |
---|
[77] | 21 | |
---|
[86] | 22 | import static com.framsticks.util.lang.Containers.*; |
---|
[84] | 23 | |
---|
[77] | 24 | /** |
---|
| 25 | * The Class ReflectionAccess. Stores data in provided object using reflection. |
---|
[84] | 26 | * |
---|
[77] | 27 | * @author Mateusz Jarus <name.surname@gmail.com> (please replace name and |
---|
| 28 | * surname with my personal data) |
---|
| 29 | * |
---|
| 30 | * @author Piotr Sniegowski |
---|
| 31 | */ |
---|
| 32 | public class ReflectionAccess extends SimpleAbstractAccess { |
---|
[87] | 33 | private final static Logger log = Logger.getLogger(ReflectionAccess.class.getName()); |
---|
[77] | 34 | |
---|
[90] | 35 | protected final Class<?> javaClass; |
---|
[87] | 36 | protected final Backend backend; |
---|
| 37 | |
---|
[77] | 38 | private Object object; |
---|
| 39 | |
---|
[87] | 40 | @Immutable |
---|
| 41 | public static class Backend { |
---|
[86] | 42 | |
---|
[88] | 43 | protected static final Map<Pair<Class<?>, FramsClass>, Backend> synchronizedCache = Collections.synchronizedMap(new HashMap<Pair<Class<?>, FramsClass>, Backend>()); |
---|
[86] | 44 | |
---|
[90] | 45 | |
---|
| 46 | public interface ReflectedGetter { |
---|
| 47 | public <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; |
---|
[87] | 48 | } |
---|
[86] | 49 | |
---|
[87] | 50 | public interface ReflectedSetter { |
---|
| 51 | public <T> void set(Object object, T value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; |
---|
| 52 | } |
---|
| 53 | |
---|
[90] | 54 | public interface ReflectedCaller { |
---|
| 55 | public Object call(Object object, Object[] arguments) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; |
---|
[87] | 56 | } |
---|
| 57 | |
---|
[90] | 58 | protected final Map<ValueParam, ReflectedSetter> setters = new HashMap<>(); |
---|
| 59 | protected final Map<ValueParam, ReflectedGetter> getters = new HashMap<>(); |
---|
| 60 | protected final Map<ProcedureParam, ReflectedCaller> callers = new HashMap<>(); |
---|
| 61 | protected final List<Method> autoAppendMethods = new ArrayList<>(); |
---|
[87] | 62 | |
---|
| 63 | /** |
---|
| 64 | * @param params |
---|
| 65 | */ |
---|
[90] | 66 | public Backend() { |
---|
[87] | 67 | } |
---|
| 68 | |
---|
| 69 | public static Backend getOrCreateFor(Class<?> reflectedClass, FramsClass framsClass) { |
---|
| 70 | |
---|
| 71 | Pair<Class<?>, FramsClass> id = new Pair<Class<?>, FramsClass>(reflectedClass, framsClass); |
---|
| 72 | Backend backend = synchronizedCache.get(id); |
---|
| 73 | if (backend != null) { |
---|
| 74 | return backend; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | log.debug("constructing backend for " + id); |
---|
[90] | 78 | backend = new Backend(); |
---|
[87] | 79 | |
---|
[90] | 80 | Map<String, ParamCandidate> candidates = ParamCandidate.getAllCandidates(reflectedClass).getCandidates(); |
---|
[87] | 81 | |
---|
| 82 | try { |
---|
[90] | 83 | for (final ProcedureParam pp : filterInstanceof(framsClass.getParamEntries(), ProcedureParam.class)) { |
---|
| 84 | if (!candidates.containsKey(pp.getId())) { |
---|
| 85 | log.trace("java class does implement method " + pp); |
---|
| 86 | continue; |
---|
| 87 | } |
---|
| 88 | ParamCandidate pc = candidates.get(pp.getId()); |
---|
| 89 | final Method method = pc.getCaller(); |
---|
| 90 | |
---|
| 91 | backend.callers.put(pp, new ReflectedCaller() { |
---|
| 92 | |
---|
| 93 | @Override |
---|
| 94 | public Object call(Object object, Object[] arguments) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { |
---|
| 95 | return method.invoke(object, arguments); |
---|
| 96 | } |
---|
| 97 | }); |
---|
| 98 | |
---|
| 99 | } |
---|
| 100 | |
---|
[87] | 101 | for (final ValueParam vp : filterInstanceof(framsClass.getParamEntries(), ValueParam.class)) { |
---|
| 102 | if (!candidates.containsKey(vp.getId())) { |
---|
| 103 | throw new ConstructionException().msg("missing candidate for param").arg("param", vp); |
---|
| 104 | } |
---|
| 105 | ParamCandidate pc = candidates.get(vp.getId()); |
---|
| 106 | if (pc.isReadOnly() && !vp.hasFlag(Flags.READONLY)) { |
---|
| 107 | throw new ConstructionException().msg("readonly state conflict").arg("param", vp); |
---|
| 108 | } |
---|
| 109 | if (!typeMatch(pc.getRawType(), vp.getStorageType())) { |
---|
| 110 | throw new ConstructionException().msg("types mismatch for param").arg("param", vp).arg("candidate", pc.getType()).arg("storage", vp.getStorageType()); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | final boolean primitive = pc.isPrimitive(); |
---|
| 114 | if (pc.getField() != null) { |
---|
| 115 | final Field f = pc.getField(); |
---|
[90] | 116 | backend.getters.put(vp, new ReflectedGetter() { |
---|
[87] | 117 | @Override |
---|
| 118 | public <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException { |
---|
| 119 | return type.cast(f.get(object)); |
---|
| 120 | } |
---|
[90] | 121 | }); |
---|
[87] | 122 | if (!pc.isFinal()) { |
---|
[90] | 123 | backend.setters.put(vp, new ReflectedSetter() { |
---|
[87] | 124 | @Override |
---|
| 125 | public <T> void set(Object object, T value) throws IllegalArgumentException, IllegalAccessException { |
---|
| 126 | if (value == null && primitive) { |
---|
| 127 | throw new FramsticksException().msg("setting null to primitive value"); |
---|
| 128 | } |
---|
| 129 | f.set(object, value); |
---|
| 130 | } |
---|
[90] | 131 | }); |
---|
[87] | 132 | } |
---|
| 133 | } else { |
---|
| 134 | final Method g = pc.getGetter(); |
---|
| 135 | |
---|
[90] | 136 | backend.getters.put(vp, new ReflectedGetter() { |
---|
[87] | 137 | @Override |
---|
| 138 | public <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { |
---|
| 139 | return type.cast(g.invoke(object)); |
---|
| 140 | } |
---|
[90] | 141 | }); |
---|
[87] | 142 | |
---|
| 143 | if (!pc.isFinal()) { |
---|
| 144 | final Method s = pc.getSetter(); |
---|
[90] | 145 | backend.setters.put(vp, new ReflectedSetter() { |
---|
[87] | 146 | @Override |
---|
| 147 | public <T> void set(Object object, T value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { |
---|
| 148 | if (value == null && primitive) { |
---|
| 149 | throw new FramsticksException().msg("setting null to primitive value"); |
---|
| 150 | } |
---|
| 151 | s.invoke(object, value); |
---|
| 152 | } |
---|
[90] | 153 | }); |
---|
[87] | 154 | } |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | } catch (ConstructionException e) { |
---|
| 158 | throw e.arg("java class", reflectedClass).arg("framsClass", framsClass); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | Class<?> javaClass = reflectedClass; |
---|
| 162 | while (javaClass != null) { |
---|
| 163 | |
---|
| 164 | for (Method m : javaClass.getDeclaredMethods()) { |
---|
| 165 | AutoAppendAnnotation a = m.getAnnotation(AutoAppendAnnotation.class); |
---|
| 166 | if (a == null) { |
---|
| 167 | continue; |
---|
| 168 | } |
---|
| 169 | Class<?>[] args = m.getParameterTypes(); |
---|
| 170 | if (args.length != 1) { |
---|
| 171 | throw new ConstructionException().msg("invalid number of arguments in AutoAppend marked method").arg("method", m).arg("arguments", args.length); |
---|
| 172 | } |
---|
[90] | 173 | backend.autoAppendMethods.add(m); |
---|
[87] | 174 | } |
---|
| 175 | |
---|
| 176 | javaClass = javaClass.getSuperclass(); |
---|
| 177 | } |
---|
| 178 | |
---|
[90] | 179 | Collections.sort(backend.autoAppendMethods, new Comparator<Method>() { |
---|
| 180 | |
---|
| 181 | @Override |
---|
| 182 | public int compare(Method m0, Method m1) { |
---|
| 183 | Class<?> arg0 = m0.getParameterTypes()[0]; |
---|
| 184 | Class<?> arg1 = m1.getParameterTypes()[0]; |
---|
| 185 | if (arg0.isAssignableFrom(arg1)) { |
---|
| 186 | return 1; |
---|
| 187 | } |
---|
| 188 | if (arg1.isAssignableFrom(arg0)) { |
---|
| 189 | return -1; |
---|
| 190 | } |
---|
| 191 | return 0; |
---|
| 192 | } |
---|
| 193 | }); |
---|
| 194 | |
---|
[87] | 195 | synchronizedCache.put(id, backend); |
---|
| 196 | return backend; |
---|
| 197 | } |
---|
| 198 | |
---|
[86] | 199 | } |
---|
| 200 | |
---|
| 201 | public ReflectionAccess(Class<?> reflectedClass) throws ConstructionException { |
---|
[87] | 202 | this(reflectedClass, FramsClass.build().forClass(reflectedClass)); |
---|
[86] | 203 | } |
---|
| 204 | |
---|
| 205 | public static boolean typeMatch(Class<?> a, Class<?> b) { |
---|
| 206 | assert !b.isPrimitive(); |
---|
| 207 | if (!a.isPrimitive()) { |
---|
| 208 | return a.equals(b); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | if (a.equals(int.class)) { |
---|
| 212 | return b.equals(Integer.class); |
---|
| 213 | } |
---|
| 214 | if (a.equals(double.class)) { |
---|
| 215 | return b.equals(Double.class); |
---|
| 216 | } |
---|
| 217 | if (a.equals(boolean.class)) { |
---|
| 218 | return b.equals(Boolean.class); |
---|
| 219 | } |
---|
| 220 | assert false; |
---|
| 221 | return false; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | public ReflectionAccess(Class<?> reflectedClass, FramsClass framsClass) throws ConstructionException { |
---|
[87] | 225 | super(framsClass); |
---|
[90] | 226 | this.javaClass = reflectedClass; |
---|
[87] | 227 | this.backend = Backend.getOrCreateFor(reflectedClass, framsClass); |
---|
[77] | 228 | } |
---|
| 229 | |
---|
[86] | 230 | |
---|
[77] | 231 | @Override |
---|
[84] | 232 | public <T> T get(ValueParam param, Class<T> type) { |
---|
[77] | 233 | try { |
---|
| 234 | try { |
---|
[86] | 235 | if (object == null) { |
---|
| 236 | throw new FramsticksException().msg("no object set"); |
---|
| 237 | } |
---|
| 238 | |
---|
[90] | 239 | return backend.getters.get(param).get(object, type); |
---|
[86] | 240 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
---|
| 241 | throw new FramsticksException().msg("failed to get").cause(e); |
---|
[77] | 242 | } |
---|
[86] | 243 | } catch (FramsticksException e) { |
---|
| 244 | throw e.arg("param", param).arg("type", type).arg("access", this); |
---|
[84] | 245 | } |
---|
[77] | 246 | } |
---|
| 247 | |
---|
| 248 | @Override |
---|
[84] | 249 | protected <T> void internalSet(ValueParam param, T value) { |
---|
[77] | 250 | setValue(param, value); |
---|
| 251 | } |
---|
| 252 | |
---|
[84] | 253 | private <T> void setValue(ValueParam param, T value) { |
---|
[77] | 254 | try { |
---|
| 255 | try { |
---|
[86] | 256 | if (object == null) { |
---|
| 257 | throw new FramsticksException().msg("no object set"); |
---|
[78] | 258 | } |
---|
[90] | 259 | Backend.ReflectedSetter s = backend.setters.get(param); |
---|
[86] | 260 | if (s == null) { |
---|
[90] | 261 | throw new FramsticksException().msg("trying to set unsettable"); |
---|
[78] | 262 | } |
---|
[86] | 263 | s.set(object, value); |
---|
| 264 | } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { |
---|
| 265 | throw new FramsticksException().msg("failed to set").cause(e); |
---|
[77] | 266 | } |
---|
[86] | 267 | } catch (FramsticksException e) { |
---|
| 268 | throw e.arg("param", param).arg("value", value).arg("access", this); |
---|
[77] | 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|
[90] | 272 | @Override |
---|
| 273 | public Object call(String id, Object[] arguments) { |
---|
| 274 | return call(framsClass.getParamEntry(id, ProcedureParam.class), arguments); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | @Override |
---|
| 278 | public Object call(ProcedureParam param, Object[] arguments) { |
---|
| 279 | try { |
---|
| 280 | try { |
---|
| 281 | if (object == null) { |
---|
| 282 | throw new FramsticksException().msg("no object set"); |
---|
| 283 | } |
---|
| 284 | Backend.ReflectedCaller c = backend.callers.get(param); |
---|
| 285 | if (c == null) { |
---|
| 286 | throw new FramsticksException().msg("method is not bound"); |
---|
| 287 | } |
---|
| 288 | return c.call(object, arguments); |
---|
| 289 | } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { |
---|
| 290 | throw new FramsticksException().msg("failed to call").cause(e); |
---|
| 291 | } |
---|
| 292 | } catch (FramsticksException e) { |
---|
| 293 | throw e.arg("param", param).arg("access", this); |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | |
---|
[77] | 297 | void resetErrors() { |
---|
| 298 | //TODO this replaces returnedObject.resetErrors(); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | @Override |
---|
| 302 | public void clearValues() { |
---|
| 303 | if (object == null) { |
---|
| 304 | return; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | resetErrors(); |
---|
| 308 | |
---|
| 309 | try { |
---|
[86] | 310 | for (ValueParam p : filterInstanceof(framsClass.getParamEntries(), ValueParam.class)) { |
---|
[78] | 311 | setValue(p, p.getDef(Object.class)); |
---|
[77] | 312 | } |
---|
[78] | 313 | } catch (IllegalArgumentException ex) { |
---|
[77] | 314 | ex.printStackTrace(); |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | /** |
---|
| 319 | * Sets the new object to operate on. |
---|
[84] | 320 | * |
---|
[77] | 321 | * @param object |
---|
| 322 | * new object to operate on |
---|
| 323 | */ |
---|
| 324 | @Override |
---|
[84] | 325 | public ReflectionAccess select(Object object) { |
---|
[90] | 326 | assert object == null || javaClass.isInstance(object); |
---|
[77] | 327 | this.object = object; |
---|
[84] | 328 | return this; |
---|
[77] | 329 | } |
---|
| 330 | |
---|
| 331 | @Override |
---|
| 332 | public Object getSelected() { |
---|
| 333 | return object; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | // TODO: find a better place for it |
---|
| 337 | public static String objectToString(Object object) { |
---|
| 338 | StringBuilder b = new StringBuilder(); |
---|
| 339 | for (Field f : object.getClass().getFields()) { |
---|
| 340 | b.append(f.getName()); |
---|
| 341 | b.append(":"); |
---|
| 342 | try { |
---|
| 343 | Object value = f.get(object); |
---|
| 344 | b.append((value != null) ? value.toString() : "<null>"); |
---|
| 345 | } catch (IllegalAccessException e) { |
---|
| 346 | e.printStackTrace(); |
---|
| 347 | } |
---|
| 348 | b.append("\n"); |
---|
| 349 | } |
---|
| 350 | return b.toString(); |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | @Override |
---|
[86] | 354 | public ReflectionAccess cloneAccess() throws ConstructionException { |
---|
[90] | 355 | return new ReflectionAccess(javaClass, framsClass); |
---|
[77] | 356 | } |
---|
| 357 | |
---|
[84] | 358 | @Override |
---|
| 359 | public Object createAccessee() { |
---|
| 360 | try { |
---|
[90] | 361 | return javaClass.newInstance(); |
---|
[85] | 362 | } catch (InstantiationException | IllegalAccessException e) { |
---|
[84] | 363 | e.printStackTrace(); |
---|
| 364 | } |
---|
[90] | 365 | log.fatal("failed to create reflected object of class " + javaClass.getCanonicalName() + " for frams type " + framsClass.getId()); |
---|
[84] | 366 | return null; |
---|
| 367 | } |
---|
[86] | 368 | |
---|
| 369 | @Override |
---|
| 370 | public String toString() { |
---|
| 371 | StringBuilder b = new StringBuilder(); |
---|
| 372 | b.append(framsClass); |
---|
| 373 | if (object != null) { |
---|
| 374 | b.append("(").append(object).append(")"); |
---|
| 375 | } |
---|
| 376 | return b.toString(); |
---|
| 377 | } |
---|
| 378 | |
---|
[87] | 379 | @Override |
---|
| 380 | public boolean tryAutoAppend(Object value) { |
---|
| 381 | assert object != null; |
---|
[88] | 382 | for (Method m : backend.autoAppendMethods) { |
---|
| 383 | if (m.getParameterTypes()[0].isAssignableFrom(value.getClass())) { |
---|
[87] | 384 | try { |
---|
[90] | 385 | log.trace("auto appending with value " + value + " with method " + m); |
---|
[88] | 386 | m.invoke(object, value); |
---|
[87] | 387 | return true; |
---|
[88] | 388 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | FramsticksException e) { |
---|
| 389 | throw new FramsticksException().msg("failed to auto append").cause(e).arg("value", value).arg("into object", object).arg("with method", m); |
---|
[87] | 390 | } |
---|
| 391 | } |
---|
| 392 | } |
---|
| 393 | return false; |
---|
| 394 | } |
---|
[90] | 395 | |
---|
[77] | 396 | } |
---|
[86] | 397 | |
---|