Search in sources :

Example 31 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project atlasdb by palantir.

the class TableClassRendererV2 method renderNamedGetRangeStartEnd.

private MethodSpec renderNamedGetRangeStartEnd(NamedColumnDescription col) {
    Preconditions.checkArgument(tableMetadata.getRowMetadata().getRowParts().size() == 1);
    NameComponentDescription rowComponent = tableMetadata.getRowMetadata().getRowParts().get(0);
    MethodSpec.Builder getterBuilder = MethodSpec.methodBuilder("getSmallRowRange" + VarName(col)).addModifiers(Modifier.PUBLIC).addJavadoc("Returns a mapping from all the row keys in a range to their value at column $L\n" + "(if that column exists for the row-key). As the $L values are all loaded in memory,\n" + "do not use for large amounts of data. The order of results is preserved in the map.", VarName(col), VarName(col)).addParameter(rowComponent.getType().getJavaClass(), "startInclusive").addParameter(rowComponent.getType().getJavaClass(), "endExclusive").returns(ParameterizedTypeName.get(ClassName.get(LinkedHashMap.class), ClassName.get(rowComponent.getType().getJavaClass()), ClassName.get(getColumnClassForGenericTypeParameter(col))));
    getterBuilder.addStatement("$T rangeRequest = $T.builder()\n" + ".startRowInclusive($T.of(startInclusive).persistToBytes())\n" + ".endRowExclusive($T.of(endExclusive).persistToBytes())\n" + ".build()", RangeRequest.class, RangeRequest.class, rowType, rowType).addStatement("return getSmallRowRange$L(rangeRequest)", VarName(col));
    return getterBuilder.build();
}
Also used : NameComponentDescription(com.palantir.atlasdb.table.description.NameComponentDescription) MethodSpec(com.squareup.javapoet.MethodSpec) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest)

Example 32 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project atlasdb by palantir.

the class TableClassRendererV2 method renderNamedGetRangeColumn.

private MethodSpec renderNamedGetRangeColumn(NamedColumnDescription col) {
    Preconditions.checkArgument(tableMetadata.getRowMetadata().getRowParts().size() == 1);
    NameComponentDescription rowComponent = tableMetadata.getRowMetadata().getRowParts().get(0);
    MethodSpec.Builder getterBuilder = MethodSpec.methodBuilder("getSmallRowRange" + VarName(col)).addModifiers(Modifier.PUBLIC).addJavadoc("Returns a mapping from all the row keys in a rangeRequest to their value at column $L\n" + "(if that column exists for the row-key). As the $L values are all loaded in memory,\n" + "do not use for large amounts of data. The order of results is preserved in the map.", VarName(col), VarName(col)).addParameter(RangeRequest.class, "rangeRequest").returns(ParameterizedTypeName.get(ClassName.get(LinkedHashMap.class), ClassName.get(rowComponent.getType().getJavaClass()), ClassName.get(getColumnClassForGenericTypeParameter(col))));
    getterBuilder.addStatement("$T colSelection =\n" + "$T.create($T.of($T.toCachedBytes($L)))", ColumnSelection.class, ColumnSelection.class, ImmutableList.class, PtBytes.class, ColumnRenderers.short_name(col)).addStatement("rangeRequest = rangeRequest.getBuilder().retainColumns(colSelection).build()").addStatement("$T.checkArgument(rangeRequest.getColumnNames().size() <= 1,\n$S)", Preconditions.class, "Must not request columns other than " + VarName(col) + ".").addCode("\n").addStatement("$T<$T, $T> resultsMap = new $T<>()", LinkedHashMap.class, rowComponent.getType().getJavaClass(), getColumnClassForGenericTypeParameter(col), LinkedHashMap.class).addStatement("$T.of(t.getRange(tableRef, rangeRequest))\n" + ".immutableCopy().forEach(entry -> {\n" + "     $T resultEntry =\n " + "         $T.of(entry);\n" + "     resultsMap.put(resultEntry.getRowName().get$L(), resultEntry.get$L());\n" + "})", BatchingVisitableView.class, rowResultType, rowResultType, CamelCase(rowComponent.getComponentName()), VarName(col)).addStatement("return resultsMap");
    return getterBuilder.build();
}
Also used : NameComponentDescription(com.palantir.atlasdb.table.description.NameComponentDescription) BatchingVisitableView(com.palantir.common.base.BatchingVisitableView) MethodSpec(com.squareup.javapoet.MethodSpec) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest)

Example 33 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project mvp4g2 by mvp4g.

the class ApplicationGenerator method generate.

public void generate(ApplicationMetaModel metaModel) throws ProcessorException {
    // check if element is existing (to avoid generating code for deleted items)
    if (!this.processorUtils.doesExist(metaModel.getApplication())) {
        return;
    }
    // generate code
    TypeSpec.Builder typeSpec = TypeSpec.classBuilder(metaModel.getApplication().getSimpleName() + ApplicationGenerator.IMPL_NAME).superclass(ParameterizedTypeName.get(ClassName.get(AbstractApplication.class), metaModel.getEventBus().getTypeName())).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addSuperinterface(metaModel.getApplication().getTypeName());
    // constructor ...
    MethodSpec constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addStatement("super()").addStatement("super.eventBus = new $N.$N()", metaModel.getEventBus().getPackage(), metaModel.getEventBus().getSimpleName() + ApplicationGenerator.IMPL_NAME).addStatement("super.historyOnStart = $L", metaModel.getHistoryOnStart()).build();
    typeSpec.addMethod(constructor);
    // method "getApplicaitonLoader"
    MethodSpec getApplicaitonLaoderMethod = MethodSpec.methodBuilder("getApplicationLoader").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(IsApplicationLoader.class).addStatement("return new $T()", metaModel.getLoader().getTypeName()).build();
    typeSpec.addMethod(getApplicaitonLaoderMethod);
    JavaFile javaFile = JavaFile.builder(metaModel.getEventBus().getPackage(), typeSpec.build()).build();
    try {
        javaFile.writeTo(this.processingEnvironment.getFiler());
    } catch (IOException e) {
        throw new ProcessorException("Unable to write generated file: >>" + metaModel.getEventBus().getSimpleName() + ApplicationGenerator.IMPL_NAME + "<< -> exception: " + e.getMessage());
    }
}
Also used : ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) MethodSpec(com.squareup.javapoet.MethodSpec) JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 34 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project mvp4g2 by mvp4g.

the class EventBusGenerator method generate.

public void generate(EventBusMetaModel eventBusMetaMetaModel, HandlerMetaModel handlerMetaModel, PresenterMetaModel presenterMetaModel, HistoryMetaModel historyMetaModel) throws ProcessorException {
    // check if element is existing (to avoid generating code for deleted items)
    if (!this.processorUtils.doesExist(eventBusMetaMetaModel.getEventBus())) {
        return;
    }
    ClassName abstractEventBusClassName = ClassName.get(AbstractEventBus.class);
    TypeSpec.Builder typeSpec = TypeSpec.classBuilder(eventBusMetaMetaModel.getEventBus().getSimpleName() + EventBusGenerator.IMPL_NAME).superclass(ParameterizedTypeName.get(abstractEventBusClassName, eventBusMetaMetaModel.getEventBus().getTypeName())).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addSuperinterface(eventBusMetaMetaModel.getEventBus().getTypeName());
    MethodSpec constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addStatement("super($S)", eventBusMetaMetaModel.getShell().getClassName()).build();
    typeSpec.addMethod(constructor);
    DebugGenerator.builder().eventBusMetaModel(eventBusMetaMetaModel).typeSpec(typeSpec).build().generate();
    FilterGenerator.builder().eventBusMetaModel(eventBusMetaMetaModel).typeSpec(typeSpec).build().generate();
    EventLoadMetaDataGenerator.builder().processingEnvironment(this.processingEnvironment).eventBusMetaModel(eventBusMetaMetaModel).typeSpec(typeSpec).build().generate();
    EventMetaDataGenerator.builder().processingEnvironment(this.processingEnvironment).eventBusMetaModel(eventBusMetaMetaModel).build().generate();
    EventHandlingMethodGenerator.builder().processorUtils(this.processorUtils).typeSpec(typeSpec).eventBusMetaModel(eventBusMetaMetaModel).historyMetaModel(historyMetaModel).presenterMetaModel(presenterMetaModel).handlerMetaModel(handlerMetaModel).build().generate();
    AddPresenterGenerator.builder().typeSpec(typeSpec).processingEnvironment(this.processingEnvironment).presenterMetaModel(presenterMetaModel).build().generate();
    HandlerAndPresenterRegristrationGenerator.builder().processingEnvironment(this.processingEnvironment).typeSpec(typeSpec).eventBusMetaModel(eventBusMetaMetaModel).presenterMetaModel(presenterMetaModel).handlerMetaModel(handlerMetaModel).build().generate();
    JavaFile javaFile = JavaFile.builder(eventBusMetaMetaModel.getEventBus().getPackage(), typeSpec.build()).build();
    // System.out.println(javaFile.toString());
    try {
        javaFile.writeTo(this.processingEnvironment.getFiler());
    } catch (IOException e) {
        throw new ProcessorException("Unable to write generated file: >>" + eventBusMetaMetaModel.getEventBus().getSimpleName() + EventBusGenerator.IMPL_NAME + "<< -> exception: " + e.getMessage());
    }
}
Also used : ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 35 with MethodSpec

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

the class AppModuleGenerator method generate.

TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClassNames) {
    ClassName appGlideModuleClassName = ClassName.get(appGlideModule);
    List<String> excludedGlideModuleClassNames = getExcludedGlideModuleClassNames(appGlideModule);
    List<String> orderedLibraryGlideModuleClassNames = new ArrayList<>(libraryGlideModuleClassNames);
    Collections.sort(orderedLibraryGlideModuleClassNames);
    MethodSpec constructor = generateConstructor(appGlideModuleClassName, orderedLibraryGlideModuleClassNames, excludedGlideModuleClassNames);
    MethodSpec registerComponents = generateRegisterComponents(orderedLibraryGlideModuleClassNames, excludedGlideModuleClassNames);
    MethodSpec getExcludedModuleClasses = generateGetExcludedModuleClasses(excludedGlideModuleClassNames);
    MethodSpec applyOptions = MethodSpec.methodBuilder("applyOptions").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(ParameterSpec.builder(ClassName.get("android.content", "Context"), "context").addAnnotation(nonNull()).build()).addParameter(ParameterSpec.builder(ClassName.get("com.bumptech.glide", "GlideBuilder"), "builder").addAnnotation(nonNull()).build()).addStatement("appGlideModule.applyOptions(context, builder)", appGlideModule).build();
    MethodSpec isManifestParsingEnabled = MethodSpec.methodBuilder("isManifestParsingEnabled").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(boolean.class).addStatement("return appGlideModule.isManifestParsingEnabled()", appGlideModule).build();
    Builder builder = TypeSpec.classBuilder(GENERATED_APP_MODULE_IMPL_SIMPLE_NAME).addModifiers(Modifier.FINAL).addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "deprecation").build()).superclass(ClassName.get(GENERATED_ROOT_MODULE_PACKAGE_NAME, GENERATED_ROOT_MODULE_SIMPLE_NAME)).addField(appGlideModuleClassName, "appGlideModule", Modifier.PRIVATE, Modifier.FINAL).addMethod(constructor).addMethod(applyOptions).addMethod(registerComponents).addMethod(isManifestParsingEnabled).addMethod(getExcludedModuleClasses);
    ClassName generatedRequestManagerFactoryClassName = ClassName.get(RequestManagerFactoryGenerator.GENERATED_REQUEST_MANAGER_FACTORY_PACKAGE_NAME, RequestManagerFactoryGenerator.GENERATED_REQUEST_MANAGER_FACTORY_SIMPLE_NAME);
    builder.addMethod(MethodSpec.methodBuilder("getRequestManagerFactory").addAnnotation(Override.class).addAnnotation(nonNull()).returns(generatedRequestManagerFactoryClassName).addStatement("return new $T()", generatedRequestManagerFactoryClassName).build());
    return builder.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) ArrayList(java.util.ArrayList)

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