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

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

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

TypeLiteral.getReturnType介绍

[英]Returns the resolved generic return type of method.
[中]返回方法的已解析泛型返回类型。

代码示例

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

@SuppressWarnings({"unchecked", "rawtypes"})
static TypeAndValue<?> typeAndValueOfMapKey(Annotation mapKeyAnnotation) {
 if (!mapKeyAnnotation.annotationType().getAnnotation(MapKey.class).unwrapValue()) {
  return new TypeAndValue(TypeLiteral.get(mapKeyAnnotation.annotationType()), mapKeyAnnotation);
 } else {
  try {
   Method valueMethod = mapKeyAnnotation.annotationType().getDeclaredMethod("value");
   valueMethod.setAccessible(true);
   TypeLiteral<?> returnType =
     TypeLiteral.get(mapKeyAnnotation.annotationType()).getReturnType(valueMethod);
   return new TypeAndValue(returnType, valueMethod.invoke(mapKeyAnnotation));
  } catch (NoSuchMethodException e) {
   throw new IllegalStateException(e);
  } catch (SecurityException e) {
   throw new IllegalStateException(e);
  } catch (IllegalAccessException e) {
   throw new IllegalStateException(e);
  } catch (InvocationTargetException e) {
   throw new IllegalStateException(e);
  }
 }
}

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

private <T> ProviderMethod<T> createProviderMethod(
  Binder binder, Method method, Annotation annotation) {
 binder = binder.withSource(method);
 Errors errors = new Errors(method);
 // prepare the parameter providers
 InjectionPoint point = InjectionPoint.forMethod(method, typeLiteral);
 @SuppressWarnings("unchecked") // Define T as the method's return type.
 TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
 Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
 try {
  key = scanner.prepareMethod(binder, annotation, key, point);
 } catch (Throwable t) {
  binder.addError(t);
 }
 Class<? extends Annotation> scopeAnnotation =
   Annotations.findScopeAnnotation(errors, method.getAnnotations());
 for (Message message : errors.getMessages()) {
  binder.addError(message);
 }
 return ProviderMethod.create(
   key,
   method,
   delegate,
   ImmutableSet.copyOf(point.getDependencies()),
   scopeAnnotation,
   skipFastClassGeneration,
   annotation);
}

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

@SuppressWarnings({"unchecked", "rawtypes"})
static TypeAndValue<?> typeAndValueOfMapKey(Annotation mapKeyAnnotation) {
 if (!mapKeyAnnotation.annotationType().getAnnotation(MapKey.class).unwrapValue()) {
  return new TypeAndValue(TypeLiteral.get(mapKeyAnnotation.annotationType()), mapKeyAnnotation);
 } else {
  try {
   Method valueMethod = mapKeyAnnotation.annotationType().getDeclaredMethod("value");
   valueMethod.setAccessible(true);
   TypeLiteral<?> returnType =
     TypeLiteral.get(mapKeyAnnotation.annotationType()).getReturnType(valueMethod);
   return new TypeAndValue(returnType, valueMethod.invoke(mapKeyAnnotation));
  } catch (NoSuchMethodException e) {
   throw new IllegalStateException(e);
  } catch (SecurityException e) {
   throw new IllegalStateException(e);
  } catch (IllegalAccessException e) {
   throw new IllegalStateException(e);
  } catch (InvocationTargetException e) {
   throw new IllegalStateException(e);
  }
 }
}

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

@SuppressWarnings({"unchecked", "rawtypes"})
static TypeAndValue<?> typeAndValueOfMapKey(Annotation mapKeyAnnotation) {
 if (!mapKeyAnnotation.annotationType().getAnnotation(MapKey.class).unwrapValue()) {
  return new TypeAndValue(TypeLiteral.get(mapKeyAnnotation.annotationType()), mapKeyAnnotation);
 } else {
  try {
   Method valueMethod = mapKeyAnnotation.annotationType().getDeclaredMethod("value");
   valueMethod.setAccessible(true);
   TypeLiteral<?> returnType =
     TypeLiteral.get(mapKeyAnnotation.annotationType()).getReturnType(valueMethod);
   return new TypeAndValue(returnType, valueMethod.invoke(mapKeyAnnotation));
  } catch (NoSuchMethodException e) {
   throw new IllegalStateException(e);
  } catch (SecurityException e) {
   throw new IllegalStateException(e);
  } catch (IllegalAccessException e) {
   throw new IllegalStateException(e);
  } catch (InvocationTargetException e) {
   throw new IllegalStateException(e);
  }
 }
}

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

@SuppressWarnings({"unchecked", "rawtypes"})
static TypeAndValue<?> typeAndValueOfMapKey(Annotation mapKeyAnnotation) {
  if (!mapKeyAnnotation.annotationType().getAnnotation(MapKey.class).unwrapValue()) {
    return new TypeAndValue(TypeLiteral.get(mapKeyAnnotation.annotationType()), mapKeyAnnotation);
  } else {
    try {
      Method valueMethod = mapKeyAnnotation.annotationType().getDeclaredMethod("value");
      valueMethod.setAccessible(true);
      TypeLiteral<?> returnType =
          TypeLiteral.get(mapKeyAnnotation.annotationType()).getReturnType(valueMethod);
      return new TypeAndValue(returnType, valueMethod.invoke(mapKeyAnnotation));
    } catch (NoSuchMethodException e) {
      throw new IllegalStateException(e);
    } catch (SecurityException e) {
      throw new IllegalStateException(e);
    } catch (IllegalAccessException e) {
      throw new IllegalStateException(e);
    } catch (InvocationTargetException e) {
      throw new IllegalStateException(e);
    }
  }
}

代码示例来源:origin: io.github.gwtplus.gin/gin

@Override
public TypeLiteral<?> getReturnType() {
 return getDeclaringType().getReturnType(getMember());
}

代码示例来源:origin: net.spals.appbuilder/spals-appbuilder-graph

@VisibleForTesting
void mergeProviderSourceVertices(final ServiceDAG serviceDAG) {
  // 1. Find all vertices within the service graph which are providers.
  final Set<IServiceDAGVertex<?>> providerSourceVertices =
    serviceDAG.findAllVertices(rawTypeThat(subclassesOf(Provider.class)));
  // 2. Search the service graph for the vertices which are provided by the provider vertices
  final Map<IServiceDAGVertex<?>, Optional<IServiceDAGVertex<?>>> providerSourceMap =
    providerSourceVertices.stream().collect(Collectors.toMap(Function.identity(),
      providerSourceVertex -> {
        final TypeLiteral<?> providerSourceLiteral = providerSourceVertex.getGuiceKey().getTypeLiteral();
        try {
          final Method getMethod = providerSourceLiteral.getRawType().getDeclaredMethod("get");
          final TypeLiteral<?> providedLiteral = providerSourceLiteral.getReturnType(getMethod);
          return serviceDAG.findVertex(Key.get(providedLiteral));
        } catch (Throwable t) {
          return Optional.empty();
        }
      }));
  // 3. For each provider, provided pair, merge them together within the service graph
  providerSourceMap.entrySet().stream()
    .filter(entry -> entry.getValue().isPresent())
    .forEach(entry -> {
      final IServiceDAGVertex<?> providerSourceVertex = entry.getKey();
      final IServiceDAGVertex<?> providedVertex = entry.getValue().get();
      final IServiceDAGVertex<?> mergedVertex = createDAGVertexWithProvider(providedVertex,
        providerSourceVertex);
      mergeVertices(serviceDAG, providerSourceVertex, providedVertex, mergedVertex);
    });
}

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

public static <F> Provider<F> newFactory(
  TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) {
 Map<Method, AssistedConstructor<?>> factoryMethodToConstructor =
   createMethodMapping(factoryType, implementationType);
 if (!factoryMethodToConstructor.isEmpty()) {
  return new FactoryProvider<F>(factoryType, implementationType, factoryMethodToConstructor);
 } else {
  BindingCollector collector = new BindingCollector();
  // Preserving backwards-compatibility:  Map all return types in a factory
  // interface to the passed implementation type.
  Errors errors = new Errors();
  Key<?> implementationKey = Key.get(implementationType);
  try {
   for (Method method : factoryType.getRawType().getMethods()) {
    Key<?> returnType =
      getKey(factoryType.getReturnType(method), method, method.getAnnotations(), errors);
    if (!implementationKey.equals(returnType)) {
     collector.addBinding(returnType, implementationType);
    }
   }
  } catch (ErrorsException e) {
   throw new ConfigurationException(e.getErrors().getMessages());
  }
  return new FactoryProvider2<F>(Key.get(factoryType), collector);
 }
}

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

public static <F> Provider<F> newFactory(
  TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) {
 Map<Method, AssistedConstructor<?>> factoryMethodToConstructor =
   createMethodMapping(factoryType, implementationType);
 if (!factoryMethodToConstructor.isEmpty()) {
  return new FactoryProvider<F>(factoryType, implementationType, factoryMethodToConstructor);
 } else {
  BindingCollector collector = new BindingCollector();
  // Preserving backwards-compatibility:  Map all return types in a factory
  // interface to the passed implementation type.
  Errors errors = new Errors();
  Key<?> implementationKey = Key.get(implementationType);
  try {
   for (Method method : factoryType.getRawType().getMethods()) {
    Key<?> returnType =
      getKey(factoryType.getReturnType(method), method, method.getAnnotations(), errors);
    if (!implementationKey.equals(returnType)) {
     collector.addBinding(returnType, implementationType);
    }
   }
  } catch (ErrorsException e) {
   throw new ConfigurationException(e.getErrors().getMessages());
  }
  return new FactoryProvider2<F>(Key.get(factoryType), collector);
 }
}

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

public static <F> Provider<F> newFactory(
  TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) {
 Map<Method, AssistedConstructor<?>> factoryMethodToConstructor =
   createMethodMapping(factoryType, implementationType);
 if (!factoryMethodToConstructor.isEmpty()) {
  return new FactoryProvider<F>(factoryType, implementationType, factoryMethodToConstructor);
 } else {
  BindingCollector collector = new BindingCollector();
  // Preserving backwards-compatibility:  Map all return types in a factory
  // interface to the passed implementation type.
  Errors errors = new Errors();
  Key<?> implementationKey = Key.get(implementationType);
  try {
   for (Method method : factoryType.getRawType().getMethods()) {
    Key<?> returnType =
      getKey(factoryType.getReturnType(method), method, method.getAnnotations(), errors);
    if (!implementationKey.equals(returnType)) {
     collector.addBinding(returnType, implementationType);
    }
   }
  } catch (ErrorsException e) {
   throw new ConfigurationException(e.getErrors().getMessages());
  }
  return new FactoryProvider2<F>(Key.get(factoryType), collector);
 }
}

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

public static <F> Provider<F> newFactory(
    TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) {
  Map<Method, AssistedConstructor<?>> factoryMethodToConstructor
      = createMethodMapping(factoryType, implementationType);
  if (!factoryMethodToConstructor.isEmpty()) {
    return new FactoryProvider<F>(factoryType, implementationType, factoryMethodToConstructor);
  } else {
    BindingCollector collector = new BindingCollector();
    // Preserving backwards-compatibility:  Map all return types in a factory
    // interface to the passed implementation type.
    Errors errors = new Errors();
    Key<?> implementationKey = Key.get(implementationType);
    try {
      for (Method method : factoryType.getRawType().getMethods()) {
        Key<?> returnType = getKey(factoryType.getReturnType(method), method,
            method.getAnnotations(), errors);
        if (!implementationKey.equals(returnType)) {
          collector.addBinding(returnType, implementationType);
        }
      }
    } catch (ErrorsException e) {
      throw new ConfigurationException(e.getErrors().getMessages());
    }
    return new FactoryProvider2<F>(Key.get(factoryType), collector);
  }
}

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

public static <F> Provider<F> newFactory(
  TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) {
 Map<Method, AssistedConstructor<?>> factoryMethodToConstructor
   = createMethodMapping(factoryType, implementationType);
 if (!factoryMethodToConstructor.isEmpty()) {
  return new FactoryProvider<F>(factoryType, factoryMethodToConstructor);
 } else {
  BindingCollector collector = new BindingCollector();
  // Preserving backwards-compatibility:  Map all return types in a factory
  // interface to the passed implementation type.
  Errors errors = new Errors();
  Key<?> implementationKey = Key.get(implementationType);
  if (implementationType != null) {
   try {
    for (Method method : factoryType.getRawType().getMethods()) {
     Key<?> returnType = getKey(factoryType.getReturnType(method), method,
       method.getAnnotations(), errors);
     if (!implementationKey.equals(returnType)) {
      collector.addBinding(returnType, implementationType);
     }
    }
   } catch (ErrorsException e) {
    throw new ConfigurationException(e.getErrors().getMessages());
   }
  }
  return new FactoryProvider2<F>(Key.get(factoryType), collector);
 }
}

代码示例来源:origin: io.github.gwtplus.gin/gin

Key<?> returnType = getKey(factoryType.getReturnType(method), method,
  method.getAnnotations(), errors);

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

TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);

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

TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);

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

TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);

代码示例来源:origin: com.mycila.com.google.inject.extensions/guice-assisted-inject

public static <F> Provider<F> newFactory(
  TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) {
 Map<Method, AssistedConstructor<?>> factoryMethodToConstructor
   = createMethodMapping(factoryType, implementationType);
 if (!factoryMethodToConstructor.isEmpty()) {
  return new FactoryProvider<F>(factoryType, factoryMethodToConstructor);
 } else {
  BindingCollector collector = new BindingCollector();
  // Preserving backwards-compatibility:  Map all return types in a factory
  // interface to the passed implementation type.
  Errors errors = new Errors();
  Key<?> implementationKey = Key.get(implementationType);
  if (implementationType != null) {
   try {
    for (Method method : factoryType.getRawType().getMethods()) {
     Key<?> returnType = getKey(factoryType.getReturnType(method), method,
       method.getAnnotations(), errors);
     if (!implementationKey.equals(returnType)) {
      collector.addBinding(returnType, implementationType);
     }
    }
   } catch (ErrorsException e) {
    throw new ConfigurationException(e.getErrors().getMessages());
   }
  }
  return new FactoryProvider2<F>(factoryType, collector);
 }
}

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

private <T> ProviderMethod<T> createProviderMethod(Binder binder, Method method,
                          Annotation annotation) {
  binder = binder.withSource(method);
  Errors errors = new Errors(method);
  // prepare the parameter providers
  InjectionPoint point = InjectionPoint.forMethod(method, typeLiteral);
  List<Dependency<?>> dependencies = point.getDependencies();
  List<Provider<?>> parameterProviders = Lists.newArrayList();
  for (Dependency<?> dependency : point.getDependencies()) {
    parameterProviders.add(binder.getProvider(dependency));
  }
  @SuppressWarnings("unchecked") // Define T as the method's return type.
      TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
  Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
  try {
    key = scanner.prepareMethod(binder, annotation, key, point);
  } catch (Throwable t) {
    binder.addError(t);
  }
  Class<? extends Annotation> scopeAnnotation
      = Annotations.findScopeAnnotation(errors, method.getAnnotations());
  for (Message message : errors.getMessages()) {
    binder.addError(message);
  }
  return ProviderMethod.create(key, method, delegate, ImmutableSet.copyOf(dependencies),
      parameterProviders, scopeAnnotation, skipFastClassGeneration, annotation);
}

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

private <T> ProviderMethod<T> createProviderMethod(
  Binder binder, Method method, Annotation annotation) {
 binder = binder.withSource(method);
 Errors errors = new Errors(method);
 // prepare the parameter providers
 InjectionPoint point = InjectionPoint.forMethod(method, typeLiteral);
 @SuppressWarnings("unchecked") // Define T as the method's return type.
 TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
 Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
 try {
  key = scanner.prepareMethod(binder, annotation, key, point);
 } catch (Throwable t) {
  binder.addError(t);
 }
 Class<? extends Annotation> scopeAnnotation =
   Annotations.findScopeAnnotation(errors, method.getAnnotations());
 for (Message message : errors.getMessages()) {
  binder.addError(message);
 }
 return ProviderMethod.create(
   key,
   method,
   delegate,
   ImmutableSet.copyOf(point.getDependencies()),
   scopeAnnotation,
   skipFastClassGeneration,
   annotation);
}

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

private <T> ProviderMethod<T> createProviderMethod(
  Binder binder, Method method, Annotation annotation) {
 binder = binder.withSource(method);
 Errors errors = new Errors(method);
 // prepare the parameter providers
 InjectionPoint point = InjectionPoint.forMethod(method, typeLiteral);
 @SuppressWarnings("unchecked") // Define T as the method's return type.
 TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
 Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
 try {
  key = scanner.prepareMethod(binder, annotation, key, point);
 } catch (Throwable t) {
  binder.addError(t);
 }
 Class<? extends Annotation> scopeAnnotation =
   Annotations.findScopeAnnotation(errors, method.getAnnotations());
 for (Message message : errors.getMessages()) {
  binder.addError(message);
 }
 return ProviderMethod.create(
   key,
   method,
   delegate,
   ImmutableSet.copyOf(point.getDependencies()),
   scopeAnnotation,
   skipFastClassGeneration,
   annotation);
}

相关文章