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