📄 callmethodrule.java
字号:
/* $Id: CallMethodRule.java 299765 2004-08-31 23:52:52Z yoavs $
*
* Copyright 2001-2004 The Apache Software Foundation.
*
* 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 org.apache.tomcat.util.digester;
import org.apache.tomcat.util.IntrospectionUtils;
import org.xml.sax.Attributes;
/**
* <p>Rule implementation that calls a method on an object on the stack
* (normally the top/parent object), passing arguments collected from
* subsequent <code>CallParamRule</code> rules or from the body of this
* element. </p>
*
* <p>By using {@link #CallMethodRule(String methodName)}
* a method call can be made to a method which accepts no
* arguments.</p>
*
* <p>Incompatible method parameter types are converted
* using <code>org.apache.commons.beanutils.ConvertUtils</code>.
* </p>
*
* <p>This rule now uses
* <a href="http://jakarta.apache.org/commons/beanutils/apidocs/org/apache/commons/beanutils/MethodUtils.html">
* org.apache.commons.beanutils.MethodUtils#invokeMethod
* </a> 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 <code>UseExactMatch</code> to true reverts to the use of this
* method.</p>
*
* <p>Note that the target method is invoked when the <i>end</i> of
* the tag the CallMethodRule fired on is encountered, <i>not</i> 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. </p>
*
* <p>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).</p>
*/
public class CallMethodRule extends Rule {
// ----------------------------------------------------------- Constructors
/**
* 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.
*/
public CallMethodRule(Digester digester, String methodName,
int paramCount) {
this(methodName, paramCount);
}
/**
* 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 <code>java.lang.Boolean</code>
* for a <code>boolean</code> parameter)
*
* @deprecated The digester instance is now set in the {@link Digester#addRule} method.
* Use {@link #CallMethodRule(String methodName,int paramCount, String [] paramTypes)} instead.
*/
public CallMethodRule(Digester digester, String methodName,
int paramCount, String paramTypes[]) {
this(methodName, paramCount, 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 <code>java.lang.Boolean.TYPE</code>
* for a <code>boolean</code> parameter)
*
* @deprecated The digester instance is now set in the {@link Digester#addRule} method.
* Use {@link #CallMethodRule(String methodName,int paramCount, Class [] paramTypes)} instead.
*/
public CallMethodRule(Digester digester, String methodName,
int paramCount, Class paramTypes[]) {
this(methodName, paramCount, paramTypes);
}
/**
* 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.
*/
public CallMethodRule(String methodName,
int paramCount) {
this(0, methodName, 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.
*/
public CallMethodRule(int targetOffset,
String methodName,
int paramCount) {
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;
}
}
}
/**
* 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
*/
public CallMethodRule(String methodName) {
this(0, methodName, 0, (Class[]) null);
}
/**
* 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
*/
public CallMethodRule(int targetOffset, String methodName) {
this(targetOffset, methodName, 0, (Class[]) null);
}
/**
* Construct a "call method" rule with the specified method name and
* parameter types. If <code>paramCount</code> is set to zero the rule
* will use the body of this element as the single argument of the
* method, unless <code>paramTypes</code> 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 <code>java.lang.Boolean</code>
* for a <code>boolean</code> parameter)
*/
public CallMethodRule(
String methodName,
int paramCount,
String paramTypes[]) {
this(0, methodName, paramCount, paramTypes);
}
/**
* Construct a "call method" rule with the specified method name and
* parameter types. If <code>paramCount</code> is set to zero the rule
* will use the body of this element as the single argument of the
* method, unless <code>paramTypes</code> 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 <code>java.lang.Boolean</code>
* for a <code>boolean</code> parameter)
*/
public CallMethodRule( int targetOffset,
String methodName,
int paramCount,
String paramTypes[]) {
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];
}
}
}
/**
* Construct a "call method" rule with the specified method name and
* parameter types. If <code>paramCount</code> is set to zero the rule
* will use the body of this element as the single argument of the
* method, unless <code>paramTypes</code> 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 <code>java.lang.Boolean.TYPE</code>
* for a <code>boolean</code> parameter)
*/
public CallMethodRule(
String methodName,
int paramCount,
Class paramTypes[]) {
this(0, methodName, paramCount, paramTypes);
}
/**
* Construct a "call method" rule with the specified method name and
* parameter types. If <code>paramCount</code> is set to zero the rule
* will use the body of this element as the single argument of the
* method, unless <code>paramTypes</code> 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -