Search in sources :

Example 1 with GenericsContext

use of ru.vyarus.java.generics.resolver.context.GenericsContext in project dropwizard-guicey by xvik.

the class JerseyBinding method bindSpecificComponent.

/**
 * Binds jersey specific component (component implements jersey interface or extends class).
 * Specific binding is required for types directly supported by jersey (e.g. ExceptionMapper).
 * Such types must be bound to target interface directly, otherwise jersey would not be able to resolve them.
 * <p> If type is {@link HK2Managed}, binds directly.
 * Otherwise, use guice "bridge" factory to lazily bind type.</p>
 *
 * @param binder       hk binder
 * @param injector     guice injector
 * @param type         type which implements specific jersey interface or extends class
 * @param specificType specific jersey type (interface or abstract class)
 */
public static void bindSpecificComponent(final AbstractBinder binder, final Injector injector, final Class<?> type, final Class<?> specificType) {
    // resolve generics of specific type
    final GenericsContext context = GenericsResolver.resolve(type).type(specificType);
    final List<Type> genericTypes = context.genericTypes();
    final Type[] generics = genericTypes.toArray(new Type[0]);
    final Type binding = generics.length > 0 ? new ParameterizedTypeImpl(specificType, generics) : specificType;
    if (isHK2Managed(type)) {
        binder.bind(type).to(binding).in(Singleton.class);
    } else {
        // hk cant find different things in different situations, so uniform registration is impossible
        if (InjectionResolver.class.equals(specificType)) {
            binder.bindFactory(new GuiceComponentFactory<>(injector, type)).to(type).in(Singleton.class);
            binder.bind(type).to(binding).in(Singleton.class);
        } else {
            binder.bindFactory(new GuiceComponentFactory<>(injector, type)).to(type).to(binding).in(Singleton.class);
        }
    }
}
Also used : GenericsContext(ru.vyarus.java.generics.resolver.context.GenericsContext) Type(java.lang.reflect.Type) GuiceComponentFactory(ru.vyarus.dropwizard.guice.module.jersey.support.GuiceComponentFactory) ParameterizedTypeImpl(org.glassfish.hk2.utilities.reflection.ParameterizedTypeImpl)

Example 2 with GenericsContext

use of ru.vyarus.java.generics.resolver.context.GenericsContext in project guice-persist-orient by xvik.

the class GenericParamExtension method createParam.

@SuppressWarnings("unchecked")
private ParamInfo createParam(final ParamInfo<Generic> paramInfo, final DelegateParamsContext context) {
    final Class<?> requiredType = paramInfo.annotation.genericHolder();
    final GenericsContext generics = requiredType == Object.class ? context.getCallerContext().generics : context.getCallerContext().generics.type(requiredType);
    final String generic = paramInfo.annotation.value();
    final int position = paramInfo.position;
    final Method method = context.getDescriptorContext().method;
    check(method.getParameterTypes()[position].equals(Class.class), "Generic type parameter must be Class, but it's %s", paramInfo.type.getSimpleName());
    final Class<?> type = generics.generic(generic);
    check(type != null, "Generic type name '%s' not found in %s", generic, generics.currentClass());
    return new ParamInfo(paramInfo.position, type);
}
Also used : GenericsContext(ru.vyarus.java.generics.resolver.context.GenericsContext) Method(java.lang.reflect.Method) ParamInfo(ru.vyarus.guice.persist.orient.repository.core.spi.parameter.ParamInfo)

Example 3 with GenericsContext

use of ru.vyarus.java.generics.resolver.context.GenericsContext in project guice-persist-orient by xvik.

the class TargetMethodAnalyzer method findPossibleMethods.

/**
 * Analyze target bean methods, finding all matching (by parameters) methods.
 * If method name was specified, only methods with the same name resolved.
 *
 * @param target target bean type
 * @param params repository method params
 * @param hint   method name hint (may be null)
 * @return descriptor of all matching methods
 */
private static List<MatchedMethod> findPossibleMethods(final List<Class<?>> params, final Class<?> target, final String hint) {
    final List<MatchedMethod> possibilities = Lists.newArrayList();
    // use generics to enforce type checks
    final GenericsContext targetGenerics = GenericsResolver.resolve(target);
    for (Method method : target.getMethods()) {
        // method hint force to check only methods with this name
        final boolean methodHintValid = hint == null || method.getName().equals(hint);
        if (!isAcceptableMethod(method) || !methodHintValid) {
            continue;
        }
        final MatchedMethod matched = analyzeMethod(method, params, targetGenerics);
        if (matched != null) {
            possibilities.add(matched);
        }
    }
    return possibilities;
}
Also used : GenericsContext(ru.vyarus.java.generics.resolver.context.GenericsContext) Method(java.lang.reflect.Method)

Aggregations

GenericsContext (ru.vyarus.java.generics.resolver.context.GenericsContext)3 Method (java.lang.reflect.Method)2 Type (java.lang.reflect.Type)1 ParameterizedTypeImpl (org.glassfish.hk2.utilities.reflection.ParameterizedTypeImpl)1 GuiceComponentFactory (ru.vyarus.dropwizard.guice.module.jersey.support.GuiceComponentFactory)1 ParamInfo (ru.vyarus.guice.persist.orient.repository.core.spi.parameter.ParamInfo)1