1 16 17 package com.google.inject.matcher; 18 19 import com.google.inject.util.Objects; 20 21 import java.lang.annotation.Annotation ; 22 import java.lang.reflect.AnnotatedElement ; 23 import java.lang.reflect.Method ; 24 25 30 public class Matchers { 31 32 private Matchers() {} 33 34 static Matcher<Object > ANY = new AbstractMatcher<Object >() { 35 public boolean matches(Object o) { 36 return true; 37 } 38 39 public String toString() { 40 return "any()"; 41 } 42 }; 43 44 47 public static Matcher<Object > any() { 48 return ANY; 49 } 50 51 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 toString() { 62 return "not(" + p + ")"; 63 } 64 }; 65 } 66 67 71 public static Matcher<AnnotatedElement > annotatedWith( 72 final Class <? extends Annotation > annotationType) { 73 Objects.nonNull(annotationType, "annotation type"); 74 return new AbstractMatcher<AnnotatedElement >() { 75 public boolean matches(AnnotatedElement element) { 76 Annotation annotation = element.getAnnotation(annotationType); 77 return annotation != null; 78 } 79 80 public String toString() { 81 return "annotatedWith(" + annotationType.getSimpleName() + ".class)"; 82 } 83 }; 84 } 85 86 90 public static Matcher<AnnotatedElement > annotatedWith( 91 final Annotation annotation) { 92 Objects.nonNull(annotation, "annotation"); 93 return new AbstractMatcher<AnnotatedElement >() { 94 public boolean matches(AnnotatedElement element) { 95 Annotation fromElement 96 = element.getAnnotation(annotation.annotationType()); 97 return fromElement != null && annotation.equals(fromElement); 98 } 99 100 public String toString() { 101 return "annotatedWith(" + annotation + ")"; 102 } 103 }; 104 } 105 106 110 public static Matcher<Class > subclassesOf(final Class <?> superclass) { 111 Objects.nonNull(superclass, "superclass"); 112 return new AbstractMatcher<Class >() { 113 public boolean matches(Class subclass) { 114 return superclass.isAssignableFrom(subclass); 115 } 116 117 public String toString() { 118 return "subclassesOf(" + superclass.getSimpleName() + ".class)"; 119 } 120 }; 121 } 122 123 126 public static Matcher<Object > only(final Object o) { 127 Objects.nonNull(o, "o"); 128 return new AbstractMatcher<Object >() { 129 public boolean matches(Object other) { 130 return o.equals(other); 131 } 132 133 public String toString() { 134 return "only(" + o + ")"; 135 } 136 }; 137 } 138 139 142 public static Matcher<Object > identicalTo(final Object o) { 143 Objects.nonNull(o, "o"); 144 return new AbstractMatcher<Object >() { 145 public boolean matches(Object other) { 146 return o == other; 147 } 148 149 public String toString() { 150 return "identicalTo(" + o + ")"; 151 } 152 }; 153 } 154 155 158 public static Matcher<Class > inPackage(final Package p) { 159 Objects.nonNull(p, "package"); 160 return new AbstractMatcher<Class >() { 161 public boolean matches(Class c) { 162 return c.getPackage().equals(p); 163 } 164 165 public String toString() { 166 return "package(" + p.getName() + ")"; 167 } 168 }; 169 } 170 171 174 public static Matcher<Method > returns( 175 final Matcher<? super Class <?>> returnType) { 176 Objects.nonNull(returnType, "return type matcher"); 177 return new AbstractMatcher<Method >() { 178 public boolean matches(Method m) { 179 return returnType.matches(m.getReturnType()); 180 } 181 182 public String toString() { 183 return "returns(" + returnType + ")"; 184 } 185 }; 186 } 187 } 188 | Popular Tags |