}
private void outputInterfaceMethods(GinjectorBindings bindings, TypeLiteral<?> ginjectorInterface,
SourceWriteUtil sourceWriteUtil, SourceWriter writer)
throws NoSourceNameException, UnableToCompleteException {
NameGenerator nameGenerator = bindings.getNameGenerator();
// Implement a provider method for each zero-arg method in the ginjector
// interface.
for (MethodLiteral<?, Method> method :
constructorInjectCollector.getMethods(ginjectorInterface)) {
Key<?> methodKey = guiceUtil.getKey(method);
Binding binding = bindings.getBinding(methodKey);
if (binding == null) {
// This should not happen, but fail with a meaningful message if it
// does.
logger.log(TreeLogger.Type.ERROR, "Unable to find a binding for the required key "
+ methodKey);
throw new UnableToCompleteException();
}
if (!reachabilityAnalyzer.isReachable(binding)) {
// Sanity-check reachability: every binding in the Ginjector ought to be
// reachable.
PrettyPrinter.log(logger, TreeLogger.Type.ERROR,
"The key %s is required by the Ginjector, but is not reachable.", methodKey);
throw new UnableToCompleteException();
}
FragmentPackageName fragmentPackageName = fragmentPackageNameFactory.create(
binding.getGetterMethodPackage());
String body = String.format("return %s.%s().%s();",
ginjectorNameGenerator.getFieldName(bindings),
nameGenerator.getFragmentGetterMethodName(fragmentPackageName),
nameGenerator.getGetterMethodName(guiceUtil.getKey(method)));
String readableDeclaration = ReflectUtil.signatureBuilder(method)
.removeAbstractModifier()
.build();
sourceWriteUtil.writeMethod(writer, readableDeclaration, body.toString());
}
// Implements methods of the form "void foo(BarType bar)", which run member
// injection on the given BarType.
for (MethodLiteral<?, Method> method : memberInjectCollector.getMethods(ginjectorInterface)) {
Key<?> injectee = guiceUtil.getKey(method);
if (!reachabilityAnalyzer.isReachableMemberInject(bindings, injectee.getTypeLiteral())) {
// Sanity-check reachability: every member injection in the Ginjector
// ought to be reachable.
PrettyPrinter.log(logger, TreeLogger.Type.ERROR,
"Method injection of %s is required by the Ginjector, but is not reachable.",
injectee.getTypeLiteral());
throw new UnableToCompleteException();
}
FragmentPackageName fragmentPackageName = fragmentPackageNameFactory.create(
ReflectUtil.getUserPackageName(injectee.getTypeLiteral()));
String body = String.format("%s.%s().%s(param);",
ginjectorNameGenerator.getFieldName(bindings),
nameGenerator.getFragmentGetterMethodName(fragmentPackageName),
nameGenerator.getMemberInjectMethodName(injectee.getTypeLiteral()));
String readableDeclaration = ReflectUtil.signatureBuilder(method)
.withParameterNames(new String[]{"param"})
.removeAbstractModifier()
.build();