Search in sources :

Example 1 with BindAnnotationDescriptor

use of org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor in project linkki by linkki-framework.

the class Binder method addFieldBinding.

/**
 * Adds the descriptor and component for the given field to the given map.
 *
 * @param field a Component typed field that is annotated with {@link Bind}
 *
 * @throws IllegalStateException if the field does not hold a component
 * @throws NullPointerException if the component held by the field is {@code null}
 */
private void addFieldBinding(Field field, LinkedHashMap<BindingDescriptor, Component> bindings) {
    Validate.validState(Component.class.isAssignableFrom(field.getType()), "%s is not a Component-typed field and cannot be annotated with @Bind", field);
    try {
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        Component component = requireNonNull((Component) field.get(view), () -> "Cannot create binding for field " + field + " as it is null");
        @SuppressWarnings("null") @Nonnull Bind bindAnnotation = field.getAnnotation(Bind.class);
        List<LinkkiAspectDefinition> aspectDefinitions = Arrays.asList(field.getAnnotations()).stream().flatMap(a -> AspectAnnotationReader.createAspectDefinitionsFrom(a).stream()).collect(Collectors.toList());
        BindAnnotationDescriptor descriptor = new BindAnnotationDescriptor(bindAnnotation, aspectDefinitions);
        bindings.put(descriptor, component);
    } catch (IllegalArgumentException | IllegalAccessException e) {
        throw new LinkkiRuntimeException(e);
    }
}
Also used : Arrays(java.util.Arrays) LinkkiAspectDefinition(org.linkki.core.binding.aspect.definition.LinkkiAspectDefinition) AspectAnnotationReader(org.linkki.core.binding.aspect.AspectAnnotationReader) BindingDescriptor(org.linkki.core.ui.section.descriptor.BindingDescriptor) LabelComponentWrapper(org.linkki.core.ui.components.LabelComponentWrapper) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) Bind(org.linkki.core.binding.annotations.Bind) BeanUtils(org.linkki.util.BeanUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) BindAnnotationDescriptor(org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor) List(java.util.List) Validate(org.apache.commons.lang3.Validate) Objects.requireNonNull(java.util.Objects.requireNonNull) LinkkiRuntimeException(org.linkki.core.exception.LinkkiRuntimeException) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) Method(java.lang.reflect.Method) Nonnull(javax.annotation.Nonnull) Component(com.vaadin.ui.Component) Bind(org.linkki.core.binding.annotations.Bind) LinkkiRuntimeException(org.linkki.core.exception.LinkkiRuntimeException) Nonnull(javax.annotation.Nonnull) BindAnnotationDescriptor(org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor) LinkkiAspectDefinition(org.linkki.core.binding.aspect.definition.LinkkiAspectDefinition) Component(com.vaadin.ui.Component)

Example 2 with BindAnnotationDescriptor

use of org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor in project linkki by linkki-framework.

the class Binder method addMethodBinding.

/**
 * Adds the descriptor and component (returned by the method) for the given method to the given
 * map.
 *
 * @throws IllegalArgumentException if the method does not return a component or requires
 *             parameters
 * @throws NullPointerException if the component returned by the method is {@code null}
 */
private void addMethodBinding(Method method, LinkedHashMap<BindingDescriptor, Component> bindings) {
    Validate.isAssignableFrom(Component.class, method.getReturnType(), "%s does not return a Component and cannot be annotated with @Bind", method);
    Validate.isTrue(method.getParameterCount() == 0, "%s has parameters and cannot be annotated with @Bind", method);
    try {
        Component component = (Component) method.invoke(view);
        if (component == null) {
            throw new NullPointerException("Cannot create binding for method " + method + " as it returned null");
        }
        @SuppressWarnings("null") @Nonnull Bind bindAnnotation = method.getAnnotation(Bind.class);
        List<LinkkiAspectDefinition> aspectDefinitions = Arrays.asList(method.getAnnotations()).stream().flatMap(a -> AspectAnnotationReader.createAspectDefinitionsFrom(a).stream()).collect(Collectors.toList());
        BindAnnotationDescriptor descriptor = new BindAnnotationDescriptor(bindAnnotation, aspectDefinitions);
        bindings.put(descriptor, component);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new LinkkiRuntimeException(e);
    }
}
Also used : Arrays(java.util.Arrays) LinkkiAspectDefinition(org.linkki.core.binding.aspect.definition.LinkkiAspectDefinition) AspectAnnotationReader(org.linkki.core.binding.aspect.AspectAnnotationReader) BindingDescriptor(org.linkki.core.ui.section.descriptor.BindingDescriptor) LabelComponentWrapper(org.linkki.core.ui.components.LabelComponentWrapper) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) Bind(org.linkki.core.binding.annotations.Bind) BeanUtils(org.linkki.util.BeanUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) BindAnnotationDescriptor(org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor) List(java.util.List) Validate(org.apache.commons.lang3.Validate) Objects.requireNonNull(java.util.Objects.requireNonNull) LinkkiRuntimeException(org.linkki.core.exception.LinkkiRuntimeException) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) Method(java.lang.reflect.Method) Nonnull(javax.annotation.Nonnull) Component(com.vaadin.ui.Component) Bind(org.linkki.core.binding.annotations.Bind) Nonnull(javax.annotation.Nonnull) BindAnnotationDescriptor(org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkkiRuntimeException(org.linkki.core.exception.LinkkiRuntimeException) LinkkiAspectDefinition(org.linkki.core.binding.aspect.definition.LinkkiAspectDefinition) Component(com.vaadin.ui.Component)

Example 3 with BindAnnotationDescriptor

use of org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor in project linkki by linkki-framework.

the class BindAnnotationDescriptorTest method testCreateBinding_CreatesFieldBindingForField.

@Test
public void testCreateBinding_CreatesFieldBindingForField() {
    BindAnnotationDescriptor descriptor = new BindAnnotationDescriptor(mock(Bind.class), new ArrayList<>());
    PropertyDispatcher dispatcher = mock(PropertyDispatcher.class);
    ElementBinding binding = descriptor.createBinding(dispatcher, Handler.NOP_HANDLER, new LabelComponentWrapper(new TextField()));
    assertThat(binding, is(instanceOf(ComponentBinding.class)));
}
Also used : Bind(org.linkki.core.binding.annotations.Bind) BindAnnotationDescriptor(org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor) TextField(com.vaadin.ui.TextField) PropertyDispatcher(org.linkki.core.binding.dispatcher.PropertyDispatcher) ElementBinding(org.linkki.core.binding.ElementBinding) LabelComponentWrapper(org.linkki.core.ui.components.LabelComponentWrapper) Test(org.junit.Test)

Example 4 with BindAnnotationDescriptor

use of org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor in project linkki by linkki-framework.

the class BindAnnotationDescriptorTest method testCreateBinding_CreatesLabelBindingForLabel.

@Test
public void testCreateBinding_CreatesLabelBindingForLabel() {
    BindAnnotationDescriptor descriptor = new BindAnnotationDescriptor(mock(Bind.class), new ArrayList<>());
    PropertyDispatcher dispatcher = mock(PropertyDispatcher.class);
    ElementBinding binding = descriptor.createBinding(dispatcher, Handler.NOP_HANDLER, new LabelComponentWrapper(new Label()));
    assertThat(binding, is(instanceOf(ElementBinding.class)));
}
Also used : Bind(org.linkki.core.binding.annotations.Bind) BindAnnotationDescriptor(org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor) Label(com.vaadin.ui.Label) PropertyDispatcher(org.linkki.core.binding.dispatcher.PropertyDispatcher) ElementBinding(org.linkki.core.binding.ElementBinding) LabelComponentWrapper(org.linkki.core.ui.components.LabelComponentWrapper) Test(org.junit.Test)

Example 5 with BindAnnotationDescriptor

use of org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor in project linkki by linkki-framework.

the class BindAnnotationDescriptorTest method testCreateBinding_CreatesButtonBindingForButton.

@Test
public void testCreateBinding_CreatesButtonBindingForButton() {
    BindAnnotationDescriptor descriptor = new BindAnnotationDescriptor(mock(Bind.class), new ArrayList<>());
    PropertyDispatcher dispatcher = mock(PropertyDispatcher.class);
    ElementBinding binding = descriptor.createBinding(dispatcher, Handler.NOP_HANDLER, new LabelComponentWrapper(new Button()));
    assertThat(binding, is(instanceOf(ElementBinding.class)));
}
Also used : Bind(org.linkki.core.binding.annotations.Bind) Button(com.vaadin.ui.Button) BindAnnotationDescriptor(org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor) PropertyDispatcher(org.linkki.core.binding.dispatcher.PropertyDispatcher) ElementBinding(org.linkki.core.binding.ElementBinding) LabelComponentWrapper(org.linkki.core.ui.components.LabelComponentWrapper) Test(org.junit.Test)

Aggregations

Bind (org.linkki.core.binding.annotations.Bind)5 LabelComponentWrapper (org.linkki.core.ui.components.LabelComponentWrapper)5 BindAnnotationDescriptor (org.linkki.core.ui.section.descriptor.BindAnnotationDescriptor)5 Test (org.junit.Test)3 ElementBinding (org.linkki.core.binding.ElementBinding)3 PropertyDispatcher (org.linkki.core.binding.dispatcher.PropertyDispatcher)3 Component (com.vaadin.ui.Component)2 Field (java.lang.reflect.Field)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Arrays (java.util.Arrays)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Collectors (java.util.stream.Collectors)2 Nonnull (javax.annotation.Nonnull)2 Validate (org.apache.commons.lang3.Validate)2 FieldUtils (org.apache.commons.lang3.reflect.FieldUtils)2 AspectAnnotationReader (org.linkki.core.binding.aspect.AspectAnnotationReader)2 LinkkiAspectDefinition (org.linkki.core.binding.aspect.definition.LinkkiAspectDefinition)2