Search in sources :

Example 1 with Component

use of com.axellience.vuegwt.core.annotations.component.Component in project vue-gwt by Axellience.

the class VueComponentFactoryGenerator method registerLocalComponents.

/**
 * Register components passed to the annotation.
 * The parameters of the generated method are the factories for the local components we depend
 * on.
 * Their values are either injected, or pass directly when using the "get()" static accessor.
 * @param component The Component we generate for
 * @param initBuilder The builder for the init method
 * @param staticInitParameters The parameters of the static init function
 */
private void registerLocalComponents(TypeElement component, MethodSpec.Builder initBuilder, List<CodeBlock> staticInitParameters) {
    List<TypeMirror> localComponents = getComponentLocalComponents(elements, component);
    if (localComponents.isEmpty())
        return;
    initBuilder.addStatement("$T<$T> components = jsConstructor.getOptionsComponents()", JsPropertyMap.class, ParameterizedTypeName.get(VueJsAsyncProvider.class, VueJsConstructor.class));
    localComponents.forEach(localComponent -> {
        ClassName factory = componentFactoryName(localComponent);
        String parameterName = factory.reflectionName().replaceAll("\\.", "_");
        initBuilder.addParameter(providerOf(factory), parameterName);
        staticInitParameters.add(CodeBlock.of("() -> $T.get()", factory));
        Element localComponentElement = ((DeclaredType) localComponent).asElement();
        String tagName = componentToTagName(localComponentElement.getSimpleName().toString(), localComponentElement.getAnnotation(Component.class));
        initBuilder.addStatement("components.set($S, render -> render.accept($L.get().getJsConstructor()))", tagName, parameterName);
    });
}
Also used : VueJsAsyncProvider(com.axellience.vuegwt.core.client.vue.VueJsAsyncProvider) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ClassName(com.squareup.javapoet.ClassName) VueJsConstructor(com.axellience.vuegwt.core.client.vue.VueJsConstructor) VueComponent(com.axellience.vuegwt.core.client.component.VueComponent) Component(com.axellience.vuegwt.core.annotations.component.Component) DeclaredType(javax.lang.model.type.DeclaredType)

Example 2 with Component

use of com.axellience.vuegwt.core.annotations.component.Component in project vue-gwt by Axellience.

the class VueComponentFactoryGenerator method createInitMethod.

@Override
protected List<CodeBlock> createInitMethod(TypeElement component, Builder vueFactoryClassBuilder) {
    MethodSpec.Builder initBuilder = MethodSpec.methodBuilder("init").addModifiers(Modifier.PROTECTED).addAnnotation(Inject.class);
    List<CodeBlock> initParametersCall = new LinkedList<>();
    // Get options
    initBuilder.addStatement("$T<$T> componentOptions = $T.getOptions()", VueComponentOptions.class, component.asType(), componentJsTypeName(component));
    processCustomizeOptions(component, initBuilder, initParametersCall);
    // Extend the parent Component
    Optional<ClassName> superFactoryType = getSuperComponentType(component).map(GeneratorsNameUtil::componentFactoryName);
    if (superFactoryType.isPresent()) {
        initBuilder.addParameter(superFactoryType.get(), "superFactory");
        initBuilder.addStatement("jsConstructor = superFactory.getJsConstructor().extendJavaComponent($L)", "componentOptions");
        initParametersCall.add(CodeBlock.of("$T.get()", superFactoryType.get()));
    } else {
        initBuilder.addStatement("jsConstructor = $T.extendJavaComponent($L)", Vue.class, "componentOptions");
    }
    Component componentAnnotation = component.getAnnotation(Component.class);
    if (hasInjectedDependencies)
        registerDependenciesProvider(component, initBuilder, initParametersCall);
    registerLocalComponents(component, initBuilder, initParametersCall);
    registerLocalDirectives(componentAnnotation, initBuilder);
    MethodSpec initMethod = initBuilder.build();
    vueFactoryClassBuilder.addMethod(initMethod);
    return initParametersCall;
}
Also used : GeneratorsNameUtil(com.axellience.vuegwt.processors.utils.GeneratorsNameUtil) MethodSpec(com.squareup.javapoet.MethodSpec) CodeBlock(com.squareup.javapoet.CodeBlock) ClassName(com.squareup.javapoet.ClassName) VueComponent(com.axellience.vuegwt.core.client.component.VueComponent) Component(com.axellience.vuegwt.core.annotations.component.Component) LinkedList(java.util.LinkedList)

Example 3 with Component

use of com.axellience.vuegwt.core.annotations.component.Component in project vue-gwt by Axellience.

the class ComponentTemplateProcessor method findLocalComponentsForComponent.

/**
 * Register all locally declared components.
 * @param localComponents The {@link LocalComponents} where we register our local components
 * @param componentTypeElement The class to process
 */
private void findLocalComponentsForComponent(LocalComponents localComponents, TypeElement componentTypeElement) {
    Component componentAnnotation = componentTypeElement.getAnnotation(Component.class);
    if (componentAnnotation == null)
        return;
    getComponentLocalComponents(elementUtils, componentTypeElement).stream().map(DeclaredType.class::cast).map(DeclaredType::asElement).map(TypeElement.class::cast).forEach(childTypeElement -> processLocalComponentClass(localComponents, childTypeElement));
    getSuperComponentType(componentTypeElement).ifPresent(superComponentType -> findLocalComponentsForComponent(localComponents, superComponentType));
}
Also used : VueComponent(com.axellience.vuegwt.core.client.component.VueComponent) Component(com.axellience.vuegwt.core.annotations.component.Component) LocalComponent(com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponent) DeclaredType(javax.lang.model.type.DeclaredType)

Example 4 with Component

use of com.axellience.vuegwt.core.annotations.component.Component in project vue-gwt by Axellience.

the class ComponentTemplateProcessor method processLocalComponentClass.

/**
 * Register the local component and all of its {@link Prop}.
 * This will be used for type validation.
 * @param localComponents The {@link LocalComponents} object where we should register our {@link LocalComponent}
 * @param localComponentType The class to process
 */
private void processLocalComponentClass(LocalComponents localComponents, TypeElement localComponentType) {
    Component componentAnnotation = localComponentType.getAnnotation(Component.class);
    String localComponentTagName = componentToTagName(localComponentType.getSimpleName().toString(), componentAnnotation);
    if (localComponents.hasLocalComponent(localComponentTagName))
        return;
    LocalComponent localComponent = localComponents.addLocalComponent(localComponentTagName);
    ElementFilter.fieldsIn(localComponentType.getEnclosedElements()).forEach(field -> {
        Prop propAnnotation = field.getAnnotation(Prop.class);
        if (propAnnotation != null) {
            localComponent.addProp(field.getSimpleName().toString(), TypeName.get(field.asType()), propAnnotation.required());
        }
    });
}
Also used : Prop(com.axellience.vuegwt.core.annotations.component.Prop) VueComponent(com.axellience.vuegwt.core.client.component.VueComponent) Component(com.axellience.vuegwt.core.annotations.component.Component) LocalComponent(com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponent) LocalComponent(com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponent)

Example 5 with Component

use of com.axellience.vuegwt.core.annotations.component.Component in project vue-gwt by Axellience.

the class ComponentJsTypeGenerator method getOptionsMethodBuilder.

/**
 * Create and return the builder for the method that creating the {@link VueComponentOptions}
 * for this {@link VueComponent}.
 * @param component The {@link VueComponent} we are generating for
 * @return A {@link MethodSpec.Builder} for the method that creates the {@link VueComponentOptions}
 */
private MethodSpec.Builder getOptionsMethodBuilder(TypeElement component) {
    TypeName optionsTypeName = ParameterizedTypeName.get(ClassName.get(VueComponentOptions.class), ClassName.get(component));
    MethodSpec.Builder optionsMethodBuilder = MethodSpec.methodBuilder("getOptions").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(optionsTypeName).addStatement("$T options = new $T()", optionsTypeName, optionsTypeName);
    Component annotation = component.getAnnotation(Component.class);
    if (!"".equals(annotation.name()))
        optionsMethodBuilder.addStatement("options.setName($S)", annotation.name());
    optionsMethodBuilder.addStatement("options.setComponentJavaPrototype($T.getJavaConstructor($T.class).prototype)", VueGWT.class, componentJsTypeName(component));
    return optionsMethodBuilder;
}
Also used : TypeName(com.squareup.javapoet.TypeName) GeneratorsNameUtil.componentJsTypeName(com.axellience.vuegwt.processors.utils.GeneratorsNameUtil.componentJsTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) VueComponentOptions(com.axellience.vuegwt.core.client.component.options.VueComponentOptions) MethodSpec(com.squareup.javapoet.MethodSpec) VueComponent(com.axellience.vuegwt.core.client.component.VueComponent) Component(com.axellience.vuegwt.core.annotations.component.Component)

Aggregations

Component (com.axellience.vuegwt.core.annotations.component.Component)7 VueComponent (com.axellience.vuegwt.core.client.component.VueComponent)7 ClassName (com.squareup.javapoet.ClassName)4 MethodSpec (com.squareup.javapoet.MethodSpec)4 Prop (com.axellience.vuegwt.core.annotations.component.Prop)3 VueComponentOptions (com.axellience.vuegwt.core.client.component.options.VueComponentOptions)3 VueJsConstructor (com.axellience.vuegwt.core.client.vue.VueJsConstructor)3 GeneratorsNameUtil.componentJsTypeName (com.axellience.vuegwt.processors.utils.GeneratorsNameUtil.componentJsTypeName)3 CodeBlock (com.squareup.javapoet.CodeBlock)3 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)3 TypeName (com.squareup.javapoet.TypeName)3 Computed (com.axellience.vuegwt.core.annotations.component.Computed)2 Emit (com.axellience.vuegwt.core.annotations.component.Emit)2 HookMethod (com.axellience.vuegwt.core.annotations.component.HookMethod)2 PropDefault (com.axellience.vuegwt.core.annotations.component.PropDefault)2 PropValidator (com.axellience.vuegwt.core.annotations.component.PropValidator)2 Watch (com.axellience.vuegwt.core.annotations.component.Watch)2 VueGWT (com.axellience.vuegwt.core.client.VueGWT)2 ComponentJavaConstructor (com.axellience.vuegwt.core.client.component.ComponentJavaConstructor)2 HasCreated (com.axellience.vuegwt.core.client.component.hooks.HasCreated)2