1 | |
package com.lexicalscope.fluentreflection; |
2 | |
|
3 | |
import static java.util.Collections.unmodifiableList; |
4 | |
|
5 | |
import java.lang.reflect.Method; |
6 | |
import java.util.ArrayList; |
7 | |
import java.util.List; |
8 | |
|
9 | |
import com.google.inject.TypeLiteral; |
10 | |
|
11 | |
final class ReflectedMethodsImpl<T> implements ReflectedMethods<T> { |
12 | |
private final ReflectedTypeFactory reflectedTypeFactory; |
13 | |
private final ReflectedSuperclassesAndInterfaces<T> allTypes; |
14 | |
private final TypeLiteral<T> typeLiteral; |
15 | |
|
16 | |
private List<FluentMethod> declaredMethods; |
17 | |
private List<FluentMethod> reflectedMethods; |
18 | |
|
19 | 14584 | ReflectedMethodsImpl( |
20 | |
final ReflectedTypeFactory reflectedTypeFactory, |
21 | |
final TypeLiteral<T> typeLiteral, |
22 | |
final ReflectedSuperclassesAndInterfaces<T> allTypes) { |
23 | 14584 | this.reflectedTypeFactory = reflectedTypeFactory; |
24 | 14584 | this.typeLiteral = typeLiteral; |
25 | 14584 | this.allTypes = allTypes; |
26 | 14584 | } |
27 | |
|
28 | |
@Override public List<FluentMethod> methods() { |
29 | 1628 | if (reflectedMethods == null) { |
30 | 1252 | final List<FluentMethod> result = new ArrayList<FluentMethod>(); |
31 | |
|
32 | 2580 | for (final FluentClass<?> klassToReflect : allTypes.superclassesAndInterfaces()) { |
33 | 284 | result.addAll(klassToReflect.declaredMethods()); |
34 | |
} |
35 | 1044 | result.addAll(declaredMethods()); |
36 | |
|
37 | 1044 | reflectedMethods = unmodifiableList(result); |
38 | |
} |
39 | 1420 | return reflectedMethods; |
40 | |
} |
41 | |
|
42 | |
private List<FluentMethod> getDeclaredMethodsOfClass(final TypeLiteral<?> typeLiteralToReflect) { |
43 | 1328 | final List<FluentMethod> result = new ArrayList<FluentMethod>(); |
44 | 1328 | final Method[] declaredMethods = typeLiteralToReflect.getRawType().getDeclaredMethods(); |
45 | 9060 | for (final Method method : declaredMethods) { |
46 | 7732 | result.add(reflectedTypeFactory.method(typeLiteralToReflect, method)); |
47 | |
} |
48 | 1328 | return result; |
49 | |
} |
50 | |
|
51 | |
@Override public List<FluentMethod> declaredMethods() { |
52 | 1328 | if (declaredMethods == null) { |
53 | 1328 | declaredMethods = unmodifiableList(getDeclaredMethodsOfClass(typeLiteral)); |
54 | |
} |
55 | 1328 | return declaredMethods; |
56 | |
} |
57 | |
} |