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.simantics.objmap.exceptions.MappingException;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
22 * Accesses the given field of the element.
23 * @author Hannu Niemistö
25 public class FieldAccessor<Range,T> implements IRangeAccessor<Range,T> {
27 static final Logger LOGGER = LoggerFactory.getLogger(FieldAccessor.class);
31 public FieldAccessor(Field field) {
36 public T get(Range element) throws MappingException {
38 @SuppressWarnings("unchecked")
39 T result = (T)field.get(element);
41 if(LOGGER.isTraceEnabled())
42 LOGGER.trace(" FieldAccessor.get " +
43 field.getName() + " -> " + result
47 } catch (IllegalArgumentException e) {
48 throw new MappingException(e);
49 } catch (IllegalAccessException e) {
50 throw new MappingException(e);
55 public boolean set(Range element, T value) throws MappingException {
57 Object currentValue = field.get(element);
59 if(LOGGER.isTraceEnabled())
60 LOGGER.trace(" FieldAccessor.set " +
61 field.getName() + " " + currentValue +
66 ? (currentValue == null || field.getType().isPrimitive())
67 : value.equals(currentValue))
69 field.set(element, value);
71 } catch (IllegalArgumentException e) {
72 throw new MappingException(e);
73 } catch (IllegalAccessException e) {
74 throw new MappingException(e);