use of com.squareup.javapoet.ClassName in project dagger by square.
the class InjectAdapterProcessor method generateStaticInjection.
/**
* Write a companion class for {@code type} that extends {@link StaticInjection}.
*/
private void generateStaticInjection(TypeElement type, List<Element> fields) throws IOException {
ClassName typeName = ClassName.get(type);
ClassName adapterClassName = adapterName(ClassName.get(type), STATIC_INJECTION_SUFFIX);
TypeSpec.Builder result = TypeSpec.classBuilder(adapterClassName.simpleName()).addOriginatingElement(type).addJavadoc(AdapterJavadocs.STATIC_INJECTION_TYPE, type).addModifiers(PUBLIC, FINAL).superclass(StaticInjection.class);
for (Element field : fields) {
result.addField(memberBindingField(false, field));
}
result.addMethod(attachMethod(null, fields, false, typeName, null, true));
result.addMethod(staticInjectMethod(fields, typeName));
String packageName = getPackage(type).getQualifiedName().toString();
JavaFile javaFile = JavaFile.builder(packageName, result.build()).addFileComment(AdapterJavadocs.GENERATED_BY_DAGGER).build();
javaFile.writeTo(processingEnv.getFiler());
}
use of com.squareup.javapoet.ClassName in project dagger by square.
the class ModuleAdapterProcessor method generateProvidesAdapter.
private TypeSpec generateProvidesAdapter(ClassName moduleClassName, ClassName adapterName, ExecutableElement providerMethod, Map<ExecutableElement, ClassName> methodToClassName, Map<String, AtomicInteger> methodNameToNextId, boolean library) {
String methodName = providerMethod.getSimpleName().toString();
TypeMirror moduleType = providerMethod.getEnclosingElement().asType();
ClassName className = bindingClassName(adapterName, providerMethod, methodToClassName, methodNameToNextId);
TypeName returnType = Util.injectableType(providerMethod.getReturnType());
List<? extends VariableElement> parameters = providerMethod.getParameters();
boolean dependent = !parameters.isEmpty();
TypeSpec.Builder result = TypeSpec.classBuilder(className.simpleName()).addJavadoc("$L", bindingTypeDocs(returnType, false, false, dependent)).addModifiers(PUBLIC, STATIC, FINAL).superclass(ParameterizedTypeName.get(ClassName.get(ProvidesBinding.class), returnType));
result.addField(moduleClassName, "module", PRIVATE, FINAL);
for (Element parameter : parameters) {
result.addField(bindingOf(parameter.asType()), parameterName(parameter), PRIVATE);
}
boolean singleton = providerMethod.getAnnotation(Singleton.class) != null;
String key = GeneratorKeys.get(providerMethod);
result.addMethod(MethodSpec.constructorBuilder().addModifiers(PUBLIC).addParameter(moduleClassName, "module").addStatement("super($S, $L, $S, $S)", key, (singleton ? "IS_SINGLETON" : "NOT_SINGLETON"), typeToString(moduleType), methodName).addStatement("this.module = module").addStatement("setLibrary($L)", library).build());
if (dependent) {
MethodSpec.Builder attachBuilder = MethodSpec.methodBuilder("attach").addJavadoc(AdapterJavadocs.ATTACH_METHOD).addAnnotation(Override.class).addAnnotation(Util.UNCHECKED).addModifiers(PUBLIC).addParameter(Linker.class, "linker");
for (VariableElement parameter : parameters) {
String parameterKey = GeneratorKeys.get(parameter);
attachBuilder.addStatement("$N = ($T) linker.requestBinding($S, $T.class, getClass().getClassLoader())", parameterName(parameter), bindingOf(parameter.asType()), parameterKey, moduleClassName);
}
result.addMethod(attachBuilder.build());
MethodSpec.Builder getDependenciesBuilder = MethodSpec.methodBuilder("getDependencies").addJavadoc(AdapterJavadocs.GET_DEPENDENCIES_METHOD).addAnnotation(Override.class).addModifiers(PUBLIC).addParameter(Util.SET_OF_BINDINGS, "getBindings").addParameter(Util.SET_OF_BINDINGS, "injectMembersBindings");
for (Element parameter : parameters) {
getDependenciesBuilder.addStatement("getBindings.add($N)", parameterName(parameter));
}
result.addMethod(getDependenciesBuilder.build());
}
MethodSpec.Builder getBuilder = MethodSpec.methodBuilder("get").addJavadoc(AdapterJavadocs.GET_METHOD, returnType).addAnnotation(Override.class).addModifiers(PUBLIC).returns(returnType).addCode("return module.$N(", methodName);
boolean first = true;
for (Element parameter : parameters) {
if (!first)
getBuilder.addCode(", ");
getBuilder.addCode("$N.get()", parameterName(parameter));
first = false;
}
getBuilder.addCode(");\n");
result.addMethod(getBuilder.build());
return result.build();
}
use of com.squareup.javapoet.ClassName in project wire by square.
the class JavaGenerator method generateAbstractAdapter.
/** Returns an abstract adapter for {@code type}. */
public TypeSpec generateAbstractAdapter(MessageType type) {
NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
ClassName adapterTypeName = abstractAdapterName(type.type());
ClassName typeName = (ClassName) typeName(type.type());
TypeSpec.Builder adapter = messageAdapter(nameAllocator, type, typeName, adapterTypeName, null).toBuilder();
if (adapterTypeName.enclosingClassName() != null) {
adapter.addModifiers(STATIC);
}
for (Type nestedType : type.nestedTypes()) {
if (profile.getAdapter(nestedType.type()) == null) {
throw new IllegalArgumentException("Missing custom proto adapter for " + nestedType.type().enclosingTypeOrPackage() + "." + nestedType.type().simpleName() + " when enclosing proto has custom proto adapter.");
}
if (nestedType instanceof MessageType) {
adapter.addType(generateAbstractAdapter((MessageType) nestedType));
}
}
return adapter.build();
}
use of com.squareup.javapoet.ClassName in project wire by square.
the class JavaGenerator method abstractAdapterName.
/**
* Returns the Java type of the abstract adapter class generated for a corresponding {@code
* protoType}. Returns null if {@code protoType} is not using a custom proto adapter.
*/
public ClassName abstractAdapterName(ProtoType protoType) {
TypeName profileJavaName = profile.getTarget(protoType);
if (profileJavaName == null)
return null;
ClassName javaName = nameToJavaName.get(protoType);
return javaName.peerClass("Abstract" + javaName.simpleName() + "Adapter");
}
use of com.squareup.javapoet.ClassName in project wire by square.
the class JavaGenerator method generateEnclosingType.
private TypeSpec generateEnclosingType(EnclosingType type) {
ClassName javaType = (ClassName) typeName(type.type());
TypeSpec.Builder builder = TypeSpec.classBuilder(javaType.simpleName()).addModifiers(PUBLIC, FINAL);
if (javaType.enclosingClassName() != null) {
builder.addModifiers(STATIC);
}
String documentation = type.documentation();
if (!documentation.isEmpty()) {
documentation += "\n\n<p>";
}
documentation += "<b>NOTE:</b> This type only exists to maintain class structure" + " for its nested types and is not an actual message.\n";
builder.addJavadoc(documentation);
builder.addMethod(MethodSpec.constructorBuilder().addModifiers(PRIVATE).addStatement("throw new $T()", AssertionError.class).build());
for (Type nestedType : type.nestedTypes()) {
builder.addType(generateType(nestedType));
}
return builder.build();
}
Aggregations