001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 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.amf.io.util;
022    
023    import java.lang.annotation.Annotation;
024    import java.lang.reflect.Method;
025    import java.lang.reflect.Type;
026    
027    import org.granite.messaging.amf.io.convert.Converters;
028    
029    /**
030     * @author Franck WOLFF
031     */
032    public class MethodProperty extends Property {
033    
034        private final Method setter;
035        private final Method getter;
036        private final Type type;
037    
038        public MethodProperty(Converters converters, String name, Method setter, Method getter) {
039            super(converters, name);
040            this.setter = setter;
041            this.getter = getter;
042            this.type = getter != null ? getter.getGenericReturnType() : setter.getParameterTypes()[0];
043        }
044    
045        @Override
046        public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
047            if (getter != null) {
048                if (getter.isAnnotationPresent(annotationClass))
049                    return true;
050            }
051            if (setter != null)
052                return setter.isAnnotationPresent(annotationClass);
053            return false;
054        }
055    
056        @Override
057            public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
058            if (getter != null && getter.isAnnotationPresent(annotationClass))
059                    return getter.getAnnotation(annotationClass);
060            if (setter != null)
061                    return setter.getAnnotation(annotationClass);
062                    return null;
063            }
064    
065            @Override
066        public Type getType() {
067            return type;
068        }
069    
070        @Override
071        public void setProperty(Object instance, Object value, boolean convert) {
072            try {
073                Object[] params = new Object[]{convert ? convert(value) : value};
074                setter.invoke(instance, params);
075            } catch (Exception e) {
076                throw new RuntimeException(e);
077            }
078        }
079    
080        @Override
081        public Object getProperty(Object instance) {
082            try {
083                return getter.invoke(instance, new Object[0]);
084            } catch (Throwable e) {
085                throw new RuntimeException(e);
086            }
087        }
088    }