View Javadoc

1   package com.lexicalscope.fluentreflection;
2   
3   import static ch.lambdaj.Lambda.*;
4   import static com.lexicalscope.fluentreflection.ReflectionMatchers.reflectingOn;
5   import static org.hamcrest.Matchers.hasItem;
6   
7   import java.lang.annotation.Annotation;
8   import java.lang.reflect.AnnotatedElement;
9   import java.util.List;
10  
11  import org.hamcrest.Matcher;
12  
13  /*
14   * Copyright 2011 Tim Wood
15   *
16   * Licensed under the Apache License, Version 2.0 (the "License");
17   * you may not use this file except in compliance with the License.
18   * You may obtain a copy of the License at
19   *
20   * http://www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing, software
23   * distributed under the License is distributed on an "AS IS" BASIS,
24   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25   * See the License for the specific language governing permissions and
26   * limitations under the License.
27   */
28  final class FluentAnnotatedImpl implements FluentAnnotated {
29      private final ReflectedTypeFactory reflectedTypeFactory;
30      private final AnnotatedElement annotatedElement;
31  
32      public FluentAnnotatedImpl(
33              final ReflectedTypeFactory reflectedTypeFactory,
34              final AnnotatedElement annotatedElement) {
35          this.reflectedTypeFactory = reflectedTypeFactory;
36          this.annotatedElement = annotatedElement;
37      }
38  
39      @Override public FluentClass<?> annotation(final Matcher<? super FluentClass<?>> annotationMatcher) {
40          return selectFirst(annotations(), annotationMatcher);
41      }
42  
43      @Override public <A extends Annotation> A annotation(final Class<A> annotationClass) {
44          return annotatedElement.getAnnotation(annotationClass);
45      }
46  
47      @Override public boolean annotatedWith(final Matcher<? super FluentClass<?>> annotationMatcher) {
48          return hasItem(annotationMatcher).matches(annotations());
49      }
50  
51      @Override public boolean annotatedWith(final Class<? extends Annotation> annotationClass) {
52          return annotatedWith(reflectingOn(annotationClass));
53      }
54  
55      private List<FluentClass<?>> annotations() {
56          return convert(
57                  convert(annotatedElement.getAnnotations(), new ConvertAnnotationToClass()),
58                  new ConvertClassToReflectedType(reflectedTypeFactory));
59      }
60  }