| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| |
| package org.eclipse.osbp.runtime.web.vaadin.databinding.component.internal; |
| |
| import java.beans.PropertyDescriptor; |
| import java.lang.reflect.InvocationTargetException; |
| import java.lang.reflect.Method; |
| |
| import org.apache.commons.beanutils.PropertyUtils; |
| import org.eclipse.osbp.runtime.web.vaadin.databinding.properties.AbstractVaadinValueProperty; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * Uses reflection to access getter and setter method. |
| */ |
| public class SimpleAccessorProperty extends AbstractVaadinValueProperty { |
| |
| /** The type. */ |
| private Class<?> type; |
| |
| /** The getter. */ |
| private Method getter; |
| |
| /** The setter. */ |
| private Method setter; |
| |
| /** |
| * Instantiates a new simple accessor property. |
| * |
| * @param componentClass |
| * the component class |
| * @param property |
| * the property |
| */ |
| public SimpleAccessorProperty(Class<?> componentClass, String property) { |
| super(); |
| |
| PropertyDescriptor result = getPropertyDescriptor(componentClass, |
| property); |
| this.getter = PropertyUtils.getReadMethod(result); |
| this.setter = PropertyUtils.getWriteMethod(result); |
| this.type = getter.getReturnType(); |
| } |
| |
| /** |
| * Returns the property descriptor for the given class and the property. |
| * |
| * @param componentClass |
| * the component class |
| * @param property |
| * the property |
| * @return the property descriptor |
| */ |
| protected PropertyDescriptor getPropertyDescriptor(Class<?> componentClass, |
| String property) { |
| PropertyDescriptor result = null; |
| for (PropertyDescriptor descriptor : PropertyUtils |
| .getPropertyDescriptors(componentClass)) { |
| if (descriptor.getName().equals(property)) { |
| result = descriptor; |
| break; |
| } |
| } |
| return result; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.core.databinding.property.value.IValueProperty#getValueType() |
| */ |
| public Object getValueType() { |
| return type; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.core.databinding.property.value.SimpleValueProperty#doGetValue(java.lang.Object) |
| */ |
| protected Object doGetValue(Object source) { |
| try { |
| return getter.invoke(source); |
| } catch (IllegalAccessException e) { |
| throw new RuntimeException(e); |
| } catch (IllegalArgumentException e) { |
| throw new RuntimeException(e); |
| } catch (InvocationTargetException e) { |
| throw new RuntimeException(e); |
| } |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.core.databinding.property.value.SimpleValueProperty#doSetValue(java.lang.Object, java.lang.Object) |
| */ |
| protected void doSetValue(Object source, Object value) { |
| if (setter == null) { |
| throw new UnsupportedOperationException("Not supported!"); |
| } |
| try { |
| setter.invoke(source, value); |
| } catch (IllegalAccessException e) { |
| throw new RuntimeException(e); |
| } catch (IllegalArgumentException e) { |
| throw new RuntimeException(e); |
| } catch (InvocationTargetException e) { |
| throw new RuntimeException(e); |
| } |
| } |
| } |