FileDocCategorySizeDatePackage
CallMethodRule.javaAPI DocApache Tomcat 6.0.1424014Fri Jul 20 04:20:36 BST 2007org.apache.tomcat.util.digester

CallMethodRule

public class CallMethodRule extends Rule

Rule implementation that calls a method on an object on the stack (normally the top/parent object), passing arguments collected from subsequent CallParamRule rules or from the body of this element.

By using {@link #CallMethodRule(String methodName)} a method call can be made to a method which accepts no arguments.

Incompatible method parameter types are converted using org.apache.commons.beanutils.ConvertUtils.

This rule now uses org.apache.commons.beanutils.MethodUtils#invokeMethod by default. This increases the kinds of methods successfully and allows primitives to be matched by passing in wrapper classes. There are rare cases when org.apache.commons.beanutils.MethodUtils#invokeExactMethod (the old default) is required. This method is much stricter in its reflection. Setting the UseExactMatch to true reverts to the use of this method.

Note that the target method is invoked when the end of the tag the CallMethodRule fired on is encountered, not when the last parameter becomes available. This implies that rules which fire on tags nested within the one associated with the CallMethodRule will fire before the CallMethodRule invokes the target method. This behaviour is not configurable.

Note also that if a CallMethodRule is expecting exactly one parameter and that parameter is not available (eg CallParamRule is used with an attribute name but the attribute does not exist) then the method will not be invoked. If a CallMethodRule is expecting more than one parameter, then it is always invoked, regardless of whether the parameters were available or not (missing parameters are passed as null values).

Fields Summary
protected String
bodyText
The body text collected from this element.
protected int
targetOffset
location of the target object for the call, relative to the top of the digester object stack. The default value of zero means the target object is the one on top of the stack.
protected String
methodName
The method name to call on the parent object.
protected int
paramCount
The number of parameters to collect from MethodParam rules. If this value is zero, a single parameter will be collected from the body of this element.
protected Class[]
paramTypes
The parameter types of the parameters to be collected.
protected String[]
paramClassNames
The names of the classes of the parameters to be collected. This attribute allows creation of the classes to be postponed until the digester is set.
protected boolean
useExactMatch
Should MethodUtils.invokeExactMethod be used for reflection.
Constructors Summary
public CallMethodRule(Digester digester, String methodName, int paramCount)
Construct a "call method" rule with the specified method name. The parameter types (if any) default to java.lang.String.

param
digester The associated Digester
param
methodName Method name of the parent method to call
param
paramCount The number of parameters to collect, or zero for a single argument from the body of this element.
deprecated
The digester instance is now set in the {@link Digester#addRule} method. Use {@link #CallMethodRule(String methodName,int paramCount)} instead.


        this(methodName, paramCount);

    
public CallMethodRule(String methodName, int paramCount, Class[] paramTypes)
Construct a "call method" rule with the specified method name and parameter types. If paramCount is set to zero the rule will use the body of this element as the single argument of the method, unless paramTypes is null or empty, in this case the rule will call the specified method with no arguments.

param
methodName Method name of the parent method to call
param
paramCount The number of parameters to collect, or zero for a single argument from the body of ths element
param
paramTypes The Java classes that represent the parameter types of the method arguments (if you wish to use a primitive type, specify the corresonding Java wrapper class instead, such as java.lang.Boolean.TYPE for a boolean parameter)

        this(0, methodName, paramCount, paramTypes);
    
public CallMethodRule(int targetOffset, String methodName, int paramCount, Class[] paramTypes)
Construct a "call method" rule with the specified method name and parameter types. If paramCount is set to zero the rule will use the body of this element as the single argument of the method, unless paramTypes is null or empty, in this case the rule will call the specified method with no arguments.

param
targetOffset location of the target object. Positive numbers are relative to the top of the digester object stack. Negative numbers are relative to the bottom of the stack. Zero implies the top object on the stack.
param
methodName Method name of the parent method to call
param
paramCount The number of parameters to collect, or zero for a single argument from the body of ths element
param
paramTypes The Java classes that represent the parameter types of the method arguments (if you wish to use a primitive type, specify the corresonding Java wrapper class instead, such as java.lang.Boolean.TYPE for a boolean parameter)


        this.targetOffset = targetOffset;
        this.methodName = methodName;
        this.paramCount = paramCount;
        if (paramTypes == null) {
            this.paramTypes = new Class[paramCount];
            for (int i = 0; i < this.paramTypes.length; i++) {
                this.paramTypes[i] = "abc".getClass();
            }
        } else {
            this.paramTypes = new Class[paramTypes.length];
            for (int i = 0; i < this.paramTypes.length; i++) {
                this.paramTypes[i] = paramTypes[i];
            }
        }

    
public CallMethodRule(Digester digester, String methodName, int paramCount, String[] paramTypes)
Construct a "call method" rule with the specified method name.

param
digester The associated Digester
param
methodName Method name of the parent method to call
param
paramCount The number of parameters to collect, or zero for a single argument from the body of ths element
param
paramTypes The Java class names of the arguments (if you wish to use a primitive type, specify the corresonding Java wrapper class instead, such as java.lang.Boolean for a boolean parameter)
deprecated
The digester instance is now set in the {@link Digester#addRule} method. Use {@link #CallMethodRule(String methodName,int paramCount, String [] paramTypes)} instead.


        this(methodName, paramCount, paramTypes);

    
public CallMethodRule(Digester digester, String methodName, int paramCount, Class[] paramTypes)
Construct a "call method" rule with the specified method name.

param
digester The associated Digester
param
methodName Method name of the parent method to call
param
paramCount The number of parameters to collect, or zero for a single argument from the body of ths element
param
paramTypes The Java classes that represent the parameter types of the method arguments (if you wish to use a primitive type, specify the corresonding Java wrapper class instead, such as java.lang.Boolean.TYPE for a boolean parameter)
deprecated
The digester instance is now set in the {@link Digester#addRule} method. Use {@link #CallMethodRule(String methodName,int paramCount, Class [] paramTypes)} instead.


        this(methodName, paramCount, paramTypes);
    
public CallMethodRule(String methodName, int paramCount)
Construct a "call method" rule with the specified method name. The parameter types (if any) default to java.lang.String.

param
methodName Method name of the parent method to call
param
paramCount The number of parameters to collect, or zero for a single argument from the body of this element.

        this(0, methodName, paramCount);
    
public CallMethodRule(int targetOffset, String methodName, int paramCount)
Construct a "call method" rule with the specified method name. The parameter types (if any) default to java.lang.String.

param
targetOffset location of the target object. Positive numbers are relative to the top of the digester object stack. Negative numbers are relative to the bottom of the stack. Zero implies the top object on the stack.
param
methodName Method name of the parent method to call
param
paramCount The number of parameters to collect, or zero for a single argument from the body of this element.


        this.targetOffset = targetOffset;
        this.methodName = methodName;
        this.paramCount = paramCount;        
        if (paramCount == 0) {
            this.paramTypes = new Class[] { String.class };
        } else {
            this.paramTypes = new Class[paramCount];
            for (int i = 0; i < this.paramTypes.length; i++) {
                this.paramTypes[i] = String.class;
            }
        }

    
public CallMethodRule(String methodName)
Construct a "call method" rule with the specified method name. The method should accept no parameters.

param
methodName Method name of the parent method to call

    
        this(0, methodName, 0, (Class[]) null);
    
    
public CallMethodRule(int targetOffset, String methodName)
Construct a "call method" rule with the specified method name. The method should accept no parameters.

param
targetOffset location of the target object. Positive numbers are relative to the top of the digester object stack. Negative numbers are relative to the bottom of the stack. Zero implies the top object on the stack.
param
methodName Method name of the parent method to call

    
        this(targetOffset, methodName, 0, (Class[]) null);
    
    
public CallMethodRule(String methodName, int paramCount, String[] paramTypes)
Construct a "call method" rule with the specified method name and parameter types. If paramCount is set to zero the rule will use the body of this element as the single argument of the method, unless paramTypes is null or empty, in this case the rule will call the specified method with no arguments.

param
methodName Method name of the parent method to call
param
paramCount The number of parameters to collect, or zero for a single argument from the body of ths element
param
paramTypes The Java class names of the arguments (if you wish to use a primitive type, specify the corresonding Java wrapper class instead, such as java.lang.Boolean for a boolean parameter)

        this(0, methodName, paramCount, paramTypes);
    
public CallMethodRule(int targetOffset, String methodName, int paramCount, String[] paramTypes)
Construct a "call method" rule with the specified method name and parameter types. If paramCount is set to zero the rule will use the body of this element as the single argument of the method, unless paramTypes is null or empty, in this case the rule will call the specified method with no arguments.

param
targetOffset location of the target object. Positive numbers are relative to the top of the digester object stack. Negative numbers are relative to the bottom of the stack. Zero implies the top object on the stack.
param
methodName Method name of the parent method to call
param
paramCount The number of parameters to collect, or zero for a single argument from the body of ths element
param
paramTypes The Java class names of the arguments (if you wish to use a primitive type, specify the corresonding Java wrapper class instead, such as java.lang.Boolean for a boolean parameter)


        this.targetOffset = targetOffset;
        this.methodName = methodName;
        this.paramCount = paramCount;
        if (paramTypes == null) {
            this.paramTypes = new Class[paramCount];
            for (int i = 0; i < this.paramTypes.length; i++) {
                this.paramTypes[i] = "abc".getClass();
            }
        } else {
            // copy the parameter class names into an array
            // the classes will be loaded when the digester is set 
            this.paramClassNames = new String[paramTypes.length];
            for (int i = 0; i < this.paramClassNames.length; i++) {
                this.paramClassNames[i] = paramTypes[i];
            }
        }

    
Methods Summary
public voidbegin(org.xml.sax.Attributes attributes)
Process the start of this element.

param
attributes The attribute list for this element


        // Push an array to capture the parameter values if necessary
        if (paramCount > 0) {
            Object parameters[] = new Object[paramCount];
            for (int i = 0; i < parameters.length; i++) {
                parameters[i] = null;
            }
            digester.pushParams(parameters);
        }

    
public voidbody(java.lang.String bodyText)
Process the body text of this element.

param
bodyText The body text of this element


        if (paramCount == 0) {
            this.bodyText = bodyText.trim();
        }

    
public voidend()
Process the end of this element.


        // Retrieve or construct the parameter values array
        Object parameters[] = null;
        if (paramCount > 0) {

            parameters = (Object[]) digester.popParams();
            
            if (digester.log.isTraceEnabled()) {
                for (int i=0,size=parameters.length;i<size;i++) {
                    digester.log.trace("[CallMethodRule](" + i + ")" + parameters[i]) ;
                }
            }
            
            // In the case where the parameter for the method
            // is taken from an attribute, and that attribute
            // isn't actually defined in the source XML file,
            // skip the method call
            if (paramCount == 1 && parameters[0] == null) {
                return;
            }

        } else if (paramTypes != null && paramTypes.length != 0) {

            // In the case where the parameter for the method
            // is taken from the body text, but there is no
            // body text included in the source XML file,
            // skip the method call
            if (bodyText == null) {
                return;
            }

            parameters = new Object[1];
            parameters[0] = bodyText;
            if (paramTypes.length == 0) {
                paramTypes = new Class[1];
                paramTypes[0] = "abc".getClass();
            }

        }

        // Construct the parameter values array we will need
        // We only do the conversion if the param value is a String and
        // the specified paramType is not String. 
        Object paramValues[] = new Object[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++) {
            // convert nulls and convert stringy parameters 
            // for non-stringy param types
            if(
                parameters[i] == null ||
                 (parameters[i] instanceof String && 
                   !String.class.isAssignableFrom(paramTypes[i]))) {
                
                paramValues[i] =
                        IntrospectionUtils.convert((String) parameters[i], paramTypes[i]);
            } else {
                paramValues[i] = parameters[i];
            }
        }

        // Determine the target object for the method call
        Object target;
        if (targetOffset >= 0) {
            target = digester.peek(targetOffset);
        } else {
            target = digester.peek( digester.getCount() + targetOffset );
        }
        
        if (target == null) {
            StringBuffer sb = new StringBuffer();
            sb.append("[CallMethodRule]{");
            sb.append(digester.match);
            sb.append("} Call target is null (");
            sb.append("targetOffset=");
            sb.append(targetOffset);
            sb.append(",stackdepth=");
            sb.append(digester.getCount());
            sb.append(")");
            throw new org.xml.sax.SAXException(sb.toString());
        }
        
        // Invoke the required method on the top object
        if (digester.log.isDebugEnabled()) {
            StringBuffer sb = new StringBuffer("[CallMethodRule]{");
            sb.append(digester.match);
            sb.append("} Call ");
            sb.append(target.getClass().getName());
            sb.append(".");
            sb.append(methodName);
            sb.append("(");
            for (int i = 0; i < paramValues.length; i++) {
                if (i > 0) {
                    sb.append(",");
                }
                if (paramValues[i] == null) {
                    sb.append("null");
                } else {
                    sb.append(paramValues[i].toString());
                }
                sb.append("/");
                if (paramTypes[i] == null) {
                    sb.append("null");
                } else {
                    sb.append(paramTypes[i].getName());
                }
            }
            sb.append(")");
            digester.log.debug(sb.toString());
        }
        Object result = IntrospectionUtils.callMethodN(target, methodName,
                paramValues, paramTypes);   
        processMethodCallResult(result);
    
public voidfinish()
Clean up after parsing is complete.


        bodyText = null;

    
public booleangetUseExactMatch()
Should MethodUtils.invokeExactMethod be used for the reflection.

    
    // --------------------------------------------------------- Public Methods
    
                
       
        return useExactMatch;
    
protected voidprocessMethodCallResult(java.lang.Object result)
Subclasses may override this method to perform additional processing of the invoked method's result.

param
result the Object returned by the method invoked, possibly null

        // do nothing
    
public voidsetDigester(Digester digester)
Set the associated digester. If needed, this class loads the parameter classes from their names.

        // call superclass
        super.setDigester(digester);
        // if necessary, load parameter classes
        if (this.paramClassNames != null) {
            this.paramTypes = new Class[paramClassNames.length];
            for (int i = 0; i < this.paramClassNames.length; i++) {
                try {
                    this.paramTypes[i] =
                            digester.getClassLoader().loadClass(this.paramClassNames[i]);
                } catch (ClassNotFoundException e) {
                    // use the digester log
                    digester.getLogger().error("(CallMethodRule) Cannot load class " + this.paramClassNames[i], e);
                    this.paramTypes[i] = null; // Will cause NPE later
                }
            }
        }
    
public voidsetUseExactMatch(boolean useExactMatch)
Set whether MethodUtils.invokeExactMethod should be used for the reflection.

 
        this.useExactMatch = useExactMatch;
    
public java.lang.StringtoString()
Render a printable version of this Rule.


        StringBuffer sb = new StringBuffer("CallMethodRule[");
        sb.append("methodName=");
        sb.append(methodName);
        sb.append(", paramCount=");
        sb.append(paramCount);
        sb.append(", paramTypes={");
        if (paramTypes != null) {
            for (int i = 0; i < paramTypes.length; i++) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(paramTypes[i].getName());
            }
        }
        sb.append("}");
        sb.append("]");
        return (sb.toString());