KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > matcher > Matchers


1 /**
2  * Copyright (C) 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.google.inject.matcher;
18
19 import com.google.inject.util.Objects;
20
21 import java.lang.annotation.Annotation JavaDoc;
22 import java.lang.reflect.AnnotatedElement JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24
25 /**
26  * Matcher implementations. Supports matching classes and methods.
27  *
28  * @author crazybob@google.com (Bob Lee)
29  */

30 public class Matchers {
31
32   private Matchers() {}
33
34   static Matcher<Object JavaDoc> ANY = new AbstractMatcher<Object JavaDoc>() {
35     public boolean matches(Object JavaDoc o) {
36       return true;
37     }
38
39     public String JavaDoc toString() {
40       return "any()";
41     }
42   };
43
44   /**
45    * Returns a matcher which matches any input.
46    */

47   public static Matcher<Object JavaDoc> any() {
48     return ANY;
49   }
50
51   /**
52    * Inverts the given matcher.
53    */

54   public static <T> Matcher<T> not(final Matcher<? super T> p) {
55     Objects.nonNull(p, "p");
56     return new AbstractMatcher<T>() {
57       public boolean matches(T t) {
58         return !p.matches(t);
59       }
60
61       public String JavaDoc toString() {
62         return "not(" + p + ")";
63       }
64     };
65   }
66
67   /**
68    * Returns a matcher which matches elements (methods, classes, etc.)
69    * with a given annotation.
70    */

71   public static Matcher<AnnotatedElement JavaDoc> annotatedWith(
72       final Class JavaDoc<? extends Annotation JavaDoc> annotationType) {
73     Objects.nonNull(annotationType, "annotation type");
74     return new AbstractMatcher<AnnotatedElement JavaDoc>() {
75       public boolean matches(AnnotatedElement JavaDoc element) {
76         Annotation JavaDoc annotation = element.getAnnotation(annotationType);
77         return annotation != null;
78       }
79
80       public String JavaDoc toString() {
81         return "annotatedWith(" + annotationType.getSimpleName() + ".class)";
82       }
83     };
84   }
85
86   /**
87    * Returns a matcher which matches elements (methods, classes, etc.)
88    * with a given annotation.
89    */

90   public static Matcher<AnnotatedElement JavaDoc> annotatedWith(
91       final Annotation JavaDoc annotation) {
92     Objects.nonNull(annotation, "annotation");
93     return new AbstractMatcher<AnnotatedElement JavaDoc>() {
94       public boolean matches(AnnotatedElement JavaDoc element) {
95         Annotation JavaDoc fromElement
96             = element.getAnnotation(annotation.annotationType());
97         return fromElement != null && annotation.equals(fromElement);
98       }
99
100       public String JavaDoc toString() {
101         return "annotatedWith(" + annotation + ")";
102       }
103     };
104   }
105
106   /**
107    * Returns a matcher which matches subclasses of the given type (as well as
108    * the given type).
109    */

110   public static Matcher<Class JavaDoc> subclassesOf(final Class JavaDoc<?> superclass) {
111     Objects.nonNull(superclass, "superclass");
112     return new AbstractMatcher<Class JavaDoc>() {
113       public boolean matches(Class JavaDoc subclass) {
114         return superclass.isAssignableFrom(subclass);
115       }
116
117       public String JavaDoc toString() {
118         return "subclassesOf(" + superclass.getSimpleName() + ".class)";
119       }
120     };
121   }
122
123   /**
124    * Returns a matcher which matches objects equal to the given object.
125    */

126   public static Matcher<Object JavaDoc> only(final Object JavaDoc o) {
127     Objects.nonNull(o, "o");
128     return new AbstractMatcher<Object JavaDoc>() {
129       public boolean matches(Object JavaDoc other) {
130         return o.equals(other);
131       }
132
133       public String JavaDoc toString() {
134         return "only(" + o + ")";
135       }
136     };
137   }
138
139   /**
140    * Returns a matcher which matches only the given object.
141    */

142   public static Matcher<Object JavaDoc> identicalTo(final Object JavaDoc o) {
143     Objects.nonNull(o, "o");
144     return new AbstractMatcher<Object JavaDoc>() {
145       public boolean matches(Object JavaDoc other) {
146         return o == other;
147       }
148
149       public String JavaDoc toString() {
150         return "identicalTo(" + o + ")";
151       }
152     };
153   }
154
155   /**
156    * Returns a matcher which matches classes in the given package.
157    */

158   public static Matcher<Class JavaDoc> inPackage(final Package JavaDoc p) {
159     Objects.nonNull(p, "package");
160     return new AbstractMatcher<Class JavaDoc>() {
161       public boolean matches(Class JavaDoc c) {
162         return c.getPackage().equals(p);
163       }
164
165       public String JavaDoc toString() {
166         return "package(" + p.getName() + ")";
167       }
168     };
169   }
170
171   /**
172    * Returns a matcher which matches methods with matching return types.
173    */

174   public static Matcher<Method JavaDoc> returns(
175       final Matcher<? super Class JavaDoc<?>> returnType) {
176     Objects.nonNull(returnType, "return type matcher");
177     return new AbstractMatcher<Method JavaDoc>() {
178       public boolean matches(Method JavaDoc m) {
179         return returnType.matches(m.getReturnType());
180       }
181
182       public String JavaDoc toString() {
183         return "returns(" + returnType + ")";
184       }
185     };
186   }
187 }
188
Popular Tags