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.exception.VelocityException;
23  import org.apache.velocity.runtime.RuntimeLogger;
24  import org.apache.velocity.runtime.log.Log;
25  import org.apache.velocity.runtime.log.RuntimeLoggerLog;
26  import org.apache.velocity.util.introspection.Introspector;
27  
28  /**
29   *  Handles discovery and valuation of a
30   *  boolean object property, of the
31   *  form public boolean is<property> when executed.
32   *
33   *  We do this separately as to preserve the current
34   *  quasi-broken semantics of get<as is property>
35   *  get< flip 1st char> get("property") and now followed
36   *  by is<Property>
37   *
38   *  @author <a href="geirm@apache.org">Geir Magnusson Jr.</a>
39   *  @version $Id: BooleanPropertyExecutor.java 687502 2008-08-20 23:19:52Z nbubna $
40   */
41  public class BooleanPropertyExecutor extends PropertyExecutor
42  {
43      /**
44       * @param log
45       * @param introspector
46       * @param clazz
47       * @param property
48       * @since 1.5
49       */
50      public BooleanPropertyExecutor(final Log log, final Introspector introspector,
51              final Class clazz, final String property)
52      {
53          super(log, introspector, clazz, property);
54      }
55  
56      /**
57       * @param rlog
58       * @param introspector
59       * @param clazz
60       * @param property
61       * @deprecated RuntimeLogger is deprecated. Use the other constructor.
62       */
63      public BooleanPropertyExecutor(final RuntimeLogger rlog, final Introspector introspector,
64              final Class clazz, final String property)
65      {
66          super(new RuntimeLoggerLog(rlog), introspector, clazz, property);
67      }
68  
69      protected void discover(final Class clazz, final String property)
70      {
71          try
72          {
73              Object [] params = {};
74  
75              StringBuffer sb = new StringBuffer("is");
76              sb.append(property);
77  
78              setMethod(getIntrospector().getMethod(clazz, sb.toString(), params));
79  
80              if (!isAlive())
81              {
82                  /*
83                   *  now the convenience, flip the 1st character
84                   */
85  
86                  char c = sb.charAt(2);
87  
88                  if (Character.isLowerCase(c))
89                  {
90                      sb.setCharAt(2, Character.toUpperCase(c));
91                  }
92                  else
93                  {
94                      sb.setCharAt(2, Character.toLowerCase(c));
95                  }
96  
97                  setMethod(getIntrospector().getMethod(clazz, sb.toString(), params));
98              }
99              
100             if (isAlive())
101             {
102                 if( getMethod().getReturnType() != Boolean.TYPE &&
103                     getMethod().getReturnType() != Boolean.class )
104                 {
105                     setMethod(null);
106                 }
107             }
108         }
109         /**
110          * pass through application level runtime exceptions
111          */
112         catch( RuntimeException e )
113         {
114             throw e;
115         }
116         catch(Exception e)
117         {
118             String msg = "Exception while looking for boolean property getter for '" + property;
119             log.error(msg, e);
120             throw new VelocityException(msg, e);
121         }
122     }
123 }