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