use of com.squareup.javapoet.ClassName in project tiger by google.
the class NewInjectorGenerator method generateInjectorBuilder.
private void generateInjectorBuilder(ComponentInfo componentInfo, TypeSpec.Builder injectorBuilder) {
TypeSpec.Builder builderBuilder = TypeSpec.classBuilder("Builder").addModifiers(Modifier.PUBLIC, Modifier.STATIC);
ClassName injectorClassName = ClassName.get(topLevelPackageString, getTopLevelInjectorName(componentInfo));
boolean hasParent = componentTree.get(componentInfo) != null;
ClassName parentClassName = null;
// set parent inject methods.
if (hasParent) {
parentClassName = ClassName.get(topLevelPackageString, getTopLevelInjectorName(componentTree.get(componentInfo)));
Utils.addSetMethod(injectorClassName, builderBuilder, parentClassName);
}
/**
* Set module methods. Theoretically, is a passed module is needed by a injector should be
* decided by whether it is needed. Here is just a simpler way. All scoped modules go to
* injector of the same scope. All unscoped modules go to every injector. It is not possible to
* have modules of different scope in a injector. This way the modules created is a superset of
* what are required. But that's fine because null can be passed in for modules not needed. This
* is even unnoticeable when using builder to create injector. The null module is just never
* referenced. Otherwise it is a bug, sadly it is not caught until runtime. This makes it easier
* to create wrapper component defined dagger around core injectors. Anyway, passed module is
* not a good idea and could/should be removed.
*/
List<TypeElement> allNonNullaryCtorModules = new ArrayList<>();
allNonNullaryCtorModules.addAll(nonNullaryCtorModules.get(componentInfo));
allNonNullaryCtorModules.addAll(nonNullaryCtorUnscopedModules);
for (TypeElement m : allNonNullaryCtorModules) {
Utils.addSetMethod(injectorClassName, builderBuilder, (ClassName) ClassName.get(m.asType()));
}
// build() method.
MethodSpec.Builder buildMethodBuilder = MethodSpec.methodBuilder("build").addModifiers(Modifier.PUBLIC).returns(injectorClassName);
StringBuilder returnCodeBuilder = new StringBuilder("return new $T(");
if (hasParent) {
String parentInjectorFieldName = Utils.getSourceCodeName(parentClassName);
returnCodeBuilder.append(parentInjectorFieldName);
if (!allNonNullaryCtorModules.isEmpty()) {
returnCodeBuilder.append(", ");
}
}
if (!allNonNullaryCtorModules.isEmpty()) {
for (TypeElement module : Utils.sortByFullName(allNonNullaryCtorModules)) {
String moduleFiledName = Utils.getSourceCodeName(TypeName.get(module.asType()));
returnCodeBuilder.append(moduleFiledName).append(", ");
}
int size = returnCodeBuilder.length();
returnCodeBuilder.delete(size - 2, size);
}
returnCodeBuilder.append(");");
buildMethodBuilder.addCode(returnCodeBuilder.toString(), injectorClassName);
builderBuilder.addMethod(buildMethodBuilder.build());
injectorBuilder.addType(builderBuilder.build());
}
use of com.squareup.javapoet.ClassName in project tiger by google.
the class NewInjectorGenerator method generateInheritedProvisionMethods.
private void generateInheritedProvisionMethods(ClassName packagedInjectorClassName) {
ComponentInfo component = getComponentFromPackagedInjectorClassName(packagedInjectorClassName);
Preconditions.checkArgument(componentTree.get(component) != null, String.format("No inherited provision methods to generate for %s", packagedInjectorClassName));
TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(packagedInjectorClassName);
ClassName containingPackagedInjectorClassName = getInjectorNameOfScope(packagedInjectorClassName, componentTree.get(component).getScope());
for (NewBindingKey key : generatedBindingsForPackagedInjector.get(containingPackagedInjectorClassName)) {
String provisionMethodName = getProvisionMethodName(key);
componentSpecBuilder.addMethod(MethodSpec.methodBuilder(provisionMethodName).addModifiers(Modifier.PUBLIC).returns(key.getTypeName()).addStatement("return $L.$L()", CONTAINING_PACKAGED_INJECTOR_FIELD, provisionMethodName).build());
Preconditions.checkState(generatedBindingsForPackagedInjector.put(packagedInjectorClassName, key), String.format("Injector %s already provides %s.", packagedInjectorClassName, key));
}
}
use of com.squareup.javapoet.ClassName in project tiger by google.
the class NewInjectorGenerator method generateProvisionMethodAndAppendAsParameter.
private void generateProvisionMethodAndAppendAsParameter(NewBindingKey key, TypeElement referencingClass, ClassName packagedInjectorClassName, StringBuilder builder) {
TypeElement scope = getComponentFromPackagedInjectorClassName(packagedInjectorClassName).getScope();
generateProvisionMethodIfNeeded(key, referencingClass);
ClassName dependencyPackagedInjectorClassName = getInjectorOfScope(key, referencingClass, scope);
if (!dependencyPackagedInjectorClassName.equals(packagedInjectorClassName)) {
builder.append(TOP_LEVEL_INJECTOR_FIELD).append(".").append(Utils.getGetMethodName(dependencyPackagedInjectorClassName)).append("().");
}
builder.append(getProvisionMethodName(key)).append("(), ");
}
use of com.squareup.javapoet.ClassName in project tiger by google.
the class Utils method getDependencyInfoByGeneric.
/**
* Return {@link NewDependencyInfo} for the generalized {@link NewBindingKey} for
* the give key. Null if not applicable or not exist.
*/
public static NewDependencyInfo getDependencyInfoByGeneric(SetMultimap<NewBindingKey, NewDependencyInfo> dependencies, NewBindingKey key) {
TypeName typeName = key.getTypeName();
Preconditions.checkArgument(key.getQualifier() == null, String.format("Binding to %s is supposed to be resolved through generic type of %s" + "but has non-null qualifier.", key, typeName));
if (typeName instanceof ParameterizedTypeName) {
ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName;
ClassName rawTypeName = parameterizedTypeName.rawType;
NewBindingKey rawKey = NewBindingKey.get(rawTypeName);
if (dependencies.containsKey(rawKey)) {
NewDependencyInfo dependencyInfo = Iterables.getOnlyElement(dependencies.get(rawKey));
TypeName formalTypeName = dependencyInfo.getDependant().getTypeName();
Preconditions.checkState(formalTypeName instanceof ParameterizedTypeName, String.format("Formal type %s is not of type ParameterizedTypeName. Related actual type is %s", formalTypeName, parameterizedTypeName));
Map<TypeVariableName, TypeName> mapTypeVariableToSpecialized = getMapFromTypeVariableToSpecialized(parameterizedTypeName, (ParameterizedTypeName) formalTypeName);
Set<NewBindingKey> specializedDependencies = specializeIfNeeded(dependencyInfo.getDependencies(), mapTypeVariableToSpecialized);
return new NewDependencyInfo(key, specializedDependencies, dependencyInfo.getSourceClassElement(), dependencyInfo.getProvisionMethodElement(), dependencyInfo.getType());
}
}
return null;
}
use of com.squareup.javapoet.ClassName in project requery by requery.
the class EntityGenerator method generateListeners.
private void generateListeners(MethodSpec.Builder constructor) {
for (Map.Entry<Element, ? extends ListenerDescriptor> entry : entity.listeners().entrySet()) {
for (Annotation annotation : entry.getValue().listenerAnnotations()) {
String annotationName = annotation.annotationType().getSimpleName().replace("Persist", "Insert").replace("Remove", "Delete");
String methodName = Names.lowerCaseFirst(annotationName);
// avoid hardcoding the package name here
Element listener = elements.getTypeElement(PreInsertListener.class.getCanonicalName());
PackageElement packageElement = elements.getPackageOf(listener);
// generate the listener name
String packageName = packageElement.getQualifiedName().toString();
ClassName listenerName = ClassName.get(packageName, annotationName + "Listener");
ParameterizedTypeName getterType = ParameterizedTypeName.get(listenerName, typeName);
TypeSpec.Builder listenerBuilder = TypeSpec.anonymousClassBuilder("").addSuperinterface(getterType).addMethod(CodeGeneration.overridePublicMethod(methodName).addParameter(typeName, "entity").addStatement("$L()", entry.getKey().getSimpleName()).build());
constructor.addStatement("$L.modifyListeners().add$L($L)", PROXY_NAME, annotationName + "Listener", listenerBuilder.build());
}
}
}
Aggregations