private static List<TypeLiteral<?>> hierarchyFor(TypeLiteral<?> type) { List<TypeLiteral<?>> hierarchy = new ArrayList<>(); TypeLiteral<?> current = type; while (current.getRawType() != Object.class) { hierarchy.add(current); current = current.getSupertype(current.getRawType().getSuperclass()); } return hierarchy; }
private static List<TypeLiteral<?>> hierarchyFor(TypeLiteral<?> type) { List<TypeLiteral<?>> hierarchy = new ArrayList<>(); TypeLiteral<?> current = type; while (current.getRawType() != Object.class) { hierarchy.add(current); current = current.getSupertype(current.getRawType().getSuperclass()); } return hierarchy; }
private static List<TypeLiteral<?>> hierarchyFor(TypeLiteral<?> type) { List<TypeLiteral<?>> hierarchy = new ArrayList<>(); TypeLiteral<?> current = type; while (current.getRawType() != Object.class) { hierarchy.add(current); current = current.getSupertype(current.getRawType().getSuperclass()); } return hierarchy; }
private static List<TypeLiteral<?>> hierarchyFor(TypeLiteral<?> type) { List<TypeLiteral<?>> hierarchy = new ArrayList<TypeLiteral<?>>(); TypeLiteral<?> current = type; while (current.getRawType() != Object.class) { hierarchy.add(current); current = current.getSupertype(current.getRawType().getSuperclass()); } return hierarchy; }
private static List<TypeLiteral<?>> hierarchyFor(TypeLiteral<?> type) { List<TypeLiteral<?>> hierarchy = new ArrayList<TypeLiteral<?>>(); TypeLiteral<?> current = type; while (current.getRawType() != Object.class) { hierarchy.add(current); current = current.getSupertype(current.getRawType().getSuperclass()); } return hierarchy; }
private static List<TypeLiteral<?>> hierarchyFor(TypeLiteral<?> type) { List<TypeLiteral<?>> hierarchy = new ArrayList<TypeLiteral<?>>(); TypeLiteral<?> current = type; while (current.getRawType() != Object.class) { hierarchy.add(current); current = current.getSupertype(current.getRawType().getSuperclass()); } return hierarchy; }
private static List<TypeLiteral<?>> hierarchyFor(TypeLiteral<?> type) { List<TypeLiteral<?>> hierarchy = new ArrayList<TypeLiteral<?>>(); TypeLiteral<?> current = type; while (current.getRawType() != Object.class) { hierarchy.add(current); current = current.getSupertype(current.getRawType().getSuperclass()); } return hierarchy; }
/** * Resolves the type parameters of a super type based on the given concrete type. * * @param type The concrete type * @param superType The generic super type * @return Resolved super type parameters */ private static TypeLiteral<?>[] getSuperTypeParameters( final Class<?> type, final Class<?> superType ) { return TypeParameters.get( TypeLiteral.get( type ).getSupertype( superType ) ); }
@Override public void configure() { if (!bound) { // Explicitly bind module instance under a specific sub-type (not Module as Guice forbids that) bind(Key.get(TypeConverterSupport.class, Names.named(getClass().getName()))).toInstance(this); bound = true; } // make sure we pick up the right super type argument, i.e. Foo from TypeConverterSupport<Foo> final TypeLiteral<?> superType = TypeLiteral.get(getClass()).getSupertype(TypeConverterSupport.class); convertToTypes(Matchers.only(TypeArguments.get(superType, 0)), this); }
public void testGenericInjection() { final Injector injector = Guice.createInjector( new WireModule( new TestModule() ) ); final GenericInstance genericInstance = (GenericInstance) injector.getInstance( Key.get( X.class, Names.named( "GI" ) ) ); assertEquals( TypeLiteral.get( Integer.class ), TypeParameters.get( TypeLiteral.get( genericInstance.number.getClass() ).getSupertype( Z.class ), 0 ) ); assertEquals( TypeLiteral.get( String.class ), TypeParameters.get( TypeLiteral.get( genericInstance.chars.getClass() ).getSupertype( Z.class ), 0 ) ); assertEquals( ZImpl.class, genericInstance.random.getClass() ); }
public final void configure( final Binder binder ) { // make sure we pick up the right super type parameter, i.e. Foo from AbstractTypeConverter<Foo> final TypeLiteral<?> superType = TypeLiteral.get( getClass() ).getSupertype( AbstractTypeConverter.class ); binder.convertToTypes( Matchers.only( TypeParameters.get( superType, 0 ) ), this ); } }
public final void configure( final Binder binder ) { // make sure we pick up the right super type parameter, i.e. Foo from AbstractTypeConverter<Foo> final TypeLiteral<?> superType = TypeLiteral.get( getClass() ).getSupertype( AbstractTypeConverter.class ); binder.convertToTypes( Matchers.only( TypeParameters.get( superType, 0 ) ), this ); } }
@SuppressWarnings("unchecked") public static <T> TypeLiteral<T> getProvidedType( TypeLiteral<? extends Provider<? extends T>> initialProviderTypeLiteral, Errors errors) throws ErrorsException { TypeLiteral<? extends Provider<? extends T>> providerTypeLiteral = initialProviderTypeLiteral; while (providerTypeLiteral.getRawType() != Provider.class) { providerTypeLiteral = (TypeLiteral<? extends Provider<? extends T>>) providerTypeLiteral.getSupertype(Provider.class); } Type providerType = providerTypeLiteral.getType(); // If the Provider has no type parameter (raw Provider)... if (!(providerType instanceof ParameterizedType)) { throw errors.cannotInjectRawProvider().toException(); } Type entryType = ((ParameterizedType) providerType).getActualTypeArguments()[0]; return (TypeLiteral<T>) TypeLiteral.get(entryType); }
@SuppressWarnings("unchecked") public static <T> TypeLiteral<T> getProvidedType( TypeLiteral<? extends Provider<? extends T>> initialProviderTypeLiteral, Errors errors) throws ErrorsException { TypeLiteral<? extends Provider<? extends T>> providerTypeLiteral = initialProviderTypeLiteral; while (providerTypeLiteral.getRawType() != Provider.class) { providerTypeLiteral = (TypeLiteral<? extends Provider<? extends T>>) providerTypeLiteral.getSupertype(Provider.class); } Type providerType = providerTypeLiteral.getType(); // If the Provider has no type parameter (raw Provider)... if (!(providerType instanceof ParameterizedType)) { throw errors.cannotInjectRawProvider().toException(); } Type entryType = ((ParameterizedType) providerType).getActualTypeArguments()[0]; return (TypeLiteral<T>) TypeLiteral.get(entryType); }
/** Returns all the methods on the given type ignoring overloaded methods */ public static List<Method> getAllMethods(TypeLiteral<?> startType) { List<Method> answer = Lists.newArrayList(); Map<MethodKey, Method> boundMethods = Maps.newHashMap(); while (true) { Class<?> type = startType.getRawType(); if (type == Object.class) { break; } Method[] methods = type.getDeclaredMethods(); for (final Method method : methods) { MethodKey key = new MethodKey(method); if (boundMethods.get(key) == null) { boundMethods.put(key, method); answer.add(method); } } //startType = startType.getSupertype(type); Class<?> supertype = type.getSuperclass(); if (supertype == Object.class) { break; } startType = startType.getSupertype(supertype); } return answer; } }
public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) { Provider<EventCasterInternal> eventCasterProvider = null; for (final Class<?> interfaceClass : interfacesInInheritanceHierarchy(type.getRawType())) { final TypeLiteral<?> interfaceType = type.getSupertype(interfaceClass); if (bindings.contains(interfaceType)) { if(eventCasterProvider == null) { eventCasterProvider = encounter.getProvider(EventCasterInternal.class); } encounter.register(new RegisterInjectedEventListeners<I>(interfaceType, eventCasterProvider)); } } }
public <I> void hear(TypeLiteral<I> injectableType, TypeEncounter<I> encounter) { Set<Field> boundFields = Sets.newHashSet(); Map<MethodKey, Method> boundMethods = Maps.newHashMap(); TypeLiteral<?> startType = injectableType; while (true) { Class<?> type = startType.getRawType(); if (type == Object.class) { break; } Field[] fields = type.getDeclaredFields(); for (Field field : fields) { if (boundFields.add(field)) { bindAnnotationInjectorToField(encounter, startType, field); } } Method[] methods = type.getDeclaredMethods(); for (final Method method : methods) { MethodKey key = new MethodKey(method); if (boundMethods.get(key) == null) { boundMethods.put(key, method); bindAnnotationInjectionToMember(encounter, startType, method); } } Class<?> supertype = type.getSuperclass(); if (supertype == Object.class) { break; } startType = startType.getSupertype(supertype); } }
@SuppressWarnings({"unchecked", "rawtypes"}) protected void bindWebsocketRouteFactory() { Key key = getWebsocketRouteKey(); try { key.getTypeLiteral().getSupertype(WebsocketRoute.class); } catch (Exception ex) { throw new RuntimeException("The websocket route Key must implement " + WebsocketRoute.class.getName() + " : " + key); } Key routeKey = parameterizeWithContextInterfaces(WebsocketRoute.class); Key factoryKey = parameterizeWithContextInterfaces(WebsocketRouteFactory.class); // Bind as assisted factory Annotation annotation = key.getAnnotation(); if (annotation != null) { install(new FactoryModuleBuilder().implement(routeKey.getTypeLiteral(), annotation, key.getTypeLiteral()) .build(factoryKey)); } else { install(new FactoryModuleBuilder().implement(routeKey, key.getTypeLiteral()) .build(factoryKey)); } }
@SuppressWarnings({"unchecked", "rawtypes"}) protected void bindStaticResourceFactory() { Key key = getStaticResourceKey(); try { key.getTypeLiteral().getSupertype(StaticResource.class); } catch (Exception ex) { throw new RuntimeException("The static resource Key must implement " + StaticResource.class.getName() + " : " + key); } Key staticResourceKey = parameterizeWithRequestContext(StaticResource.class); Key factoryKey = parameterizeWithRequestContext(StaticResourceFactory.class); // Bind as assisted factory Annotation annotation = key.getAnnotation(); if (annotation != null) { install(new FactoryModuleBuilder().implement(staticResourceKey.getTypeLiteral(), annotation, key.getTypeLiteral()) .build(factoryKey)); } else { install(new FactoryModuleBuilder().implement(staticResourceKey, key.getTypeLiteral()) .build(factoryKey)); } }
@SuppressWarnings({"unchecked", "rawtypes"}) protected void bindRouteFactory() { Key key = getRouteKey(); try { key.getTypeLiteral().getSupertype(Route.class); } catch (Exception ex) { throw new RuntimeException("The route Key must implement " + Route.class.getName() + " : " + key); } Key routeKey = parameterizeWithRequestContext(Route.class); Key factoryKey = parameterizeWithRequestContext(RouteFactory.class); // Bind as assisted factory Annotation annotation = key.getAnnotation(); if (annotation != null) { install(new FactoryModuleBuilder().implement(routeKey.getTypeLiteral(), annotation, key.getTypeLiteral()) .build(factoryKey)); } else { install(new FactoryModuleBuilder().implement(routeKey, key.getTypeLiteral()) .build(factoryKey)); } }