001 /**
002 * GRANITE DATA SERVICES
003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 * This file is part of the Granite Data Services Platform.
006 *
007 * Granite Data Services is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * Granite Data Services is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 * General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 * USA, or see <http://www.gnu.org/licenses/>.
021 */
022 package org.granite.messaging.reflect;
023
024 import java.lang.annotation.Annotation;
025 import java.lang.reflect.InvocationTargetException;
026 import java.lang.reflect.Method;
027
028 /**
029 * @author Franck WOLFF
030 */
031 public class SimpleMethodProperty implements MethodProperty {
032
033 private final Method getter;
034 private final Method setter;
035 private final String name;
036
037 public SimpleMethodProperty(Method getter, Method setter, String name) {
038 if (getter == null || name == null)
039 throw new NullPointerException("getter and name cannot be null");
040
041 this.getter = getter;
042 this.setter = setter;
043 this.name = name;
044 }
045
046 public Method getGetter() {
047 return getter;
048 }
049
050 public Method getSetter() {
051 return setter;
052 }
053
054 public Class<?> getType() {
055 return getter.getReturnType();
056 }
057
058 public String getName() {
059 return name;
060 }
061
062 public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
063 return getter.isAnnotationPresent(annotationClass) || (setter != null && setter.isAnnotationPresent(annotationClass));
064 }
065
066 public boolean isReadable() {
067 return getter != null;
068 }
069
070 public boolean isWritable() {
071 return setter != null;
072 }
073
074 public boolean getBoolean(Object holder) throws IllegalArgumentException,
075 IllegalAccessException, InvocationTargetException {
076 return ((Boolean)getter.invoke(holder)).booleanValue();
077 }
078
079 public char getChar(Object holder) throws IllegalArgumentException,
080 IllegalAccessException, InvocationTargetException {
081 return ((Character)getter.invoke(holder)).charValue();
082 }
083
084 public byte getByte(Object holder) throws IllegalArgumentException,
085 IllegalAccessException, InvocationTargetException {
086 return ((Byte)getter.invoke(holder)).byteValue();
087 }
088
089 public short getShort(Object holder) throws IllegalArgumentException,
090 IllegalAccessException, InvocationTargetException {
091 return ((Short)getter.invoke(holder)).shortValue();
092 }
093
094 public int getInt(Object holder) throws IllegalArgumentException,
095 IllegalAccessException, InvocationTargetException {
096 return ((Integer)getter.invoke(holder)).intValue();
097 }
098
099 public long getLong(Object holder) throws IllegalArgumentException,
100 IllegalAccessException, InvocationTargetException {
101 return ((Long)getter.invoke(holder)).intValue();
102 }
103
104 public float getFloat(Object holder) throws IllegalArgumentException,
105 IllegalAccessException, InvocationTargetException {
106 return ((Float)getter.invoke(holder)).floatValue();
107 }
108
109 public double getDouble(Object holder) throws IllegalArgumentException,
110 IllegalAccessException, InvocationTargetException {
111 return ((Double)getter.invoke(holder)).doubleValue();
112 }
113
114 public Object getObject(Object holder) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
115 return getter.invoke(holder);
116 }
117
118 public Object getRawObject(Object holder) throws IllegalArgumentException,
119 IllegalAccessException, InvocationTargetException {
120 return getter.invoke(holder);
121 }
122
123 public void setBoolean(Object holder, boolean value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
124 if (setter == null)
125 throw new IllegalAccessException("Property " + this + " isn't writable");
126 setter.invoke(holder, value);
127 }
128
129 public void setChar(Object holder, char value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
130 if (setter == null)
131 throw new IllegalAccessException("Property " + this + " isn't writable");
132 setter.invoke(holder, value);
133 }
134
135 public void setByte(Object holder, byte value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
136 if (setter == null)
137 throw new IllegalAccessException("Property " + this + " isn't writable");
138 setter.invoke(holder, value);
139 }
140
141 public void setShort(Object holder, short value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
142 if (setter == null)
143 throw new IllegalAccessException("Property " + this + " isn't writable");
144 setter.invoke(holder, value);
145 }
146
147 public void setInt(Object holder, int value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
148 if (setter == null)
149 throw new IllegalAccessException("Property " + this + " isn't writable");
150 setter.invoke(holder, value);
151 }
152
153 public void setLong(Object holder, long value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
154 if (setter == null)
155 throw new IllegalAccessException("Property " + this + " isn't writable");
156 setter.invoke(holder, value);
157 }
158
159 public void setFloat(Object holder, float value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
160 if (setter == null)
161 throw new IllegalAccessException("Property " + this + " isn't writable");
162 setter.invoke(holder, value);
163 }
164
165 public void setDouble(Object holder, double value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
166 if (setter == null)
167 throw new IllegalAccessException("Property " + this + " isn't writable");
168 setter.invoke(holder, value);
169 }
170
171 public void setObject(Object holder, Object value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
172 if (setter == null)
173 throw new IllegalAccessException("Property " + this + " isn't writable");
174 setter.invoke(holder, value);
175 }
176
177 @Override
178 public int hashCode() {
179 return getter.hashCode();
180 }
181
182 @Override
183 public boolean equals(Object obj) {
184 return (obj instanceof MethodProperty && ((MethodProperty)obj).getGetter().equals(getter));
185 }
186
187 @Override
188 public String toString() {
189 return getter.toString();
190 }
191 }