View Javadoc

1   package org.apache.commons.ognl;
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.commons.ognl.enhance.UnsupportedCompilationException;
23  
24  /**
25   * Base class for boolean expressions.
26   * 
27   * @author jkuhnert
28   */
29  public abstract class BooleanExpression
30      extends ExpressionNode
31      implements NodeType
32  {
33  
34      private static final long serialVersionUID = 8630306635724834872L;
35  
36      protected Class<?> getterClass;
37  
38      public BooleanExpression( int id )
39      {
40          super( id );
41      }
42  
43      public BooleanExpression( OgnlParser p, int id )
44      {
45          super( p, id );
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      public Class<?> getGetterClass()
52      {
53          return getterClass;
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      public Class<?> getSetterClass()
60      {
61          return null;
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public String toGetSourceString( OgnlContext context, Object target )
69      {
70          try
71          {
72  
73              Object value = getValueBody( context, target );
74  
75              if ( value != null && Boolean.class.isAssignableFrom( value.getClass() ) )
76              {
77                  getterClass = Boolean.TYPE;
78              }
79              else if ( value != null )
80              {
81                  getterClass = value.getClass();
82              }
83              else
84              {
85                  getterClass = Boolean.TYPE;
86              }
87  
88              String ret = super.toGetSourceString( context, target );
89  
90              if ( "(false)".equals( ret ) )
91              {
92                  return "false";
93              }
94              else if ( "(true)".equals( ret ) )
95              {
96                  return "true";
97              }
98              return ret;
99  
100         }
101         catch ( NullPointerException e )
102         {
103 
104             // expected to happen in some instances
105             e.printStackTrace();
106 
107             throw new UnsupportedCompilationException( "evaluation resulted in null expression." );
108         }
109         catch ( Throwable t )
110         {
111             throw OgnlOps.castToRuntime( t );
112         }
113     }
114 }