com.google.inject.matcher.Matcher Java Examples

The following examples show how to use com.google.inject.matcher.Matcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GuiceUtils.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
          }
        }
      });
}
 
Example #2
Source File: GuiceUtils.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
/**
 * 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));
    }
  };
}
 
Example #3
Source File: FunctionalMatcher.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@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);
        }
    };
}
 
Example #4
Source File: OrientModule.java    From guice-persist-orient with MIT License 5 votes vote down vote up
@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);
}
 
Example #5
Source File: AbstractObjectInitializer.java    From guice-persist-orient with MIT License 5 votes vote down vote up
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;
}
 
Example #6
Source File: ValidationModule.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
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;
        }
    };
}
 
Example #7
Source File: ValidationModule.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
private Matcher<Method> dynamicValidationMatcher() {
    return new AbstractMatcher<Method>() {
        @Override
        public boolean matches(Method method) {
            return shouldValidateParameters(method) || shouldValidateReturnType(method);
        }
    };
}
 
Example #8
Source File: TransactionModule.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
private static Matcher<Method> buildMethodMatcher() {
    MethodMatcherBuilder methodMatcherBuilder = new MethodMatcherBuilder(TransactionalResolver.INSTANCE);
    if (TransactionPlugin.JTA_12_OPTIONAL.isPresent()) {
        methodMatcherBuilder.or(JtaTransactionalResolver.INSTANCE);
    }
    return methodMatcherBuilder.build();
}
 
Example #9
Source File: MultiTransactionalModule.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #10
Source File: JdbcModule.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@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);

}
 
Example #11
Source File: CoreModule.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
private <T> Matcher<T> createMatcherFromPredicate(Predicate<T> predicate) {
    return new AbstractMatcher<T>() {
        @Override
        public boolean matches(T t) {
            return predicate.test(t);
        }
    };
}
 
Example #12
Source File: AbstractInterceptorModule.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@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);
}
 
Example #13
Source File: ValidationModule.java    From guice-validator with MIT License 5 votes vote down vote up
@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);
}
 
Example #14
Source File: ValidationModule.java    From guice-validator with MIT License 5 votes vote down vote up
@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);
}
 
Example #15
Source File: AopModule.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #16
Source File: AbstractAspect.java    From act-platform with ISC License 5 votes vote down vote up
/**
 * 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);
    }
  };
}
 
Example #17
Source File: FunctionalMatcher.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@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);
        }
    };
}
 
Example #18
Source File: ELBinder.java    From seed with Mozilla Public License 2.0 4 votes vote down vote up
private Matcher<Method> handlerMethodMatcher(final Class<? extends Annotation> annotationClass) {
    return new MethodMatcherBuilder(AnnotationPredicates.elementAnnotatedWith(annotationClass, true)).build();
}
 
Example #19
Source File: MethodMatcherBuilder.java    From seed with Mozilla Public License 2.0 4 votes vote down vote up
public Matcher<Method> build() {
    return new PredicateMatcherAdapter(
            ExecutablePredicates.<Method>executableIsSynthetic().negate().and(predicate));
}
 
Example #20
Source File: ValidationModule.java    From guice-validator with MIT License 4 votes vote down vote up
protected Matcher<? super Method> getValidatedMethodMatcher() {
    final Matcher<Method> res = new ValidatedMethodMatcher();
    // combine custom filter with annotation
    return methodMatcher == Matchers.any() ? res : res.and(methodMatcher);
}
 
Example #21
Source File: DomainModule.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
private Matcher<Method> factoryMethods() {
    return new MethodMatcherBuilder(ExecutablePredicates.<Method>executableBelongsToClassAssignableTo(Factory.class)
            .and(m -> !m.getName().equals(GET_PRODUCED_CLASS))
            .and(CreateResolver.INSTANCE)
    ).build();
}
 
Example #22
Source File: DefaultToSoyDataConverter.java    From spring-soy-view with Apache License 2.0 4 votes vote down vote up
public void setIgnorablePropertiesMatcher(
		Matcher<PropertyDescriptor> ignorablePropertiesMatcher) {
	this.ignorablePropertiesMatcher = ignorablePropertiesMatcher;
}
 
Example #23
Source File: GuiceAopConfig.java    From dropwizard-guicey with MIT License 4 votes vote down vote up
/**
 * @return configured type matcher or null
 */
public Matcher<? super Class<?>> getTypeMatcher() {
    return typeMatcher;
}
 
Example #24
Source File: Matchers.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Matcher<? extends TypeLiteral<?>> subtypesOf(TypeLiteral<?> type) {
    return (FunctionalMatcher<? extends TypeLiteral<?>>) t -> Types.isAssignable(type, t);
}
 
Example #25
Source File: Matchers.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Matcher<? extends Binding<?>> bindingsForKeys(Matcher<? super Key<?>> keys) {
    return (FunctionalMatcher<? extends Binding<?>>) binding -> keys.matches(binding.getKey());
}
 
Example #26
Source File: Matchers.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Matcher<? extends Binding<?>> bindingsForTypeLiterals(Matcher<? super TypeLiteral<?>> types) {
    return (FunctionalMatcher<? extends Binding<?>>) binding -> types.matches(binding.getKey().getTypeLiteral());
}
 
Example #27
Source File: Matchers.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Matcher<? extends Binding<?>> bindingsForClasses(Matcher<? super Class<?>> types) {
    return (FunctionalMatcher<? extends Binding<?>>) binding -> types.matches(binding.getKey().getTypeLiteral().getRawType());
}
 
Example #28
Source File: Matchers.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Matcher<? extends Binding<?>> bindingsForSubtypesOf(TypeLiteral<?> type) {
    return bindingsForTypeLiterals((Matcher<TypeLiteral<?>>) subtypesOf(type));
}
 
Example #29
Source File: Matchers.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Matcher<? extends Binding<?>> bindingsForSubtypesOf(Class<?> type) {
    return bindingsForSubtypesOf(TypeLiteral.get(type));
}
 
Example #30
Source File: FrameModule.java    From bobcat with Apache License 2.0 4 votes vote down vote up
@Override
protected void bindInterceptor(Matcher<? super Class<?>> classMatcher,
    Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors) {
  super.bindInterceptor(classMatcher, NoSyntheticMethodMatcher.INSTANCE.and(methodMatcher),
      interceptors);
}