View Javadoc

1   package org.apache.velocity.runtime.parser.node;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import org.apache.velocity.runtime.RuntimeLogger;
23  import org.apache.velocity.runtime.log.Log;
24  import org.apache.velocity.runtime.log.RuntimeLoggerLog;
25  import org.apache.velocity.util.introspection.Introspector;
26  
27  /**
28   *  Handles discovery and valuation of a
29   *  boolean object property, of the
30   *  form public boolean is<property> when executed.
31   *
32   *  We do this separately as to preserve the current
33   *  quasi-broken semantics of get<as is property>
34   *  get< flip 1st char> get("property") and now followed
35   *  by is<Property>
36   *
37   *  @author <a href="geirm@apache.org">Geir Magnusson Jr.</a>
38   *  @version $Id: BooleanPropertyExecutor.java 463298 2006-10-12 16:10:32Z henning $
39   */
40  public class BooleanPropertyExecutor extends PropertyExecutor
41  {
42      /**
43       * @param log
44       * @param introspector
45       * @param clazz
46       * @param property
47       */
48      public BooleanPropertyExecutor(final Log log, final Introspector introspector,
49              final Class clazz, final String property)
50      {
51          super(log, introspector, clazz, property);
52      }
53  
54      /**
55       * @param rlog
56       * @param introspector
57       * @param clazz
58       * @param property
59       * @deprecated RuntimeLogger is deprecated. Use the other constructor.
60       */
61      public BooleanPropertyExecutor(final RuntimeLogger rlog, final Introspector introspector,
62              final Class clazz, final String property)
63      {
64          super(new RuntimeLoggerLog(rlog), introspector, clazz, property);
65      }
66  
67      protected void discover(final Class clazz, final String property)
68      {
69          try
70          {
71              Object [] params = {};
72  
73              StringBuffer sb = new StringBuffer("is");
74              sb.append(property);
75  
76              setMethod(getIntrospector().getMethod(clazz, sb.toString(), params));
77  
78              if (!isAlive())
79              {
80                  /*
81                   *  now the convenience, flip the 1st character
82                   */
83  
84                  char c = sb.charAt(2);
85  
86                  if (Character.isLowerCase(c))
87                  {
88                      sb.setCharAt(2, Character.toUpperCase(c));
89                  }
90                  else
91                  {
92                      sb.setCharAt(2, Character.toLowerCase(c));
93                  }
94  
95                  setMethod(getIntrospector().getMethod(clazz, sb.toString(), params));
96              }
97  
98              if (isAlive())
99              {
100                 if (getMethod().getReturnType() != Boolean.TYPE)
101                 {
102                     setMethod(null); // That case is rare but not unknown
103                 }
104             }
105         }
106         /**
107          * pass through application level runtime exceptions
108          */
109         catch( RuntimeException e )
110         {
111             throw e;
112         }
113         catch(Exception e)
114         {
115             log.error("While looking for boolean property getter for '" + property + "':", e);
116         }
117     }
118 }