use of com.squareup.javapoet.TypeSpec in project ActivityRouter by mzule.
the class RouterProcessor method generateDefaultRouterInit.
private void generateDefaultRouterInit() {
MethodSpec.Builder initMethod = MethodSpec.methodBuilder("init").addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC);
initMethod.addStatement("RouterMapping.map()");
TypeSpec routerInit = TypeSpec.classBuilder("RouterInit").addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(initMethod.build()).build();
try {
JavaFile.builder("com.github.mzule.activityrouter.router", routerInit).build().writeTo(filer);
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.squareup.javapoet.TypeSpec in project ActivityRouter by mzule.
the class RouterProcessor method generateModulesRouterInit.
private void generateModulesRouterInit(String[] moduleNames) {
MethodSpec.Builder initMethod = MethodSpec.methodBuilder("init").addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC);
for (String module : moduleNames) {
initMethod.addStatement("RouterMapping_" + module + ".map()");
}
TypeSpec routerInit = TypeSpec.classBuilder("RouterInit").addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(initMethod.build()).build();
try {
JavaFile.builder("com.github.mzule.activityrouter.router", routerInit).build().writeTo(filer);
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.squareup.javapoet.TypeSpec 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.TypeSpec 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.TypeSpec 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