下面列出了怎么用com.google.inject.matcher.AbstractMatcher的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void configure() {
registerConfigFiles(this.configFiles);
bind(IRestExportService.class).to(RestExportService.class).in(Scopes.SINGLETON);
bind(IRestImportService.class).to(RestImportService.class).in(Scopes.SINGLETON);
bind(IRestGitBackupService.class).to(RestGitBackupService.class).in(Scopes.SINGLETON);
// AutoGit Injection - all methods that are annotated with @ConfigurationUpdate and start with
// update or delete trigger a new commit/push
bindInterceptor(Matchers.any(), new AbstractMatcher<>() {
@Override
public boolean matches(Method method) {
return method.isAnnotationPresent(IResourceStore.ConfigurationUpdate.class) && !method.isSynthetic();
}
}, new GitConfigurationUpdateService(getProvider(IRestGitBackupService.class),
getProvider(IBotStore.class),
getProvider(IDeploymentStore.class),
getProvider(IPackageStore.class),
getProvider(IJsonSerialization.class)));
bind(IZipArchive.class).to(ZipArchive.class).in(Scopes.SINGLETON);
}
@Override
protected void configure() {
convertToTypes(new AbstractMatcher<TypeLiteral<?>>() {
@Override
public boolean matches(TypeLiteral<?> typeLiteral) {
return typeLiteral.getType().equals(Sample.class);
}
}, new TypeConverter() {
@Override
public Object convert(String value, TypeLiteral<?> toType) {
return new Sample();
}
});
bindConstant().annotatedWith(Names.named("smth")).to(12);
bindConstant().annotatedWith(Names.named("string")).to("12");
}
@Override
protected void configure() {
bindListener(new AbstractMatcher<TypeLiteral<?>>() {
@Override
public boolean matches(TypeLiteral<?> typeLiteral) {
return false;
}
}, new CustomTypeListener());
bindListener(new AbstractMatcher<Object>() {
@Override
public boolean matches(Object o) {
return false;
}
}, new CustomProvisionListener());
bindInterceptor(Matchers.subclassesOf(AopedService.class), Matchers.any(),
new CustomAop());
bind(AopedService.class);
bind(BindService.class).to(OverriddenService.class);
bind(BindService2.class).toInstance(new BindService2() {});
}
/**
* Configures repository annotations interceptor.
*/
protected void configureAop() {
final RepositoryMethodInterceptor proxy = new RepositoryMethodInterceptor();
requestInjection(proxy);
// repository specific method annotations (query, function, delegate, etc.)
bindInterceptor(Matchers.any(), new AbstractMatcher<Method>() {
@Override
public boolean matches(final Method method) {
// this will throw error if two or more annotations specified (fail fast)
try {
return ExtUtils.findMethodAnnotation(method) != null;
} catch (Exception ex) {
throw new MethodDefinitionException(String.format("Error declaration on method %s",
RepositoryUtils.methodToString(method)), ex);
}
}
}, proxy);
}
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));
}
};
}
/**
* 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 configure() {
bind(GitHubOAuthAuthenticator.class)
.to(OpenShiftGitHubOAuthAuthenticator.class)
.asEagerSingleton();
bind(OAuthAPIProvider.class).to(Fabric8OAuthAPIProvider.class);
bind(KeycloakServiceClient.class).to(Fabric8AuthServiceClient.class).asEagerSingleton();
final DisableAuthenticationInterceptor disableAuthenticationInterceptor =
new DisableAuthenticationInterceptor();
requestInjection(disableAuthenticationInterceptor);
bindInterceptor(
Matchers.subclassesOf(MultiUserEnvironmentInitializationFilter.class),
new AbstractMatcher<Method>() {
@Override
public boolean matches(Method m) {
return "doFilter".equals(m.getName());
}
},
disableAuthenticationInterceptor);
final Multibinder<MachineAuthenticatedResource> machineAuthenticatedResources =
Multibinder.newSetBinder(binder(), MachineAuthenticatedResource.class);
machineAuthenticatedResources
.addBinding()
.toInstance(
new MachineAuthenticatedResource(
"/fabric8-che-analytics", "segmentWriteKey", "woopraDomain", "warning", "error"));
machineAuthenticatedResources
.addBinding()
.toInstance(new MachineAuthenticatedResource("/bayesian", "getBayesianToken"));
}
/**
* Binds an {@link AdaptableTypeListener} (via
* {@link #bindListener(Matcher, TypeListener)}) and ensures it gets
* properly injected ({@link #requestInjection(Object)}).
*/
@Override
protected void configure() {
AdaptableTypeListener adaptableTypeListener = new AdaptableTypeListener(
loggingMode);
requestInjection(adaptableTypeListener);
bindListener(new AbstractMatcher<TypeLiteral<?>>() {
@Override
public boolean matches(TypeLiteral<?> t) {
return IAdaptable.class.isAssignableFrom(t.getRawType());
}
}, adaptableTypeListener);
}
@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);
}
private Matcher<Method> dynamicValidationMatcher() {
return new AbstractMatcher<Method>() {
@Override
public boolean matches(Method method) {
return shouldValidateParameters(method) || shouldValidateReturnType(method);
}
};
}
private <T> Matcher<T> createMatcherFromPredicate(Predicate<T> predicate) {
return new AbstractMatcher<T>() {
@Override
public boolean matches(T t) {
return predicate.test(t);
}
};
}