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();
}
use of com.squareup.javapoet.ClassName in project wire by square.
the class CodegenSample method execute.
public void execute() throws IOException {
Schema schema = loadSchema();
if (!identifierSet.isEmpty()) {
schema = retainRoots(schema);
}
JavaGenerator javaGenerator = JavaGenerator.get(schema);
ServiceGenerator serviceGenerator = new ServiceGenerator(javaGenerator);
for (ProtoFile protoFile : schema.protoFiles()) {
for (Type type : protoFile.types()) {
Stopwatch stopwatch = Stopwatch.createStarted();
TypeSpec typeSpec = javaGenerator.generateType(type);
ClassName javaTypeName = (ClassName) javaGenerator.typeName(type.type());
writeJavaFile(javaTypeName, typeSpec, type.location(), stopwatch);
}
for (Service service : protoFile.services()) {
Stopwatch stopwatch = Stopwatch.createStarted();
ClassName javaTypeName = (ClassName) javaGenerator.typeName(service.type());
TypeSpec typeSpec = serviceGenerator.api(service);
writeJavaFile(javaTypeName, typeSpec, service.location(), stopwatch);
}
}
}
use of com.squareup.javapoet.ClassName in project wire by square.
the class RepoBuilder method generateCode.
public String generateCode(String typeName, String profile) throws IOException {
Schema schema = schema();
JavaGenerator javaGenerator = JavaGenerator.get(schema);
if (profile != null) {
javaGenerator = javaGenerator.withProfile(profile(profile));
}
Type type = schema.getType(typeName);
TypeSpec typeSpec = javaGenerator.generateType(type);
ClassName typeName1 = javaGenerator.generatedTypeName(type);
return JavaFile.builder(typeName1.packageName(), typeSpec).build().toString();
}
Aggregations