Added: FieldPropertyExtractor contributed by Daisuke Miyamoto.
@@ -0,0 +1,58 @@ | ||
1 | +/* | |
2 | + * Copyright 2011 Daisuke Miyamoto. | |
3 | + * Created on 2011/10/21 | |
4 | + * | |
5 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | + * you may not use this file except in compliance with the License. | |
7 | + * You may obtain a copy of the License at | |
8 | + * | |
9 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | + * | |
11 | + * Unless required by applicable law or agreed to in writing, software | |
12 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
14 | + * either express or implied. See the License for the specific language | |
15 | + * governing permissions and limitations under the License. | |
16 | + */ | |
17 | +package jp.sf.amateras.mirage.bean; | |
18 | + | |
19 | +import java.lang.reflect.Field; | |
20 | +import java.util.LinkedHashMap; | |
21 | +import java.util.Map; | |
22 | + | |
23 | +import jp.sf.amateras.mirage.bean.PropertyExtractor; | |
24 | + | |
25 | +/** | |
26 | + * An implementation of {@link PropertyExtractor} which always retrieves property types and names | |
27 | + * from the entity field if it has getters and setters. | |
28 | + * | |
29 | + * @since 1.1.4 | |
30 | + * @author daisuke | |
31 | + */ | |
32 | +public class FieldPropertyExtractor implements PropertyExtractor { | |
33 | + | |
34 | +// @Override | |
35 | + public Map<String, PropertyInfo> extractProperties(Class<?> clazz) { | |
36 | + Map<String, PropertyInfo> map = new LinkedHashMap<String, PropertyInfo>(); | |
37 | + extractProperties0(clazz, map); | |
38 | + return map; | |
39 | + } | |
40 | + | |
41 | + private void extractProperties0(Class<?> clazz, Map<String, PropertyInfo> map) { | |
42 | + if (clazz == null) { | |
43 | + return; | |
44 | + } | |
45 | + Field[] fields = clazz.getDeclaredFields(); | |
46 | + for (Field field : fields) { | |
47 | + field.setAccessible(true); | |
48 | + if (!map.containsKey(field.getName())) { | |
49 | + PropertyInfo info = new PropertyInfo(); | |
50 | + info.name = field.getName(); | |
51 | + info.field = field; | |
52 | + info.type = field.getType(); | |
53 | + map.put(field.getName(), info); | |
54 | + } | |
55 | + } | |
56 | + extractProperties0(clazz.getSuperclass(), map); | |
57 | + } | |
58 | +} |