View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.el;
20  
21  import java.io.Externalizable;
22  import java.io.IOException;
23  import java.io.ObjectInput;
24  import java.io.ObjectOutput;
25  
26  import javax.el.ELContext;
27  import javax.el.MethodExpression;
28  import javax.el.MethodInfo;
29  import javax.el.ValueExpression;
30  import javax.faces.FacesWrapper;
31  import javax.faces.context.FacesContext;
32  
33  /**
34   * This MethodExpression contains a ValueExpression which resolves to 
35   * the "real" MethodExpression that should be invoked. This is needed 
36   * when the MethodExpression is on the parent composite component attribute map.
37   * See FaceletViewDeclarationLanguage.retargetMethodExpressions() for details.
38   * 
39   * @author Jakob Korherr (latest modification by $Author: lu4242 $)
40   * @version $Revision: 1152505 $ $Date: 2011-07-30 15:11:19 -0500 (Sat, 30 Jul 2011) $
41   */
42  public class ValueExpressionMethodExpression extends MethodExpression 
43      implements FacesWrapper<ValueExpression>, Externalizable
44  {
45      
46      private static final long serialVersionUID = -2847633717581167765L;
47      
48      private ValueExpression valueExpression;
49      
50      public ValueExpressionMethodExpression()
51      {
52      }
53      
54      public ValueExpressionMethodExpression(ValueExpression valueExpression)
55      {
56          this.valueExpression = valueExpression;   
57      }
58  
59      @Override
60      public MethodInfo getMethodInfo(ELContext context)
61      {
62          return getMethodExpression(context).getMethodInfo(context);
63      }
64  
65      @Override
66      public Object invoke(ELContext context, Object[] params)
67      {
68          return getMethodExpression(context).invoke(context, params);
69      }
70  
71      @Override
72      public boolean equals(Object obj)
73      {
74          return getMethodExpression().equals(obj);
75      }
76  
77      @Override
78      public String getExpressionString()
79      {
80          //getMethodExpression().getExpressionString()
81          return valueExpression.getExpressionString();
82      }
83  
84      @Override
85      public int hashCode()
86      {
87          return getMethodExpression().hashCode();
88      }
89  
90      @Override
91      public boolean isLiteralText()
92      {
93          return getMethodExpression().isLiteralText();
94      }
95      
96      private MethodExpression getMethodExpression()
97      {
98          return getMethodExpression(FacesContext.getCurrentInstance().getELContext());
99      }
100     
101     private MethodExpression getMethodExpression(ELContext context)
102     {
103         Object meOrVe = valueExpression.getValue(context);
104         if (meOrVe instanceof MethodExpression)
105         {
106             return (MethodExpression) meOrVe;
107         }
108         else if (meOrVe instanceof ValueExpression)
109         {
110             while (meOrVe != null && meOrVe instanceof ValueExpression)
111             {
112                 meOrVe = ((ValueExpression)meOrVe).getValue(context);
113             }
114             return (MethodExpression) meOrVe;
115         }
116         else
117         {
118             return null;
119         }
120     }
121 
122     public ValueExpression getWrapped()
123     {
124         return valueExpression;
125     }
126     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
127     {
128         this.valueExpression = (ValueExpression) in.readObject();
129     }
130 
131     public void writeExternal(ObjectOutput out) throws IOException
132     {
133         out.writeObject(this.valueExpression);
134     }
135 }