You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2006/10/30 01:14:53 UTC

svn commit: r469029 - in /incubator/wicket/trunk/wicket/src/main/java/wicket: Component.java annot/ annot/OnAttach.java annot/OnDetach.java

Author: ivaynberg
Date: Sun Oct 29 16:14:53 2006
New Revision: 469029

URL: http://svn.apache.org/viewvc?view=rev&rev=469029
Log:
wip: WICKET-23 introduced @OnAttach, @OnDetach

Added:
    incubator/wicket/trunk/wicket/src/main/java/wicket/annot/
    incubator/wicket/trunk/wicket/src/main/java/wicket/annot/OnAttach.java
    incubator/wicket/trunk/wicket/src/main/java/wicket/annot/OnDetach.java
Modified:
    incubator/wicket/trunk/wicket/src/main/java/wicket/Component.java

Modified: incubator/wicket/trunk/wicket/src/main/java/wicket/Component.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/main/java/wicket/Component.java?view=diff&rev=469029&r1=469028&r2=469029
==============================================================================
--- incubator/wicket/trunk/wicket/src/main/java/wicket/Component.java (original)
+++ incubator/wicket/trunk/wicket/src/main/java/wicket/Component.java Sun Oct 29 16:14:53 2006
@@ -19,6 +19,8 @@
 package wicket;
 
 import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
@@ -29,6 +31,8 @@
 import org.apache.commons.logging.LogFactory;
 
 import wicket.ajax.AjaxRequestTarget;
+import wicket.annot.OnAttach;
+import wicket.annot.OnDetach;
 import wicket.authorization.Action;
 import wicket.authorization.AuthorizationException;
 import wicket.authorization.IAuthorizationStrategy;
@@ -52,6 +56,8 @@
 import wicket.util.convert.IConverter;
 import wicket.util.lang.Classes;
 import wicket.util.lang.Objects;
+import wicket.util.lang.reflect.ClassOrder;
+import wicket.util.lang.reflect.ReflectionUtils;
 import wicket.util.string.PrependingStringBuffer;
 import wicket.util.string.Strings;
 import wicket.util.value.IValueMap;
@@ -472,6 +478,10 @@
 	/** Reserved subclass-definable flag bit */
 	protected static final int FLAG_RESERVED8 = 0x80000;
 
+	/** empty object[] array used for invoking listener methods */
+	private static final Object[] LISTENER_ARGS = new Object[] {};
+
+
 	/** Basic model IModelComparator implementation for normal object models */
 	private static final IModelComparator defaultModelComparator = new IModelComparator()
 	{
@@ -650,9 +660,9 @@
 			throw new WicketRuntimeException(
 					"component without a parent is not allowed, default constructor can only be called by a page");
 		}
-		
+
 		getApplication().notifyComponentInstantiationListeners(this);
-		
+
 		this.markupFragment = getMarkupFragment();
 	}
 
@@ -1783,7 +1793,7 @@
 
 				// attach
 				internalAttach();
-				
+
 				// check authorization
 				// first the component itself
 				// (after attach as otherwise list views etc wont work)
@@ -1804,7 +1814,7 @@
 						}
 					});
 				}
-				
+
 				// Render the component and all its children
 				onBeforeRender();
 				render(markupStream);
@@ -2708,8 +2718,48 @@
 	{
 		onAttach();
 		internalOnAttach();
+
+		List<Method> listeners = ReflectionUtils.invocationChainForAnnotation(getClass(),
+				OnAttach.class, ClassOrder.SUPER_TO_SUB);
+		for (Method method : listeners)
+		{
+			invokeAnnotatedListenerMethod(method, OnAttach.class);
+		}
+	}
+
+	/**
+	 * Invokes a listener method
+	 * 
+	 * @param method
+	 *            listener method
+	 * @param annot
+	 *            annotation responsible for invocation
+	 */
+	private void invokeAnnotatedListenerMethod(Method method, Class<? extends Annotation> annot)
+	{
+		if (!method.getReturnType().equals(void.class) || method.getParameterTypes().length != 0)
+		{
+			throw new IllegalStateException("Method [[" + method.getName()
+					+ "]] cannot be annotated with [[" + OnAttach.class.getSimpleName()
+					+ "]] because it doesnt match signature [[void method()]]");
+		}
+		try
+		{
+			if (!method.isAccessible())
+			{
+				method.setAccessible(true);
+			}
+			method.invoke(this, LISTENER_ARGS);
+		}
+		catch (Exception e)
+		{
+			throw new WicketRuntimeException("Error while invoking listener method [["
+					+ method.getName() + "]] for [[" + annot.getClass().getSimpleName()
+					+ "]] event", e);
+		}
 	}
 
+
 	/**
 	 * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL OR
 	 * OVERRIDE.
@@ -2720,6 +2770,14 @@
 	{
 		internalOnDetach();
 		onDetach();
+
+		List<Method> listeners = ReflectionUtils.invocationChainForAnnotation(getClass(),
+				OnDetach.class, ClassOrder.SUB_TO_SUPER);
+		for (Method method : listeners)
+		{
+			invokeAnnotatedListenerMethod(method, OnAttach.class);
+		}
+
 	}
 
 	/**

Added: incubator/wicket/trunk/wicket/src/main/java/wicket/annot/OnAttach.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/main/java/wicket/annot/OnAttach.java?view=auto&rev=469029
==============================================================================
--- incubator/wicket/trunk/wicket/src/main/java/wicket/annot/OnAttach.java (added)
+++ incubator/wicket/trunk/wicket/src/main/java/wicket/annot/OnAttach.java Sun Oct 29 16:14:53 2006
@@ -0,0 +1,48 @@
+/*
+ * $Id: org.eclipse.jdt.ui.prefs 5004 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 2006) eelco12 $
+ * $Revision: 5004 $
+ * $Date: 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 2006) $
+ * 
+ * ==============================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package wicket.annot;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Methods annotated with this annotation are invoked when the object is
+ * attached by the framework.
+ * 
+ * Objects are always attached once per request before any method on the object
+ * is invoked by the framework during that request.
+ * 
+ * Supported object types:
+ * <ul>
+ * <li> Component </li>
+ * <li> Session </li>
+ * <li> RequestCycle </li>
+ * </ul>
+ * 
+ * FIXME so far only Componnet is supported
+ * 
+ * @author ivaynberg
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface OnAttach {
+
+}

Added: incubator/wicket/trunk/wicket/src/main/java/wicket/annot/OnDetach.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/main/java/wicket/annot/OnDetach.java?view=auto&rev=469029
==============================================================================
--- incubator/wicket/trunk/wicket/src/main/java/wicket/annot/OnDetach.java (added)
+++ incubator/wicket/trunk/wicket/src/main/java/wicket/annot/OnDetach.java Sun Oct 29 16:14:53 2006
@@ -0,0 +1,48 @@
+/*
+ * $Id: org.eclipse.jdt.ui.prefs 5004 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 2006) eelco12 $
+ * $Revision: 5004 $
+ * $Date: 2006-03-17 20:47:08 -0800 (Fri, 17 Mar 2006) $
+ * 
+ * ==============================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package wicket.annot;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Methods annotated with this annotation are invoked when the object is
+ * detached by the framework.
+ * 
+ * Objects are always detached at the end of the request. This is the last
+ * action the framework performs on the object.
+ * 
+ * Supported object types:
+ * <ul>
+ * <li> Component </li>
+ * <li> Session </li>
+ * <li> RequestCycle </li>
+ * </ul>
+ * 
+ * FIXME so far only Component is supported
+ * 
+ * @author ivaynberg
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface OnDetach {
+
+}