org.mmbase.util.functions
Class BeanFunction

java.lang.Object
  extended by org.mmbase.util.functions.AbstractFunction<Object>
      extended by org.mmbase.util.functions.BeanFunction
All Implemented Interfaces:
Serializable, Comparable<Function<Object>>, Function<Object>

public class BeanFunction
extends AbstractFunction<Object>

One or more functions based on a Java-bean. Every setter method of the bean corresponds with one parameter. The default value of the parameter can be defined with the getter method (which will be called immediately after instantiation of such a Class). So the setters/getters define Parameters. More specific DataTypes can be attributes to such parameters using the Type annotation.

All other methods (with no arguments) of the class correspond to the functions. So, you can implement more bean-functions in the same class, as long as they have the same parameters.

A BeanFunction can be aquired via FunctionFactory.getFunction(Class, String) (which delegates to a static method in this class).

Since:
MMBase-1.8
Version:
$Id: BeanFunction.java 41943 2010-04-16 17:18:19Z michiel $
Author:
Michiel Meeuwissen
See Also:
MethodFunction, FunctionFactory, Serialized Form

Nested Class Summary
static class BeanFunction.Producer
          A producer can instantiate beans
 
Field Summary
static Pattern NCName
           
 
Fields inherited from class org.mmbase.util.functions.AbstractFunction
autoReturnType, name, returnType
 
Constructor Summary
BeanFunction(Object bean, String name)
           
 
Method Summary
static Class getClass(Class claz, String name)
          Utility function, searches an inner class of a given class.
static DataType getDataType(String value, BasicDataType base)
          Given a string and a 'base' datatype, produces a new DataType.
static BeanFunction getFunction(Class claz, String name)
          This defaulting version of getFunction(Class, String, Producer) uses a producer that uses Class.newInstance().
static BeanFunction getFunction(Class claz, String name, BeanFunction.Producer producer)
          Gives back a Function object based on the 'bean' concept.
 Object getFunctionValue(Parameters parameters)
          Executes the defined function supplying the given arguments.
static
<C> C
getInstance(Class<C> claz, Object constructorArgument)
          Utitily function to create an instance of a certain class.
static Parameter<?>[] getParameterDefinition(Object sampleInstance, List<Method> setMethods)
           
 BeanFunction.Producer getProducer()
           
static void main(String[] argv)
           
static void setParameters(Object b, Parameters parameters, List<Method> setMethods)
           
 
Methods inherited from class org.mmbase.util.functions.AbstractFunction
compareTo, createParameters, equals, getDescription, getFunctionValueWithArgs, getFunctionValueWithList, getName, getParameterDefinition, getReturnType, hashCode, setDescription, setParameterDefinition, setReturnType, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NCName

public static final Pattern NCName
Since:
MMBase-1.9.2
Constructor Detail

BeanFunction

public BeanFunction(Object bean,
                    String name)
             throws IllegalAccessException,
                    InstantiationException,
                    InvocationTargetException,
                    DependencyException
Throws:
IllegalAccessException
InstantiationException
InvocationTargetException
DependencyException
Since:
MMBase-1.8.5
Method Detail

getClass

public static Class getClass(Class claz,
                             String name)
Utility function, searches an inner class of a given class. This inner class can perhaps be used as a bean. Used in JSP/taglib.

Parameters:
claz - The class to be considered
name - The name of the inner class
Throws:
IllegalArgumentException - if claz has no inner class with that name

getFunction

public static BeanFunction getFunction(Class claz,
                                       String name,
                                       BeanFunction.Producer producer)
                                throws IllegalAccessException,
                                       InstantiationException,
                                       InvocationTargetException,
                                       DependencyException
Gives back a Function object based on the 'bean' concept.

Parameters:
claz - The class which must be considered a 'bean' function
name - The name of the function (the name of a Method in the given class)
producer - An object that can produce in instance of the class claz. Defaults to a producer that simply calls Class.newInstance().
Throws:
IllegalAccessException
InstantiationException
InvocationTargetException
DependencyException
Since:
MMBase-1.8.5

getFunction

public static BeanFunction getFunction(Class claz,
                                       String name)
                                throws IllegalAccessException,
                                       InstantiationException,
                                       InvocationTargetException,
                                       DependencyException
This defaulting version of getFunction(Class, String, Producer) uses a producer that uses Class.newInstance(). Called from FunctionFactory

Throws:
IllegalAccessException
InstantiationException
InvocationTargetException
DependencyException

getInstance

public static <C> C getInstance(Class<C> claz,
                                Object constructorArgument)
                     throws IllegalAccessException,
                            InstantiationException,
                            InvocationTargetException
Utitily function to create an instance of a certain class. Two constructors are tried, a one argument one, and if that fails, simply newInstance is used.

Throws:
IllegalAccessException
InstantiationException
InvocationTargetException
Since:
MMBase-1.8.5

getParameterDefinition

public static Parameter<?>[] getParameterDefinition(Object sampleInstance,
                                                    List<Method> setMethods)
                                             throws IllegalAccessException,
                                                    InvocationTargetException,
                                                    DependencyException
Throws:
IllegalAccessException
InvocationTargetException
DependencyException
Since:
MMBase-1.9.2

setParameters

public static void setParameters(Object b,
                                 Parameters parameters,
                                 List<Method> setMethods)
                          throws IllegalAccessException,
                                 InvocationTargetException
Parameters:
b - The 'bean' on which the setter methods must be invoked
parameters - The object containing the parameter values
setMethods - The setter methods
Throws:
IllegalAccessException
InvocationTargetException
Since:
MMBase-1.9.2

getDataType

public static DataType getDataType(String value,
                                   BasicDataType base)
                            throws DependencyException
Given a string and a 'base' datatype, produces a new DataType. If the string matches NCName then the datatype is looked up in the MMBase DataType repository at DataTypes.getDataType(java.lang.String). Otherwise the String is interpreted as a piece of XML.

Throws:
DependencyException
Since:
MMBase-1.9.2

getProducer

public BeanFunction.Producer getProducer()
Since:
MMBase-1.8.5

getFunctionValue

public Object getFunctionValue(Parameters parameters)
Executes the defined function supplying the given arguments. Instantiates the bean, calls all setters using the parameters, and executes the method associated with this function.

Specified by:
getFunctionValue in interface Function<Object>
Specified by:
getFunctionValue in class AbstractFunction<Object>
Parameters:
parameters - The parameters for the function. To specify an empty parameter list use Parameters.VOID. Implementors are encouraged to support null too.
Returns:
The function value, which can be of any type compatible to AbstractFunction.getReturnType()
See Also:
AbstractFunction.createParameters()

main

public static void main(String[] argv)
                 throws Exception
Throws:
Exception


MMBase 2.0-SNAPSHOT - null