本文整理汇总了Java中com.google.inject.spi.Dependency.getParameterIndex方法的典型用法代码示例。如果您正苦于以下问题:Java Dependency.getParameterIndex方法的具体用法?Java Dependency.getParameterIndex怎么用?Java Dependency.getParameterIndex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.inject.spi.Dependency
的用法示例。
在下文中一共展示了Dependency.getParameterIndex方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkForNull
import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
/**
* Returns {@code value} if it is non-null allowed to be null. Otherwise a message is added and
* an {@code ErrorsException} is thrown.
*/
public <T> T checkForNull(T value, Object source, Dependency<?> dependency)
throws ErrorsException {
if (value != null || dependency.isNullable() ) {
return value;
}
int parameterIndex = dependency.getParameterIndex();
String parameterName = (parameterIndex != -1)
? "parameter " + parameterIndex + " of "
: "";
addMessage("null returned by binding at %s%n but %s%s is not @Nullable",
source, parameterName, dependency.getInjectionPoint().getMember());
throw toException();
}
示例2: findInjectionPoint
import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
@Nullable
private static InjectionPoint findInjectionPoint(List<DependencyAndSource> dependencyChain) {
if (dependencyChain.size() < 3) {
new AssertionError("Provider is not included in the dependency chain").printStackTrace();
}
// @Inject InjectionPoint is the last, so we can skip it
for (int i = dependencyChain.size() - 2; i >= 0; i--) {
final Dependency<?> dependency = dependencyChain.get(i).getDependency();
if (dependency == null) {
return null;
}
final com.google.inject.spi.InjectionPoint spiInjectionPoint = dependency.getInjectionPoint();
if (spiInjectionPoint != null) {
final TypeToken<?> source = TypeToken.of(spiInjectionPoint.getDeclaringType().getType());
final Member member = spiInjectionPoint.getMember();
final InjectionPoint injectionPoint;
if (member instanceof Field) {
final Field field = (Field) member;
injectionPoint = new InjectionPoint(source, TypeToken.of(field.getGenericType()), field.getAnnotations());
} else if (member instanceof Executable) {
final Executable executable = (Executable) member;
final Annotation[][] parameterAnnotations = executable.getParameterAnnotations();
final Type[] parameterTypes = executable.getGenericParameterTypes();
final int index = dependency.getParameterIndex();
injectionPoint = new InjectionPoint(source, TypeToken.of(parameterTypes[index]), parameterAnnotations[index]);
} else {
throw new IllegalStateException("Unsupported Member type: " + member.getClass().getName());
}
return injectionPoint;
}
}
return null;
}
示例3: onNullInjectedIntoNonNullableDependency
import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
/**
* Returns {@code value} if it is non-null or allowed to be null. Otherwise a message is added and
* an {@code InternalProvisionException} is thrown.
*/
static void onNullInjectedIntoNonNullableDependency(Object source, Dependency<?> dependency)
throws InternalProvisionException {
// Hack to allow null parameters to @Provides methods, for backwards compatibility.
if (dependency.getInjectionPoint().getMember() instanceof Method) {
Method annotated = (Method) dependency.getInjectionPoint().getMember();
if (annotated.isAnnotationPresent(Provides.class)) {
switch (InternalFlags.getNullableProvidesOption()) {
case ERROR:
break; // break out & let the below exception happen
case IGNORE:
return; // user doesn't care about injecting nulls to [email protected]
case WARN:
// Warn only once, otherwise we spam logs too much.
if (warnedDependencies.add(dependency)) {
logger.log(
Level.WARNING,
"Guice injected null into {0} (a {1}), please mark it @Nullable."
+ " Use -Dguice_check_nullable_provides_params=ERROR to turn this into an"
+ " error.",
new Object[] {
Messages.formatParameter(dependency), Messages.convert(dependency.getKey())
});
}
return;
}
}
}
Object formattedDependency =
(dependency.getParameterIndex() != -1)
? Messages.formatParameter(dependency)
: StackTraceElements.forMember(dependency.getInjectionPoint().getMember());
throw InternalProvisionException.create(
"null returned by binding at %s%n but %s is not @Nullable", source, formattedDependency)
.addSource(source);
}
示例4: formatParameter
import com.google.inject.spi.Dependency; //导入方法依赖的package包/类
static String formatParameter(Dependency<?> dependency) {
int ordinal = dependency.getParameterIndex() + 1;
return String.format(
"the %s%s parameter of %s",
ordinal,
getOrdinalSuffix(ordinal),
StackTraceElements.forMember(dependency.getInjectionPoint().getMember()));
}