Author: kylem
Date: Wed Oct 6 12:29:54 2004
New Revision: 53896
Added:
incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/context/ContextParam.java
incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/context/ContextParamExt.jcx
incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/context/ContextParamImpl.jcs
incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/context/ContextParamTest.java
Modified:
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/bean/ControlBean.java
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/bean/ControlBeanContext.java
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/ControlBean.vm
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/GenMethod.java
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptEvent.java
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptEventHandler.java
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptMethodHelper.java
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptOperation.java
Log:
Added method parameter name and annotation accessors to ControlBeanContext
APIs. It's now
possible to include parameter name as part of extensible control semantics, and
to extract
parameter values by name from within an extensible method invocation. Also
included are three
new checkin tests that verify accessor APIs.
Modified:
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/bean/ControlBean.java
==============================================================================
---
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/bean/ControlBean.java
(original)
+++
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/bean/ControlBean.java
Wed Oct 6 12:29:54 2004
@@ -626,6 +626,16 @@
}
/**
+ * Returns the parameter names for a method on the ControlBean. Actual
mapping is done
+ * by generated subclasses, so if we reach the base ControlBean
implementation, then
+ * no parameter names are available for the target method.
+ */
+ protected String [] getParameterNames(Method m)
+ {
+ throw new IllegalArgumentException("No parameter name data for " + m);
+ }
+
+ /**
* Determines if the control impl class uses controls (is a control client)
*/
protected boolean isControlClient( Class implClass )
Modified:
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/bean/ControlBeanContext.java
==============================================================================
---
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/bean/ControlBeanContext.java
(original)
+++
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/bean/ControlBeanContext.java
Wed Oct 6 12:29:54 2004
@@ -581,14 +581,7 @@
//
public String [] getParameterNames(Method m) throws
IllegalArgumentException
{
- //
- // TEMPORARY HACK IMPLEMENTATION
- //
- String [] names = new String [m.getParameterTypes().length];
- for (int i = 0; i < names.length; i++)
- names[i] = "arg" + i;
-
- return names;
+ return _bean.getParameterNames(m);
}
//
Modified:
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/ControlBean.vm
==============================================================================
---
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/ControlBean.vm
(original)
+++
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/ControlBean.vm
Wed Oct 6 12:29:54 2004
@@ -36,20 +36,29 @@
## This macro provides the template for declaring the static final Method
fields that
## are associated with all declared operations
##
-#macro (declareMethodFields)
+#macro (declareMethodStatics)
#foreach ($operation in $intf.operations)
static final Method $operation.methodField;
#end
+
+ //
+ // This HashMap will map from a Method to the array of names for
parameters of the
+ // method. This is necessary because parameter name data isn't carried
along in the
+ // class file, but if available can enable ease of use by referencing
parameters by
+ // the declared name (vs. by index).
+ //
+ static HashMap<Method, String []> _methodParamMap = new HashMap<Method,
String []>();
#end
##
## This macros provides the template for initializing the static final Method
fields that
## are associated with declared operations
##
-#macro (initMethodFields)
+#macro (initMethodStatics)
try
{
#foreach ($operation in $intf.operations)
- $operation.methodField =
${intf.className}.class.getMethod("${operation.name}", new Class []
{$operation.argTypes});
+ $operation.methodField =
${intf.className}.class.getMethod("${operation.name}", new Class []
{$operation.argTypes});
+ _methodParamMap.put($operation.methodField, new String [] {
$operation.getArgList(true) });
#end
}
catch (NoSuchMethodException nsme)
@@ -102,6 +111,26 @@
#end
##
+## This macro defines the implementation of of the getParameterNames helper
method
+##
+#macro (declareGetParameterNames)
+ /**
+ * Returns an array of parameter names for the request method, or null if
no parameter
+ * data is available.
+ */
+ protected String [] getParameterNames(Method m)
+ {
+ // Check the local map for operations on this bean type
+ if (_methodParamMap.containsKey(m))
+ {
+ return _methodParamMap.get(m);
+ }
+
+ // Delegate up if not found locally
+ return super.getParameterNames(m);
+ }
+#end
+##
## This macro defines the implementation of an operation
##
#macro (declareOperationImpl $operation)
@@ -299,18 +328,22 @@
public class ${bean.shortName} extends $bean.superClass.className implements
$intf.className
{
#if ($intf.operations.size() != 0)
- #declareMethodFields()
+ #declareMethodStatics()
static
{
- #initMethodFields()
+ #initMethodStatics()
}
#end
#declareConstructors()
- #foreach ($operation in $intf.operations)
- #declareOperationImpl ($operation)
+ #if ($intf.operations.size() != 0)
+ #declareGetParameterNames()
+
+ #foreach ($operation in $intf.operations)
+ #declareOperationImpl ($operation)
+ #end
#end
#foreach ($propertySet in $intf.propertySets)
Modified:
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/GenMethod.java
==============================================================================
---
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/GenMethod.java
(original)
+++
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/GenMethod.java
Wed Oct 6 12:29:54 2004
@@ -55,9 +55,15 @@
abstract public String getArgDecl();
/**
- * Returns the the method argument names, in a comma separated list
+ * Returns the the method argument names, in a comma separated list. The
+ * quoteDelimit specifies whether names are quoted (Strings) or raw form
*/
- abstract public String getArgList();
+ abstract public String getArgList(boolean quoteDelimit);
+
+ /**
+ * Returns the method argument names as a comma delmited list, in raw form.
+ */
+ public String getArgList() { return getArgList(false); }
/**
* Returns the the method argument classes, in a comma separated list
Modified:
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptEvent.java
==============================================================================
---
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptEvent.java
(original)
+++
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptEvent.java
Wed Oct 6 12:29:54 2004
@@ -52,7 +52,7 @@
//
public String getName() { return _helper.getName(); }
public String getArgDecl() { return _helper.getArgDecl(); }
- public String getArgList() { return _helper.getArgList(); }
+ public String getArgList(boolean quoteDelimit) { return
_helper.getArgList(quoteDelimit); }
public String getArgTypes() { return _helper.getArgTypes(); }
public String getReturnType() { return _helper.getReturnType(); }
public String getThrowsClause() { return _helper.getThrowsClause(); }
Modified:
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptEventHandler.java
==============================================================================
---
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptEventHandler.java
(original)
+++
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptEventHandler.java
Wed Oct 6 12:29:54 2004
@@ -53,7 +53,7 @@
//
public String getName() { return _helper.getName(); }
public String getArgDecl() { return _helper.getArgDecl(); }
- public String getArgList() { return _helper.getArgList(); }
+ public String getArgList(boolean quoteDelimit) { return
_helper.getArgList(quoteDelimit); }
public String getArgTypes() { return _helper.getArgTypes(); }
public String getReturnType() { return _helper.getReturnType(); }
public String getThrowsClause() { return _helper.getThrowsClause(); }
Modified:
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptMethodHelper.java
==============================================================================
---
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptMethodHelper.java
(original)
+++
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptMethodHelper.java
Wed Oct 6 12:29:54 2004
@@ -108,7 +108,7 @@
/**
* Returns the the method argument names, in a comma separated list
*/
- public String getArgList()
+ public String getArgList(boolean quoteDelimit)
{
StringBuffer sb = new StringBuffer();
int i = 0;
@@ -124,11 +124,12 @@
// BUGBUG: when the MethodDeclaration is derived from Reflection,
this seems
// to return 'arg0' for all arguments!
String argName = paramDecl.getSimpleName();
+ if (quoteDelimit) sb.append('"');
if (argName.equals("arg0"))
sb.append("arg" + i);
else
sb.append(argName);
-
+ if (quoteDelimit) sb.append('"');
i++;
}
return sb.toString();
Modified:
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptOperation.java
==============================================================================
---
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptOperation.java
(original)
+++
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/generator/apt/AptOperation.java
Wed Oct 6 12:29:54 2004
@@ -54,7 +54,7 @@
//
public String getName() { return _helper.getName(); }
public String getArgDecl() { return _helper.getArgDecl(); }
- public String getArgList() { return _helper.getArgList(); }
+ public String getArgList(boolean quoteDelimit) { return
_helper.getArgList(quoteDelimit); }
public String getArgTypes() { return _helper.getArgTypes(); }
public String getReturnType() { return _helper.getReturnType(); }
public String getThrowsClause() { return _helper.getThrowsClause(); }
Added:
incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/context/ContextParam.java
==============================================================================
--- (empty file)
+++
incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/context/ContextParam.java
Wed Oct 6 12:29:54 2004
@@ -0,0 +1,33 @@
+package org.apache.beehive.controls.test.controls.context;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.reflect.Method;
+
+import org.apache.beehive.controls.api.bean.ControlInterface;
+import org.apache.beehive.controls.api.context.Context;
+import org.apache.beehive.controls.api.properties.PropertySet;
+
[EMAIL PROTECTED]
+public interface ContextParam
+{
+ @PropertySet
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(ElementType.PARAMETER)
+ public @interface Param
+ {
+ String value();
+ }
+
+ //
+ // Returns the array of names for parameters of the referenced method
+ //
+ public String [] paramNameTest(Method m);
+
+ //
+ // Returns the array of @Param annotations for parameters for the
referenced method
+ //
+ public Param [] paramAnnotTest(Method m);
+}
Added:
incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/context/ContextParamExt.jcx
==============================================================================
--- (empty file)
+++
incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/context/ContextParamExt.jcx
Wed Oct 6 12:29:54 2004
@@ -0,0 +1,18 @@
+package org.apache.beehive.controls.test.controls.context;
+
+import org.apache.beehive.controls.api.bean.ControlExtension;
+
+/**
+ * This is a test extension that declares various methods that can be used to
test
+ * ControlBeanContext parameter access APIs
+ * <p>
+ */
[EMAIL PROTECTED]
+public interface ContextParamExt extends ContextParam
+{
+ public int valueTest(@Param("paramName") String paramName,
+ @Param("firstArg") int firstArg,
+ @Param("anotherArg") int anotherArg,
+ /* No @Param */ int hasNoParam,
+ @Param("lastArg") int lastArg);
+}
Added:
incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/context/ContextParamImpl.jcs
==============================================================================
--- (empty file)
+++
incubator/beehive/trunk/controls/test/src/controls/org/apache/beehive/controls/test/controls/context/ContextParamImpl.jcs
Wed Oct 6 12:29:54 2004
@@ -0,0 +1,49 @@
+package org.apache.beehive.controls.test.controls.context;
+
+import java.lang.reflect.Method;
+
+import org.apache.beehive.controls.api.bean.ControlImplementation;
+import org.apache.beehive.controls.api.bean.Extensible;
+import org.apache.beehive.controls.api.context.Context;
+import org.apache.beehive.controls.api.context.ControlBeanContext;
+import org.apache.beehive.controls.api.context.ControlBeanContext.LifeCycle;
+import org.apache.beehive.controls.api.context.ResourceContext;
+import org.apache.beehive.controls.api.context.ResourceContext.ResourceEvents;
+import org.apache.beehive.controls.api.events.EventHandler;
+
+import org.apache.beehive.controls.test.controls.util.TestContext;
+
[EMAIL PROTECTED]
+public class ContextParamImpl implements ContextParam, Extensible
+{
+ @Context ControlBeanContext context;
+
+ public String [] paramNameTest(Method m)
+ {
+ return context.getParameterNames(m);
+ }
+
+ public Param [] paramAnnotTest(Method m)
+ {
+ Param [] annots = new Param [m.getParameterTypes().length];
+ for (int i = 0; i < annots.length; i++)
+ annots[i] = context.getParameterPropertySet(m, i, Param.class);
+
+ return annots;
+ }
+
+ //
+ // The implementation of Extensible.invoke() for this class will echo back
one of the
+ // named parameters of the invoked method.
+ //
+ // JCX-defined methods *must* follow two simple rules:
+ //
+ // 1. the first method is expected to contain the name of one of the
method parameters
+ // 2. the method return type must be assignment-compatible with the type
of the request
+ // parameter
+ //
+ public Object invoke(Method m, Object [] params)
+ {
+ return context.getParameterValue(m, (String)params[0], params);
+ }
+}
Added:
incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/context/ContextParamTest.java
==============================================================================
--- (empty file)
+++
incubator/beehive/trunk/controls/test/src/units/org/apache/beehive/controls/test/java/context/ContextParamTest.java
Wed Oct 6 12:29:54 2004
@@ -0,0 +1,107 @@
+package org.apache.beehive.controls.test.java.context;
+
+import java.beans.Beans;
+import java.lang.reflect.Method;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.beehive.controls.test.controls.context.ContextParam;
+import org.apache.beehive.controls.test.controls.context.ContextParamExt;
+import org.apache.beehive.controls.test.controls.context.ContextParamExtBean;
+import org.apache.beehive.test.tools.milton.common.Report;
+import org.apache.beehive.test.tools.mantis.annotations.tch.Freq;
+
+
+/**
+ * A TestCase that tests controls context services
+ */
[EMAIL PROTECTED]("checkin")
+public class ContextParamTest extends TestCase
+{
+ public ContextParamTest( String s ) { super( s ); }
+
+ ContextParamExtBean paramBean;
+ Method intfMethod;
+ Method extMethod;
+
+ public void setUp() throws Exception
+ {
+ paramBean =
+
(ContextParamExtBean)Beans.instantiate(Thread.currentThread().getContextClassLoader(),
+
ContextParamExtBean.class.getName());
+
+ intfMethod =
+ ContextParam.class.getMethod("paramNameTest", new Class [] {
Method.class });
+
+ extMethod =
+ ContextParamExt.class.getMethod("valueTest",
+ new Class [] {String.class,
Integer.TYPE, Integer.TYPE,
+ Integer.TYPE, Integer.TYPE });
+ }
+
+ public void tearDown()
+ {
+ paramBean = null;
+ extMethod = null;
+ }
+
+ /**
+ * Tests context access to parameter names
+ */
+ public void testContextParamNames() throws Exception
+ {
+ String [] paramNames;
+
+ // Try to read parameter names from methods declared on a base
ControlInterface
+ paramNames = paramBean.paramNameTest(intfMethod);
+ Assert.assertNotNull(paramNames);
+ Assert.assertTrue(paramNames.length ==
intfMethod.getParameterTypes().length);
+ Assert.assertEquals(paramNames[0], "m");
+
+ // Try to read parameter names from methods declared on a
ControlExtension
+ paramNames = paramBean.paramNameTest(extMethod);
+ Assert.assertNotNull(paramNames);
+ Assert.assertTrue(paramNames.length ==
extMethod.getParameterTypes().length);
+ Assert.assertEquals(paramNames[0], "paramName");
+ Assert.assertEquals(paramNames[1], "firstArg");
+ Assert.assertEquals(paramNames[2], "anotherArg");
+ Assert.assertEquals(paramNames[3], "hasNoParam");
+ Assert.assertEquals(paramNames[4], "lastArg");
+ }
+
+ /**
+ * Tests context access to parameter annotations
+ */
+ public void testContextParamAnnotations() throws Exception
+ {
+ ContextParam.Param [] paramAnnots =
paramBean.paramAnnotTest(extMethod);
+ Assert.assertNotNull(paramAnnots);
+ Assert.assertTrue(paramAnnots.length ==
extMethod.getParameterTypes().length);
+ Assert.assertNotNull(paramAnnots[0]);
+ Assert.assertEquals(paramAnnots[0].value(), "paramName");
+ Assert.assertNotNull(paramAnnots[1]);
+ Assert.assertEquals(paramAnnots[1].value(), "firstArg");
+ Assert.assertNotNull(paramAnnots[2]);
+ Assert.assertEquals(paramAnnots[2].value(), "anotherArg");
+ Assert.assertNull(paramAnnots[3]);
+ Assert.assertNotNull(paramAnnots[4]);
+ Assert.assertEquals(paramAnnots[4].value(), "lastArg");
+ }
+
+ /**
+ * Tests context access to parameter values by name
+ */
+ public void testContextParamValues() throws Exception
+ {
+ int value;
+ value = paramBean.valueTest("firstArg", 0, 1, 2, 3);
+ Assert.assertTrue(value == 0);
+ value = paramBean.valueTest("anotherArg", 0, 1, 2, 3);
+ Assert.assertTrue(value == 1);
+ value = paramBean.valueTest("hasNoParam", 0, 1, 2, 3);
+ Assert.assertTrue(value == 2);
+ value = paramBean.valueTest("lastArg", 0, 1, 2, 3);
+ Assert.assertTrue(value == 3);
+ }
+}