Search in sources :

Example 1 with ApiFactory

use of com.app.annotation.apt.ApiFactory in project T-MVP by north2016.

the class ApiFactoryProcessor method process.

@Override
public void process(RoundEnvironment roundEnv, AnnotationProcessor mAbstractProcessor) {
    String CLASS_NAME = "ApiFactory";
    String DATA_ARR_CLASS = "DataArr";
    TypeSpec.Builder tb = classBuilder(CLASS_NAME).addModifiers(PUBLIC, FINAL).addJavadoc("@ API工厂 此类由apt自动生成");
    try {
        for (TypeElement element : ElementFilter.typesIn(roundEnv.getElementsAnnotatedWith(ApiFactory.class))) {
            mAbstractProcessor.mMessager.printMessage(Diagnostic.Kind.NOTE, "正在处理: " + element.toString());
            for (Element e : element.getEnclosedElements()) {
                ExecutableElement executableElement = (ExecutableElement) e;
                MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(e.getSimpleName().toString()).addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC);
                if (TypeName.get(executableElement.getReturnType()).toString().contains(DATA_ARR_CLASS)) {
                    // 返回列表数据
                    methodBuilder.returns(ClassName.get("io.reactivex", "Flowable"));
                    Map<String, Object> params = new HashMap<>();
                    methodBuilder.addParameter(params.getClass(), "param");
                    ClassName apiUtil = ClassName.get("com.base.util", "ApiUtil");
                    ClassName C = ClassName.get("com", "C");
                    CodeBlock.Builder blockBuilder = CodeBlock.builder();
                    int len = executableElement.getParameters().size();
                    for (int i = 0; i < len; i++) {
                        VariableElement ep = executableElement.getParameters().get(i);
                        boolean isLast = i == len - 1;
                        String split = (isLast ? "" : ",");
                        switch(ep.getSimpleName().toString()) {
                            case "include":
                                blockBuilder.add("$L.getInclude(param)" + split, apiUtil);
                                break;
                            case "where":
                                blockBuilder.add("$L.getWhere(param)" + split, apiUtil);
                                break;
                            case "skip":
                                blockBuilder.add("$L.getSkip(param)" + split, apiUtil);
                                break;
                            case "limit":
                                blockBuilder.add("$L.PAGE_COUNT" + split, C);
                                break;
                            case "order":
                                blockBuilder.add("$L._CREATED_AT" + split, C);
                                break;
                        }
                    }
                    methodBuilder.addStatement("return $T.getInstance()" + ".service.$L($L)" + ".compose($T.io_main())", ClassName.get("com.api", "Api"), e.getSimpleName().toString(), blockBuilder.build().toString(), ClassName.get("com.base.util.helper", "RxSchedulers"));
                    tb.addMethod(methodBuilder.build());
                } else {
                    // 返回普通数据
                    methodBuilder.returns(TypeName.get(executableElement.getReturnType()));
                    String paramsString = "";
                    for (VariableElement ep : executableElement.getParameters()) {
                        methodBuilder.addParameter(TypeName.get(ep.asType()), ep.getSimpleName().toString());
                        paramsString += ep.getSimpleName().toString() + ",";
                    }
                    methodBuilder.addStatement("return $T.getInstance()" + ".service.$L($L)" + ".compose($T.io_main())", ClassName.get("com.api", "Api"), e.getSimpleName().toString(), paramsString.substring(0, paramsString.length() - 1), ClassName.get("com.base.util.helper", "RxSchedulers"));
                    tb.addMethod(methodBuilder.build());
                }
            }
        }
        // 生成源代码
        JavaFile javaFile = JavaFile.builder(Utils.PackageName, tb.build()).build();
        // 在 app module/build/generated/source/apt 生成一份源代码
        javaFile.writeTo(mAbstractProcessor.mFiler);
    } catch (FilerException e) {
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ApiFactory(com.app.annotation.apt.ApiFactory) MethodSpec(com.squareup.javapoet.MethodSpec) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeBlock(com.squareup.javapoet.CodeBlock) VariableElement(javax.lang.model.element.VariableElement) IOException(java.io.IOException) IOException(java.io.IOException) FilerException(javax.annotation.processing.FilerException) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) FilerException(javax.annotation.processing.FilerException) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ApiFactory (com.app.annotation.apt.ApiFactory)1 ClassName (com.squareup.javapoet.ClassName)1 CodeBlock (com.squareup.javapoet.CodeBlock)1 JavaFile (com.squareup.javapoet.JavaFile)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 FilerException (javax.annotation.processing.FilerException)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1