Search in sources :

Example 6 with FieldSpec

use of com.squareup.javapoet.FieldSpec 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)

Example 7 with FieldSpec

use of com.squareup.javapoet.FieldSpec in project web3sdk by FISCO-BCOS.

the class AbiTypesGenerator method generateFixedTypes.

private <T extends Type> void generateFixedTypes(Class<T> superclass, Path path) throws IOException {
    String packageName = createPackageName(superclass);
    ClassName className;
    for (int mBitSize = 8; mBitSize < Type.MAX_BIT_LENGTH; mBitSize += 8) {
        inner: for (int nBitSize = 8; nBitSize < Type.MAX_BIT_LENGTH; nBitSize += 8) {
            if (mBitSize + nBitSize > Type.MAX_BIT_LENGTH) {
                break inner;
            }
            MethodSpec constructorSpec1 = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(BigInteger.class, "value").addStatement("super($L, $L, $N)", mBitSize, nBitSize, "value").build();
            MethodSpec constructorSpec2 = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(int.class, "mBitSize").addParameter(int.class, "nBitSize").addParameter(BigInteger.class, "m").addParameter(BigInteger.class, "n").addStatement("super($L, $L, $N, $N)", mBitSize, nBitSize, "m", "n").build();
            className = ClassName.get(packageName, superclass.getSimpleName() + mBitSize + "x" + nBitSize);
            FieldSpec defaultFieldSpec = FieldSpec.builder(className, DEFAULT, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("new $T(BigInteger.ZERO)", className).build();
            TypeSpec fixedType = TypeSpec.classBuilder(className.simpleName()).addJavadoc(CODEGEN_WARNING).superclass(superclass).addModifiers(Modifier.PUBLIC).addField(defaultFieldSpec).addMethod(constructorSpec1).addMethod(constructorSpec2).build();
            write(packageName, fixedType, path);
        }
    }
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) BigInteger(java.math.BigInteger) Uint(org.bcos.web3j.abi.datatypes.Uint) FieldSpec(com.squareup.javapoet.FieldSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 8 with FieldSpec

use of com.squareup.javapoet.FieldSpec in project web3sdk by FISCO-BCOS.

the class AbiTypesGenerator method generateBytesTypes.

private <T extends Type> void generateBytesTypes(Class<T> superclass, Path path) throws IOException {
    String packageName = createPackageName(superclass);
    ClassName className;
    for (int byteSize = 1; byteSize <= 32; byteSize++) {
        MethodSpec constructorSpec = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(byte[].class, "value").addStatement("super($L, $N)", byteSize, "value").build();
        className = ClassName.get(packageName, superclass.getSimpleName() + byteSize);
        FieldSpec defaultFieldSpec = FieldSpec.builder(className, DEFAULT, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("new $T(new byte[$L])", className, byteSize).build();
        TypeSpec bytesType = TypeSpec.classBuilder(className.simpleName()).addJavadoc(CODEGEN_WARNING).superclass(superclass).addModifiers(Modifier.PUBLIC).addField(defaultFieldSpec).addMethod(constructorSpec).build();
        write(packageName, bytesType, path);
    }
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) Uint(org.bcos.web3j.abi.datatypes.Uint) FieldSpec(com.squareup.javapoet.FieldSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 9 with FieldSpec

use of com.squareup.javapoet.FieldSpec in project web3sdk by FISCO-BCOS.

the class AbiTypesGenerator method generateIntTypes.

private <T extends Type> void generateIntTypes(Class<T> superclass, Path path) throws IOException {
    String packageName = createPackageName(superclass);
    ClassName className;
    for (int bitSize = 8; bitSize <= Type.MAX_BIT_LENGTH; bitSize += 8) {
        className = ClassName.get(packageName, superclass.getSimpleName() + bitSize);
        MethodSpec constructorSpec = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(BigInteger.class, "value").addStatement("super($L, $N)", bitSize, "value").build();
        MethodSpec overideConstructorSpec = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(long.class, "value").addStatement("this(BigInteger.valueOf(value))").build();
        FieldSpec defaultFieldSpec = FieldSpec.builder(className, DEFAULT, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("new $T(BigInteger.ZERO)", className).build();
        TypeSpec intType = TypeSpec.classBuilder(className.simpleName()).addJavadoc(CODEGEN_WARNING).superclass(superclass).addModifiers(Modifier.PUBLIC).addField(defaultFieldSpec).addMethods(Arrays.asList(constructorSpec, overideConstructorSpec)).build();
        write(packageName, intType, path);
    }
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) Uint(org.bcos.web3j.abi.datatypes.Uint) FieldSpec(com.squareup.javapoet.FieldSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 10 with FieldSpec

use of com.squareup.javapoet.FieldSpec in project potato by eyeem.

the class CodeWriter method process.

private void process() throws ClassNotFoundException, IOException {
    // Build fields
    ArrayList<FieldSpec> fields = new ArrayList<>();
    FieldSpec field;
    //FieldSpec.builder(String.class, "instance", Modifier.PRIVATE, Modifier.STATIC);
    field = FieldSpec.builder(generatedClass, "instance").addModifiers(Modifier.PRIVATE, Modifier.STATIC).build();
    fields.add(field);
    // Build methods
    ArrayList<MethodSpec> methods = new ArrayList<>();
    MethodSpec method;
    method = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addParameter(context, "context").addStatement("super(context)").build();
    methods.add(method);
    method = MethodSpec.methodBuilder("id").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(dataClass, "object").returns(String.class).addStatement("return object." + id).build();
    methods.add(method);
    method = MethodSpec.methodBuilder("classname").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(Class.class).addStatement("return $T.class", dataClass).build();
    methods.add(method);
    method = MethodSpec.methodBuilder("get").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(context, "context").returns(generatedClass).addStatement("if(instance == null) initialise(context)").addStatement("return instance").build();
    methods.add(method);
    method = MethodSpec.methodBuilder("initialise").addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.SYNCHRONIZED).addParameter(context, "context").beginControlFlow("if(instance == null)").addStatement("instance = new $T(context)", generatedClass).addStatement("instance.init()").endControlFlow().build();
    methods.add(method);
    // build class
    TypeSpec.Builder builder = TypeSpec.classBuilder(generatedClassName).addModifiers(Modifier.PUBLIC).superclass(ParameterizedTypeName.get(ClassName.get("com.eyeem.storage", "Storage"), dataClass));
    for (FieldSpec fieldSpec : fields) builder.addField(fieldSpec);
    for (MethodSpec methodSpec : methods) builder.addMethod(methodSpec);
    TypeSpec typeSpec = builder.build();
    JavaFile javaFile = JavaFile.builder(packageName, typeSpec).build();
    Writer writer = filer.createSourceFile(packageName + "." + generatedClassName).openWriter();
    javaFile.writeTo(writer);
    writer.close();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ArrayList(java.util.ArrayList) JavaFile(com.squareup.javapoet.JavaFile) FieldSpec(com.squareup.javapoet.FieldSpec) Writer(java.io.Writer) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

FieldSpec (com.squareup.javapoet.FieldSpec)12 ClassName (com.squareup.javapoet.ClassName)11 TypeSpec (com.squareup.javapoet.TypeSpec)11 MethodSpec (com.squareup.javapoet.MethodSpec)9 ArrayList (java.util.ArrayList)6 CodeBlock (com.squareup.javapoet.CodeBlock)5 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)4 TypeElement (javax.lang.model.element.TypeElement)4 JavaFile (com.squareup.javapoet.JavaFile)3 TypeName (com.squareup.javapoet.TypeName)3 List (java.util.List)3 Element (javax.lang.model.element.Element)3 Uint (org.bcos.web3j.abi.datatypes.Uint)3 WireField (com.squareup.wire.WireField)2 Field (com.squareup.wire.schema.Field)2 HashMap (java.util.HashMap)2 ByteString (okio.ByteString)2 Extra (com.app.annotation.apt.Extra)1 Router (com.app.annotation.apt.Router)1 SceneTransition (com.app.annotation.apt.SceneTransition)1