Search in sources :

Example 1 with ReflectionUtils

use of cn.taketoday.util.ReflectionUtils in project today-infrastructure by TAKETODAY.

the class AnnotationTypeMappingsTests method resolveMirrorSets.

private Method[] resolveMirrorSets(AnnotationTypeMapping mapping, Class<?> element, Class<? extends Annotation> annotationClass) {
    Annotation annotation = element.getAnnotation(annotationClass);
    int[] resolved = mapping.getMirrorSets().resolve(element.getName(), annotation, ReflectionUtils::invokeMethod);
    Method[] result = new Method[resolved.length];
    for (int i = 0; i < resolved.length; i++) {
        result[i] = resolved[i] != -1 ? mapping.getAttributes().get(resolved[i]) : null;
    }
    return result;
}
Also used : Method(java.lang.reflect.Method) ReflectionUtils(cn.taketoday.util.ReflectionUtils) Annotation(java.lang.annotation.Annotation)

Example 2 with ReflectionUtils

use of cn.taketoday.util.ReflectionUtils in project today-infrastructure by TAKETODAY.

the class ApplicationContextEventTests method lambdaAsListenerWithErrorHandler.

@Test
public void lambdaAsListenerWithErrorHandler() {
    final Set<MyEvent> seenEvents = new HashSet<>();
    StaticApplicationContext context = new StaticApplicationContext();
    SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
    multicaster.setErrorHandler(ReflectionUtils::rethrowRuntimeException);
    context.getBeanFactory().registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, multicaster);
    ApplicationListener<MyEvent> listener = seenEvents::add;
    context.addApplicationListener(listener);
    context.refresh();
    MyEvent event1 = new MyEvent(context);
    context.publishEvent(event1);
    context.publishEvent(new MyOtherEvent(context));
    MyEvent event2 = new MyEvent(context);
    context.publishEvent(event2);
    assertThat(seenEvents.size()).isSameAs(2);
    assertThat(seenEvents.contains(event1)).isTrue();
    assertThat(seenEvents.contains(event2)).isTrue();
    context.close();
}
Also used : StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) ReflectionUtils(cn.taketoday.util.ReflectionUtils) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with ReflectionUtils

use of cn.taketoday.util.ReflectionUtils in project today-framework by TAKETODAY.

the class AnnotationTypeMappingsTests method resolveMirrorSets.

private Method[] resolveMirrorSets(AnnotationTypeMapping mapping, Class<?> element, Class<? extends Annotation> annotationClass) {
    Annotation annotation = element.getAnnotation(annotationClass);
    int[] resolved = mapping.getMirrorSets().resolve(element.getName(), annotation, ReflectionUtils::invokeMethod);
    Method[] result = new Method[resolved.length];
    for (int i = 0; i < resolved.length; i++) {
        result[i] = resolved[i] != -1 ? mapping.getAttributes().get(resolved[i]) : null;
    }
    return result;
}
Also used : Method(java.lang.reflect.Method) ReflectionUtils(cn.taketoday.util.ReflectionUtils) Annotation(java.lang.annotation.Annotation)

Example 4 with ReflectionUtils

use of cn.taketoday.util.ReflectionUtils in project today-framework by TAKETODAY.

the class ApplicationContextEventTests method lambdaAsListenerWithErrorHandler.

@Test
public void lambdaAsListenerWithErrorHandler() {
    final Set<MyEvent> seenEvents = new HashSet<>();
    StaticApplicationContext context = new StaticApplicationContext();
    SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
    multicaster.setErrorHandler(ReflectionUtils::rethrowRuntimeException);
    context.getBeanFactory().registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, multicaster);
    ApplicationListener<MyEvent> listener = seenEvents::add;
    context.addApplicationListener(listener);
    context.refresh();
    MyEvent event1 = new MyEvent(context);
    context.publishEvent(event1);
    context.publishEvent(new MyOtherEvent(context));
    MyEvent event2 = new MyEvent(context);
    context.publishEvent(event2);
    assertThat(seenEvents.size()).isSameAs(2);
    assertThat(seenEvents.contains(event1)).isTrue();
    assertThat(seenEvents.contains(event2)).isTrue();
    context.close();
}
Also used : StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) ReflectionUtils(cn.taketoday.util.ReflectionUtils) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

ReflectionUtils (cn.taketoday.util.ReflectionUtils)4 StaticApplicationContext (cn.taketoday.context.support.StaticApplicationContext)2 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 HashSet (java.util.HashSet)2 Test (org.junit.jupiter.api.Test)2