use of com.squareup.javapoet.ClassName in project tiger by google.
the class TigerDaggerGeneratorProcessor method generateWrapperComponents.
/**
* Generates Dagger component implementations that wrap around the core injectors.
*/
private void generateWrapperComponents(Set<TypeElement> components) {
Map<TypeElement, ComponentInfo> componentToCoreInjectorMap = calculateAllMappingFromComponentsToCoreInjectors(components);
for (TypeElement component : components) {
ComponentInfo coreInjector = componentToCoreInjectorMap.get(component);
ClassName coreInjectorClassName = getCoreInejectorClassName(coreInjectorPackage, coreInjector);
String packageString = Utils.getPackageString(component);
String generatedComponentSimpleName = getComponentImplementationSimpleNameFromInterface(component);
TypeSpec.Builder componentBuilder = TypeSpec.classBuilder(generatedComponentSimpleName).addModifiers(Modifier.PUBLIC).addSuperinterface(TypeName.get(component.asType())).superclass(coreInjectorClassName);
// Ctor with parent component and modules as parameters.
MethodSpec.Builder ctorBuilder = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE);
List<TypeElement> sortedComponentDependencies = Utils.sortByFullName(getComponentDependencies(component));
TypeElement dependencyComponent = null;
if (!sortedComponentDependencies.isEmpty()) {
dependencyComponent = Iterables.getOnlyElement(sortedComponentDependencies);
ctorBuilder.addParameter(TypeName.get(dependencyComponent.asType()), Utils.getSourceCodeName(dependencyComponent));
}
Set<TypeElement> allComponentModules = getAllModulesOfComponentRecursively(component);
List<TypeElement> sortedComponentPassedModules = Utils.sortByFullName(Utils.getNonNullaryCtorOnes(allComponentModules));
for (TypeElement typeElement : sortedComponentPassedModules) {
ctorBuilder.addParameter(TypeName.get(typeElement.asType()), Utils.getSourceCodeName(typeElement));
}
Set<TypeElement> coreInjectorPassedModules = new HashSet<>();
coreInjectorPassedModules.addAll(scopedPassedModules.get(coreInjector));
coreInjectorPassedModules.addAll(unscopedPassedModules);
List<TypeElement> sortedCoreInjectorPassedModules = Utils.sortByFullName(coreInjectorPassedModules);
StringBuilder stringBuilder = new StringBuilder("super(");
if (dependencyComponent != null) {
String generatedDependencyComponentSimpleName = getComponentImplementationSimpleNameFromInterface(dependencyComponent);
// Cast the interface to implement which is a subclass of core injector therefore will be
// accept by compiler.
stringBuilder.append("($T) ").append(Utils.getSourceCodeName(dependencyComponent));
if (!sortedCoreInjectorPassedModules.isEmpty()) {
stringBuilder.append(", ");
}
}
if (!sortedCoreInjectorPassedModules.isEmpty()) {
for (TypeElement typeElement : sortedCoreInjectorPassedModules) {
if (sortedComponentPassedModules.contains(typeElement)) {
stringBuilder.append(Utils.getSourceCodeName(typeElement));
} else {
stringBuilder.append("null");
}
stringBuilder.append(", ");
}
stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length());
}
stringBuilder.append(");");
if (dependencyComponent != null) {
ctorBuilder.addCode(stringBuilder.toString(), ClassName.get(packageString, getComponentImplementationSimpleNameFromInterface(dependencyComponent)));
} else {
ctorBuilder.addCode(stringBuilder.toString());
}
componentBuilder.addMethod(ctorBuilder.build());
ClassName generatedComponentClassName = ClassName.get(packageString, generatedComponentSimpleName);
generateComponentBuilder(generatedComponentClassName, dependencyComponent, sortedComponentPassedModules, componentBuilder, coreInjector);
ClassName builderClassName = ClassName.get(packageString, generatedComponentSimpleName, "Builder");
componentBuilder.addMethod(MethodSpec.methodBuilder("builder").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(builderClassName).addCode("return new $T();", builderClassName).build());
JavaFile javaFile = JavaFile.builder(packageString, componentBuilder.build()).build();
try {
messager.printMessage(Kind.NOTE, String.format("%s: writing java file: %s", TAG, javaFile.toJavaFileObject().getName()));
javaFile.writeTo(processingEnv.getFiler());
} catch (IOException e) {
Throwables.propagate(e);
}
}
}
use of com.squareup.javapoet.ClassName in project tiger by google.
the class Utils method hasBuiltinBinding.
public static boolean hasBuiltinBinding(TypeName type) {
if (!(type instanceof ParameterizedTypeName)) {
return false;
}
ParameterizedTypeName typeName = (ParameterizedTypeName) type;
ClassName rawType = typeName.rawType;
return rawType.equals(ClassName.get(Provider.class)) || rawType.equals(ClassName.get(Lazy.class));
}
use of com.squareup.javapoet.ClassName in project tiger by google.
the class NewInjectorGenerator method generateProvisionMethodIfNeeded.
private void generateProvisionMethodIfNeeded(final NewBindingKey key, TypeElement referencingClass) {
ClassName packagedInjectorClassName = getInjectorFor(key, referencingClass);
Builder injectorSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
if (!generatedBindingsForPackagedInjector.put(packagedInjectorClassName, key)) {
return;
}
NewDependencyInfo dependencyInfo = Iterables.getFirst(dependencies.get(key), null);
// System.out.println("generateProvisionMethodIfNeeded, key: " + key);
// System.out.println("generateProvisionMethodIfNeeded, NewDependencyInfo: " +
// dependencyInfo);
// System.out.println("generateProvisionMethodIfNeeded, scope: " +
// scopeCalculator.calculate(key));
boolean scoped = explicitScopes.contains(key);
String suffix = scoped ? UNSCOPED_SUFFIX : "";
if (dependencyInfo != null) {
if (dependencyInfo.isSet()) {
// TODO(freeman): support binding for Set of Provider or Lazy.
generateProvisionMethodForSet(key, referencingClass, suffix);
} else {
if (dependencyInfo.getProvisionMethodElement() == null) {
generateProvisionMethodFromClass(key, referencingClass, suffix);
} else {
generateUniqueTypeProvisionMethodFromModule(key, suffix);
}
}
} else if (Utils.hasBuiltinBinding(key)) {
generateProvisionMethodForProviderOrLazy(key, referencingClass, suffix);
} else if (Utils.isMap(key)) {
generateProvisionMethodForMap(key, referencingClass, suffix);
} else {
NewDependencyInfo genericDependencyInfo = Utils.getDependencyInfoByGeneric(dependencies, key);
if (genericDependencyInfo != null) {
if (genericDependencyInfo.getProvisionMethodElement() == null) {
generateProvisionMethodFromClass(key, referencingClass, suffix);
} else {
errors.add(String.format("Generic provision method not supported yet: %s -> %s", key, genericDependencyInfo));
}
} else {
errors.add(String.format("Cannot resolve %s.", key));
throw new RuntimeException();
}
}
if (scoped) {
generateField(injectorSpecBuilder, key);
generateScopedProvisionMethod(injectorSpecBuilder, key);
}
}
use of com.squareup.javapoet.ClassName in project tiger by google.
the class NewInjectorGenerator method getSourceCodeName.
/**
* Returns {@link #getSourceCodeName}(qualifier) + "__" + {@link #getSourceCodeName} (type), or
* {@link #getSourceCodeName}(type) if no qualifier.
*/
private String getSourceCodeName(NewBindingKey key) {
// System.out.println("getMethodName for key: " + key);
StringBuilder builder = new StringBuilder();
AnnotationSpec qualifier = key.getQualifier();
if (qualifier != null) {
ClassName qualifierType = (ClassName) qualifier.type;
builder.append(Utils.getSourceCodeName(qualifierType));
/**
* TODO(freeman): handle all illegal chars.
*/
if (Utils.getCanonicalName(qualifierType).equals(Named.class.getCanonicalName())) {
builder.append("_").append(qualifier.members.get("value").toString().replace("\"", "_").replace("[", "_").replace("]", "_"));
}
builder.append("__");
}
builder.append(Utils.getSourceCodeName(key.getTypeName()));
return builder.toString();
}
use of com.squareup.javapoet.ClassName in project tiger by google.
the class NewInjectorGenerator method generateInheritedInjectionMethods.
private void generateInheritedInjectionMethods(ClassName packagedInjectorClassName) {
ComponentInfo component = getComponentFromPackagedInjectorClassName(packagedInjectorClassName);
if (componentTree.get(component) == null) {
return;
}
TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
ClassName containingPackagedInjectorClassName = getInjectorNameOfScope(packagedInjectorClassName, componentTree.get(component).getScope());
for (ClassName injectedClassName : injectedClassNamesForPackagedInjector.get(containingPackagedInjectorClassName)) {
componentSpecBuilder.addMethod(MethodSpec.methodBuilder("inject").addModifiers(Modifier.PUBLIC).addParameter(injectedClassName, "arg").addStatement("$L.inject(arg)", CONTAINING_PACKAGED_INJECTOR_FIELD).build());
injectedClassNamesForPackagedInjector.put(packagedInjectorClassName, injectedClassName);
}
}
Aggregations