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;
21 * Accessing a property of an object.
23 * @author Erik Brakkee
26 * Type of the property.
28 public class PropertyAccessor<T> implements Accessor<T> {
29 private Method getter;
30 private Method setter;
33 * Constructs the accessor.
40 public PropertyAccessor(Method aGetter, Method aSetter) {
43 getter.setAccessible(true);
44 setter.setAccessible(true);
48 public T get(Object aEntity) {
50 return (T) getter.invoke(aEntity);
51 } catch (Exception e) {
52 throw new RuntimeException(e);
57 public void set(Object aEntity, T aValue) {
59 setter.invoke(aEntity, aValue);
60 } catch (Exception e) {
61 throw new RuntimeException(e);
68 public Method getGetter() {
75 public Method getSetter() {
80 public String toString() {
81 return "propertyInjection(" + getter + ", " + setter + ")";
85 public Class<T> getType() {
86 return (Class<T>) getter.getReturnType();