Search in sources :

Example 16 with MethodSpec

use of com.squareup.javapoet.MethodSpec 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 17 with MethodSpec

use of com.squareup.javapoet.MethodSpec 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 18 with MethodSpec

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

the class SolidityFunctionWrapper method buildEventObservableFunction.

static MethodSpec buildEventObservableFunction(String responseClassName, String functionName, List<NamedTypeName> indexedParameters, List<NamedTypeName> nonIndexedParameters) throws ClassNotFoundException {
    String generatedFunctionName = Strings.lowercaseFirstLetter(functionName) + "EventObservable";
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(rx.Observable.class), ClassName.get("", responseClassName));
    MethodSpec.Builder observableMethodBuilder = MethodSpec.methodBuilder(generatedFunctionName).addModifiers(Modifier.PUBLIC).addParameter(DefaultBlockParameter.class, START_BLOCK).addParameter(DefaultBlockParameter.class, END_BLOCK).returns(parameterizedTypeName);
    buildVariableLengthEventConstructor(observableMethodBuilder, functionName, indexedParameters, nonIndexedParameters);
    TypeSpec converter = TypeSpec.anonymousClassBuilder("").addSuperinterface(ParameterizedTypeName.get(ClassName.get(Func1.class), ClassName.get(Log.class), ClassName.get("", responseClassName))).addMethod(MethodSpec.methodBuilder("call").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(Log.class, "log").returns(ClassName.get("", responseClassName)).addStatement("$T eventValues = extractEventParameters(event, log)", EventValues.class).addStatement("$1T typedResponse = new $1T()", ClassName.get("", responseClassName)).addCode(buildTypedResponse("typedResponse", indexedParameters, nonIndexedParameters)).addStatement("return typedResponse").build()).build();
    observableMethodBuilder.addStatement("$1T filter = new $1T($2L, $3L, " + "getContractAddress())", EthFilter.class, START_BLOCK, END_BLOCK).addStatement("filter.addSingleTopic($T.encode(event))", EventEncoder.class).addStatement("return web3j.ethLogObservable(filter).map($L)", converter);
    return observableMethodBuilder.build();
}
Also used : DefaultBlockParameter(org.bcos.web3j.protocol.core.DefaultBlockParameter) MethodSpec(com.squareup.javapoet.MethodSpec) EventEncoder(org.bcos.web3j.abi.EventEncoder) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 19 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project Rocket by mozilla-tw.

the class AppModuleGenerator method generateRegisterComponents.

private MethodSpec generateRegisterComponents(Set<String> libraryGlideModuleClassNames, Set<String> excludedGlideModuleClassNames) {
    MethodSpec.Builder registerComponents = MethodSpec.methodBuilder("registerComponents").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(ClassName.get("android.content", "Context"), "context").addParameter(ClassName.get("com.bumptech.glide", "Glide"), "glide").addParameter(ClassName.get("com.bumptech.glide", "Registry"), "registry");
    for (String glideModule : libraryGlideModuleClassNames) {
        if (excludedGlideModuleClassNames.contains(glideModule)) {
            continue;
        }
        ClassName moduleClassName = ClassName.bestGuess(glideModule);
        registerComponents.addStatement("new $T().registerComponents(context, glide, registry)", moduleClassName);
    }
    // Order matters here. The AppGlideModule must be called last.
    registerComponents.addStatement("appGlideModule.registerComponents(context, glide, registry)");
    return registerComponents.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName)

Example 20 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project Rocket by mozilla-tw.

the class AppModuleGenerator method generateConstructor.

private MethodSpec generateConstructor(ClassName appGlideModule, Set<String> libraryGlideModuleClassNames, Set<String> excludedGlideModuleClassNames) {
    MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
    constructorBuilder.addStatement("appGlideModule = new $T()", appGlideModule);
    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 AppGlideModule from annotation: " + appGlideModule);
    // Excluded GlideModule classes from the manifest are logged in Glide's singleton.
    for (String glideModule : libraryGlideModuleClassNames) {
        ClassName moduleClassName = ClassName.bestGuess(glideModule);
        if (excludedGlideModuleClassNames.contains(glideModule)) {
            constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG, "AppGlideModule excludes LibraryGlideModule from annotation: " + moduleClassName);
        } else {
            constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG, "Discovered LibraryGlideModule 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