Support @WithStateMachine as meta annotation
- Remove explicit check for @Component in favour of @WithStateMachine which makes it more logical to check meta annotations. - Fixes #179
This commit is contained in:
@@ -831,6 +831,14 @@ application context by using annotation `name` field.
|
||||
include::samples/DocsConfigurationSampleTests4.java[tags=snippetAA]
|
||||
----
|
||||
|
||||
_@WithStateMachine_ can also be used as a meta-annotation as shown
|
||||
above. In this case you could annotate your bean with _WithMyBean_.
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
include::samples/DocsConfigurationSampleTests4.java[tags=snippetAAA]
|
||||
----
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
Return type of these methods doesn't matter and is effectively
|
||||
|
||||
@@ -17,11 +17,8 @@ package org.springframework.statemachine.processor;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -52,7 +49,7 @@ import org.springframework.statemachine.annotation.OnStateMachineStop;
|
||||
import org.springframework.statemachine.annotation.OnTransition;
|
||||
import org.springframework.statemachine.annotation.OnTransitionEnd;
|
||||
import org.springframework.statemachine.annotation.OnTransitionStart;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.statemachine.annotation.WithStateMachine;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
@@ -137,11 +134,13 @@ public class StateMachineAnnotationPostProcessor implements BeanPostProcessor, B
|
||||
Assert.notNull(beanFactory, "BeanFactory must not be null");
|
||||
final Class<?> beanClass = getBeanClass(bean);
|
||||
|
||||
if (!isStereotype(beanClass)) {
|
||||
// we only post-process stereotype components
|
||||
if (AnnotationUtils.findAnnotation(beanClass, WithStateMachine.class) == null) {
|
||||
// we only post-process beans having WithStateMachine
|
||||
// in it or as a meta annotation
|
||||
return bean;
|
||||
}
|
||||
|
||||
|
||||
ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() {
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@@ -247,28 +246,6 @@ public class StateMachineAnnotationPostProcessor implements BeanPostProcessor, B
|
||||
return (targetClass != null) ? targetClass : bean.getClass();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if class is a stereotype meaning if there is
|
||||
* a Component annotation present.
|
||||
*
|
||||
* @param beanClass the bean class
|
||||
* @return true, if is stereotype
|
||||
*/
|
||||
private boolean isStereotype(Class<?> beanClass) {
|
||||
List<Annotation> annotations = new ArrayList<Annotation>(Arrays.asList(beanClass.getAnnotations()));
|
||||
Class<?>[] interfaces = beanClass.getInterfaces();
|
||||
for (Class<?> iface : interfaces) {
|
||||
annotations.addAll(Arrays.asList(iface.getAnnotations()));
|
||||
}
|
||||
for (Annotation annotation : annotations) {
|
||||
Class<? extends Annotation> annotationType = annotation.annotationType();
|
||||
if (annotationType.equals(Component.class) || annotationType.isAnnotationPresent(Component.class)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String generateBeanName(String originalBeanName, Method method, Class<? extends Annotation> annotationType) {
|
||||
String baseName = originalBeanName + "." + method.getName() + "." + ClassUtils.getShortNameAsProperty(annotationType);
|
||||
String name = baseName;
|
||||
|
||||
@@ -18,6 +18,10 @@ package org.springframework.statemachine.annotation;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -41,7 +45,7 @@ public class ClassAnnotationTests extends AbstractStateMachineTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testClassAnnotations() throws Exception {
|
||||
AnnotationConfigApplicationContext context =
|
||||
new AnnotationConfigApplicationContext(BaseConfig.class, BeanConfig.class, FooConfig.class, BarConfig.class);
|
||||
new AnnotationConfigApplicationContext(BaseConfig.class, BeanConfig1.class, FooConfig.class, BarConfig.class);
|
||||
|
||||
ObjectStateMachine<TestStates,TestEvents> fooMachine =
|
||||
context.getBean("fooMachine", ObjectStateMachine.class);
|
||||
@@ -76,6 +80,28 @@ public class ClassAnnotationTests extends AbstractStateMachineTests {
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testClassAnnotationsWithMeta() throws Exception {
|
||||
AnnotationConfigApplicationContext context =
|
||||
new AnnotationConfigApplicationContext(BaseConfig.class, BeanConfig2.class, JeeConfig.class, FooConfig.class);
|
||||
|
||||
ObjectStateMachine<TestStates,TestEvents> jeeMachine =
|
||||
context.getBean("jeeMachine", ObjectStateMachine.class);
|
||||
|
||||
assertThat(context.containsBean("fooMachine"), is(true));
|
||||
assertThat(context.containsBean("jeeMachine"), is(true));
|
||||
|
||||
JeeBean jeeBean = context.getBean(JeeBean.class);
|
||||
FooBean fooBean = context.getBean(FooBean.class);
|
||||
fooBean.resetMethodLatch();
|
||||
jeeMachine.start();
|
||||
jeeMachine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).build());
|
||||
assertThat(jeeBean.onJeeMethodLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(fooBean.onFooMethodLatch.await(2, TimeUnit.SECONDS), is(false));
|
||||
context.close();
|
||||
}
|
||||
|
||||
@WithStateMachine(name = "fooMachine")
|
||||
static class FooBean {
|
||||
|
||||
@@ -104,8 +130,26 @@ public class ClassAnnotationTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@WithStateMachine(name = "jeeMachine")
|
||||
public @interface WithJeeMachine {
|
||||
}
|
||||
|
||||
@WithJeeMachine
|
||||
static class JeeBean {
|
||||
|
||||
CountDownLatch onJeeMethodLatch = new CountDownLatch(1);
|
||||
|
||||
@OnTransition(source = "S1", target = "S2")
|
||||
public void jeeMethod() {
|
||||
onJeeMethodLatch.countDown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BeanConfig {
|
||||
static class BeanConfig1 {
|
||||
|
||||
@Bean
|
||||
public FooBean fooBean() {
|
||||
@@ -119,6 +163,21 @@ public class ClassAnnotationTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BeanConfig2 {
|
||||
|
||||
@Bean
|
||||
public FooBean fooBean() {
|
||||
return new FooBean();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JeeBean jeeBean() {
|
||||
return new JeeBean();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine(name = "fooMachine")
|
||||
static class FooConfig extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
|
||||
@@ -189,4 +248,39 @@ public class ClassAnnotationTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine(name = "jeeMachine")
|
||||
static class JeeConfig extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial(TestStates.S1)
|
||||
.state(TestStates.S2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
|
||||
transitions
|
||||
.withExternal()
|
||||
.source(TestStates.S1)
|
||||
.target(TestStates.S2)
|
||||
.event(TestEvents.E1)
|
||||
.guard(testGuard())
|
||||
.action(testAction());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TestGuard testGuard() {
|
||||
return new TestGuard();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TestAction testAction() {
|
||||
return new TestAction();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -60,6 +60,14 @@ public class DocsConfigurationSampleTests4 extends AbstractStateMachineTests {
|
||||
}
|
||||
// end::snippetAA[]
|
||||
|
||||
// tag::snippetAAA[]
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@WithStateMachine(name = "myMachineBeanName")
|
||||
public @interface WithMyBean {
|
||||
}
|
||||
// end::snippetAAA[]
|
||||
|
||||
// tag::snippetB[]
|
||||
@WithStateMachine
|
||||
public class Bean3 {
|
||||
|
||||
@@ -143,6 +143,18 @@ public class AnnotatedMethodTests extends AbstractStateMachineTests {
|
||||
assertThat(bean1.onMethod9Latch.await(2, TimeUnit.SECONDS), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testMetaAnnotation1() throws Exception {
|
||||
context.register(Config1.class, BeanConfig2.class);
|
||||
context.refresh();
|
||||
ObjectStateMachine<TestStates,TestEvents> machine =
|
||||
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
|
||||
Bean2 bean2 = context.getBean(Bean2.class);
|
||||
machine.start();
|
||||
assertThat(bean2.onMethod0Latch.await(2, TimeUnit.SECONDS), is(true));
|
||||
}
|
||||
|
||||
@WithStateMachine
|
||||
static class Bean1 {
|
||||
|
||||
@@ -220,6 +232,23 @@ public class AnnotatedMethodTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@WithStateMachine
|
||||
public @interface WithStateMachineMeta1 {
|
||||
}
|
||||
|
||||
@WithStateMachineMeta1
|
||||
static class Bean2 {
|
||||
|
||||
CountDownLatch onMethod0Latch = new CountDownLatch(1);
|
||||
|
||||
@OnTransition(target = "S1")
|
||||
public void method0() {
|
||||
onMethod0Latch.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BeanConfig1 {
|
||||
|
||||
@@ -230,6 +259,16 @@ public class AnnotatedMethodTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BeanConfig2 {
|
||||
|
||||
@Bean
|
||||
public Bean2 bean2() {
|
||||
return new Bean2();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@OnTransition
|
||||
|
||||
Reference in New Issue
Block a user