MethodExpression (Java EE 5)

Java EE API


javax.el Class MethodExpression

java.lang.Object
  extended by javax.el.Expression
      extended by javax.el.MethodExpression
All Implemented Interfaces:
Serializable

public abstract class MethodExpression
extends Expression

Extends: Expression

引用对象方法的 Expression

可以使用 ExpressionFactory#createMethodExpression 方法解析表达式字符串,并返回封装已解析表达式的 MethodExpression 的具体实例。解析时(而不是计算时)使用 FunctionMapper,所以在计算表达式时不需要它。但是,计算时需要 ELContext

每次调用 #getMethodInfo#invoke 方法时将对表达式求值。使用 ELContext 中的 ELResolver 解析顶级变量,并确定 .[] 操作符的行为。对于这两个方法中的任何一个,使用 ELResolver#getValue 方法解析除最后一个属性以外的所有属性。这提供出现该方法的 base 对象。如果 base 对象为 null,则必定抛出 PropertyNotFoundException。在最后解析时,最后的 property 被强制匹配为 String,它提供要查找的方法的名称。将找到与该名称以及在解析时提供的期望参数匹配的方法,它将被查询或调用(取决于对此 MethodExpression 调用的方法)。

请参阅 Expression javadoc 中有关比较、序列化和不可变性的注释。

英文文档:

An Expression that refers to a method on an object.

The ExpressionFactory.createMethodExpression(javax.el.ELContext, java.lang.String, java.lang.Class, java.lang.Class[]) method can be used to parse an expression string and return a concrete instance of MethodExpression that encapsulates the parsed expression. The FunctionMapper is used at parse time, not evaluation time, so one is not needed to evaluate an expression using this class. However, the ELContext is needed at evaluation time.

The getMethodInfo(javax.el.ELContext) and invoke(javax.el.ELContext, java.lang.Object[]) methods will evaluate the expression each time they are called. The ELResolver in the ELContext is used to resolve the top-level variables and to determine the behavior of the . and [] operators. For any of the two methods, the ELResolver.getValue(javax.el.ELContext, java.lang.Object, java.lang.Object) method is used to resolve all properties up to but excluding the last one. This provides the base object on which the method appears. If the base object is null, a PropertyNotFoundException must be thrown. At the last resolution, the final property is then coerced to a String, which provides the name of the method to be found. A method matching the name and expected parameters provided at parse time is found and it is either queried or invoked (depending on the method called on this MethodExpression).

See the notes about comparison, serialization and immutability in the Expression javadocs.

Since:
JSP 2.1
See Also:
ELResolver, Expression, ExpressionFactory, Serialized Form

Constructor Summary
 
Method Summary
abstract  MethodInfo
abstract  Object
 
Methods inherited from class javax.el.Expression
 
Methods inherited from class java.lang.Object
 

Constructor Detail

public MethodExpression()
英文文档:

MethodExpression

public MethodExpression()
Method Detail

abstract public MethodInfo getMethodInfo(ELContext context)
相对于提供的上下文对表达式求值,并返回有关实际引用方法的信息。
context 此计算的上下文
return 一个 MethodInfo 实例,它包含有关对表达式求值的方法的信息。
ThrowsNullPointerException: 如果 context 为 null
ThrowsPropertyNotFoundException: 如果由于指定变量或属性不存在或不可读导致某个属性解析操作失败。
ThrowsMethodNotFoundException: 如果不存在合适的方法。
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。
英文文档:

getMethodInfo

public abstract MethodInfo getMethodInfo(ELContext context)
Evaluates the expression relative to the provided context, and returns information about the actual referenced method.

Parameters:
context - The context of this evaluation
Returns:
an instance of MethodInfo containing information about the method the expression evaluated to.
Throws:
NullPointerException - if context is null
PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable.
MethodNotFoundException - if no suitable method can be found.
ELException - if an exception was thrown while performing property or variable resolution. The thrown exception must be included as the cause property of this exception, if available.

abstract public Object invoke(ELContext context, Object[] params)
如果字面值 String 被指定为表达式,则返回被强制匹配为方法签名的期望返回类型的字面值 String。如果 expectedReturnType 为 void 或字面值 String 强制匹配为 expectedReturnType 时产生一个错误,则抛出 ELException(请参阅 EL 规范的“1.18 类型转换”一节)。 如果不是字面值 String,则相对于提供的上下文对表达式求值,调用通过提供的参数找到的方法,并返回该方法调用的结果。 如果 isLiteralText() 为 true,则忽略传递给此方法的任何参数。
context 此计算的上下文。
params 传递给方法的参数;如果没有参数,则为 null
return 方法调用的结果(如果该方法具有一个 void 返回类型,则返回 null)。
ThrowsNullPointerException: 如果 context 为 null
ThrowsPropertyNotFoundException: 如果由于指定变量或属性不存在或不可读导致某个属性解析操作失败。
ThrowsMethodNotFoundException: 如果不存在合适的方法。
ThrowsELException: 如果指定了字面值 String 并且 MethodExpression 的 expectedReturnType 为 void,或者字面值 String 强制匹配为 expectedReturnType 时产生一个错误(请参阅“1.18 类型转换”一节)。
ThrowsELException: 如果执行属性或变量解析时抛出异常。抛出的异常必须作为此异常的 cause 属性包含(如果可用)。如果抛出的异常为 InvocationTargetException,则提取它的 cause 并将其传递给 ELException 构造方法。
英文文档:

invoke

public abstract Object invoke(ELContext context,
                              Object[] params)
If a String literal is specified as the expression, returns the String literal coerced to the expected return type of the method signature. An ELException is thrown if expectedReturnType is void or if the coercion of the String literal to the expectedReturnType yields an error (see Section "1.18 Type Conversion" of the EL specification). If not a String literal, evaluates the expression relative to the provided context, invokes the method that was found using the supplied parameters, and returns the result of the method invocation. Any parameters passed to this method is ignored if isLiteralText() is true.

Parameters:
context - The context of this evaluation.
params - The parameters to pass to the method, or null if no parameters.
Returns:
the result of the method invocation (null if the method has a void return type).
Throws:
NullPointerException - if context is null
PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable.
MethodNotFoundException - if no suitable method can be found.
ELException - if a String literal is specified and expectedReturnType of the MethodExpression is void or if the coercion of the String literal to the expectedReturnType yields an error (see Section "1.18 Type Conversion").
ELException - if an exception was thrown while performing property or variable resolution. The thrown exception must be included as the cause property of this exception, if available. If the exception thrown is an InvocationTargetException, extract its cause and pass it to the ELException constructor.


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!