[77] | 1 | package com.framsticks.params; |
---|
| 2 | |
---|
| 3 | import java.lang.reflect.InvocationTargetException; |
---|
[86] | 4 | import java.lang.reflect.Method; |
---|
[77] | 5 | |
---|
[87] | 6 | |
---|
[100] | 7 | import org.apache.logging.log4j.Logger; |
---|
| 8 | import org.apache.logging.log4j.LogManager; |
---|
[77] | 9 | |
---|
[99] | 10 | import com.framsticks.params.types.EventParam; |
---|
[90] | 11 | import com.framsticks.params.types.ProcedureParam; |
---|
[86] | 12 | import com.framsticks.util.FramsticksException; |
---|
[107] | 13 | import com.framsticks.util.Misc; |
---|
[77] | 14 | |
---|
[86] | 15 | import static com.framsticks.util.lang.Containers.*; |
---|
[84] | 16 | |
---|
[77] | 17 | /** |
---|
| 18 | * The Class ReflectionAccess. Stores data in provided object using reflection. |
---|
[84] | 19 | * |
---|
[77] | 20 | * @author Mateusz Jarus <name.surname@gmail.com> (please replace name and |
---|
| 21 | * surname with my personal data) |
---|
| 22 | * |
---|
| 23 | * @author Piotr Sniegowski |
---|
| 24 | */ |
---|
| 25 | public class ReflectionAccess extends SimpleAbstractAccess { |
---|
[100] | 26 | private final static Logger log = LogManager.getLogger(ReflectionAccess.class.getName()); |
---|
[77] | 27 | |
---|
[90] | 28 | protected final Class<?> javaClass; |
---|
[101] | 29 | protected final ReflectionAccessBackend backend; |
---|
[87] | 30 | |
---|
[77] | 31 | private Object object; |
---|
| 32 | |
---|
[99] | 33 | public ReflectionAccess(Class<?> javaClass) throws ConstructionException { |
---|
| 34 | this(javaClass, FramsClass.build().forClass(javaClass)); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | public ReflectionAccess(Class<?> javaClass, FramsClass framsClass) throws ConstructionException { |
---|
[101] | 39 | this(javaClass, framsClass, ReflectionAccessBackend.getOrCreateFor(javaClass, framsClass)); |
---|
[99] | 40 | } |
---|
| 41 | |
---|
[101] | 42 | protected ReflectionAccess(Class<?> javaClass, FramsClass framsClass, ReflectionAccessBackend backend) throws ConstructionException { |
---|
[87] | 43 | super(framsClass); |
---|
[99] | 44 | this.javaClass = javaClass; |
---|
| 45 | this.backend = backend; |
---|
[77] | 46 | } |
---|
| 47 | |
---|
[99] | 48 | @Override |
---|
[101] | 49 | public ReflectionAccess cloneAccess() { |
---|
[99] | 50 | return new ReflectionAccess(javaClass, framsClass, backend); |
---|
| 51 | } |
---|
[86] | 52 | |
---|
[77] | 53 | @Override |
---|
[84] | 54 | public <T> T get(ValueParam param, Class<T> type) { |
---|
[77] | 55 | try { |
---|
| 56 | try { |
---|
[86] | 57 | if (object == null) { |
---|
| 58 | throw new FramsticksException().msg("no object set"); |
---|
| 59 | } |
---|
| 60 | |
---|
[101] | 61 | return backend.getters.get(param.getId()).get(object, type); |
---|
[86] | 62 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
---|
| 63 | throw new FramsticksException().msg("failed to get").cause(e); |
---|
[77] | 64 | } |
---|
[86] | 65 | } catch (FramsticksException e) { |
---|
| 66 | throw e.arg("param", param).arg("type", type).arg("access", this); |
---|
[84] | 67 | } |
---|
[77] | 68 | } |
---|
| 69 | |
---|
| 70 | @Override |
---|
[84] | 71 | protected <T> void internalSet(ValueParam param, T value) { |
---|
[77] | 72 | setValue(param, value); |
---|
| 73 | } |
---|
| 74 | |
---|
[84] | 75 | private <T> void setValue(ValueParam param, T value) { |
---|
[77] | 76 | try { |
---|
| 77 | try { |
---|
[86] | 78 | if (object == null) { |
---|
| 79 | throw new FramsticksException().msg("no object set"); |
---|
[78] | 80 | } |
---|
[101] | 81 | ReflectionAccessBackend.ReflectedSetter s = backend.setters.get(param.getId()); |
---|
[86] | 82 | if (s == null) { |
---|
[90] | 83 | throw new FramsticksException().msg("trying to set unsettable"); |
---|
[100] | 84 | // return; |
---|
| 85 | // if (value != backend.getters.get(param).get(object, Object.class)) { |
---|
| 86 | // } |
---|
[78] | 87 | } |
---|
[86] | 88 | s.set(object, value); |
---|
| 89 | } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { |
---|
| 90 | throw new FramsticksException().msg("failed to set").cause(e); |
---|
[77] | 91 | } |
---|
[86] | 92 | } catch (FramsticksException e) { |
---|
[107] | 93 | throw e.arg("param", param).arg("value", value).arg("value type", Misc.getClass(value)).arg("access", this); |
---|
[77] | 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
[90] | 97 | @Override |
---|
[99] | 98 | public void reg(EventParam param, EventListener<?> listener) { |
---|
| 99 | try { |
---|
| 100 | try { |
---|
| 101 | if (object == null) { |
---|
| 102 | throw new FramsticksException().msg("no object set"); |
---|
| 103 | } |
---|
| 104 | |
---|
[101] | 105 | backend.adders.get(param.getId()).reg(object, listener); |
---|
[99] | 106 | return; |
---|
| 107 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
---|
| 108 | throw new FramsticksException().msg("failed to add listener").cause(e); |
---|
| 109 | } |
---|
| 110 | } catch (FramsticksException e) { |
---|
| 111 | throw e.arg("param", param).arg("access", this); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | @Override |
---|
| 116 | public void regRemove(EventParam param, EventListener<?> listener) { |
---|
| 117 | try { |
---|
| 118 | try { |
---|
| 119 | if (object == null) { |
---|
| 120 | throw new FramsticksException().msg("no object set"); |
---|
| 121 | } |
---|
| 122 | |
---|
[101] | 123 | backend.removers.get(param.getId()).regRemove(object, listener); |
---|
[99] | 124 | return; |
---|
| 125 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
---|
| 126 | throw new FramsticksException().msg("failed to remove listener").cause(e); |
---|
| 127 | } |
---|
| 128 | } catch (FramsticksException e) { |
---|
| 129 | throw e.arg("param", param).arg("access", this); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | @Override |
---|
[105] | 134 | public Object call(String id, Object... arguments) { |
---|
[90] | 135 | try { |
---|
| 136 | try { |
---|
| 137 | if (object == null) { |
---|
| 138 | throw new FramsticksException().msg("no object set"); |
---|
| 139 | } |
---|
[101] | 140 | ReflectionAccessBackend.ReflectedCaller c = backend.callers.get(id); |
---|
[90] | 141 | if (c == null) { |
---|
| 142 | throw new FramsticksException().msg("method is not bound"); |
---|
| 143 | } |
---|
| 144 | return c.call(object, arguments); |
---|
| 145 | } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { |
---|
| 146 | throw new FramsticksException().msg("failed to call").cause(e); |
---|
| 147 | } |
---|
| 148 | } catch (FramsticksException e) { |
---|
[101] | 149 | throw e.arg("param", framsClass.getParam(id)).arg("access", this); |
---|
[90] | 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
[101] | 153 | @Override |
---|
[105] | 154 | public Object call(ProcedureParam param, Object... arguments) { |
---|
[101] | 155 | return call(param.getId(), arguments); |
---|
[77] | 156 | } |
---|
| 157 | |
---|
| 158 | @Override |
---|
| 159 | public void clearValues() { |
---|
| 160 | if (object == null) { |
---|
| 161 | return; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | try { |
---|
[105] | 165 | for (ValueParam p : filterInstanceof(framsClass.getParams(), ValueParam.class)) { |
---|
[78] | 166 | setValue(p, p.getDef(Object.class)); |
---|
[77] | 167 | } |
---|
[78] | 168 | } catch (IllegalArgumentException ex) { |
---|
[77] | 169 | ex.printStackTrace(); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * Sets the new object to operate on. |
---|
[84] | 175 | * |
---|
[77] | 176 | * @param object |
---|
| 177 | * new object to operate on |
---|
| 178 | */ |
---|
| 179 | @Override |
---|
[84] | 180 | public ReflectionAccess select(Object object) { |
---|
[101] | 181 | this.object = ParamsUtil.selectObjectForAccess(this, object, javaClass); |
---|
[84] | 182 | return this; |
---|
[77] | 183 | } |
---|
| 184 | |
---|
| 185 | @Override |
---|
| 186 | public Object getSelected() { |
---|
| 187 | return object; |
---|
| 188 | } |
---|
| 189 | |
---|
[84] | 190 | @Override |
---|
| 191 | public Object createAccessee() { |
---|
| 192 | try { |
---|
[90] | 193 | return javaClass.newInstance(); |
---|
[85] | 194 | } catch (InstantiationException | IllegalAccessException e) { |
---|
[101] | 195 | throw new FramsticksException().msg("failed to create reflected object").arg("java class", javaClass).arg("frams class", framsClass).cause(e); |
---|
[84] | 196 | } |
---|
| 197 | } |
---|
[86] | 198 | |
---|
[87] | 199 | @Override |
---|
[96] | 200 | public void tryAutoAppend(Object value) { |
---|
[87] | 201 | assert object != null; |
---|
[96] | 202 | try { |
---|
| 203 | for (Method m : backend.autoAppendMethods) { |
---|
| 204 | if (m.getParameterTypes()[0].isAssignableFrom(value.getClass())) { |
---|
| 205 | try { |
---|
[100] | 206 | log.trace("auto appending with value {} with method {}", value, m); |
---|
[96] | 207 | m.invoke(object, value); |
---|
| 208 | return; |
---|
| 209 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | FramsticksException e) { |
---|
| 210 | throw new FramsticksException().msg("failed to auto append").cause(e).arg("with method", m); |
---|
| 211 | } |
---|
[87] | 212 | } |
---|
| 213 | } |
---|
[96] | 214 | throw new FramsticksException().msg("no method found to append"); |
---|
| 215 | } catch (FramsticksException e) { |
---|
| 216 | throw e.arg("value", value).arg("into object", object); |
---|
[87] | 217 | } |
---|
[96] | 218 | |
---|
[87] | 219 | } |
---|
[90] | 220 | |
---|
[100] | 221 | @Override |
---|
| 222 | public String toString() { |
---|
| 223 | StringBuilder b = new StringBuilder(); |
---|
| 224 | b.append(framsClass); |
---|
| 225 | if (getSelected() != null) { |
---|
| 226 | b.append("(").append(getSelected()).append(")"); |
---|
| 227 | } |
---|
| 228 | return b.toString(); |
---|
| 229 | } |
---|
[77] | 230 | } |
---|
[86] | 231 | |
---|