View Javadoc

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      ReflectedMethodsImpl(
20              final ReflectedTypeFactory reflectedTypeFactory,
21              final TypeLiteral<T> typeLiteral,
22              final ReflectedSuperclassesAndInterfaces<T> allTypes) {
23          this.reflectedTypeFactory = reflectedTypeFactory;
24          this.typeLiteral = typeLiteral;
25          this.allTypes = allTypes;
26      }
27  
28      @Override public List<FluentMethod> methods() {
29          if (reflectedMethods == null) {
30              final List<FluentMethod> result = new ArrayList<FluentMethod>();
31  
32              for (final FluentClass<?> klassToReflect : allTypes.superclassesAndInterfaces()) {
33                  result.addAll(klassToReflect.declaredMethods());
34              }
35              result.addAll(declaredMethods());
36  
37              reflectedMethods = unmodifiableList(result);
38          }
39          return reflectedMethods;
40      }
41  
42      private List<FluentMethod> getDeclaredMethodsOfClass(final TypeLiteral<?> typeLiteralToReflect) {
43          final List<FluentMethod> result = new ArrayList<FluentMethod>();
44          final Method[] declaredMethods = typeLiteralToReflect.getRawType().getDeclaredMethods();
45          for (final Method method : declaredMethods) {
46              result.add(reflectedTypeFactory.method(typeLiteralToReflect, method));
47          }
48          return result;
49      }
50  
51      @Override public List<FluentMethod> declaredMethods() {
52          if (declaredMethods == null) {
53              declaredMethods = unmodifiableList(getDeclaredMethodsOfClass(typeLiteral));
54          }
55          return declaredMethods;
56      }
57  }