Search in sources :

Example 41 with JavaFile

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);
    }
}
Also used : JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) ProtoFile(com.squareup.wire.schema.ProtoFile) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile)

Example 42 with JavaFile

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();
}
Also used : JavaFile(com.squareup.javapoet.JavaFile)

Example 43 with JavaFile

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);
}
Also used : JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) ProtoFile(com.squareup.wire.schema.ProtoFile) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile)

Example 44 with JavaFile

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.");
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) JavaFile(com.squareup.javapoet.JavaFile) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 45 with JavaFile

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);
    }
}
Also used : RouteMeta(com.xsm.easy.annotation.modle.RouteMeta) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) IOException(java.io.IOException) JavaFile(com.squareup.javapoet.JavaFile) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

JavaFile (com.squareup.javapoet.JavaFile)68 TypeSpec (com.squareup.javapoet.TypeSpec)43 IOException (java.io.IOException)42 TypeElement (javax.lang.model.element.TypeElement)25 MethodSpec (com.squareup.javapoet.MethodSpec)21 ClassName (com.squareup.javapoet.ClassName)19 Element (javax.lang.model.element.Element)15 ExecutableElement (javax.lang.model.element.ExecutableElement)12 HashMap (java.util.HashMap)10 File (java.io.File)9 HashSet (java.util.HashSet)9 Map (java.util.Map)9 VariableElement (javax.lang.model.element.VariableElement)9 Builder (com.squareup.javapoet.TypeSpec.Builder)8 ArrayList (java.util.ArrayList)8 ProcessorException (com.github.mvp4g.mvp4g2.processor.ProcessorException)6 PackageElement (javax.lang.model.element.PackageElement)6 DeclaredType (javax.lang.model.type.DeclaredType)6 TypeMirror (javax.lang.model.type.TypeMirror)6 CodeBlock (com.squareup.javapoet.CodeBlock)5