use of com.squareup.javapoet.JavaFile in project wire by square.
the class WireGenerateSourcesMojo method writeJavaFile.
private void writeJavaFile(ClassName javaTypeName, TypeSpec typeSpec, Location location) throws IOException {
JavaFile.Builder builder = JavaFile.builder(javaTypeName.packageName(), typeSpec).addFileComment("$L", "Code generated by Wire protocol buffer compiler, do not edit.");
if (location != null) {
builder.addFileComment("\nSource file: $L", location);
}
JavaFile javaFile = builder.build();
try {
javaFile.writeTo(new File(generatedSourceDirectory));
} catch (IOException e) {
throw new IOException("Failed to write " + javaFile.packageName + "." + javaFile.typeSpec.name + " to " + generatedSourceDirectory, e);
}
}
use of com.squareup.javapoet.JavaFile in project wire by square.
the class ServiceGeneratorTest method toString.
private String toString(TypeSpec typeSpec) throws IOException {
StringBuilder result = new StringBuilder();
JavaFile javaFile = JavaFile.builder("squareup.wire.sample", typeSpec).build();
javaFile.writeTo(result);
return result.toString();
}
use of com.squareup.javapoet.JavaFile in project wire by square.
the class CodegenSample method writeJavaFile.
private void writeJavaFile(ClassName javaTypeName, TypeSpec typeSpec, Location location, Stopwatch stopwatch) throws IOException {
JavaFile.Builder builder = JavaFile.builder(javaTypeName.packageName(), typeSpec).addFileComment("Code generated by $L, do not edit.", CodegenSample.class.getName());
if (location != null) {
builder.addFileComment("\nSource file: $L", location.path());
}
JavaFile javaFile = builder.build();
try {
javaFile.writeTo(new File(generatedSourceDirectory));
} catch (IOException e) {
throw new IOException("Failed to write " + javaFile.packageName + "." + javaFile.typeSpec.name + " to " + generatedSourceDirectory, e);
}
log.info("Generated %s in %s", javaTypeName, stopwatch);
}
use of com.squareup.javapoet.JavaFile in project motan by weibocom.
the class MotanAsyncProcessor method writeAsyncClass.
private void writeAsyncClass(Element elem) throws ClassNotFoundException, IOException, Exception {
if (elem.getKind().isInterface()) {
TypeElement interfaceClazz = (TypeElement) elem;
String className = interfaceClazz.getSimpleName().toString();
TypeSpec.Builder classBuilder = TypeSpec.interfaceBuilder(className + ASYNC).addModifiers(Modifier.PUBLIC).addSuperinterface(TypeName.get(elem.asType()));
// add class generic type
classBuilder.addTypeVariables(getTypeNames(interfaceClazz.getTypeParameters()));
// add direct method
addMethods(interfaceClazz, classBuilder);
// add method form superinterface
addSuperInterfaceMethods(interfaceClazz.getInterfaces(), classBuilder);
// write class
JavaFile javaFile = JavaFile.builder(processingEnv.getElementUtils().getPackageOf(interfaceClazz).getQualifiedName().toString(), classBuilder.build()).build();
javaFile.writeTo(new File(System.getProperty("basedir"), TARGET_DIR));
} else {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "MotanAsyncProcessor not process, because " + elem.toString() + " not a interface.");
}
}
use of com.squareup.javapoet.JavaFile in project EasyRouter by Xiasm.
the class RouterProcessor method generatedGroup.
private void generatedGroup(TypeElement iRouteGroup) {
// 创建参数类型 Map<String, RouteMeta>
ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), ClassName.get(RouteMeta.class));
ParameterSpec altas = ParameterSpec.builder(parameterizedTypeName, "atlas").build();
for (Map.Entry<String, List<RouteMeta>> entry : groupMap.entrySet()) {
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(Constant.METHOD_LOAD_INTO).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(altas);
String groupName = entry.getKey();
List<RouteMeta> groupData = entry.getValue();
for (RouteMeta routeMeta : groupData) {
// 函数体的添加
methodBuilder.addStatement("atlas.put($S,$T.build($T.$L,$T.class,$S,$S))", routeMeta.getPath(), ClassName.get(RouteMeta.class), ClassName.get(RouteMeta.Type.class), routeMeta.getType(), ClassName.get(((TypeElement) routeMeta.getElement())), routeMeta.getPath(), routeMeta.getGroup());
}
String groupClassName = Constant.NAME_OF_GROUP + groupName;
TypeSpec typeSpec = TypeSpec.classBuilder(groupClassName).addSuperinterface(ClassName.get(iRouteGroup)).addModifiers(Modifier.PUBLIC).addMethod(methodBuilder.build()).build();
JavaFile javaFile = JavaFile.builder(Constant.PACKAGE_OF_GENERATE_FILE, typeSpec).build();
try {
javaFile.writeTo(filerUtils);
} catch (IOException e) {
e.printStackTrace();
}
rootMap.put(groupName, groupClassName);
}
}
Aggregations