use of com.google.gwt.inject.rebind.reflect.NoSourceNameException in project google-gin by gwtplus.
the class GinjectorBindingsOutputter method outputStaticInjectionMethods.
/**
* 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);
}
use of com.google.gwt.inject.rebind.reflect.NoSourceNameException in project google-gin by gwtplus.
the class GinjectorBindingsOutputter method outputMemberInjections.
/**
* Adds member injections to each fragment.
*/
private void outputMemberInjections(GinjectorBindings bindings, FragmentMap fragments, SourceWriteUtil sourceWriteUtil) {
NameGenerator nameGenerator = bindings.getNameGenerator();
for (TypeLiteral<?> type : bindings.getMemberInjectRequests()) {
if (!reachabilityAnalyzer.isReachableMemberInject(bindings, type)) {
continue;
}
List<InjectorMethod> memberInjectionHelpers = new ArrayList<InjectorMethod>();
try {
sourceWriteUtil.createMemberInjection(type, nameGenerator, memberInjectionHelpers);
outputMethods(memberInjectionHelpers, fragments);
} catch (NoSourceNameException e) {
errorManager.logError(e.getMessage(), e);
}
}
}
use of com.google.gwt.inject.rebind.reflect.NoSourceNameException in project google-gin by gwtplus.
the class GinjectorFragmentOutputter method writeBindingGetter.
/**
* Writes a method describing the getter for the given key, along with any
* other code necessary to support it. Produces a list of helper methods that
* still need to be written.
*/
void writeBindingGetter(Key<?> key, Binding binding, GinScope scope, List<InjectorMethod> helperMethodsOutput) {
Context bindingContext = binding.getContext();
SourceSnippetBuilder getterBuilder = new SourceSnippetBuilder();
SourceSnippet creationStatements;
String getter = nameGenerator.getGetterMethodName(key);
String typeName;
try {
typeName = ReflectUtil.getSourceName(key.getTypeLiteral());
creationStatements = binding.getCreationStatements(nameGenerator, helperMethodsOutput);
} catch (NoSourceNameException e) {
errorManager.logError("Error trying to write getter for [%s] -> [%s];" + " binding declaration: %s", e, key, binding, bindingContext);
return;
}
// Name of the field that we might need.
String field = nameGenerator.getSingletonFieldName(key);
switch(scope) {
case EAGER_SINGLETON:
initializeEagerSingletonsBody.append("// Eager singleton bound at:\n");
appendBindingContextCommentToMethod(bindingContext, initializeEagerSingletonsBody);
initializeEagerSingletonsBody.append(getter).append("();\n");
// $FALL-THROUGH$
case SINGLETON:
writer.println("private " + typeName + " " + field + " = null;");
writer.println();
getterBuilder.append(String.format("\nif (%s == null) {\n", field)).append(creationStatements).append("\n").append(String.format(" %s = result;\n", field)).append("}\n").append(String.format("return %s;\n", field));
break;
case NO_SCOPE:
sourceWriteUtil.writeBindingContextJavadoc(writer, bindingContext, key);
getterBuilder.append(creationStatements).append("\n").append("return result;\n");
break;
default:
throw new IllegalStateException();
}
outputMethod(SourceSnippets.asMethod(false, String.format("public %s %s()", typeName, getter), fragmentPackageName.toString(), getterBuilder.build()));
}
use of com.google.gwt.inject.rebind.reflect.NoSourceNameException in project google-gin by gwtplus.
the class GinjectorImplOutputter method writeInterface.
private void writeInterface(TypeLiteral<?> ginjectorInterface, String packageName, String implClassName, PrintWriter printWriter, GinjectorBindings rootBindings) throws UnableToCompleteException {
ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, implClassName);
SourceWriter writer = null;
try {
composerFactory.addImplementedInterface(ReflectUtil.getSourceName(ginjectorInterface));
writer = composerFactory.createSourceWriter(ctx, printWriter);
String rootInjectorClass = ginjectorNameGenerator.getClassName(rootBindings);
String rootFieldName = ginjectorNameGenerator.getFieldName(rootBindings);
writer.beginJavaDocComment();
writer.print("Top-level injector instance for injector " + rootBindings.getModule() + ".");
writer.endJavaDocComment();
writer.println("private final %1$s %2$s = new %1$s(this);", rootInjectorClass, rootFieldName);
SourceWriteUtil sourceWriteUtil = sourceWriteUtilFactory.create(rootBindings);
String staticInjectionInitialization = rootBindings.hasStaticInjectionRequestInSubtree() ? String.format("%s.initializeStaticInjections();\n", rootFieldName) : "";
String eagerSingletonsInitialization = rootBindings.hasEagerSingletonBindingInSubtree() ? String.format("%s.initializeEagerSingletons();\n", rootFieldName) : "";
sourceWriteUtil.writeMethod(writer, "public " + implClassName + "()", String.format(// See http://code.google.com/p/google-guice/wiki/Bootstrap
"%s%s", staticInjectionInitialization, eagerSingletonsInitialization));
outputInterfaceMethods(rootBindings, ginjectorInterface, sourceWriteUtil, writer);
} catch (NoSourceNameException e) {
// TODO(schmitt): Collect errors and log list of them.
logger.log(TreeLogger.Type.ERROR, e.getMessage(), e);
}
if (writer != null) {
writer.commit(logger);
}
}
Aggregations