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  
20  package org.apache.myfaces.tobago.facelets;
21  
22  import javax.el.ELContext;
23  import javax.el.ELException;
24  import javax.el.MethodExpression;
25  import javax.el.MethodInfo;
26  import javax.el.MethodNotFoundException;
27  import javax.el.PropertyNotFoundException;
28  import javax.faces.FacesWrapper;
29  import javax.faces.view.facelets.TagAttribute;
30  import java.io.Externalizable;
31  import java.io.IOException;
32  import java.io.ObjectInput;
33  import java.io.ObjectOutput;
34  
35  /*
36   * Was copied from MyFaces-Impl.
37   */
38  
39  /**
40   * Jacob Hookom
41   */
42  public final class TagMethodExpression extends MethodExpression
43      implements Externalizable, FacesWrapper<MethodExpression> {
44  
45    private static final long serialVersionUID = 1L;
46  
47    private String attr;
48    private MethodExpression orig;
49  
50    public TagMethodExpression() {
51      super();
52    }
53  
54    public TagMethodExpression(final TagAttribute attr, final MethodExpression orig) {
55      this.attr = attr.toString();
56      this.orig = orig;
57    }
58  
59    @Override
60    public MethodInfo getMethodInfo(final ELContext context) {
61      try {
62        return this.orig.getMethodInfo(context);
63      } catch (final PropertyNotFoundException pnfe) {
64        throw new PropertyNotFoundException(this.attr + ": " + pnfe.getMessage(), pnfe.getCause());
65      } catch (final MethodNotFoundException mnfe) {
66        throw new MethodNotFoundException(this.attr + ": " + mnfe.getMessage(), mnfe.getCause());
67      } catch (final ELException e) {
68        throw new ELException(this.attr + ": " + e.getMessage(), e.getCause());
69      }
70    }
71  
72    @Override
73    public Object invoke(final ELContext context, final Object[] params) {
74      try {
75        return this.orig.invoke(context, params);
76      } catch (final PropertyNotFoundException pnfe) {
77        throw new PropertyNotFoundException(this.attr + ": " + pnfe.getMessage(), pnfe.getCause());
78      } catch (final MethodNotFoundException mnfe) {
79        throw new MethodNotFoundException(this.attr + ": " + mnfe.getMessage(), mnfe.getCause());
80      } catch (final ELException e) {
81        throw new ELException(this.attr + ": " + e.getMessage(), e.getCause());
82      }
83    }
84  
85    @Override
86    public String getExpressionString() {
87      return this.orig.getExpressionString();
88    }
89  
90    public boolean equals(final Object obj) {
91      return this.orig.equals(obj);
92    }
93  
94    public int hashCode() {
95      return this.orig.hashCode();
96    }
97  
98    @Override
99    public boolean isLiteralText() {
100     return this.orig.isLiteralText();
101   }
102 
103   @Override
104   public void writeExternal(final ObjectOutput out) throws IOException {
105     out.writeObject(this.orig);
106     out.writeUTF(this.attr);
107   }
108 
109   @Override
110   public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
111     this.orig = (MethodExpression) in.readObject();
112     this.attr = in.readUTF();
113   }
114 
115   public String toString() {
116     return this.attr + ": " + this.orig;
117   }
118 
119   @Override
120   public MethodExpression getWrapped() {
121     return this.orig;
122   }
123 }