Java 类javax.el.MethodExpression 实例源码

项目:apache-tomcat-7.0.73-with-comment    文件:TestMethodExpressionImpl.java   
@Test
public void testIsParametersProvided() {
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    ValueExpression var =
        factory.createValueExpression(beanB, TesterBeanB.class);
    context.getVariableMapper().setVariable("beanB", var);

    MethodExpression me1 = factory.createMethodExpression(
            context, "${beanB.getName}", String.class, new Class<?>[] {});
    MethodExpression me2 = factory.createMethodExpression(
            context, "${beanB.sayHello('JUnit')}", String.class,
            new Class<?>[] { String.class });

    assertFalse(me1.isParmetersProvided());
    assertTrue(me2.isParmetersProvided());
}
项目:tomcat7    文件:ExpressionBuilder.java   
public MethodExpression createMethodExpression(Class<?> expectedReturnType,
        Class<?>[] expectedParamTypes) throws ELException {
    Node n = this.build();
    if (!n.isParametersProvided() && expectedParamTypes == null) {
        throw new NullPointerException(MessageFactory
                .get("error.method.nullParms"));
    }
    if (n instanceof AstValue || n instanceof AstIdentifier) {
        return new MethodExpressionImpl(expression, n, this.fnMapper,
                this.varMapper, expectedReturnType, expectedParamTypes);
    } else if (n instanceof AstLiteralExpression) {
        return new MethodExpressionLiteral(expression, expectedReturnType,
                expectedParamTypes);
    } else {
        throw new ELException("Not a Valid Method Expression: "
                + expression);
    }
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testIsParametersProvided() {
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    ValueExpression var =
        factory.createValueExpression(beanB, TesterBeanB.class);
    context.getVariableMapper().setVariable("beanB", var);

    MethodExpression me1 = factory.createMethodExpression(
            context, "${beanB.getName}", String.class, new Class<?>[] {});
    MethodExpression me2 = factory.createMethodExpression(
            context, "${beanB.sayHello('JUnit')}", String.class,
            new Class<?>[] { String.class });

    assertFalse(me1.isParmetersProvided());
    assertTrue(me2.isParmetersProvided());
}
项目:apache-tomcat-7.0.73-with-comment    文件:TestMethodExpressionImpl.java   
@Test
public void testBug53792a() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    me = factory.createMethodExpression(context,
            "${beanB.setName('" + BUG53792 + "')}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);

    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.getBean().name}", java.lang.String.class);
    String actual = (String) ve.getValue(context);
    assertEquals(BUG53792, actual);
}
项目:lams    文件:AstIdentifier.java   
private final Object invokeTarget(EvaluationContext ctx, Object target,
        Object[] paramValues) throws ELException {
    if (target instanceof MethodExpression) {
        MethodExpression me = (MethodExpression) target;
        return me.invoke(ctx.getELContext(), paramValues);
    } else if (target == null) {
        throw new MethodNotFoundException("Identity '" + this.image
                + "' was null and was unable to invoke");
    } else {
        throw new ELException(
                "Identity '"
                        + this.image
                        + "' does not reference a MethodExpression instance, returned type: "
                        + target.getClass().getName());
    }
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithVarArgsAAAB() throws Exception {
    MethodExpression me7 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAAA,beanB,beanB)}", null , null);
    Exception e = null;
    try {
        me7.invoke(context, null);
    } catch (Exception e1) {
        e = e1;
    }
    // Expected to fail
    assertNotNull(e);
}
项目:apache-tomcat-7.0.73-with-comment    文件:TestMethodExpressionImpl.java   
@Test
public void testBug53792b() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    me = factory.createMethodExpression(context,
            "${beanB.setName('" + BUG53792 + "')}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);

    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.getBean().name.length()}", java.lang.Integer.class);
    Integer actual = (Integer) ve.getValue(context);
    assertEquals(Integer.valueOf(BUG53792.length()), actual);
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testBug53792b() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    me = factory.createMethodExpression(context,
            "${beanB.setName('" + BUG53792 + "')}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);

    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.getBean().name.length()}", java.lang.Integer.class);
    Integer actual = (Integer) ve.getValue(context);
    assertEquals(Integer.valueOf(BUG53792.length()), actual);
}
项目:myfaces-trinidad    文件:ImmutableItemNode.java   
public void actionListener(ActionEvent event)
{
  String value = _actionListener;
  if (value != null)
  {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExpressionFactory expressionFactory =
        facesContext.getApplication().getExpressionFactory();
    ELContext context = facesContext.getELContext();

    MethodExpression methodExpression =
        expressionFactory.createMethodExpression(context, value, Void.TYPE,
            new Class<?>[]
            { ActionEvent.class });
    methodExpression.invoke(context, new Object[]
    { event });
  }

}
项目:myfaces-trinidad    文件:ImmutableItemNode.java   
public String doAction()
{
  String value = _action;

  if (value != null)
  {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExpressionFactory expressionFactory =
        facesContext.getApplication().getExpressionFactory();
    ELContext context = facesContext.getELContext();
    MethodExpression methodExpression =
        expressionFactory.createMethodExpression(context, value,
            String.class, new Class<?>[]
            {});
    value = (String) methodExpression.invoke(context, null);
  }

  // Post me as the selected Node for the request
  postSelectedNode(this);

  return value;
}
项目:myfaces-trinidad    文件:ItemNode.java   
public void actionListener(ActionEvent event)
{
  String value = _actionListener;
  if (value != null)
  {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExpressionFactory expressionFactory =
        facesContext.getApplication().getExpressionFactory();
    ELContext context = facesContext.getELContext();

    MethodExpression methodExpression =
        expressionFactory.createMethodExpression(context, value, Void.TYPE,
            new Class<?>[]
            { ActionEvent.class });
    methodExpression.invoke(context, new Object[]{ event });
  }

}
项目:myfaces-trinidad    文件:ImmutableItemNode.java   
public String doAction()
{
  String value = _action;

  if (value != null)
  {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExpressionFactory expressionFactory =
        facesContext.getApplication().getExpressionFactory();
    ELContext context = facesContext.getELContext();
    MethodExpression methodExpression =
        expressionFactory.createMethodExpression(context, value,
            String.class, new Class<?>[]
            {});
    value = (String) methodExpression.invoke(context, null);
  }

  // Post me as the selected Node for the request
  postSelectedNode(this);

  return value;
}
项目:myfaces-trinidad    文件:ItemNode.java   
public void actionListener(ActionEvent event)
{
  String value = _actionListener;
  if (value != null)
  {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExpressionFactory expressionFactory =
        facesContext.getApplication().getExpressionFactory();
    ELContext context = facesContext.getELContext();

    MethodExpression methodExpression =
        expressionFactory.createMethodExpression(context, value, Void.TYPE,
            new Class<?>[]
            { ActionEvent.class });
    methodExpression.invoke(context, new Object[]{ event });
  }

}
项目:myfaces-trinidad    文件:UIXComponentBase.java   
/**
 * Broadcast an event to a MethodExpression.
 * This can be used to support MethodBindings such as the "actionListener"
 * binding on ActionSource components:
 * &lt;tr:commandButton actionListener="#{mybean.myActionListener}">
 */
protected final void broadcastToMethodExpression(
  FacesEvent event,
  MethodExpression method) throws AbortProcessingException
{
  if (method != null)
  {
    try
    {
      FacesContext context = getFacesContext();
      method.invoke(context.getELContext(), new Object[] { event });
    }
    catch (ELException ee)
    {
      Throwable t = ee.getCause();
      // Unwrap AbortProcessingExceptions
      if (t instanceof AbortProcessingException)
        throw ((AbortProcessingException) t);
      throw ee;
    }
  }
}
项目:myfaces-trinidad    文件:HierarchyUtils.java   
static void __handleBroadcast(
  UIXHierarchy  comp,
  FacesEvent    event,
  RowKeySet     state,
  MethodExpression method) throws AbortProcessingException
{
  // Notify the specified disclosure listener method (if any)
  if (event instanceof RowDisclosureEvent)
  {
    RowDisclosureEvent dEvent = (RowDisclosureEvent) event;
    state.removeAll(dEvent.getRemovedSet());
    state.addAll(dEvent.getAddedSet());
    //pu: Implicitly record a Change for 'disclosedRowKeys' attribute
    comp.addAttributeChange("disclosedRowKeys", state);
    comp.broadcastToMethodExpression(event, method);
  }
}
项目:apache-tomcat-7.0.73-with-comment    文件:ExpressionFactoryImpl.java   
@Override
public MethodExpression createMethodExpression(ELContext context,
        String expression, Class<?> expectedReturnType,
        Class<?>[] expectedParamTypes) {
    ExpressionBuilder builder = new ExpressionBuilder(expression, context);
    return builder.createMethodExpression(expectedReturnType,
            expectedParamTypes);
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvoke() {
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("B");

    context.getVariableMapper().setVariable("beanB",
            factory.createValueExpression(beanB, TesterBeanB.class));

    MethodExpression me1 = factory.createMethodExpression(
            context, "${beanB.getName}", String.class, new Class<?>[] {});
    MethodExpression me2 = factory.createMethodExpression(
            context, "${beanB.sayHello('JUnit')}", String.class,
            new Class<?>[] { String.class });
    MethodExpression me3 = factory.createMethodExpression(
            context, "${beanB.sayHello}", String.class,
            new Class<?>[] { String.class });

    assertEquals("B", me1.invoke(context, null));
    assertEquals("Hello JUnit from B", me2.invoke(context, null));
    assertEquals("Hello JUnit from B",
            me2.invoke(context, new Object[] { "JUnit2" }));
    assertEquals("Hello JUnit2 from B",
            me3.invoke(context, new Object[] { "JUnit2" }));
    assertEquals("Hello JUnit from B",
            me2.invoke(context, new Object[] { null }));
    assertEquals("Hello  from B",
            me3.invoke(context, new Object[] { null }));
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithSuper() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanBB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    ValueExpression ve = factory.createValueExpression(context,
            "${beanA.bean.name}", String.class);
    Object r = ve.getValue(context);
    assertEquals("BB", r);
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithSuperABNoReturnTypeNoParamTypes() {
    MethodExpression me2 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanB)}", null , null);
    Object r2 = me2.invoke(context, null);
    assertEquals("AB: Hello A from B", r2.toString());
}
项目:apache-tomcat-7.0.73-with-comment    文件:AstIdentifier.java   
private final MethodExpression getMethodExpression(EvaluationContext ctx)
        throws ELException {
    Object obj = null;

    // case A: ValueExpression exists, getValue which must
    // be a MethodExpression
    VariableMapper varMapper = ctx.getVariableMapper();
    ValueExpression ve = null;
    if (varMapper != null) {
        ve = varMapper.resolveVariable(this.image);
        if (ve != null) {
            obj = ve.getValue(ctx);
        }
    }

    // case B: evaluate the identity against the ELResolver, again, must be
    // a MethodExpression to be able to invoke
    if (ve == null) {
        ctx.setPropertyResolved(false);
        obj = ctx.getELResolver().getValue(ctx, null, this.image);
    }

    // finally provide helpful hints
    if (obj instanceof MethodExpression) {
        return (MethodExpression) obj;
    } else if (obj == null) {
        throw new MethodNotFoundException("Identity '" + this.image
                + "' was null and was unable to invoke");
    } else {
        throw new ELException(
                "Identity '"
                        + this.image
                        + "' does not reference a MethodExpression instance, returned type: "
                        + obj.getClass().getName());
    }
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithSuperABNoReturnTypeParamTypes() {
    MethodExpression me4 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanB)}", null ,
            new Class<?>[] {TesterBeanA.class, TesterBeanB.class});
    Object r4 = me4.invoke(context, null);
    assertEquals("AB: Hello A from B", r4.toString());
}
项目:apache-tomcat-7.0.73-with-comment    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithVarArgsAABBB() throws Exception {
    MethodExpression me6 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAA,beanBBB,beanBBB)}", null , null);
    Object r6 = me6.invoke(context, null);
    assertEquals("ABB[]: Hello AA from BBB, BBB", r6.toString());
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithSuperABB() {
    MethodExpression me6 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanBB)}", null , null);
    Object r6 = me6.invoke(context, null);
    assertEquals("ABB: Hello A from BB", r6.toString());
}
项目:lazycat    文件:AstIdentifier.java   
private final MethodExpression getMethodExpression(EvaluationContext ctx) throws ELException {
    Object obj = null;

    // case A: ValueExpression exists, getValue which must
    // be a MethodExpression
    VariableMapper varMapper = ctx.getVariableMapper();
    ValueExpression ve = null;
    if (varMapper != null) {
        ve = varMapper.resolveVariable(this.image);
        if (ve != null) {
            obj = ve.getValue(ctx);
        }
    }

    // case B: evaluate the identity against the ELResolver, again, must be
    // a MethodExpression to be able to invoke
    if (ve == null) {
        ctx.setPropertyResolved(false);
        obj = ctx.getELResolver().getValue(ctx, null, this.image);
    }

    // finally provide helpful hints
    if (obj instanceof MethodExpression) {
        return (MethodExpression) obj;
    } else if (obj == null) {
        throw new MethodNotFoundException("Identity '" + this.image + "' was null and was unable to invoke");
    } else {
        throw new ELException("Identity '" + this.image
                + "' does not reference a MethodExpression instance, returned type: " + obj.getClass().getName());
    }
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithSuperAABBB() {
    // The Java compiler reports this as ambiguous. Using the parameter that
    // matches exactly seems reasonable to limit the scope of the method
    // search so the EL will find a match.
    MethodExpression me10 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAA,beanBBB)}", null , null);
    Object r10 = me10.invoke(context, null);
    assertEquals("AAB: Hello AA from BBB", r10.toString());
}
项目:apache-tomcat-7.0.73-with-comment    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithSuper() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanBB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    ValueExpression ve = factory.createValueExpression(context,
            "${beanA.bean.name}", String.class);
    Object r = ve.getValue(context);
    assertEquals("BB", r);
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithSuperAAABB() {
    // The Java compiler reports this as ambiguous. Using the parameter that
    // matches exactly seems reasonable to limit the scope of the method
    // search so the EL will find a match.
    MethodExpression me12 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAAA,beanBB)}", null , null);
    Object r12 = me12.invoke(context, null);
    assertEquals("ABB: Hello AAA from BB", r12.toString());
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithSuperAAABBB() {
    MethodExpression me13 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAAA,beanBBB)}", null , null);
    Exception e = null;
    try {
        me13.invoke(context, null);
    } catch (Exception e1) {
        e = e1;
    }
    // Expected to fail
    assertNotNull(e);
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithVarArgsAB() throws Exception {
    MethodExpression me1 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanB,beanB)}", null , null);
    Exception e = null;
    try {
        me1.invoke(context, null);
    } catch (Exception e1) {
        e = e1;
    }
    // Expected to fail
    assertNotNull(e);
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithVarArgsABB() throws Exception {
    MethodExpression me2 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanBB,beanBB)}", null , null);
    Object r2 = me2.invoke(context, null);
    assertEquals("ABB[]: Hello A from BB, BB", r2.toString());
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithVarArgsABBB() throws Exception {
    MethodExpression me3 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanBBB,beanBBB)}", null , null);
    Object r3 = me3.invoke(context, null);
    assertEquals("ABB[]: Hello A from BBB, BBB", r3.toString());
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithVarArgsAAB() throws Exception {
    MethodExpression me4 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAA,beanB,beanB)}", null , null);
    Exception e = null;
    try {
        me4.invoke(context, null);
    } catch (Exception e1) {
        e = e1;
    }
    // Expected to fail
    assertNotNull(e);
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithVarArgsAABB() throws Exception {
    MethodExpression me5 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAA,beanBB,beanBB)}", null , null);
    Object r5 = me5.invoke(context, null);
    assertEquals("ABB[]: Hello AA from BB, BB", r5.toString());
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithVarArgsAABBB() throws Exception {
    MethodExpression me6 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAA,beanBBB,beanBBB)}", null , null);
    Object r6 = me6.invoke(context, null);
    assertEquals("ABB[]: Hello AA from BBB, BBB", r6.toString());
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithVarArgsAAABBB() throws Exception {
    MethodExpression me9 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAAA,beanBBB,beanBBB)}", null , null);
    Object r9 = me9.invoke(context, null);
    assertEquals("ABB[]: Hello AAA from BBB, BBB", r9.toString());
}
项目:apache-tomcat-7.0.73-with-comment    文件:TestMethodExpressionImpl.java   
@Test
public void testBug50449a() throws Exception {
    MethodExpression me1 = factory.createMethodExpression(context,
            "${beanB.sayHello()}", null, null);
    String actual = (String) me1.invoke(context, null);
    assertEquals("Hello from B", actual);
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testBugPrimitives() throws Exception {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setValLong(5)}", null, null);
    me.invoke(context, null);
    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.valLong}", java.lang.String.class);
    assertEquals("5", ve.getValue(context));
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testBug50449a() throws Exception {
    MethodExpression me1 = factory.createMethodExpression(context,
            "${beanB.sayHello()}", null, null);
    String actual = (String) me1.invoke(context, null);
    assertEquals("Hello from B", actual);
}
项目:apache-tomcat-7.0.73-with-comment    文件:TestMethodExpressionImpl.java   
@Test
public void testInvokeWithSuperAAABBB() {
    MethodExpression me13 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAAA,beanBBB)}", null , null);
    Exception e = null;
    try {
        me13.invoke(context, null);
    } catch (Exception e1) {
        e = e1;
    }
    // Expected to fail
    assertNotNull(e);
}
项目:tomcat7    文件:TestMethodExpressionImpl.java   
@Test
public void testBug52445a() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanBB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);

    MethodExpression me1 = factory.createMethodExpression(context,
            "${beanA.bean.sayHello()}", null, null);
    String actual = (String) me1.invoke(context, null);
    assertEquals("Hello from BB", actual);
}