Search in sources :

Example 1 with EventData

use of com.vaadin.flow.component.EventData in project flow by vaadin.

the class PolymerServerEventHandlers method getParameters.

private String[] getParameters(Method method) {
    List<String> result = new ArrayList<>();
    for (Parameter parameter : method.getParameters()) {
        EventData eventData = parameter.getAnnotation(EventData.class);
        if (eventData != null) {
            result.add(eventData.value());
        }
        if (ReflectTools.hasAnnotation(parameter, REPEAT_INDEX_FQN)) {
            result.add(REPEAT_INDEX_VALUE);
        }
        Optional<Annotation> annotation = ReflectTools.getAnnotation(parameter, MODEL_ITEM_FQN);
        if (annotation.isPresent()) {
            result.add(ReflectTools.getAnnotationMethodValue(annotation.get(), VALUE).toString());
        }
    }
    return result.toArray(new String[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) Parameter(java.lang.reflect.Parameter) EventData(com.vaadin.flow.component.EventData) Annotation(java.lang.annotation.Annotation)

Example 2 with EventData

use of com.vaadin.flow.component.EventData in project flow by vaadin.

the class ComponentGenerator method addEventDataParameters.

private void addEventDataParameters(JavaClassSource javaClass, ComponentEventData event, String eventClassName, JavaClassSource eventClass, MethodSource<JavaClassSource> eventConstructor) {
    for (ComponentPropertyBaseData property : event.getProperties()) {
        // Add new parameter to constructor
        final String propertyName = property.getName();
        String normalizedProperty = ComponentGeneratorUtils.formatStringToValidJavaIdentifier(propertyName);
        Class<?> propertyJavaType;
        if (containsObjectType(property)) {
            JavaClassSource nestedClass = generateNestedPojo(javaClass, property.getObjectType().get(0), eventClassName + "-" + propertyName, String.format("Class that encapsulates the data received on the '%s' property of @{link %s} events, from the @{link %s} component.", propertyName, eventClass.getName(), javaClass.getName()));
            propertyJavaType = JsonObject.class;
            eventClass.addField().setType(propertyJavaType).setPrivate().setFinal(true).setName(normalizedProperty);
            eventClass.addMethod().setName(ComponentGeneratorUtils.generateMethodNameForProperty("get", propertyName)).setPublic().setReturnType(nestedClass).setBody(String.format("return new %s().readJson(%s);", nestedClass.getName(), normalizedProperty));
        } else {
            if (!property.getType().isEmpty()) {
                // for varying types, using the first type declared in
                // the
                // JSDoc
                // it is anyway very rare to have varying property type
                propertyJavaType = ComponentGeneratorUtils.toJavaType(property.getType().iterator().next());
            } else {
                // object property
                propertyJavaType = JsonObject.class;
            }
            // Create private field
            eventClass.addProperty(propertyJavaType, normalizedProperty).setAccessible(true).setMutable(false);
        }
        ParameterSource<JavaClassSource> parameter = ComponentGeneratorUtils.addMethodParameter(javaClass, eventConstructor, propertyJavaType, normalizedProperty);
        parameter.addAnnotation(EventData.class).setStringValue(String.format("event.%s", propertyName));
        // Set value to private field
        eventConstructor.setBody(String.format("%s%nthis.%s = %s;", eventConstructor.getBody(), normalizedProperty, normalizedProperty));
        // Add the EventData as a import
        javaClass.addImport(EventData.class);
    }
}
Also used : ComponentPropertyBaseData(com.vaadin.generator.metadata.ComponentPropertyBaseData) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) EventData(com.vaadin.flow.component.EventData) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData)

Aggregations

EventData (com.vaadin.flow.component.EventData)2 ComponentEventData (com.vaadin.generator.metadata.ComponentEventData)1 ComponentPropertyBaseData (com.vaadin.generator.metadata.ComponentPropertyBaseData)1 Annotation (java.lang.annotation.Annotation)1 Parameter (java.lang.reflect.Parameter)1 ArrayList (java.util.ArrayList)1 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)1