Search in sources :

Example 1 with Extra

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

the class RouterProcessor method process.

@Override
public void process(RoundEnvironment roundEnv, AnnotationProcessor mAbstractProcessor) {
    String CLASS_NAME = "TRouter";
    TypeSpec.Builder tb = classBuilder(CLASS_NAME).addModifiers(PUBLIC, FINAL).addJavadoc("@ 全局路由器 此类由apt自动生成");
    FieldSpec extraField = FieldSpec.builder(ParameterizedTypeName.get(HashMap.class, String.class, Object.class), "mCurActivityExtra").addModifiers(Modifier.PUBLIC, Modifier.STATIC).build();
    tb.addField(extraField);
    MethodSpec.Builder methodBuilder1 = MethodSpec.methodBuilder("go").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(String.class, "name").addParameter(HashMap.class, "extra").addParameter(ClassName.get("android.view", "View"), "view");
    MethodSpec.Builder methodBuilder2 = MethodSpec.methodBuilder("bind").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(ClassName.get("android.app", "Activity"), "mContext");
    List<ClassName> mList = new ArrayList<>();
    CodeBlock.Builder blockBuilderGo = CodeBlock.builder();
    CodeBlock.Builder blockBuilderBind = CodeBlock.builder();
    ClassName appClassName = ClassName.get("com", "App");
    blockBuilderGo.addStatement("mCurActivityExtra=extra");
    blockBuilderGo.addStatement("Activity mContext=$T.getAppContext().getCurActivity()", appClassName);
    // 括号开始
    blockBuilderGo.beginControlFlow(" switch (name)");
    blockBuilderBind.addStatement("if(mCurActivityExtra==null) return");
    // 括号开始
    blockBuilderBind.beginControlFlow(" switch (mContext.getClass().getSimpleName())");
    List<RouterActivityModel> mRouterActivityModels = new ArrayList<>();
    try {
        for (TypeElement element : ElementFilter.typesIn(roundEnv.getElementsAnnotatedWith(Router.class))) {
            ClassName currentType = ClassName.get(element);
            if (mList.contains(currentType))
                continue;
            mList.add(currentType);
            RouterActivityModel mRouterActivityModel = new RouterActivityModel();
            mRouterActivityModel.setElement(element);
            mRouterActivityModel.setActionName(element.getAnnotation(Router.class).value());
            List<Element> mExtraElements = new ArrayList<>();
            List<String> mExtraElementKeys = new ArrayList<>();
            for (Element childElement : element.getEnclosedElements()) {
                SceneTransition mSceneTransitionAnnotation = childElement.getAnnotation(SceneTransition.class);
                if (mSceneTransitionAnnotation != null) {
                    mRouterActivityModel.setSceneTransitionElementName(mSceneTransitionAnnotation.value());
                    mRouterActivityModel.setSceneTransitionElement(childElement);
                }
                Extra mExtraAnnotation = childElement.getAnnotation(Extra.class);
                if (mExtraAnnotation != null) {
                    mExtraElementKeys.add(mExtraAnnotation.value());
                    mExtraElements.add(childElement);
                }
            }
            mRouterActivityModel.setExtraElementKeys(mExtraElementKeys);
            mRouterActivityModel.setExtraElements(mExtraElements);
            boolean isNeedBind = (mExtraElementKeys != null && mExtraElementKeys.size() > 0 || mRouterActivityModel.getSceneTransitionElement() != null);
            mRouterActivityModel.setNeedBind(isNeedBind);
            mRouterActivityModels.add(mRouterActivityModel);
        }
        ClassName mActivityCompatName = ClassName.get("android.support.v4.app", "ActivityCompat");
        ClassName mIntentClassName = ClassName.get("android.content", "Intent");
        ClassName mActivityOptionsCompatName = ClassName.get("android.support.v4.app", "ActivityOptionsCompat");
        for (RouterActivityModel item : mRouterActivityModels) {
            // 1
            blockBuilderGo.add("case $S: \n", item.getActionName());
            if (item.isNeedBind())
                // 1
                blockBuilderBind.add("case $S: \n", item.getElement().getSimpleName());
            if (item.getExtraElements() != null && item.getExtraElements().size() > 0) {
                for (int i = 0; i < item.getExtraElements().size(); i++) {
                    Element mFiled = item.getExtraElements().get(i);
                    blockBuilderBind.add(// 1
                    "(($T)mContext)." + // 2
                    "$L" + // 3
                    "= ($T) " + // 4
                    "mCurActivityExtra.get(" + // 5
                    "$S);\n", // 1
                    item.getElement(), // 2
                    mFiled, // 3
                    mFiled, // 5
                    item.getExtraElementKeys().get(i));
                // 5
                }
            }
            if (item.getSceneTransitionElement() != null) {
                blockBuilderGo.add(// 2
                "$L.startActivity(mContext," + // 3
                "\nnew $L(mContext," + // 4
                "\n$L.class)," + // 5
                "\n$T.makeSceneTransitionAnimation(" + // 6
                "\nmContext,view," + // 7
                "\n$S).toBundle());", // 2
                mActivityCompatName, // 3
                mIntentClassName, // 4
                item.getElement(), // 5
                mActivityOptionsCompatName, // 6
                item.getSceneTransitionElementName());
                blockBuilderBind.add(// 2
                "$T.setTransitionName(" + // 3
                "(($T)mContext).mViewBinding." + // 4
                "$L, " + // 5
                "$S);\n", // 2
                ClassName.get("android.support.v4.view", "ViewCompat"), // 3
                item.getElement(), // 4
                item.getSceneTransitionElement(), // 5
                item.getSceneTransitionElementName());
            } else {
                blockBuilderGo.add(// 2
                "mContext.startActivity(" + // 3
                "\nnew $L(mContext," + // 7
                "\n$L.class));", // 3
                mIntentClassName, // 4
                item.getElement());
            }
            // 1
            blockBuilderGo.addStatement("\nbreak");
            // 1
            if (item.isNeedBind())
                blockBuilderBind.addStatement("break");
        }
        blockBuilderGo.addStatement("default: break");
        blockBuilderGo.endControlFlow();
        methodBuilder1.addCode(blockBuilderGo.build());
        blockBuilderBind.addStatement("default: break");
        blockBuilderBind.endControlFlow();
        methodBuilder2.addCode(blockBuilderBind.build());
        tb.addMethod(methodBuilder1.build());
        tb.addMethod(methodBuilder2.build());
        // 增加go(action)和go(action,extra):两个重载方法
        tb.addMethod(MethodSpec.methodBuilder("go").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(String.class, "name").addParameter(HashMap.class, "extra").addCode("go(name,extra,null);\n").build());
        tb.addMethod(MethodSpec.methodBuilder("go").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(String.class, "name").addCode("go(name,null,null);\n").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 : HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) SceneTransition(com.app.annotation.apt.SceneTransition) ClassName(com.squareup.javapoet.ClassName) FilerException(javax.annotation.processing.FilerException) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) CodeBlock(com.squareup.javapoet.CodeBlock) Router(com.app.annotation.apt.Router) IOException(java.io.IOException) FieldSpec(com.squareup.javapoet.FieldSpec) IOException(java.io.IOException) FilerException(javax.annotation.processing.FilerException) RouterActivityModel(com.app.apt.helper.RouterActivityModel) Extra(com.app.annotation.apt.Extra) JavaFile(com.squareup.javapoet.JavaFile) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

Extra (com.app.annotation.apt.Extra)1 Router (com.app.annotation.apt.Router)1 SceneTransition (com.app.annotation.apt.SceneTransition)1 RouterActivityModel (com.app.apt.helper.RouterActivityModel)1 ClassName (com.squareup.javapoet.ClassName)1 CodeBlock (com.squareup.javapoet.CodeBlock)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 JavaFile (com.squareup.javapoet.JavaFile)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 FilerException (javax.annotation.processing.FilerException)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1