[picocontainer-scm] [5791] java/2.x/trunk/pico/container/src/test/org/picocontainer/lifecycle: PICO-383: completion of sophisticated J2EE lifecycle scenario/defect
paul-yCVjj/[email protected] Tue, 21 Jun 2011 06:35:23 -0500 (CDT)
| Newsgroups | gmane.comp.java.picocontainer.cvs |
|---|---|
| Message-ID | <[email protected]> |
Revision 5791
Author paul
Date 2011-06-21 06:35:22 -0500 (Tue, 21 Jun 2011)
Log Message
PICO-383 : completion of sophisticated J2EE lifecycle scenario/defect
Modified Paths
- java/2.x/trunk/pico/container/src/java/org/picocontainer/lifecycle/JavaEE5LifecycleStrategy.java
- java/2.x/trunk/pico/container/src/test/org/picocontainer/lifecycle/JavaEE5LifecycleStrategyTestCase.java
Diff
Modified: java/2.x/trunk/pico/container/src/java/org/picocontainer/lifecycle/JavaEE5LifecycleStrategy.java (5790 => 5791)
--- java/2.x/trunk/pico/container/src/java/org/picocontainer/lifecycle/JavaEE5LifecycleStrategy.java 2011-06-14 15:18:55 UTC (rev 5790)
+++ java/2.x/trunk/pico/container/src/java/org/picocontainer/lifecycle/JavaEE5LifecycleStrategy.java 2011-06-21 11:35:22 UTC (rev 5791)
@@ -15,6 +15,8 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.HashSet;
+import java.util.Set;
/**
* Java EE 5 has some annotations PreDestroy and PostConstruct that map to start() and dispose() in our world
@@ -36,7 +38,7 @@
/** {@inheritDoc} **/
public void start(final Object component) {
- doLifecycleMethod(component, PostConstruct.class);
+ doLifecycleMethod(component, PostConstruct.class, true);
}
/** {@inheritDoc} **/
@@ -45,28 +47,27 @@
/** {@inheritDoc} **/
public void dispose(final Object component) {
- doLifecycleMethod(component, PreDestroy.class);
+ doLifecycleMethod(component, PreDestroy.class, false);
}
-
-
- private void doLifecycleMethod(final Object component, Class<? extends Annotation> annotation) {
- doLifecycleMethod(component, annotation, component.getClass());
+ private void doLifecycleMethod(final Object component, Class<? extends Annotation> annotation, boolean superFirst) {
+ doLifecycleMethod(component, annotation, component.getClass(), superFirst, new HashSet<String>());
}
- private void doLifecycleMethod(Object component, Class<? extends Annotation> annotation, Class<? extends Object> clazz) {
+ private void doLifecycleMethod(Object component, Class<? extends Annotation> annotation, Class<? extends Object> clazz, boolean superFirst, Set<String> doneAlready) {
Class<?> parent = clazz.getSuperclass();
- if (parent != Object.class) {
- doLifecycleMethod(component, annotation, parent);
+ if (superFirst && parent != Object.class) {
+ doLifecycleMethod(component, annotation, parent, superFirst, doneAlready);
}
Method[] methods = clazz.getDeclaredMethods();
- for (int i = 0; i < methods.length; i++) {
- Method method = methods[i];
- if (method.isAnnotationPresent(annotation)) {
+ for (Method method : methods) {
+ String signature = signature(method);
+ if (method.isAnnotationPresent(annotation) && !doneAlready.contains(signature)) {
try {
long str = System.currentTimeMillis();
currentMonitor().invoking(null, null, method, component, new Object[0]);
method.invoke(component);
+ doneAlready.add(signature);
currentMonitor().invoked(null, null, method, component, System.currentTimeMillis() - str, new Object[0], null);
} catch (IllegalAccessException e) {
throw new PicoLifecycleException(method, component, e);
@@ -75,9 +76,21 @@
}
}
}
+ if (!superFirst && parent != Object.class) {
+ doLifecycleMethod(component, annotation, parent, superFirst, doneAlready);
+ }
}
+ private static String signature(Method method) {
+ StringBuilder sb = new StringBuilder(method.getName());
+ Class<?>[] pt = method.getParameterTypes();
+ for (Class<?> objectClass : pt) {
+ sb.append(objectClass.getName());
+ }
+ return sb.toString();
+ }
+
/**
* {@inheritDoc} The component has a lifecycle PreDestroy or PostConstruct are on a method
*/
Modified: java/2.x/trunk/pico/container/src/test/org/picocontainer/lifecycle/JavaEE5LifecycleStrategyTestCase.java (5790 => 5791)
--- java/2.x/trunk/pico/container/src/test/org/picocontainer/lifecycle/JavaEE5LifecycleStrategyTestCase.java 2011-06-14 15:18:55 UTC (rev 5790)
+++ java/2.x/trunk/pico/container/src/test/org/picocontainer/lifecycle/JavaEE5LifecycleStrategyTestCase.java 2011-06-21 11:35:22 UTC (rev 5791)
@@ -99,10 +99,37 @@
pico.addComponent(ProPostAnnotationJava5Startable2.class);
pico.start();
pico.dispose();
- assertEquals("post()subPost()pre()subPre()", pico.getComponent(StringBuilder.class).toString());
+ assertEquals("post()subPost()subPre()pre()", pico.getComponent(StringBuilder.class).toString());
}
@Test public void testSerializable(){
}
+ public static class ProPostAnnotationJava5Startable3 extends ProPostAnnotationJava5Startable {
+
+ public ProPostAnnotationJava5Startable3(StringBuilder sb) {
+ super(sb);
+ }
+
+ @PostConstruct
+ @Override
+ public void post() {
+ sb.append("subPost3()");
+ }
+
+ @PreDestroy
+ public void subPre() {
+ sb.append("subPre3()");
+ }
+ }
+
+ @Test
+ public void testLifecycleOfSubclassWhichOverrides(){
+ pico.removeComponent(ProPostAnnotationJava5Startable.class);
+ pico.addComponent(ProPostAnnotationJava5Startable3.class);
+ pico.start();
+ pico.dispose();
+ assertEquals("subPost3()subPre3()pre()", pico.getComponent(StringBuilder.class).toString());
+ }
+
}
\ No newline at end of file
----------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email