1 | |
package com.lexicalscope.fluentreflection; |
2 | |
|
3 | |
import static java.util.Collections.unmodifiableList; |
4 | |
|
5 | |
import java.lang.reflect.Field; |
6 | |
import java.util.ArrayList; |
7 | |
import java.util.Collections; |
8 | |
import java.util.List; |
9 | |
|
10 | |
import com.google.inject.TypeLiteral; |
11 | |
|
12 | |
final class ReflectedFieldsImpl<T> implements ReflectedFields<T> { |
13 | |
private final ReflectedTypeFactory reflectedTypeFactory; |
14 | |
private final ReflectedSuperclassesAndInterfaces<T> allTypes; |
15 | |
private final TypeLiteral<T> typeLiteral; |
16 | |
|
17 | |
private List<FluentField> declaredFields; |
18 | |
private List<FluentField> reflectedFields; |
19 | |
|
20 | 14584 | ReflectedFieldsImpl( |
21 | |
final ReflectedTypeFactory reflectedTypeFactory, |
22 | |
final TypeLiteral<T> typeLiteral, |
23 | |
final ReflectedSuperclassesAndInterfaces<T> allTypes) { |
24 | 14584 | this.reflectedTypeFactory = reflectedTypeFactory; |
25 | 14584 | this.typeLiteral = typeLiteral; |
26 | 14584 | this.allTypes = allTypes; |
27 | 14584 | } |
28 | |
|
29 | |
@Override public List<FluentField> fields() { |
30 | 288 | if (reflectedFields == null) { |
31 | 288 | final List<FluentField> result = new ArrayList<FluentField>(); |
32 | |
|
33 | 288 | result.addAll(declaredFields()); |
34 | 576 | for (final FluentClass<?> klassToReflect : allTypes.superclassesAndInterfaces()) { |
35 | 0 | result.addAll(klassToReflect.declaredFields()); |
36 | |
} |
37 | |
|
38 | 288 | Collections.reverse(result); |
39 | 288 | reflectedFields = unmodifiableList(result); |
40 | |
} |
41 | 288 | return reflectedFields; |
42 | |
} |
43 | |
|
44 | |
private List<FluentField> getDeclaredFieldsOfClass(final TypeLiteral<?> typeLiteralToReflect) { |
45 | 288 | final List<FluentField> result = new ArrayList<FluentField>(); |
46 | 288 | final Field[] declaredFields = typeLiteralToReflect.getRawType().getDeclaredFields(); |
47 | 2024 | for (final Field method : declaredFields) { |
48 | 1736 | result.add(reflectedTypeFactory.field(typeLiteralToReflect, method)); |
49 | |
} |
50 | 288 | return result; |
51 | |
} |
52 | |
|
53 | |
@Override public List<FluentField> declaredFields() { |
54 | 288 | if (declaredFields == null) { |
55 | 288 | declaredFields = unmodifiableList(getDeclaredFieldsOfClass(typeLiteral)); |
56 | |
} |
57 | 288 | return declaredFields; |
58 | |
} |
59 | |
} |