use of com.squareup.javapoet.ClassName in project tiger by google.
the class NewInjectorGenerator method addCallingProvisionMethod.
/**
* Adds "[topLevelInjector.getxxxPackagedInjector.]getxxx()" to the builder.
*/
private void addCallingProvisionMethod(StringBuilder stringBuilder, NewBindingKey key, TypeElement referencingClass, ClassName packagedInjectorClassName) {
generateProvisionMethodIfNeeded(key, referencingClass);
ClassName originalPackagedInjector = getInjectorFor(key, referencingClass);
ComponentInfo givenComponent = getComponentFromPackagedInjectorClassName(packagedInjectorClassName);
ClassName targetPackagedInjectorClassName = getInjectorNameOfScope(originalPackagedInjector, givenComponent.getScope());
if (!targetPackagedInjectorClassName.equals(packagedInjectorClassName)) {
// System.out.println("addCallingProvisionMethod. packageInjector: " +
// packagedInjectorClassName
// + " field key: " + key);
stringBuilder.append(TOP_LEVEL_INJECTOR_FIELD).append(".").append(Utils.getGetMethodName(targetPackagedInjectorClassName)).append("().");
}
stringBuilder.append(getProvisionMethodName(key)).append("()");
}
use of com.squareup.javapoet.ClassName in project tiger by google.
the class NewInjectorGenerator method generateProvisionMethodForProviderOrLazy.
/**
* For key like javax.inject.Provider<Foo> and dagger.Lazy<Foo>. Qualifier, if presented, will
* also apply to element binding.
*/
private void generateProvisionMethodForProviderOrLazy(NewBindingKey key, TypeElement referencingClass, String suffix) {
// System.out.println(String.format(
// "generateProvisionMethodForProviderOrLazy: key %s, referencingClass: %s, suffix : %s.", key,
// referencingClass, suffix));
ClassName injectorClassName = getInjectorFor(key, referencingClass);
TypeSpec anonymousTypeSpec = createAnonymousBuiltinTypeForUniqueBinding(injectorClassName, key, referencingClass);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix).addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(key.getTypeName()).addStatement("return $L", anonymousTypeSpec);
Builder componentSpecBuilder = getInjectorTypeSpecBuilder(getInjectorFor(key, referencingClass));
componentSpecBuilder.addMethod(methodSpecBuilder.build());
}
use of com.squareup.javapoet.ClassName in project tiger by google.
the class NewInjectorGenerator method generateProvisionMethodFromClass.
/**
* Handles generic.
*/
private void generateProvisionMethodFromClass(NewBindingKey key, TypeElement referencingClass, String suffix) {
// System.out.println(
// "generateProvisionMethodFromClass. key: " + key + " referencingClass: " +
// referencingClass);
ClassName packagedInjectorClassName = getInjectorFor(key, referencingClass);
TypeSpec.Builder injectorSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
TypeElement cls = getClassFromKey(key);
ExecutableElement ctor = Utils.findInjectedCtor(cls, processingEnv);
Preconditions.checkNotNull(ctor, String.format("Did not find ctor for %s", cls));
List<NewBindingKey> dependencyKeys = Utils.getDependenciesFromExecutableElement(ctor);
if (key.getTypeName() instanceof ParameterizedTypeName) {
List<NewBindingKey> specializedKeys = new ArrayList<>();
Map<TypeVariableName, TypeName> map = Utils.getMapFromTypeVariableToSpecialized((ParameterizedTypeName) key.getTypeName(), (ParameterizedTypeName) TypeName.get(cls.asType()));
for (NewBindingKey k : dependencyKeys) {
specializedKeys.add(Utils.specializeIfNeeded(k, map));
}
dependencyKeys = specializedKeys;
}
// System.out.println("generateProvisionMethodFromClass. dependencyKeys: " +
// dependencyKeys);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix);
methodSpecBuilder.addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(key.getTypeName());
StringBuilder builder = new StringBuilder("$T result = new $T(");
if (dependencyKeys.size() > 0) {
for (NewBindingKey dependencyKey : dependencyKeys) {
// System.out.println("generateProvisionMethodFromClass. dependencyKey: "
// + dependencyKey);
generateProvisionMethodAndAppendAsParameter(dependencyKey, cls, packagedInjectorClassName, builder);
}
builder.delete(builder.length() - 2, builder.length());
}
builder.append(")");
methodSpecBuilder.addStatement(builder.toString(), key.getTypeName(), key.getTypeName());
if (Utils.hasInjectedFieldsOrMethods(cls, processingEnv)) {
// System.out.println("generateProvisionMethodFromClass. hasInjected");
generateInjectionMethod(key, referencingClass);
methodSpecBuilder.addStatement("inject(result)");
}
methodSpecBuilder.addStatement("return result");
injectorSpecBuilder.addMethod(methodSpecBuilder.build());
}
use of com.squareup.javapoet.ClassName in project tiger by google.
the class NewInjectorGenerator method generateGetModuleMethod.
/**
* Creates get_Foo_Module() for FooModule.
*/
private void generateGetModuleMethod(TypeElement scope, NewDependencyInfo dependencyInfo) {
Preconditions.checkArgument(dependencyInfo.getProvisionMethodElement() != null, String.format("Expect one from module but get %s.", dependencyInfo));
TypeElement module = dependencyInfo.getSourceClassElement();
TypeName moduleTypeName = TypeName.get(module.asType());
ClassName packagedInjectorClassName = getPackagedInjectorForNewDependencyInfo(scope, dependencyInfo);
TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
String moduleCanonicalNameConverted = Utils.getQualifiedName(module).replace(".", "_");
componentSpecBuilder.addField(moduleTypeName, moduleCanonicalNameConverted, Modifier.PRIVATE);
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(Utils.getGetMethodName(module)).addModifiers(Modifier.PRIVATE).addStatement("$T result = $N", module, moduleCanonicalNameConverted).returns(moduleTypeName);
methodBuilder.beginControlFlow("if (result == null)");
if (!isPassedModule(module)) {
methodBuilder.addStatement("result = $N = new $T()", moduleCanonicalNameConverted, module);
} else {
methodBuilder.addStatement("result = $N = $L.$N()", moduleCanonicalNameConverted, TOP_LEVEL_INJECTOR_FIELD, Utils.getGetMethodName(module));
}
methodBuilder.endControlFlow();
methodBuilder.addStatement("return result");
MethodSpec methodSpec = methodBuilder.build();
componentSpecBuilder.addMethod(methodSpec);
modulesWithGetter.get(packagedInjectorClassName).put(ClassName.get(module), methodSpec);
}
use of com.squareup.javapoet.ClassName in project tiger by google.
the class NewInjectorGenerator method generateInjectionMethod.
private void generateInjectionMethod(TypeElement cls, ClassName packagedInjectorClassName, String methodName, boolean isPublic) {
if (!injectedClassNamesForPackagedInjector.put(packagedInjectorClassName, ClassName.get(cls))) {
return;
}
// messager.printMessage(Kind.NOTE,
// String.format("generateInjectionMethod. cls: %s, injector: %s, method: %s", cls,
// packagedInjectorClassName, methodName));
Builder componentSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(methodName).addModifiers(isPublic ? Modifier.PUBLIC : Modifier.PRIVATE).addParameter(ClassName.get(cls), "arg");
// Inject closest ancestor first.
TypeElement clsClosestInjectAncestor = getSuper(cls);
while (clsClosestInjectAncestor != null && !Utils.hasInjectedFieldsOrMethods(clsClosestInjectAncestor, processingEnv)) {
clsClosestInjectAncestor = getSuper(clsClosestInjectAncestor);
}
if (clsClosestInjectAncestor != null) {
TypeElement scope = getComponentFromPackagedInjectorClassName(packagedInjectorClassName).getScope();
generateInjectionMethod(clsClosestInjectAncestor, scope);
ClassName ancestorPackagedInjector = getPackagedInjectorNameOfScope(Utils.getPackageString(clsClosestInjectAncestor), scope);
StringBuilder stringBuilder = new StringBuilder();
if (!ancestorPackagedInjector.equals(packagedInjectorClassName)) {
stringBuilder.append(TOP_LEVEL_INJECTOR_FIELD).append(".$N().");
}
stringBuilder.append("inject(($T) arg)");
if (!ancestorPackagedInjector.equals(packagedInjectorClassName)) {
methodSpecBuilder.addStatement(stringBuilder.toString(), Utils.getGetMethodName(ancestorPackagedInjector), ClassName.get(clsClosestInjectAncestor));
} else {
// TODO(freeman): ClassName.get() removed the type parameters for now. Support it.
methodSpecBuilder.addStatement(stringBuilder.toString(), ClassName.get(clsClosestInjectAncestor));
}
}
for (VariableElement field : Utils.getInjectedFields(cls, processingEnv)) {
// System.out.println("generateInjectionMethod. field: " + field);
TypeMirror fieldType = field.asType();
AnnotationMirror fieldQualifier = Utils.getQualifier(field);
NewBindingKey fieldKey = NewBindingKey.get(fieldType, fieldQualifier);
StringBuilder stringBuilder = new StringBuilder("arg.").append(field.getSimpleName()).append(" = ");
addCallingProvisionMethod(stringBuilder, fieldKey, cls, packagedInjectorClassName);
methodSpecBuilder.addStatement(stringBuilder.toString());
}
for (ExecutableElement method : Utils.getInjectedMethods(cls, processingEnv)) {
// System.out.println("generateInjectionMethod. method: " + method);
StringBuilder builder = new StringBuilder("arg.").append(method.getSimpleName()).append("(");
List<NewBindingKey> methodArgs = Utils.getDependenciesFromExecutableElement(method);
if (methodArgs.size() > 0) {
for (NewBindingKey dependentKey : methodArgs) {
addCallingProvisionMethod(builder, dependentKey, cls, packagedInjectorClassName);
builder.append(", ");
}
builder.delete(builder.length() - 2, builder.length());
}
builder.append(")");
methodSpecBuilder.addStatement(builder.toString());
}
componentSpecBuilder.addMethod(methodSpecBuilder.build());
}
Aggregations