* Outputs all the static injection methods for the given class.
*/
void outputStaticInjectionMethods(Class<?> type, FragmentMap fragments,
NameGenerator nameGenerator, SourceWriteUtil sourceWriteUtil) {
String methodName = nameGenerator.convertToValidMemberName("injectStatic_" + type.getName());
SourceSnippetBuilder body = new SourceSnippetBuilder();
for (InjectionPoint injectionPoint : InjectionPoint.forStaticMethodsAndFields(type)) {
Member member = injectionPoint.getMember();
try {
List<InjectorMethod> staticInjectionHelpers = new ArrayList<InjectorMethod>();
if (member instanceof Method) {
MethodLiteral<?, Method> method =
MethodLiteral.get((Method) member, TypeLiteral.get(member.getDeclaringClass()));
body.append(methodCallUtil.createMethodCallWithInjection(method, null, nameGenerator,
staticInjectionHelpers));
} else if (member instanceof Field) {
FieldLiteral<?> field =
FieldLiteral.get((Field) member, TypeLiteral.get(member.getDeclaringClass()));
body.append(sourceWriteUtil.createFieldInjection(field, null, nameGenerator,
staticInjectionHelpers));
}
outputMethods(staticInjectionHelpers, fragments);
} catch (NoSourceNameException e) {
errorManager.logError(e.getMessage(), e);
}
}
// Note that the top-level method that performs static injection will only
// invoke a bunch of other injector methods. Therefore, it doesn't matter
// which package it goes in, and we don't need to invoke getUserPackageName
// (which is good, because in practice users statically inject types that
// have no user package name because they're private inner classes!)
String packageName = type.getPackage().getName();
InjectorMethod method = SourceSnippets.asMethod(false, "private void " + methodName + "()",
packageName, body.build());
GinjectorFragmentOutputter fragment =
fragments.get(fragmentPackageNameFactory.create(packageName));
fragment.outputMethod(method);
fragment.invokeInInitializeStaticInjections(methodName);
}