Search in sources :

Example 16 with Builder

use of com.squareup.javapoet.TypeSpec.Builder in project vue-gwt by Axellience.

the class AbstractVueComponentFactoryGenerator method generate.

/**
 * Generate our {@link VueComponentFactory} class.
 *
 * @param component The {@link IsVueComponent} class to generate {@link VueComponentOptions} from
 */
public void generate(TypeElement component) {
    ClassName vueFactoryClassName = componentFactoryName(component);
    Builder vueFactoryBuilder = createFactoryBuilderClass(component, vueFactoryClassName);
    createGetName(vueFactoryBuilder, component);
    createProperties(vueFactoryClassName, vueFactoryBuilder);
    List<CodeBlock> staticInitParameters = createInitMethod(component, vueFactoryBuilder);
    createStaticGetMethod(component, vueFactoryClassName, vueFactoryBuilder, staticInitParameters);
    vueFactoryBuilder.addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PROTECTED).addAnnotation(Inject.class).build());
    // Build the ComponentOptions class
    GeneratorsUtil.toJavaFile(filer, vueFactoryBuilder, vueFactoryClassName, component);
}
Also used : Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) CodeBlock(com.squareup.javapoet.CodeBlock)

Example 17 with Builder

use of com.squareup.javapoet.TypeSpec.Builder in project vue-gwt by Axellience.

the class VueDirectiveOptionsGenerator method generate.

/**
 * Generate and save the Java file for the typeElement passed to the constructor
 *
 * @param directiveTypeElement The {@link VueDirective} class to generate {@link
 * VueDirectiveOptions} from
 */
public void generate(TypeElement directiveTypeElement) {
    ClassName optionsClassName = GeneratorsNameUtil.directiveOptionsName(directiveTypeElement);
    Builder componentClassBuilder = TypeSpec.classBuilder(optionsClassName).addModifiers(Modifier.PUBLIC, Modifier.FINAL).superclass(VueDirectiveOptions.class).addAnnotation(JsType.class).addJavadoc("VueComponent Directive Options for directive {@link $S}", directiveTypeElement.getQualifiedName().toString());
    // Initialize constructor
    MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC);
    // Add the Java Component Instance initialization
    constructorBuilder.addStatement("this.$L = new $T()", "vuegwt$javaDirectiveInstance", TypeName.get(directiveTypeElement.asType()));
    // Call the method to copy hooks functions
    constructorBuilder.addStatement("this.copyHooks()");
    // Finish building the constructor
    componentClassBuilder.addMethod(constructorBuilder.build());
    // Build the DirectiveOptions class
    GeneratorsUtil.toJavaFile(filer, componentClassBuilder, optionsClassName, directiveTypeElement);
}
Also used : JsType(jsinterop.annotations.JsType) MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName)

Example 18 with Builder

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

the class AppModuleGenerator method generate.

TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClassNames) {
    ClassName appGlideModuleClassName = ClassName.get(appGlideModule);
    Set<String> excludedGlideModuleClassNames = getExcludedGlideModuleClassNames(appGlideModule);
    MethodSpec constructor = generateConstructor(appGlideModuleClassName, libraryGlideModuleClassNames, excludedGlideModuleClassNames);
    MethodSpec registerComponents = generateRegisterComponents(libraryGlideModuleClassNames, excludedGlideModuleClassNames);
    MethodSpec getExcludedModuleClasses = generateGetExcludedModuleClasses(excludedGlideModuleClassNames);
    MethodSpec applyOptions = MethodSpec.methodBuilder("applyOptions").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(ClassName.get("android.content", "Context"), "context").addParameter(ClassName.get("com.bumptech.glide", "GlideBuilder"), "builder").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).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)

Example 19 with Builder

use of com.squareup.javapoet.TypeSpec.Builder in project kripton by xcesco.

the class BindDaoBuilder method visit.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.abubusoft.kripton.processor.sqlite.model.SQLiteModelElementVisitor#
	 * visit(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)
	 */
@Override
public void visit(SQLiteDaoDefinition value) throws Exception {
    currentDaoDefinition = value;
    // check if we need to generate or not
    if (value.getElement().getAnnotation(BindDaoMany2Many.class) != null && value.getElement().getAnnotation(BindGeneratedDao.class) == null) {
        return;
    }
    String classTableName = daoName(value);
    PackageElement pkg = elementUtils.getPackageOf(value.getElement());
    String packageName = pkg.isUnnamed() ? "" : pkg.getQualifiedName().toString();
    AnnotationProcessorUtilis.infoOnGeneratedClasses(BindDao.class, packageName, classTableName);
    builder = TypeSpec.classBuilder(classTableName).superclass(Dao.class).addSuperinterface(typeName(value.getElement())).addModifiers(Modifier.PUBLIC);
    for (TypeName item : value.implementedInterface) {
        builder.addSuperinterface(item);
    }
    BindTypeContext context = new BindTypeContext(builder, TypeUtility.typeName(packageName, classTableName), Modifier.PRIVATE, Modifier.STATIC);
    String entityName = BindDataSourceSubProcessor.generateEntityName(value, value.getEntity());
    // javadoc for class
    builder.addJavadoc("<p>");
    builder.addJavadoc("\nDAO implementation for entity <code>$L</code>, based on interface <code>$L</code>\n", entityName, value.getElement().getSimpleName().toString());
    builder.addJavadoc("</p>\n\n");
    JavadocUtility.generateJavadocGeneratedBy(builder);
    builder.addJavadoc(" @see $T\n", TypeUtility.className(value.getEntityClassName()));
    builder.addJavadoc(" @see $T\n", TypeUtility.className(value.getElement().getQualifiedName().toString()));
    builder.addJavadoc(" @see $T\n", BindTableGenerator.tableClassName(value, value.getEntity()));
    {
        // constructor
        MethodSpec.Builder methodBuilder = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(BindDaoFactoryBuilder.generateDaoFactoryClassName(value.getParent()), "daoFactory");
        methodBuilder.addStatement("super(daoFactory.getContext())");
        if (value.hasRelations()) {
            methodBuilder.addStatement("this.daoFactory=daoFactory");
            builder.addField(BindDaoFactoryBuilder.generateDaoFactoryClassName(value.getParent()), "daoFactory", Modifier.PRIVATE);
        }
        builder.addMethod(methodBuilder.build());
    }
    // define column typeName set
    for (SQLiteModelMethod item : value.getCollection()) {
        item.accept(this);
    }
    // generate live data support methods
    if (value.hasLiveData()) {
        // method sendEvent
        {
            MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(METHOD_NAME_REGISTRY_EVENT).addModifiers(Modifier.PROTECTED).addParameter(Integer.TYPE, "affectedRows");
            methodBuilder.beginControlFlow("if (affectedRows==0)");
            methodBuilder.addStatement("return");
            methodBuilder.endControlFlow();
            methodBuilder.beginControlFlow("if (_context.isInSession())");
            methodBuilder.addStatement("_context.registrySQLEvent($T.$L)", BindDataSourceBuilder.generateDataSourceName(value.getParent()), value.daoUidName);
            methodBuilder.nextControlFlow("else");
            methodBuilder.addStatement("invalidateLiveData()");
            methodBuilder.endControlFlow();
            builder.addMethod(methodBuilder.build());
        }
        // method registryChange
        {
            MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("registryChange").addModifiers(Modifier.PUBLIC);
            methodBuilder.addJavadoc("<p>Allows to registry change on this DAO in a transaction, in an batch operation or in a standalone operation.</p>\n\n");
            methodBuilder.addStatement("registryEvent(1)");
            builder.addMethod(methodBuilder.build());
        }
        // field liveDatas
        {
            FieldSpec.Builder liveDataBuilder = FieldSpec.builder(ParameterizedTypeName.get(ClassName.get(Collection.class), ParameterizedTypeName.get(ClassName.get(WeakReference.class), // WildcardTypeName.subtypeOf(Object.class)))),
            ClassName.get(LiveDataHandler.class))), "liveDatas").addModifiers(Modifier.STATIC).initializer(CodeBlock.builder().add("new $T()", ParameterizedTypeName.get(ClassName.get(CopyOnWriteArraySet.class), ParameterizedTypeName.get(ClassName.get(WeakReference.class), ClassName.get(LiveDataHandler.class)))).build());
            builder.addField(liveDataBuilder.build());
        }
        // registryLiveData
        {
            // .addParameter(ParameterizedTypeName.get(ClassName.get(LiveDataHandler.class),
            // WildcardTypeName.subtypeOf(Object.class)), "value");
            MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(METHOD_NAME_REGISTRY_LIVE_DATA).addModifiers(Modifier.PROTECTED).addParameter(ClassName.get(LiveDataHandler.class), "value");
            methodBuilder.addStatement("liveDatas.add(new $T(value))", ParameterizedTypeName.get(ClassName.get(WeakReference.class), ClassName.get(LiveDataHandler.class)));
            builder.addMethod(methodBuilder.build());
        }
        // invalidateLiveData
        {
            // check datasource and dao package must be the same, otherwise
            // invalidate must be public
            MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(METHOD_NAME_INVALIDATE_LIVE_DATA).addJavadoc("<p>Invalidate livedata.</p>\n\n").addModifiers(Modifier.PUBLIC);
            methodBuilder.beginControlFlow("for ($T item: liveDatas)", ParameterizedTypeName.get(ClassName.get(WeakReference.class), // WildcardTypeName.subtypeOf(Object.class))));
            ClassName.get(LiveDataHandler.class)));
            methodBuilder.beginControlFlow("if (item.get()!=null)");
            methodBuilder.addStatement("item.get().invalidate()");
            methodBuilder.endControlFlow();
            methodBuilder.endControlFlow();
            builder.addMethod(methodBuilder.build());
        }
    }
    // generate serializer params
    for (Entry<TypeName, String> item : currentDaoDefinition.managedParams.entrySet()) {
        BindTransformer.checkIfIsInUnsupportedPackage(item.getKey());
        ManagedPropertyPersistenceHelper.generateParamSerializer(context, item.getValue(), item.getKey(), PersistType.BYTE);
        ManagedPropertyPersistenceHelper.generateParamParser(context, item.getValue(), item.getKey(), PersistType.BYTE);
    }
    // generate subject
    if (currentDaoDefinition.getParent().generateRx) {
        ParameterizedTypeName subjectTypeName = ParameterizedTypeName.get(ClassName.get(PublishSubject.class), ClassName.get(SQLiteEvent.class));
        // subject
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("getSubject").addModifiers(Modifier.PUBLIC);
        methodBuilder.addStatement("return subject").returns(subjectTypeName);
        builder.addMethod(methodBuilder.build());
        // subject instance
        FieldSpec.Builder fieldBuilder = FieldSpec.builder(subjectTypeName, "subject", Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC).initializer("$T.create()", ClassName.get(PublishSubject.class));
        builder.addField(fieldBuilder.build());
    }
    // generate prepared statement cleaner
    {
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("clearCompiledStatements").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(Void.TYPE);
        if (value.preparedStatementNames.size() > 0) {
            methodBuilder.beginControlFlow("try");
            for (String item : value.preparedStatementNames) {
                methodBuilder.beginControlFlow("if ($L!=null)", item);
                methodBuilder.addStatement("$L.close()", item);
                methodBuilder.addStatement("$L=null", item);
                methodBuilder.endControlFlow();
            }
            methodBuilder.nextControlFlow("catch($T e)", IOException.class);
            methodBuilder.addStatement("e.printStackTrace()");
            methodBuilder.endControlFlow();
        }
        builder.addMethod(methodBuilder.build());
    }
    TypeSpec typeSpec = builder.build();
    JavaWriterHelper.writeJava2File(filer, packageName, typeSpec);
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.TypeSpec.Builder) BindTypeContext(com.abubusoft.kripton.processor.bind.BindTypeContext) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) IOException(java.io.IOException) FieldSpec(com.squareup.javapoet.FieldSpec) SQLiteEvent(com.abubusoft.kripton.android.sqlite.SQLiteEvent) BindDao(com.abubusoft.kripton.android.annotation.BindDao) Dao(com.abubusoft.kripton.android.sqlite.Dao) BindGeneratedDao(com.abubusoft.kripton.android.annotation.BindGeneratedDao) SQLiteModelMethod(com.abubusoft.kripton.processor.sqlite.model.SQLiteModelMethod) PublishSubject(io.reactivex.subjects.PublishSubject) LiveDataHandler(com.abubusoft.kripton.android.LiveDataHandler) WeakReference(java.lang.ref.WeakReference) Collection(java.util.Collection) PackageElement(javax.lang.model.element.PackageElement) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 20 with Builder

use of com.squareup.javapoet.TypeSpec.Builder in project aws-sdk-java-v2 by aws.

the class SyncClientClass method poetSpec.

@Override
public TypeSpec poetSpec() {
    ClassName interfaceClass = poetExtensions.getClientClass(model.getMetadata().getSyncInterface());
    Builder classBuilder = PoetUtils.createClassBuilder(className).addAnnotation(SdkInternalApi.class).addModifiers(FINAL).addSuperinterface(interfaceClass).addJavadoc("Internal implementation of {@link $1T}.\n\n@see $1T#builder()", interfaceClass).addField(logger()).addField(SyncClientHandler.class, "clientHandler", PRIVATE, FINAL).addField(protocolSpec.protocolFactory(model)).addField(SdkClientConfiguration.class, "clientConfiguration", PRIVATE, FINAL).addMethod(constructor()).addMethod(nameMethod()).addMethods(protocolSpec.additionalMethods()).addMethods(operations()).addMethod(resolveMetricPublishersMethod());
    protocolSpec.createErrorResponseHandler().ifPresent(classBuilder::addMethod);
    classBuilder.addMethod(protocolSpec.initProtocolFactory(model));
    classBuilder.addMethod(closeMethod());
    if (model.hasPaginators()) {
        classBuilder.addMethod(applyPaginatorUserAgentMethod(poetExtensions, model));
    }
    if (model.containsRequestSigners()) {
        classBuilder.addMethod(applySignerOverrideMethod(poetExtensions, model));
    }
    if (model.getCustomizationConfig().getUtilitiesMethod() != null) {
        classBuilder.addMethod(utilitiesMethod());
    }
    model.getEndpointOperation().ifPresent(o -> classBuilder.addField(EndpointDiscoveryRefreshCache.class, "endpointDiscoveryCache", PRIVATE));
    if (model.hasWaiters()) {
        classBuilder.addMethod(waiterMethod());
    }
    return classBuilder.build();
}
Also used : SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) EndpointDiscoveryRefreshCache(software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRefreshCache) SyncClientHandler(software.amazon.awssdk.core.client.handler.SyncClientHandler)

Aggregations

Builder (com.squareup.javapoet.TypeSpec.Builder)42 ClassName (com.squareup.javapoet.ClassName)28 MethodSpec (com.squareup.javapoet.MethodSpec)25 TypeSpec (com.squareup.javapoet.TypeSpec)18 TypeElement (javax.lang.model.element.TypeElement)16 TypeName (com.squareup.javapoet.TypeName)14 ExecutableElement (javax.lang.model.element.ExecutableElement)13 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)12 IOException (java.io.IOException)7 PackageElement (javax.lang.model.element.PackageElement)7 VariableElement (javax.lang.model.element.VariableElement)7 TypeMirror (javax.lang.model.type.TypeMirror)7 JavaFile (com.squareup.javapoet.JavaFile)6 ArrayList (java.util.ArrayList)6 DeclaredType (javax.lang.model.type.DeclaredType)6 Element (javax.lang.model.element.Element)5 HashSet (java.util.HashSet)4 BindTypeBuilder (com.abubusoft.kripton.processor.bind.BindTypeBuilder)3 PrefsProperty (com.abubusoft.kripton.processor.sharedprefs.model.PrefsProperty)3 HashMap (java.util.HashMap)3