1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
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.Field;
16 import org.apache.log4j.Logger;
17 import org.simantics.objmap.exceptions.MappingException;
21 * Accesses the given field of the element.
22 * @author Hannu Niemistö
24 public class FieldAccessor<Range,T> implements IRangeAccessor<Range,T> {
26 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
30 public FieldAccessor(Field field) {
35 public T get(Range element) throws MappingException {
37 @SuppressWarnings("unchecked")
38 T result = (T)field.get(element);
40 if(LOGGER.isInfoEnabled())
41 LOGGER.info(" FieldAccessor.get " +
42 field.getName() + " -> " + result
46 } catch (IllegalArgumentException e) {
47 throw new MappingException(e);
48 } catch (IllegalAccessException e) {
49 throw new MappingException(e);
54 public boolean set(Range element, T value) throws MappingException {
56 Object currentValue = field.get(element);
58 if(LOGGER.isInfoEnabled())
59 LOGGER.info(" FieldAccessor.set " +
60 field.getName() + " " + currentValue +
65 ? (currentValue == null || field.getType().isPrimitive())
66 : value.equals(currentValue))
68 field.set(element, value);
70 } catch (IllegalArgumentException e) {
71 throw new MappingException(e);
72 } catch (IllegalAccessException e) {
73 throw new MappingException(e);