- Timestamp:
- 07/10/13 22:41:02 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/ReflectionAccess.java
r98 r99 17 17 18 18 import com.framsticks.params.annotations.AutoAppendAnnotation; 19 import com.framsticks.params.types.EventParam; 19 20 import com.framsticks.params.types.ProcedureParam; 20 21 import com.framsticks.util.FramsticksException; … … 57 58 } 58 59 60 public interface ReflectedAdder{ 61 public void reg(Object object, EventListener<?> listener) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; 62 } 63 64 public interface ReflectedRemover{ 65 public void regRemove(Object object, EventListener<?> listener) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; 66 } 67 59 68 protected final Map<ValueParam, ReflectedSetter> setters = new IdentityHashMap<>(); 60 69 protected final Map<ValueParam, ReflectedGetter> getters = new IdentityHashMap<>(); 61 70 protected final Map<ProcedureParam, ReflectedCaller> callers = new IdentityHashMap<>(); 71 protected final Map<EventParam, ReflectedAdder> adders = new IdentityHashMap<>(); 72 protected final Map<EventParam, ReflectedRemover> removers = new IdentityHashMap<>(); 73 62 74 protected final List<Method> autoAppendMethods = new ArrayList<>(); 63 75 … … 100 112 } 101 113 114 for (final EventParam ep : filterInstanceof(framsClass.getParamEntries(), EventParam.class)) { 115 if (!candidates.containsKey(ep.getId())) { 116 log.trace("java class does not implement the event param " + ep); 117 continue; 118 } 119 ParamCandidate ec = candidates.get(ep.getId()); 120 final Method adder = ec.getAdder(); 121 final Method remover = ec.getRemover(); 122 123 backend.adders.put(ep, new ReflectedAdder() { 124 125 @Override 126 public void reg(Object object, EventListener<?> listener) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { 127 adder.invoke(object, listener); 128 } 129 }); 130 131 backend.removers.put(ep, new ReflectedRemover() { 132 133 @Override 134 public void regRemove(Object object, EventListener<?> listener) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { 135 remover.invoke(object, listener); 136 } 137 }); 138 } 139 102 140 for (final ValueParam vp : filterInstanceof(framsClass.getParamEntries(), ValueParam.class)) { 103 141 if (!candidates.containsKey(vp.getId())) { … … 105 143 } 106 144 ParamCandidate pc = candidates.get(vp.getId()); 107 if (pc.isReadOnly() && !vp.hasFlag( Flags.READONLY)) {145 if (pc.isReadOnly() && !vp.hasFlag(ParamFlags.READONLY)) { 108 146 throw new ConstructionException().msg("readonly state conflict").arg("param", vp); 109 147 } … … 200 238 } 201 239 202 public ReflectionAccess(Class<?> reflectedClass) throws ConstructionException {203 this(reflectedClass, FramsClass.build().forClass(reflectedClass));204 }205 206 240 public static boolean typeMatch(Class<?> a, Class<?> b) { 207 assert !b.isPrimitive(); 241 if (b.isPrimitive()) { 242 throw new FramsticksException().msg("failed to match type, right argument is primitive").arg("left", a).arg("right", b); 243 } 208 244 if (!a.isPrimitive()) { 209 245 return a.equals(b); … … 219 255 return b.equals(Boolean.class); 220 256 } 221 assert false; 222 return false; 223 } 224 225 public ReflectionAccess(Class<?> reflectedClass, FramsClass framsClass) throws ConstructionException { 257 throw new FramsticksException().msg("failed to match types").arg("left", a).arg("right", b); 258 } 259 260 261 262 263 public ReflectionAccess(Class<?> javaClass) throws ConstructionException { 264 this(javaClass, FramsClass.build().forClass(javaClass)); 265 } 266 267 268 public ReflectionAccess(Class<?> javaClass, FramsClass framsClass) throws ConstructionException { 269 this(javaClass, framsClass, Backend.getOrCreateFor(javaClass, framsClass)); 270 } 271 272 protected ReflectionAccess(Class<?> javaClass, FramsClass framsClass, Backend backend) throws ConstructionException { 226 273 super(framsClass); 227 this.javaClass = reflectedClass; 228 this.backend = Backend.getOrCreateFor(reflectedClass, framsClass); 229 } 230 274 this.javaClass = javaClass; 275 this.backend = backend; 276 } 277 278 @Override 279 public ReflectionAccess cloneAccess() throws ConstructionException { 280 return new ReflectionAccess(javaClass, framsClass, backend); 281 } 231 282 232 283 @Override … … 268 319 } catch (FramsticksException e) { 269 320 throw e.arg("param", param).arg("value", value).arg("access", this); 321 } 322 } 323 324 @Override 325 public void reg(EventParam param, EventListener<?> listener) { 326 try { 327 try { 328 if (object == null) { 329 throw new FramsticksException().msg("no object set"); 330 } 331 332 backend.adders.get(param).reg(object, listener); 333 return; 334 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 335 throw new FramsticksException().msg("failed to add listener").cause(e); 336 } 337 } catch (FramsticksException e) { 338 throw e.arg("param", param).arg("access", this); 339 } 340 } 341 342 @Override 343 public void regRemove(EventParam param, EventListener<?> listener) { 344 try { 345 try { 346 if (object == null) { 347 throw new FramsticksException().msg("no object set"); 348 } 349 350 backend.removers.get(param).regRemove(object, listener); 351 return; 352 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 353 throw new FramsticksException().msg("failed to remove listener").cause(e); 354 } 355 } catch (FramsticksException e) { 356 throw e.arg("param", param).arg("access", this); 270 357 } 271 358 } … … 352 439 } 353 440 354 @Override355 public ReflectionAccess cloneAccess() throws ConstructionException {356 return new ReflectionAccess(javaClass, framsClass);357 }358 441 359 442 @Override … … 368 451 } 369 452 370 @Override371 public String toString() {372 StringBuilder b = new StringBuilder();373 b.append(framsClass);374 if (object != null) {375 b.append("(").append(object).append(")");376 }377 return b.toString();378 }379 453 380 454 @Override
Note: See TracChangeset
for help on using the changeset viewer.