Update of /var/cvs/src/org/mmbase/util/functions
In directory james.mmbase.org:/tmp/cvs-serv27157
Modified Files:
MethodFunction.java Required.java
Log Message:
Made MethodFunction's wrappable into NodeFunction too, some javadoc
See also: http://cvs.mmbase.org/viewcvs/src/org/mmbase/util/functions
Index: MethodFunction.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/util/functions/MethodFunction.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -b -r1.11 -r1.12
--- MethodFunction.java 25 Nov 2007 18:25:49 -0000 1.11
+++ MethodFunction.java 4 Dec 2008 15:23:50 -0000 1.12
@@ -12,21 +12,35 @@
import java.lang.reflect.*;
import java.lang.annotation.*;
+import org.mmbase.util.logging.Logger;
+import org.mmbase.util.logging.Logging;
+
/**
- * A function based on an abritrary method. Since the name of the parameters
cannot be found by
- * reflection, this is only of limited use. Normally you would probably better
use BeanFunction. A
- * method-function can come in handy on JSP's. With the advent of java 1.5 we
can use annotations to
- * annotate acutal parameter names.
- *
+ * A function based on an abritrary method. Use the annotation [EMAIL
PROTECTED] Name} to attribute the
+ * parameter names. A method function can e.g. be defined like so in the
builder xml:
+<pre><![CDATA[
+ <function key="canCloseLesson" name="canCloseLesson">
+ <class>nl.didactor.component.assessment.LessonChecker</class>
+</function>]]>
+ </pre>
+And be implemented like so:
+<pre>
+ public static boolean canCloseLesson(@Required @Name("node") Node user,
+ @Required @Name("lesson") Node
lesson) {
+ ...
+ }
+</pre>
+
* @author Michiel Meeuwissen
- * @version $Id: MethodFunction.java,v 1.11 2007/11/25 18:25:49 nklasens Exp $
+ * @version $Id: MethodFunction.java,v 1.12 2008/12/04 15:23:50 michiel Exp $
* @see org.mmbase.module.core.MMObjectBuilder#executeFunction
* @see org.mmbase.bridge.Node#getFunctionValue
* @see org.mmbase.util.functions.BeanFunction
* @since MMBase-1.7
*/
public class MethodFunction extends AbstractFunction<Object> {
+ private static final Logger log =
Logging.getLoggerInstance(MethodFunction.class);
public static Function<Object> getFunction(Method method, String name) {
@@ -116,14 +130,24 @@
Parameter<?>[] def = new Parameter[parameters.length];
for (int i = 0; i < parameters.length; i++) {
String paramName = null;
+ boolean required = false;
for (Annotation annot : annots[i]) {
+ // no other way to find the name of the parameter than with
annotations
if (annot.annotationType().equals(Name.class)) {
paramName = ((Name) annot).value();
}
+ if (annot.annotationType().equals(Required.class)) {
+ required = true;
+ }
}
if (paramName == null) paramName = "parameter" + (i + 1);
- def[i] = new Parameter<String>(paramName, parameters[i]); // no
way to find the name of the parameter
+ // make it possible to default also NodeFunction as
MethodFunctions.
+ if (paramName.equals("node") &&
org.mmbase.bridge.Node.class.isAssignableFrom(parameters[i])) {
+ def[i] = Parameter.NODE;
+ } else {
+ def[i] = new Parameter<Object>(paramName, parameters[i],
required);
+ }
}
setParameterDefinition(def);
@@ -135,7 +159,7 @@
public Object getFunctionValue(Parameters parameters) {
try {
- return method.invoke(instance, parameters.toArray());
+ return method.invoke(instance, parameters.subList(0,
getParameterDefinition().length).toArray());
} catch (Exception e) {
throw new RuntimeException(e);
}
Index: Required.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/util/functions/Required.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- Required.java 2 Aug 2008 15:14:40 -0000 1.3
+++ Required.java 4 Dec 2008 15:23:50 -0000 1.4
@@ -15,11 +15,11 @@
* the corresponding [EMAIL PROTECTED] BeanFunction} required.
*
* @author Michiel Meeuwissen
- * @version $Id: Required.java,v 1.3 2008/08/02 15:14:40 michiel Exp $
+ * @version $Id: Required.java,v 1.4 2008/12/04 15:23:50 michiel Exp $
* @since MMBase-1.9
*/
@Retention(RetentionPolicy.RUNTIME)
[EMAIL PROTECTED](ElementType.METHOD)
[EMAIL PROTECTED]({ElementType.METHOD, ElementType.PARAMETER})
public @interface Required {
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs