1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.objmap.graph.rules.range;
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
17 import org.simantics.objmap.exceptions.MappingException;
21 * Accessor for mapped objects. Uses two methods:
22 * - Getter: returns the current object.
23 * - Setter: sets the current object. The object may be null.
25 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
29 public class GetSetObjectAccessor<Range,T> implements IRangeAccessor<Range,T> {
31 private Method getter;
32 private Method setter;
35 public GetSetObjectAccessor(Method getter, Method setter) {
41 @SuppressWarnings("unchecked")
42 public T get(Range element) throws MappingException {
44 return (T) getter.invoke(element);
45 } catch (IllegalArgumentException e) {
46 throw new MappingException(e);
47 } catch (IllegalAccessException e) {
48 throw new MappingException(e);
49 } catch (InvocationTargetException e) {
50 throw new MappingException(e.getCause());
55 public boolean set(Range element, T value)
56 throws MappingException {
58 setter.invoke(element, value);
59 } catch (IllegalArgumentException e) {
60 throw new MappingException(e);
61 } catch (IllegalAccessException e) {
62 throw new MappingException(e);
63 } catch (InvocationTargetException e) {
64 throw new MappingException(e.getCause());