Search in sources :

Example 1 with TemplateParserContext

use of com.axellience.vuegwt.processors.component.template.parser.context.TemplateParserContext in project vue-gwt by Axellience.

the class ComponentTemplateProcessor method registerFieldsAndMethodsInContext.

/**
 * Process the ComponentJsType class to register all the fields and methods visible in
 * the context.
 * TODO: Improve this method by putting things together with {@link ComponentJsTypeGenerator}
 * @param componentTypeElement The class to process
 */
private void registerFieldsAndMethodsInContext(TemplateParserContext templateParserContext, TypeElement componentTypeElement, Set<String> alreadyDoneVariable, Set<String> alreadyDoneMethods) {
    ElementFilter.fieldsIn(componentTypeElement.getEnclosedElements()).stream().filter(ComponentGeneratorsUtil::isFieldVisibleInJS).forEach(field -> {
        String name = field.getSimpleName().toString();
        if (alreadyDoneVariable.contains(name))
            return;
        alreadyDoneVariable.add(name);
        templateParserContext.addRootVariable(ClassName.get(field.asType()), name);
    });
    ElementFilter.methodsIn(componentTypeElement.getEnclosedElements()).stream().filter(method -> hasAnnotation(method, Computed.class)).filter(method -> !"void".equals(method.getReturnType().toString())).forEach(method -> {
        String name = getComputedPropertyName(method);
        if (alreadyDoneVariable.contains(name))
            return;
        alreadyDoneVariable.add(name);
        TypeMirror propertyType;
        if ("void".equals(method.getReturnType().toString()))
            propertyType = method.getParameters().get(0).asType();
        else
            propertyType = method.getReturnType();
        templateParserContext.addRootVariable(ClassName.get(propertyType), name);
    });
    ElementFilter.methodsIn(componentTypeElement.getEnclosedElements()).stream().filter(ComponentGeneratorsUtil::isMethodVisibleInTemplate).map(ExecutableElement::getSimpleName).map(Object::toString).forEach(methodName -> {
        if (alreadyDoneMethods.contains(methodName))
            return;
        alreadyDoneMethods.add(methodName);
        templateParserContext.addRootMethod(methodName);
    });
    getSuperComponentType(componentTypeElement).ifPresent(superComponent -> registerFieldsAndMethodsInContext(templateParserContext, superComponent, alreadyDoneVariable, alreadyDoneMethods));
}
Also used : Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) GeneratorsUtil.hasAnnotation(com.axellience.vuegwt.processors.utils.GeneratorsUtil.hasAnnotation) TypeElement(javax.lang.model.element.TypeElement) ComponentGeneratorsUtil.getSuperComponentType(com.axellience.vuegwt.processors.utils.ComponentGeneratorsUtil.getSuperComponentType) VueComponent(com.axellience.vuegwt.core.client.component.VueComponent) Elements(javax.lang.model.util.Elements) FileObject(javax.tools.FileObject) HashSet(java.util.HashSet) ComponentGeneratorsUtil.getComponentLocalComponents(com.axellience.vuegwt.processors.utils.ComponentGeneratorsUtil.getComponentLocalComponents) Kind(javax.tools.Diagnostic.Kind) ComponentGeneratorsUtil(com.axellience.vuegwt.processors.utils.ComponentGeneratorsUtil) Computed(com.axellience.vuegwt.core.annotations.component.Computed) DeclaredType(javax.lang.model.type.DeclaredType) Prop(com.axellience.vuegwt.core.annotations.component.Prop) ElementFilter(javax.lang.model.util.ElementFilter) Messager(javax.annotation.processing.Messager) TemplateMethodsBuilder(com.axellience.vuegwt.processors.component.template.builder.TemplateMethodsBuilder) StandardLocation(javax.tools.StandardLocation) Component(com.axellience.vuegwt.core.annotations.component.Component) TemplateParser(com.axellience.vuegwt.processors.component.template.parser.TemplateParser) ExecutableElement(javax.lang.model.element.ExecutableElement) ComponentJsTypeGenerator(com.axellience.vuegwt.processors.component.ComponentJsTypeGenerator) LocalComponents(com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponents) TemplateParserResult(com.axellience.vuegwt.processors.component.template.parser.result.TemplateParserResult) Set(java.util.Set) IOException(java.io.IOException) LocalComponent(com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponent) TypeMirror(javax.lang.model.type.TypeMirror) Filer(javax.annotation.processing.Filer) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) GeneratorsUtil.getComputedPropertyName(com.axellience.vuegwt.processors.utils.GeneratorsUtil.getComputedPropertyName) TemplateParserContext(com.axellience.vuegwt.processors.component.template.parser.context.TemplateParserContext) GeneratorsNameUtil.componentToTagName(com.axellience.vuegwt.processors.utils.GeneratorsNameUtil.componentToTagName) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) Computed(com.axellience.vuegwt.core.annotations.component.Computed)

Example 2 with TemplateParserContext

use of com.axellience.vuegwt.processors.component.template.parser.context.TemplateParserContext in project vue-gwt by Axellience.

the class ComponentTemplateProcessor method processComponentTemplate.

public void processComponentTemplate(TypeElement componentTypeElement, Builder componentJsTypeBuilder) {
    ClassName componentTypeName = ClassName.get(componentTypeElement);
    Optional<String> optionalTemplateContent = getTemplateContent(componentTypeName, componentTypeElement);
    if (!optionalTemplateContent.isPresent())
        return;
    LocalComponents localComponents = new LocalComponents();
    findLocalComponentsForComponent(localComponents, componentTypeElement);
    // Initialize the template parser context based on the VueComponent type element
    TemplateParserContext templateParserContext = new TemplateParserContext(componentTypeElement, localComponents);
    registerFieldsAndMethodsInContext(templateParserContext, componentTypeElement, new HashSet<>(), new HashSet<>());
    // Parse the template
    TemplateParserResult templateParserResult = new TemplateParser().parseHtmlTemplate(optionalTemplateContent.get(), templateParserContext, messager);
    // Add expressions from the template to JsType and compile template
    TemplateMethodsBuilder templateMethodsBuilder = new TemplateMethodsBuilder();
    templateMethodsBuilder.addTemplateMethodsToComponentJsType(componentJsTypeBuilder, templateParserResult);
}
Also used : TemplateParserContext(com.axellience.vuegwt.processors.component.template.parser.context.TemplateParserContext) TemplateMethodsBuilder(com.axellience.vuegwt.processors.component.template.builder.TemplateMethodsBuilder) ClassName(com.squareup.javapoet.ClassName) TemplateParser(com.axellience.vuegwt.processors.component.template.parser.TemplateParser) TemplateParserResult(com.axellience.vuegwt.processors.component.template.parser.result.TemplateParserResult) ComponentGeneratorsUtil.getComponentLocalComponents(com.axellience.vuegwt.processors.utils.ComponentGeneratorsUtil.getComponentLocalComponents) LocalComponents(com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponents)

Aggregations

TemplateMethodsBuilder (com.axellience.vuegwt.processors.component.template.builder.TemplateMethodsBuilder)2 TemplateParser (com.axellience.vuegwt.processors.component.template.parser.TemplateParser)2 TemplateParserContext (com.axellience.vuegwt.processors.component.template.parser.context.TemplateParserContext)2 LocalComponents (com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponents)2 TemplateParserResult (com.axellience.vuegwt.processors.component.template.parser.result.TemplateParserResult)2 ComponentGeneratorsUtil.getComponentLocalComponents (com.axellience.vuegwt.processors.utils.ComponentGeneratorsUtil.getComponentLocalComponents)2 ClassName (com.squareup.javapoet.ClassName)2 Component (com.axellience.vuegwt.core.annotations.component.Component)1 Computed (com.axellience.vuegwt.core.annotations.component.Computed)1 Prop (com.axellience.vuegwt.core.annotations.component.Prop)1 VueComponent (com.axellience.vuegwt.core.client.component.VueComponent)1 ComponentJsTypeGenerator (com.axellience.vuegwt.processors.component.ComponentJsTypeGenerator)1 LocalComponent (com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponent)1 ComponentGeneratorsUtil (com.axellience.vuegwt.processors.utils.ComponentGeneratorsUtil)1 ComponentGeneratorsUtil.getSuperComponentType (com.axellience.vuegwt.processors.utils.ComponentGeneratorsUtil.getSuperComponentType)1 GeneratorsNameUtil.componentToTagName (com.axellience.vuegwt.processors.utils.GeneratorsNameUtil.componentToTagName)1 GeneratorsUtil.getComputedPropertyName (com.axellience.vuegwt.processors.utils.GeneratorsUtil.getComputedPropertyName)1 GeneratorsUtil.hasAnnotation (com.axellience.vuegwt.processors.utils.GeneratorsUtil.hasAnnotation)1 TypeName (com.squareup.javapoet.TypeName)1 Builder (com.squareup.javapoet.TypeSpec.Builder)1