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 import org.granite.util.TypeUtil;
029 import org.granite.util.TypeUtil.DeclaredAnnotation;
030
031 /**
032 * @author Franck WOLFF
033 */
034 public class MethodProperty extends Property {
035
036 private final Method setter;
037 private final Method getter;
038 private final Type type;
039
040 public MethodProperty(Converters converters, String name, Method setter, Method getter) {
041 super(converters, name);
042 this.setter = setter;
043 this.getter = getter;
044 this.type = getter != null ? getter.getGenericReturnType() : setter.getParameterTypes()[0];
045 }
046
047 @Override
048 public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass, boolean recursive) {
049 if (getter != null) {
050 if (getter.isAnnotationPresent(annotationClass))
051 return true;
052 if (recursive && TypeUtil.isAnnotationPresent(getter, annotationClass))
053 return true;
054 }
055 if (setter != null) {
056 if (setter.isAnnotationPresent(annotationClass))
057 return true;
058 if (recursive && TypeUtil.isAnnotationPresent(setter, annotationClass))
059 return true;
060 }
061 return false;
062 }
063
064 @Override
065 public <T extends Annotation> T getAnnotation(Class<T> annotationClass, boolean recursive) {
066 T annotation = null;
067 if (getter != null) {
068 annotation = getter.getAnnotation(annotationClass);
069 if (annotation == null && recursive) {
070 DeclaredAnnotation<T> declaredAnnotation = TypeUtil.getAnnotation(getter, annotationClass);
071 if (declaredAnnotation != null)
072 annotation = declaredAnnotation.annotation;
073 }
074 }
075 if (annotation == null && setter != null) {
076 annotation = setter.getAnnotation(annotationClass);
077 if (annotation == null && recursive) {
078 DeclaredAnnotation<T> declaredAnnotation = TypeUtil.getAnnotation(setter, annotationClass);
079 if (declaredAnnotation != null)
080 annotation = declaredAnnotation.annotation;
081 }
082 }
083 return annotation;
084 }
085
086 @Override
087 public Type getType() {
088 return type;
089 }
090
091 @Override
092 public Class<?> getDeclaringClass() {
093 return getter != null ? getter.getDeclaringClass() : setter.getDeclaringClass();
094 }
095
096 @Override
097 public void setProperty(Object instance, Object value, boolean convert) {
098 try {
099 Object[] params = new Object[]{convert ? convert(value) : value};
100 setter.invoke(instance, params);
101 } catch (Exception e) {
102 throw new RuntimeException(e);
103 }
104 }
105
106 @Override
107 public Object getProperty(Object instance) {
108 try {
109 return getter.invoke(instance, new Object[0]);
110 } catch (Throwable e) {
111 throw new RuntimeException(e);
112 }
113 }
114 }