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.Field;
026    import java.lang.reflect.InvocationTargetException;
027    
028    /**
029     * @author Franck WOLFF
030     */
031    public class SimpleFieldProperty implements FieldProperty {
032    
033            private final Field field;
034            
035            public SimpleFieldProperty(Field field) {
036                    if (field == null)
037                            throw new NullPointerException("field cannot be null");
038                    
039                    field.setAccessible(true);
040                    
041                    this.field = field;
042            }
043    
044            public Field getField() {
045                    return field;
046            }
047    
048            public Class<?> getType() {
049                    return field.getType();
050            }
051    
052            public String getName() {
053                    return field.getName();
054            }
055    
056            public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
057                    return field.isAnnotationPresent(annotationClass);
058            }
059    
060            public boolean isReadable() {
061                    return true;
062            }
063    
064            public boolean isWritable() {
065                    return true;
066            }
067    
068            public boolean getBoolean(Object holder) throws IllegalArgumentException, IllegalAccessException {
069                    return field.getBoolean(holder);
070            }
071    
072            public char getChar(Object holder) throws IllegalArgumentException, IllegalAccessException {
073                    return field.getChar(holder);
074            }
075    
076            public byte getByte(Object holder) throws IllegalArgumentException, IllegalAccessException {
077                    return field.getByte(holder);
078            }
079    
080            public short getShort(Object holder) throws IllegalArgumentException, IllegalAccessException {
081                    return field.getShort(holder);
082            }
083    
084            public int getInt(Object holder) throws IllegalArgumentException, IllegalAccessException {
085                    return field.getInt(holder);
086            }
087    
088            public long getLong(Object holder) throws IllegalArgumentException, IllegalAccessException {
089                    return field.getLong(holder);
090            }
091    
092            public float getFloat(Object holder) throws IllegalArgumentException, IllegalAccessException {
093                    return field.getFloat(holder);
094            }
095    
096            public double getDouble(Object holder) throws IllegalArgumentException, IllegalAccessException {
097                    return field.getDouble(holder);
098            }
099    
100            public Object getObject(Object holder) throws IllegalArgumentException, IllegalAccessException {
101                    return field.get(holder);
102            }
103    
104            public Object getRawObject(Object holder) throws IllegalArgumentException,
105                            IllegalAccessException, InvocationTargetException {
106                    return field.get(holder);
107            }
108    
109            public void setBoolean(Object holder, boolean value) throws IllegalArgumentException, IllegalAccessException {
110                    field.setBoolean(holder, value);
111            }
112    
113            public void setChar(Object holder, char value) throws IllegalArgumentException, IllegalAccessException {
114                    field.setChar(holder, value);
115            }
116    
117            public void setByte(Object holder, byte value) throws IllegalArgumentException, IllegalAccessException {
118                    field.setByte(holder, value);
119            }
120    
121            public void setShort(Object holder, short value) throws IllegalArgumentException, IllegalAccessException {
122                    field.setShort(holder, value);
123            }
124    
125            public void setInt(Object holder, int value) throws IllegalArgumentException, IllegalAccessException {
126                    field.setInt(holder, value);
127            }
128    
129            public void setLong(Object holder, long value) throws IllegalArgumentException, IllegalAccessException {
130                    field.setLong(holder, value);
131            }
132    
133            public void setFloat(Object holder, float value) throws IllegalArgumentException, IllegalAccessException {
134                    field.setFloat(holder, value);
135            }
136    
137            public void setDouble(Object holder, double value) throws IllegalArgumentException, IllegalAccessException {
138                    field.setDouble(holder, value);
139            }
140    
141            public void setObject(Object holder, Object value) throws IllegalArgumentException, IllegalAccessException {
142                    field.set(holder, value);
143            }
144    
145            @Override
146            public int hashCode() {
147                    return field.hashCode();
148            }
149    
150            @Override
151            public boolean equals(Object obj) {
152                    return (obj instanceof FieldProperty && ((FieldProperty)obj).getField().equals(field));
153            }
154    
155            @Override
156            public String toString() {
157                    return field.toString();
158            }
159    }