下面列出了怎么用com.google.inject.matcher.Matcher的API类实例代码及写法,或者点击链接到github查看源代码。
private Matcher<? super Binding<?>> staticValidationMatcher() {
return new AbstractMatcher<Binding<?>>() {
@Override
public boolean matches(Binding<?> binding) {
Class<?> candidate = binding.getKey().getTypeLiteral().getRawType();
for (Field field : candidate.getDeclaredFields()) {
for (Annotation annotation : field.getAnnotations()) {
if (hasConstraintOrValidAnnotation(annotation)) {
return true;
}
}
}
return false;
}
};
}
/**
* Creates a matcher that will match methods of an interface, optionally excluding inherited
* methods.
*
* @param matchInterface The interface to match.
* @param declaredMethodsOnly if {@code true} only methods directly declared in the interface
* will be matched, otherwise all methods on the interface are matched.
* @return A new matcher instance.
*/
public static Matcher<Method> interfaceMatcher(
Class<?> matchInterface,
boolean declaredMethodsOnly) {
Method[] methods =
declaredMethodsOnly ? matchInterface.getDeclaredMethods() : matchInterface.getMethods();
final Set<Pair<String, Class<?>[]>> interfaceMethods =
ImmutableSet.copyOf(Iterables.transform(ImmutableList.copyOf(methods), CANONICALIZE));
final LoadingCache<Method, Pair<String, Class<?>[]>> cache = CacheBuilder.newBuilder()
.build(CacheLoader.from(CANONICALIZE));
return new AbstractMatcher<Method>() {
@Override
public boolean matches(Method method) {
return interfaceMethods.contains(cache.getUnchecked(method));
}
};
}
/**
* Binds an exception trap on all interface methods of all classes bound against an interface.
* Individual methods may opt out of trapping by annotating with {@link AllowUnchecked}.
* Only void methods are allowed, any non-void interface methods must explicitly opt out.
*
* @param binder The binder to register an interceptor with.
* @param wrapInterface Interface whose methods should be wrapped.
* @throws IllegalArgumentException If any of the non-whitelisted interface methods are non-void.
*/
public static void bindExceptionTrap(Binder binder, Class<?> wrapInterface)
throws IllegalArgumentException {
Set<Method> disallowed = ImmutableSet.copyOf(Iterables.filter(
ImmutableList.copyOf(wrapInterface.getMethods()),
Predicates.and(Predicates.not(IS_WHITELISTED), Predicates.not(VOID_METHOD))));
Preconditions.checkArgument(disallowed.isEmpty(),
"Non-void methods must be explicitly whitelisted with @AllowUnchecked: %s", disallowed);
Matcher<Method> matcher =
Matchers.not(WHITELIST_MATCHER).and(interfaceMatcher(wrapInterface, false));
binder.bindInterceptor(Matchers.subclassesOf(wrapInterface), matcher,
new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
return invocation.proceed();
} catch (RuntimeException e) {
LOG.warn("Trapped uncaught exception: " + e, e);
return null;
}
}
});
}
/**
* Create a Matcher which matches against a service method.
*
* @return Matcher matching a service method
*/
Matcher<Method> matchServiceMethod() {
return new AbstractMatcher<Method>() {
@Override
public boolean matches(Method method) {
return isServiceMethod(method);
}
};
}
@Override
default Matcher<T> and(com.google.inject.matcher.Matcher<? super T> other) {
return new AbstractMatcher<T>() {
@Override public boolean matches(T t) {
return FunctionalMatcher.this.matches(t) && other.matches(t);
}
};
}
@Override
default Matcher<T> or(com.google.inject.matcher.Matcher<? super T> other) {
return new AbstractMatcher<T>() {
@Override public boolean matches(T t) {
return FunctionalMatcher.this.matches(t) || other.matches(t);
}
};
}
@Override
protected void bindInterceptor(final Matcher<? super Class<?>> classMatcher,
final Matcher<? super Method> methodMatcher,
final MethodInterceptor... interceptors)
{
if (!bound) {
// Explicitly bind module instance under a specific sub-type (not Module as Guice forbids that)
bind(Key.get(AbstractInterceptorModule.class, Names.named(getClass().getName()))).toInstance(this);
bound = true;
}
super.bindInterceptor(classMatcher, methodMatcher, interceptors);
}
@Override
protected void configure() {
JdbcAdapter.newInstance(configs.values(), poolType, this.getClass());
JdbcTransactionalMethodInterceptor interceptor = new JdbcTransactionalMethodInterceptor();
requestInjection(interceptor);
Matcher<AnnotatedElement> annotatedElement = annotatedWith(JdbcTransactional.class);
bindInterceptor(any(), annotatedElement, interceptor);
bindInterceptor(annotatedElement, not(annotatedElement), interceptor);
}
@Override
protected void configure() {
final MultiTransactionalMethodInterceptor interceptor = new MultiTransactionalMethodInterceptor();
requestInjection(interceptor);
final Matcher<AnnotatedElement> annotatedElement = annotatedWith(MultiTransactional.class);
bindInterceptor(any(), annotatedElement, interceptor);
bindInterceptor(annotatedElement, not(annotatedElement), interceptor);
}
@Override
protected void bindInterceptor(final Matcher<? super Class<?>> classMatcher,
final Matcher<? super Method> methodMatcher,
final MethodInterceptor... interceptors) {
// hack to correctly bind @Transactional annotation for java8:
// aop tries to intercept synthetic methods which cause a lot of warnings
// (and generally not correct)
super.bindInterceptor(classMatcher, new AbstractMatcher<Method>() {
@Override
public boolean matches(final Method method) {
return !method.isSynthetic() && !method.isBridge() && methodMatcher.matches(method);
}
}, interceptors);
}
protected AbstractObjectInitializer(final Provider<ODatabaseObject> dbProvider,
final ObjectSchemeInitializer schemeInitializer,
final Matcher<? super Class<?>> classMatcher,
final String... packages) {
this.schemeInitializer = schemeInitializer;
this.dbProvider = dbProvider;
this.classMatcher = classMatcher;
this.packages = packages.length == 0 ? new String[]{""} : packages;
}
private Matcher<Method> dynamicValidationMatcher() {
return new AbstractMatcher<Method>() {
@Override
public boolean matches(Method method) {
return shouldValidateParameters(method) || shouldValidateReturnType(method);
}
};
}
private static Matcher<Method> buildMethodMatcher() {
MethodMatcherBuilder methodMatcherBuilder = new MethodMatcherBuilder(TransactionalResolver.INSTANCE);
if (TransactionPlugin.JTA_12_OPTIONAL.isPresent()) {
methodMatcherBuilder.or(JtaTransactionalResolver.INSTANCE);
}
return methodMatcherBuilder.build();
}
private <T> Matcher<T> createMatcherFromPredicate(Predicate<T> predicate) {
return new AbstractMatcher<T>() {
@Override
public boolean matches(T t) {
return predicate.test(t);
}
};
}
@SuppressWarnings("unchecked")
protected Matcher<? super Class<?>> getClassMatcher(final Class<? extends Annotation> annotation) {
final Matcher<AnnotatedElement> res = Matchers.annotatedWith(annotation);
return classMatcher == Matchers.any()
// combine custom filter with annotation
? res : res.and((Matcher<? super AnnotatedElement>) classMatcher);
}
@SuppressWarnings({"unchecked", "PMD.CompareObjectsWithEquals"})
protected Matcher<? super Method> getMethodMatcher(final Class<? extends Annotation> annotation) {
final Matcher<AnnotatedElement> res = Matchers.annotatedWith(annotation);
return methodMatcher == DECLARED_METHOD_MATCHER
// combine custom filter with annotation
? res : res.and((Matcher<? super AnnotatedElement>) methodMatcher);
}
public static void bindThriftDecorator(
Binder binder,
Matcher<? super Class<?>> classMatcher,
MethodInterceptor interceptor) {
binder.bindInterceptor(
classMatcher,
Matchers.returns(Matchers.subclassesOf(Response.class)),
interceptor);
binder.requestInjection(interceptor);
}
public static Matcher<? extends TypeLiteral<?>> subtypesOf(TypeLiteral<?> type) {
return (FunctionalMatcher<? extends TypeLiteral<?>>) t -> Types.isAssignable(type, t);
}
public static Matcher<? extends Binding<?>> bindingsForKeys(Matcher<? super Key<?>> keys) {
return (FunctionalMatcher<? extends Binding<?>>) binding -> keys.matches(binding.getKey());
}
public static Matcher<? extends Binding<?>> bindingsForTypeLiterals(Matcher<? super TypeLiteral<?>> types) {
return (FunctionalMatcher<? extends Binding<?>>) binding -> types.matches(binding.getKey().getTypeLiteral());
}
public static Matcher<? extends Binding<?>> bindingsForClasses(Matcher<? super Class<?>> types) {
return (FunctionalMatcher<? extends Binding<?>>) binding -> types.matches(binding.getKey().getTypeLiteral().getRawType());
}
public static Matcher<? extends Binding<?>> bindingsForSubtypesOf(TypeLiteral<?> type) {
return bindingsForTypeLiterals((Matcher<TypeLiteral<?>>) subtypesOf(type));
}
public static Matcher<? extends Binding<?>> bindingsForSubtypesOf(Class<?> type) {
return bindingsForSubtypesOf(TypeLiteral.get(type));
}
@Override
protected void bindInterceptor(Matcher<? super Class<?>> classMatcher,
Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors) {
super.bindInterceptor(classMatcher, NoSyntheticMethodMatcher.INSTANCE.and(methodMatcher),
interceptors);
}
/** Returns a matcher which matches methods with matching name. */
public static Matcher<Method> names(String methodName) {
return new Names(methodName);
}
public LogModule(final Matcher<Object> typeMatcher) {
this.typeMatcher = typeMatcher;
}
@Override
public void matching(Matcher<? super TypeLiteral<?>> typeMatcher) {
this.typeMatcher = checkNotNull(typeMatcher, "Argument 'typeMatcher' must be not null");
}
/**
* @return configured type matcher or null
*/
public Matcher<? super Class<?>> getTypeMatcher() {
return typeMatcher;
}
/**
* @return configured method matcher or null
*/
public Matcher<? super Method> getMethodMatcher() {
return methodMatcher;
}
private Matcher<Method> factoryMethods() {
return new MethodMatcherBuilder(ExecutablePredicates.<Method>executableBelongsToClassAssignableTo(Factory.class)
.and(m -> !m.getName().equals(GET_PRODUCED_CLASS))
.and(CreateResolver.INSTANCE)
).build();
}