com.google.inject.TypeLiteral.getSupertype()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(83)

本文整理了Java中com.google.inject.TypeLiteral.getSupertype()方法的一些代码示例,展示了TypeLiteral.getSupertype()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TypeLiteral.getSupertype()方法的具体详情如下:
包路径:com.google.inject.TypeLiteral
类名称:TypeLiteral
方法名:getSupertype

TypeLiteral.getSupertype介绍

[英]Returns the generic form of supertype. For example, if this is ArrayList, this returns Iterable given the input Iterable.class.
[中]返回超类型的泛型形式。例如,如果这是ArrayList,则在给定输入Iterable的情况下返回Iterable。班

代码示例

代码示例来源:origin: com.google.inject/guice

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;
}

代码示例来源:origin: org.sonatype.sisu/sisu-guice

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;
}

代码示例来源:origin: com.jwebmp.inject/guice

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;
}

代码示例来源:origin: com.google/inject

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;
}

代码示例来源:origin: org.xbib/guice

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;
}

代码示例来源:origin: at.bestsolution.efxclipse.eclipse/com.google.inject

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;
}

代码示例来源:origin: Nextdoor/bender

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;
}

代码示例来源:origin: com.atlassian.org.eclipse.sisu/org.eclipse.sisu.inject

/**
 * 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 ) );
}

代码示例来源:origin: org.sonatype.sisu.goodies/goodies-inject

@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);
}

代码示例来源:origin: org.eclipse.sisu/org.eclipse.sisu.inject.tests

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() );
}

代码示例来源:origin: com.atlassian.org.eclipse.sisu/org.eclipse.sisu.inject

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 );
  }
}

代码示例来源:origin: org.sonatype.sisu.inject/guice-bean-converters

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 );
  }
}

代码示例来源:origin: ArcBees/Jukito

@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);
}

代码示例来源:origin: org.jukito/jukito

@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);
}

代码示例来源:origin: org.guiceyfruit/guiceyfruit-core

/** 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;
 }
}

代码示例来源:origin: com.lexicalscope.eventcast/eventcast

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));
    }
  }
}

代码示例来源:origin: org.guiceyfruit/guiceyfruit-core

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);
 }
}

代码示例来源:origin: org.spincast/spincast-plugins-routing

@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));
  }
}

代码示例来源:origin: org.spincast/spincast-plugins-routing

@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));
  }
}

代码示例来源:origin: org.spincast/spincast-plugins-routing

@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));
  }
}

相关文章