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.amf.io.util;
023
024 import java.lang.annotation.Annotation;
025 import java.lang.reflect.Field;
026 import java.lang.reflect.Type;
027
028 import org.granite.messaging.amf.io.convert.Converters;
029
030 /**
031 * @author Franck WOLFF
032 */
033 public class FieldProperty extends Property {
034
035 private final Field field;
036
037 public FieldProperty(Converters converters, Field field) {
038 super(converters, field.getName());
039 this.field = field;
040 }
041
042 @Override
043 public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass, boolean recursive) {
044 return field.isAnnotationPresent(annotationClass);
045 }
046
047 @Override
048 public <T extends Annotation> T getAnnotation(Class<T> annotationClass, boolean recursive) {
049 return field.getAnnotation(annotationClass);
050 }
051
052 @Override
053 public Type getType() {
054 return field.getGenericType();
055 }
056
057 @Override
058 public Class<?> getDeclaringClass() {
059 return field.getDeclaringClass();
060 }
061
062 @Override
063 public void setProperty(Object instance, Object value, boolean convert) {
064 try {
065 field.setAccessible(true);
066 field.set(instance, convert ? convert(value) : value);
067 } catch (Exception e) {
068 throw new RuntimeException(e);
069 }
070 }
071
072 @Override
073 public Object getProperty(Object instance) {
074 try {
075 field.setAccessible(true);
076 return field.get(instance);
077 } catch (Exception e) {
078 throw new RuntimeException(e);
079 }
080 }
081 }