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  import javax.el.ELContext;
26  import javax.el.ELException;
27  import javax.el.MethodExpression;
28  import javax.el.MethodInfo;
29  import javax.el.MethodNotFoundException;
30  import javax.el.PropertyNotFoundException;
31  
32  /**
33   *
34   * @author Leonardo Uribe
35   */
36  public class MethodExpressionMethodExpression extends MethodExpression implements Externalizable
37  {
38      
39      private static final Object[] EMPTY_PARAMS = new Object[0];
40      
41      private MethodExpression methodExpressionOneArg;
42      private MethodExpression methodExpressionZeroArg;
43  
44      public MethodExpressionMethodExpression()
45      {
46      }
47      
48      public MethodExpressionMethodExpression(MethodExpression methodExpressionOneArg,
49                                              MethodExpression methodExpressionZeroArg)
50      {
51          this.methodExpressionOneArg = methodExpressionOneArg;
52          this.methodExpressionZeroArg = methodExpressionZeroArg;
53      }
54  
55      @Override
56      public MethodInfo getMethodInfo(ELContext context)
57              throws NullPointerException, PropertyNotFoundException, MethodNotFoundException, ELException
58      {
59          try
60          {
61              // call to the one argument MethodExpression
62              return methodExpressionOneArg.getMethodInfo(context);
63          }
64          catch (MethodNotFoundException mnfe)
65          {
66              // call to the zero argument MethodExpression
67              return methodExpressionZeroArg.getMethodInfo(context);
68          }
69      }
70  
71      @Override
72      public Object invoke(ELContext context, Object[] params)
73              throws NullPointerException, PropertyNotFoundException, MethodNotFoundException, ELException
74      {
75          try
76          {
77              // call to the one argument MethodExpression
78              return methodExpressionOneArg.invoke(context, params);
79          }
80          catch (MethodNotFoundException mnfe)
81          {
82              // call to the zero argument MethodExpression
83              return methodExpressionZeroArg.invoke(context, EMPTY_PARAMS);
84          }
85      }
86  
87      @Override
88      public String getExpressionString()
89      {
90          try
91          {
92              // call to the one argument MethodExpression
93              return methodExpressionOneArg.getExpressionString();
94          }
95          catch (MethodNotFoundException mnfe)
96          {
97              // call to the zero argument MethodExpression
98              return methodExpressionZeroArg.getExpressionString();
99          }
100     }
101 
102     @Override
103     public boolean isLiteralText()
104     {
105         return false;
106     }
107 
108     public void writeExternal(ObjectOutput out) throws IOException
109     {
110         out.writeObject(methodExpressionOneArg);
111         out.writeObject(methodExpressionZeroArg);
112     }
113 
114     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
115     {
116         methodExpressionOneArg = (MethodExpression) in.readObject();
117         methodExpressionZeroArg = (MethodExpression) in.readObject();
118     }
119 
120     @Override
121     public boolean equals(Object obj)
122     {
123         if (obj instanceof MethodExpressionMethodExpression)
124         {
125             MethodExpressionMethodExpression me = (MethodExpressionMethodExpression) obj;
126             return methodExpressionOneArg.equals(me.methodExpressionOneArg) &&
127                    methodExpressionZeroArg.equals(me.methodExpressionZeroArg);
128         }
129         return false;
130     }
131 
132     @Override
133     public int hashCode()
134     {
135         int hash = 31;
136         hash = 19 * hash + (this.methodExpressionOneArg != null ? this.methodExpressionOneArg.hashCode() : 0);
137         hash = 19 * hash + (this.methodExpressionZeroArg != null ? this.methodExpressionZeroArg.hashCode() : 0);
138         return hash;
139     }
140 }