View Javadoc

1   package com.lexicalscope.fluentreflection;
2   
3   import static ch.lambdaj.Lambda.*;
4   
5   import java.util.List;
6   
7   import org.hamcrest.Matcher;
8   
9   import com.google.inject.TypeLiteral;
10  
11  final class ReflectedMembersImpl<T> implements ReflectedMembers<T> {
12      private final ReflectedSuperclassesAndInterfaces<T> superclassesAndInterfaces;
13      private final ReflectedMethods<T> methods;
14      private final ReflectedConstructors<T> constructors;
15      private final ReflectedFields<T> fields;
16      private final Class<T> klass;
17  
18      public ReflectedMembersImpl(final ReflectedTypeFactory reflectedTypeFactory, final TypeLiteral<T> typeLiteral) {
19          this.klass = (Class<T>) typeLiteral.getRawType();
20          this.superclassesAndInterfaces =
21                  new ReflectedSuperclassesAndInterfacesImpl<T>(reflectedTypeFactory, typeLiteral);
22          this.methods = new ReflectedMethodsImpl<T>(reflectedTypeFactory, typeLiteral, superclassesAndInterfaces);
23          this.constructors = new ReflectedConstructorsImpl<T>(reflectedTypeFactory, typeLiteral);
24          this.fields = new ReflectedFieldsImpl<T>(reflectedTypeFactory, typeLiteral, superclassesAndInterfaces);
25      }
26  
27      @Override public List<FluentConstructor<T>> constructors() {
28          return constructors.constructors();
29      }
30  
31      @Override public List<FluentMethod> methods() {
32          return methods.methods();
33      }
34  
35      @Override public List<FluentMethod> declaredMethods() {
36          return methods.declaredMethods();
37      }
38  
39      @Override public List<FluentClass<?>> superclassesAndInterfaces() {
40          return superclassesAndInterfaces.superclassesAndInterfaces();
41      }
42  
43      @Override public List<FluentMethod> methods(final Matcher<? super FluentMethod> methodMatcher) {
44          return select(methods(), methodMatcher);
45      }
46  
47      @Override public FluentMethod method(final Matcher<? super FluentMethod> methodMatcher) {
48          final FluentMethod selectedMethod = selectFirst(methods(), methodMatcher);
49          if (selectedMethod == null) {
50              throw new MethodNotFoundException(klass, methodMatcher);
51          }
52          return selectedMethod;
53      }
54  
55      @Override public List<FluentConstructor<T>> constructors(
56              final Matcher<? super FluentConstructor<?>> constructorMatcher) {
57          return select(constructors(), constructorMatcher);
58      }
59  
60      @Override public FluentConstructor<T> constructor(
61              final Matcher<? super FluentConstructor<?>> constructorMatcher) {
62          return selectFirst(constructors(), constructorMatcher);
63      }
64  
65      @Override public List<FluentClass<?>> superclassesAndInterfaces(
66              final Matcher<? super FluentClass<?>> supertypeMatcher) {
67          return select(superclassesAndInterfaces(), supertypeMatcher);
68      }
69  
70      @Override public List<FluentField> declaredFields() {
71          return fields.declaredFields();
72      }
73  
74      @Override public List<FluentField> fields() {
75          return fields.fields();
76      }
77  
78      @Override public List<FluentField> fields(final ReflectionMatcher<? super FluentField> fieldMatcher) {
79          return select(fields(), fieldMatcher);
80      }
81  
82      @Override public FluentField field(final Matcher<? super FluentField> fieldMatcher) {
83          final FluentField selectedMethod = selectFirst(fields(), fieldMatcher);
84          if (selectedMethod == null) {
85              throw new FieldNotFoundException(klass, fieldMatcher);
86          }
87          return selectedMethod;
88      }
89  }