Class Expressions

java.lang.Object
org.ocpsoft.rewrite.cdi.expressions.Expressions

public class Expressions extends Object

Provides various utility methods for working with EL expressions.

This utility can be used through injection:

 @Inject
 Expressions expressions;
 

Alternatively, if you aren't working in a CDI environment, it can be instantiated using the new keyword:

 Expressions expressions = new Expressions(context, expressionFactory);
 
Author:
Pete Muir, Dan Allen, Stuart Douglas
  • Constructor Details

    • Expressions

      @Inject public Expressions(@Composite jakarta.el.ELContext context, @Composite jakarta.el.ExpressionFactory expressionFactory)
      Create a new instance of the Expressions class, providing the ELContext and ExpressionFactory to be used.
      Parameters:
      context - the ELContext against which to operate
      expressionFactory - the ExpressionFactory to use
      Throws:
      IllegalArgumentException - if context is null or expressionFactory is null
  • Method Details

    • getELContext

      public jakarta.el.ELContext getELContext()
      Obtain the ELContext that this instance of Expressions is using.
      Returns:
      the ELContext in use
    • getExpressionFactory

      public jakarta.el.ExpressionFactory getExpressionFactory()
      Obtain the ExpressionFactory that this instance of Expressions is using.
      Returns:
      the ExpressionFactory in use
    • evaluateValueExpression

      public <T> T evaluateValueExpression(String expression, Class<T> expectedType)

      Evaluate a ValueExpression.

      A ValueExpression is created by calling ExpressionFactory.createValueExpression(ELContext, String, Class) and then ValueExpression.getValue(ELContext) is called to obtain the value. For more details on the semantics of EL, refer to the javadoc for these classes and methods.

      Type Parameters:
      T - the type of the evaluated expression
      Parameters:
      expression - the expression to evaluate
      expectedType - the expected type of the evaluated expression
      Returns:
      the result of evaluating the expression
      Throws:
      NullPointerException - if expectedType is null
      jakarta.el.ELException - if there are syntactical errors in the provided expression or if an exception was thrown while performing property or variable resolution. The thrown exception will be included as the cause property of this exception, if available.
      jakarta.el.PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable.
      ClassCastException - if the result cannot be cast to the expected type
      See Also:
      • ExpressionFactory.createValueExpression(ELContext, String, Class)
      • ValueExpression.getValue(ELContext)
    • evaluateValueExpression

      public <T> T evaluateValueExpression(String expression)

      Evaluate a ValueExpression inferring the return type.

      A ValueExpression is created by calling ExpressionFactory.createValueExpression(ELContext, String, Class) and then ValueExpression.getValue(ELContext) is called to obtain the value. For more details on the semantics of EL, refer to the javadoc for these methods.

      Type Parameters:
      T - the type of the evaluated expression
      Parameters:
      expression - expression the expression to evaluate
      Returns:
      the result of evaluating the expression
      Throws:
      jakarta.el.ELException - if there are syntactical errors in the provided expression
      jakarta.el.ELException - if an exception was thrown while performing property or variable resolution. The thrown exception will be included as the cause property of this exception, if available.
      jakarta.el.PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable
      ClassCastException - if the result cannot be cast to T
      See Also:
      • ExpressionFactory.createValueExpression(ELContext, String, Class)
      • ValueExpression.getValue(ELContext)
    • evaluateMethodExpression

      public <T> T evaluateMethodExpression(String expression, Class<T> expectedReturnType, Object[] params, Class<?>[] expectedParamTypes)

      Evaluate a MethodExpression, passing arguments and argument types to the method.

      A MethodExpression is created by calling ExpressionFactory.createMethodExpression(ELContext, String, Class, Class[]) and then MethodExpression.invoke(ELContext, Object[]) is called to obtain the value. For more details on the semantics of EL, refer to the javadoc for these methods.

      Type Parameters:
      T - the type of the evaluated expression
      Parameters:
      expression - expression the expression to evaluate
      expectedReturnType - the expected return type of the evaluated expression
      params - arguments to the method
      expectedParamTypes - of the arguments to the method
      Returns:
      the result of evaluating the expression
      Throws:
      ClassCastException - if the result cannot be cast to expectedReturnType
      jakarta.el.ELException - if there are syntactical errors in the provided expression.
      NullPointerException - if expectedParamTypes is null.
      jakarta.el.PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable.
      jakarta.el.MethodNotFoundException - if no suitable method can be found.
      jakarta.el.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").
      jakarta.el.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.
      See Also:
      • MethodExpression.invoke(ELContext, Object[])
      • ExpressionFactory.createMethodExpression(ELContext, String, Class, Class[])
    • evaluateMethodExpression

      public <T> T evaluateMethodExpression(String expression, Class<T> expectedReturnType)

      Evaluate a MethodExpression with no parameters.

      A MethodExpression is created by calling ExpressionFactory.createMethodExpression(ELContext, String, Class, Class[]) and then MethodExpression.invoke(ELContext, Object[]) is called to obtain the value. For more details on the semantics of EL, refer to the javadoc for these methods.

      Type Parameters:
      T - the type of the evaluated expression
      Parameters:
      expression - expression the expression to evaluate
      expectedReturnType - the expected return type of the evaluated expression
      Returns:
      the result of evaluating the expression
      Throws:
      ClassCastException - if the result cannot be cast to expectedReturnType
      jakarta.el.ELException - if there are syntactical errors in the provided expression.
      NullPointerException - if expectedParamTypes is null.
      jakarta.el.PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable.
      jakarta.el.MethodNotFoundException - if no suitable method can be found.
      jakarta.el.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").
      jakarta.el.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.
      See Also:
      • MethodExpression.invoke(ELContext, Object[])
      • ExpressionFactory.createMethodExpression(ELContext, String, Class, Class[])
    • evaluateMethodExpression

      public <T> T evaluateMethodExpression(String expression)

      Evaluate a MethodExpression with no parameters, inferring the return type.

      A MethodExpression is created by calling ExpressionFactory.createMethodExpression(ELContext, String, Class, Class[]) and then MethodExpression.invoke(ELContext, Object[]) is called to obtain the value. For more details on the semantics of EL, refer to the javadoc for these methods.

      Type Parameters:
      T - the type of the evaluated expression
      Parameters:
      expression - expression the expression to evaluate
      Returns:
      the result of evaluating the expression
      Throws:
      ClassCastException - if the result cannot be cast to T
      jakarta.el.ELException - if there are syntactical errors in the provided expression.
      NullPointerException - if expectedParamTypes is null.
      jakarta.el.PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable.
      jakarta.el.MethodNotFoundException - if no suitable method can be found.
      jakarta.el.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").
      jakarta.el.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.
      See Also:
      • MethodExpression.invoke(ELContext, Object[])
      • ExpressionFactory.createMethodExpression(ELContext, String, Class, Class[])
    • evaluateMethodExpression

      public <T> T evaluateMethodExpression(String expression, Object... params)

      Evaluate a MethodExpression, passing arguments to the method. The types of the arguments are discoverted from the arguments, and the return type is inferred.

      A MethodExpression is created by calling ExpressionFactory.createMethodExpression(ELContext, String, Class, Class[]) and then MethodExpression.invoke(ELContext, Object[]) is called to obtain the value. For more details on the semantics of EL, refer to the javadoc for these methods.

      Type Parameters:
      T - the type of the evaluated expression
      Parameters:
      expression - expression the expression to evaluate
      params - arguments to the method
      Returns:
      the result of evaluating the expression
      Throws:
      ClassCastException - if the result cannot be cast to T
      jakarta.el.ELException - if there are syntactical errors in the provided expression.
      NullPointerException - if expectedParamTypes is null.
      jakarta.el.PropertyNotFoundException - if one of the property resolutions failed because a specified variable or property does not exist or is not readable.
      jakarta.el.MethodNotFoundException - if no suitable method can be found.
      jakarta.el.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").
      jakarta.el.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.
      See Also:
      • MethodExpression.invoke(ELContext, Object[])
      • ExpressionFactory.createMethodExpression(ELContext, String, Class, Class[])
    • toExpression

      public String toExpression(String name)
      Convert's a bean name to an EL expression string.
      Parameters:
      name - the name of the bean to convert
      Returns:
      the expression string