下面列出了怎么用com.google.inject.spi.ConstructorBinding的API类实例代码及写法,或者点击链接到github查看源代码。
public static <T> Optional<TypeLiteral<? extends T>> targetType(Injector injector, Binding<T> binding) {
if(binding instanceof UntargettedBinding) {
return Optional.of(binding.getKey().getTypeLiteral());
} else if(binding instanceof ConstructorBinding) {
return Optional.of((TypeLiteral<? extends T>) ((ConstructorBinding) binding).getConstructor().getDeclaringType());
} else if(binding instanceof InstanceBinding) {
return Optional.of(TypeLiteral.get((Class<T>) ((InstanceBinding) binding).getInstance().getClass()));
} else if(binding instanceof LinkedKeyBinding) {
return targetType(injector, injector.getBinding(((LinkedKeyBinding) binding).getLinkedKey()));
} else if(binding instanceof ExposedBinding) {
return targetType(((ExposedBinding) binding).getPrivateElements().getInjector(), binding.getKey());
}
return Optional.empty();
}
@Override
public String renderReport(final GuiceAopConfig config) {
final StringBuilder res = new StringBuilder();
// AOP declarations
final List<Element> declared = Elements.getElements(Stage.TOOL, modules).stream()
.filter(it -> it instanceof InterceptorBinding)
.collect(Collectors.toList());
if (!config.isHideDeclarationsBlock()) {
final List<ModuleDeclaration> declarationModules = GuiceModelParser.parse(injector, declared);
res.append(Reporter.NEWLINE).append(Reporter.NEWLINE);
renderDeclared(declarationModules, res);
}
if (!declared.isEmpty()) {
// AOP appliance map
final List<Binding> guiceManagedObjects = injector.getAllBindings().values().stream()
.filter(it -> it instanceof ConstructorBinding)
.collect(Collectors.toList());
final List<AopDeclaration> tree = filter(GuiceModelParser.parse(injector, guiceManagedObjects), config);
if (!tree.isEmpty()) {
res.append(Reporter.NEWLINE).append(Reporter.NEWLINE);
renderMap(tree, res);
}
}
return res.toString();
}
@SuppressWarnings("unchecked")
private List<AopDeclaration> filter(final List<ModuleDeclaration> modules, final GuiceAopConfig config) {
final List<AopDeclaration> res = new ArrayList<>();
GuiceModelUtils.visitBindings(modules, it -> {
final Class<?> type = it.getKey().getTypeLiteral().getRawType();
// filter by class
if (config.getTypeMatcher() != null && !config.getTypeMatcher().matches(type)) {
return;
}
final AopDeclaration dec = new AopDeclaration(it.getKey());
final Map<Method, List<MethodInterceptor>> interceptors =
((ConstructorBinding) it.getElement()).getMethodInterceptors();
for (Map.Entry<Method, List<MethodInterceptor>> entry : interceptors.entrySet()) {
final Method method = entry.getKey();
// filter by method
if (config.getMethodMatcher() != null && !config.getMethodMatcher().matches(method)) {
continue;
}
final List<Class<? extends MethodInterceptor>> handlers = entry.getValue()
.stream().map(MethodInterceptor::getClass).collect(Collectors.toList());
// filter by required interceptors
if (config.getShowOnly().isEmpty() || !Collections.disjoint(config.getShowOnly(), handlers)) {
dec.getInterceptors().put(method, handlers);
}
}
if (!dec.getInterceptors().isEmpty()) {
res.add(dec);
}
});
// for predictable order in report
res.sort(Comparator.comparing(it -> it.getKey().getTypeLiteral().getRawType().getSimpleName()));
return res;
}
@Override
public Optional<TypeLiteral<?>> visit(ConstructorBinding<?> binding) {
// Return the owning type of the constructor
return of(binding.getConstructor().getDeclaringType());
}
@Override
public Object visit(ConstructorBinding<? extends T> constructorBinding) {
processInjectionPoint(constructorBinding.getConstructor());
processInjectionPoints(constructorBinding.getInjectableMembers());
return super.visit(constructorBinding);
}
@Override
public Void visit(ConstructorBinding<? extends T> binding) {
final InjectionPoint point = binding.getConstructor();
scope(binding, rebind(binding).toConstructor((Constructor) point.getMember(), point.getDeclaringType()));
return null;
}
@Override
public TypeToken<?> visit(
ConstructorBinding<? extends Object> binding) {
return TypeToken.of(binding.getKey().getTypeLiteral().getType());
}
@Override
public MapBinderBinding<?> visit(
ConstructorBinding<? extends Object> binding) {
return null;
}