2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.reflection;
18 import java.lang.reflect.Method;
22 * Accessing a property of an object.
24 * @author Erik Brakkee
26 * @param <T> Type of the property.
28 public class PropertyAccessor<T> implements Accessor<T> {
29 private Method getter;
30 private Method setter;
33 * Constructs the accessor.
34 * @param aGetter Getter method.
35 * @param aSetter Setter method.
37 public PropertyAccessor(Method aGetter, Method aSetter) {
40 getter.setAccessible(true);
41 setter.setAccessible(true);
45 public T get(Object aEntity) {
47 return (T) getter.invoke(aEntity);
48 } catch (Exception e) {
49 throw new RuntimeException(e);
54 public void set(Object aEntity, T aValue) {
56 setter.invoke(aEntity, aValue);
57 } catch (Exception e) {
58 throw new RuntimeException(e);
65 public Method getGetter() {
72 public Method getSetter() {
77 public String toString() {
78 return "propertyInjection(" + getter + ", " + setter + ")";
82 public Class<T> getType() {
83 return (Class<T>)getter.getReturnType();