use of org.linkki.core.binding.aspect.definition.LinkkiAspectDefinition 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);
}
}
use of org.linkki.core.binding.aspect.definition.LinkkiAspectDefinition 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);
}
}
use of org.linkki.core.binding.aspect.definition.LinkkiAspectDefinition in project linkki by linkki-framework.
the class AspectAnnotationReaderTest method testCreateAspectDefinitionsFrom.
@Test
public void testCreateAspectDefinitionsFrom() throws NoSuchMethodException, SecurityException {
TestAnnotation annotationToTest = TestClass.class.getMethod("something").getAnnotation(TestAnnotation.class);
List<LinkkiAspectDefinition> definitions = AspectAnnotationReader.createAspectDefinitionsFrom(annotationToTest);
assertThat(definitions.size(), is(2));
LinkkiAspectDefinition definition = definitions.get(0);
assertThat(definition, instanceOf(TestAspectDefinition.class));
assertThat(((TestAspectDefinition) definition).initialized, is(true));
LinkkiAspectDefinition anotherDefinition = definitions.get(1);
assertThat(anotherDefinition, instanceOf(AnotherTestAspectDefinition.class));
assertThat(((AnotherTestAspectDefinition) anotherDefinition).anotherInitialized, is(true));
}
use of org.linkki.core.binding.aspect.definition.LinkkiAspectDefinition in project linkki by linkki-framework.
the class ComponentBindingTest method testUpdateFromPmo_updateAspect.
@Test
public void testUpdateFromPmo_updateAspect() {
Handler componentUpdater = mock(Handler.class);
LinkkiAspectDefinition aspectDefinition = mock(LinkkiAspectDefinition.class);
when(aspectDefinition.createUiUpdater(any(), any())).thenReturn(componentUpdater);
ComponentBinding fieldBinding = new ComponentBinding(new LabelComponentWrapper(label, field), propertyDispatcherValue, Handler.NOP_HANDLER, Arrays.asList(aspectDefinition));
fieldBinding.updateFromPmo();
verify(componentUpdater).apply();
}
use of org.linkki.core.binding.aspect.definition.LinkkiAspectDefinition in project linkki by linkki-framework.
the class UIAnnotationReader method createAndAddDescriptor.
private void createAndAddDescriptor(Annotation annotation, Method method) {
List<LinkkiAspectDefinition> aspectDefs = AspectAnnotationReader.createAspectDefinitionsFrom(annotation);
String pmoPropertyName = getPmoPropertyName(method);
PropertyElementDescriptors elementDescriptors = descriptorsByProperty.computeIfAbsent(pmoPropertyName, PropertyElementDescriptors::new);
if (BindingDefinition.isLinkkiBindingDefinition(annotation)) {
BindingDefinition uiElement = BindingDefinition.from(annotation);
addDescriptor(elementDescriptors, uiElement, pmoPropertyName, annotation, aspectDefs);
} else {
elementDescriptors.addAspect(aspectDefs);
if (annotation instanceof UITableColumn) {
columnDescriptors.put(elementDescriptors, new TableColumnDescriptor(annotatedClass, method, (UITableColumn) annotation));
}
}
}
Aggregations