1 package org.simantics.g3d.objmap.rules.range;
\r
3 import java.lang.reflect.InvocationTargetException;
\r
4 import java.lang.reflect.Method;
\r
6 import org.simantics.objmap.MappingException;
\r
7 import org.simantics.objmap.rules.range.IRangeAccessor;
\r
10 * Accessor for mapped objects. Uses two methods:
\r
11 * - Getter: returns the current object.
\r
12 * - Setter: sets the current object. The object may be null.
\r
14 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
18 public class GetSetObjectAccessor<T> implements IRangeAccessor<T> {
\r
20 private Method getter;
\r
21 private Method setter;
\r
24 public GetSetObjectAccessor(Method getter, Method setter) {
\r
25 this.getter = getter;
\r
26 this.setter = setter;
\r
30 @SuppressWarnings("unchecked")
\r
31 public T get(Object element) throws org.simantics.objmap.MappingException {
\r
33 return (T) getter.invoke(element);
\r
34 } catch (IllegalArgumentException e) {
\r
35 throw new MappingException(e);
\r
36 } catch (IllegalAccessException e) {
\r
37 throw new MappingException(e);
\r
38 } catch (InvocationTargetException e) {
\r
39 throw new MappingException(e);
\r
44 public boolean set(Object element, T value)
\r
45 throws MappingException {
\r
47 setter.invoke(element, value);
\r
48 } catch (IllegalArgumentException e) {
\r
49 throw new MappingException(e);
\r
50 } catch (IllegalAccessException e) {
\r
51 throw new MappingException(e);
\r
52 } catch (InvocationTargetException e) {
\r
53 throw new MappingException(e);
\r