Search in sources :

Example 76 with MethodSpec

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

the class SolidityFunctionWrapper method buildRegisterEventLogPushFunction.

private MethodSpec buildRegisterEventLogPushFunction(String eventName) throws ClassNotFoundException {
    String generatedFunctionName = "register" + eventName + "EventLogFilter";
    MethodSpec.Builder getEventMethodBuilder = MethodSpec.methodBuilder(generatedFunctionName).addModifiers(Modifier.PUBLIC).addParameter(String.class, FROM_BLOCK).addParameter(String.class, TO_BLOCK);
    addParameter(getEventMethodBuilder, "string[]", OTHER_TOPICS).addParameter(AbiTypes.getType("EventLogPushCallback"), CALLBACK_VALUE);
    getEventMethodBuilder.addStatement("String topic0 = $T.encode(" + buildEventDefinitionName(eventName) + ")", EventEncoder.class);
    getEventMethodBuilder.addStatement("registerEventLogPushFilter(ABI,BINARY" + "," + "topic0" + "," + FROM_BLOCK + "," + TO_BLOCK + "," + OTHER_TOPICS + "," + CALLBACK_VALUE + ")");
    return getEventMethodBuilder.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String)

Example 77 with MethodSpec

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

the class AbiTypesMapperGenerator method generate.

private void generate(String destinationDir) throws IOException {
    String typesPackageName = "org.fisco.bcos.web3j.abi.datatypes";
    String autoGeneratedTypesPackageName = typesPackageName + ".generated";
    MethodSpec.Builder builder = MethodSpec.methodBuilder("getType").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(String.class, TYPE).returns(ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(Object.class))).beginControlFlow("switch (type)");
    builder = addTypes(builder, typesPackageName);
    builder = addGeneratedTypes(builder, autoGeneratedTypesPackageName);
    builder = builder.addStatement("default:\nthrow new $T($S\n+ $N)", UnsupportedOperationException.class, "Unsupported type encountered: ", TYPE);
    builder.endControlFlow();
    MethodSpec methodSpec = builder.build();
    MethodSpec constructorSpec = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build();
    TypeSpec typeSpec = TypeSpec.classBuilder("AbiTypes").addJavadoc(buildWarning(AbiTypesMapperGenerator.class)).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(constructorSpec).addMethod(methodSpec).build();
    write(autoGeneratedTypesPackageName, typeSpec, destinationDir);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 78 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project bazel by bazelbuild.

the class JavaCodeGeneratorHelper method targetWithNextHelper.

/**
   * Writes a class file {@code Deps(index).java} to the directory
   * {@code projectPath/com/example/deps(index)}
   *
   * @param callNext if we should call the method from {@code Deps(index+1).java}
   */
static void targetWithNextHelper(int index, boolean callNext, Path projectPath) throws IOException {
    ClassName nextClass = ClassName.get("com.example.deps" + (index + 1), "Deps" + (index + 1));
    MethodSpec callNextMethod = MethodSpec.methodBuilder("CallNext").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(void.class).addStatement("$T.PrintSth()", nextClass).build();
    TypeSpec.Builder klassBuilder = TypeSpec.classBuilder("Deps" + index).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(randomMethod);
    if (callNext) {
        klassBuilder.addMethod(callNextMethod);
    }
    TypeSpec klass = klassBuilder.build();
    writeClassToDir(klass, "com.example.deps" + index, projectPath);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 79 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project bazel by bazelbuild.

the class JavaCodeGeneratorHelper method targetWithNextExtraHelper.

/**
   * Writes a class file {@code Deps(index).java} with extra method {@code printSthElse()}
   * to the directory {@code projectPath/com/example/deps(index)}
   *
   * @param callNext if we should call the method from {@code Deps(index+1).java}
   */
static void targetWithNextExtraHelper(int index, boolean callNext, Path projectPath) throws IOException {
    ClassName nextClass = ClassName.get("com.example.deps" + (index + 1), "Deps" + (index + 1));
    MethodSpec callNextMethod = MethodSpec.methodBuilder("CallNext").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(void.class).addStatement("$T.PrintSth()", nextClass).build();
    TypeSpec.Builder klassBuilder = TypeSpec.classBuilder("Deps" + index).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(randomMethod).addMethod(somethingElseMethod);
    if (callNext) {
        klassBuilder.addMethod(callNextMethod);
    }
    TypeSpec klass = klassBuilder.build();
    writeClassToDir(klass, "com.example.deps" + index, projectPath);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 80 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project glide by bumptech.

the class RootModuleGenerator method generateConstructor.

private static MethodSpec generateConstructor(ClassName rootGlideModule, Set<String> childGlideModuleClassNames, Set<String> excludedGlideModuleClassNames) {
    MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
    constructorBuilder.addStatement("rootGlideModule = new $T()", rootGlideModule);
    ClassName androidLogName = ClassName.get("android.util", "Log");
    // Add some log lines to indicate to developers which modules where discovered.
    constructorBuilder.beginControlFlow("if ($T.isLoggable($S, $T.DEBUG))", androidLogName, GLIDE_LOG_TAG, androidLogName);
    constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG, "Discovered RootGlideModule from annotation: " + rootGlideModule);
    // Excluded GlideModule classes from the manifest are logged in Glide's singleton.
    for (String glideModule : childGlideModuleClassNames) {
        ClassName moduleClassName = ClassName.bestGuess(glideModule);
        if (excludedGlideModuleClassNames.contains(glideModule)) {
            constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG, "RootGlideModule excludes ChildGlideModule from annotation: " + moduleClassName);
        } else {
            constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG, "Discovered ChildGlideModule from annotation: " + moduleClassName);
        }
    }
    constructorBuilder.endControlFlow();
    return constructorBuilder.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName)

Aggregations

MethodSpec (com.squareup.javapoet.MethodSpec)155 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)43 TypeName (com.squareup.javapoet.TypeName)42 ArrayList (java.util.ArrayList)38 ClassName (com.squareup.javapoet.ClassName)34 TypeSpec (com.squareup.javapoet.TypeSpec)31 ParameterSpec (com.squareup.javapoet.ParameterSpec)24 CodeBlock (com.squareup.javapoet.CodeBlock)23 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)20 Nonnull (javax.annotation.Nonnull)17 TypeMirror (javax.lang.model.type.TypeMirror)15 WireField (com.squareup.wire.WireField)14 Field (com.squareup.wire.schema.Field)14 TypeElement (javax.lang.model.element.TypeElement)14 ByteString (okio.ByteString)13 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)12 List (java.util.List)11 VariableElement (javax.lang.model.element.VariableElement)11 Utf8String (org.fisco.bcos.web3j.abi.datatypes.Utf8String)11 FieldSpec (com.squareup.javapoet.FieldSpec)10