1 /*
2  * Copyright 2011 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.modelmapper.internal;
17
18 import java.lang.reflect.Field;
19 import java.lang.reflect.Member;
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Modifier;
22
23 import org.modelmapper.config.Configuration;
24 import org.modelmapper.spi.PropertyInfo;
25
26 /**
27  * Resolves PropertyInfo for individual members.
28  * 
29  * @param <M> property type
30  * @param <PI> property info type
31  * 
32  * @author Jonathan Halterman
33  */

34 interface PropertyInfoResolver<M extends Member, PI extends PropertyInfo> {
35   PropertyInfoResolver<Field, Mutator> FIELDS = new DefaultPropertyResolver<Field, Mutator>() {
36     public Mutator propertyInfoFor(Class<?> initialType, Field field, Configuration configuration,
37         String name) {
38       return PropertyInfoRegistry.fieldPropertyFor(initialType, field, configuration, name);
39     }
40
41     public Field[] membersFor(Class<?> type) {
42       return type.getDeclaredFields();
43     }
44   };
45
46   PropertyInfoResolver<Method, Accessor> ACCESSORS = new DefaultPropertyResolver<Method, Accessor>() {
47     @Override
48     public boolean isValid(Method method) {
49       return super.isValid(method) && !method.isBridge() && method.getParameterTypes().length == 0
50           && !method.getReturnType().equals(void.class);
51     }
52
53     public Accessor propertyInfoFor(Class<?> initialType, Method method,
54         Configuration configuration, String name) {
55       return PropertyInfoRegistry.accessorFor(initialType, method, configuration, name);
56     }
57
58     public Method[] membersFor(Class<?> type) {
59       return type.getDeclaredMethods();
60     }
61   };
62
63   PropertyInfoResolver<Method, Mutator> MUTATORS = new DefaultPropertyResolver<Method, Mutator>() {
64     @Override
65     public boolean isValid(Method method) {
66       return super.isValid(method)
67           && !method.isBridge()
68           && method.getParameterTypes().length == 1
69           && (method.getReturnType().equals(void.class) || method.getReturnType().equals(
70               method.getDeclaringClass()));
71     }
72
73     public Mutator propertyInfoFor(Class<?> initialType, Method method,
74         Configuration configuration, String name) {
75       return PropertyInfoRegistry.mutatorFor(initialType, method, configuration, name);
76     }
77
78     public Method[] membersFor(Class<?> type) {
79       return type.getDeclaredMethods();
80     }
81   };
82
83   static abstract class DefaultPropertyResolver<M extends Member, PI extends PropertyInfo>
84       implements PropertyInfoResolver<M, PI> {
85     public boolean isValid(M member) {
86       return !Modifier.isStatic(member.getModifiers()) && !member.isSynthetic();
87     }
88   }
89
90   boolean isValid(M member);
91
92   PI propertyInfoFor(Class<?> initialType, M member, Configuration configuration, String name);
93
94   M[] membersFor(Class<?> type);
95 }
96