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 types that compare values.
26   */
27  public abstract class ComparisonExpression
28      extends BooleanExpression
29  {
30  
31      private static final long serialVersionUID = -5945855000509930682L;
32  
33      public ComparisonExpression( int id )
34      {
35          super( id );
36      }
37  
38      public ComparisonExpression( OgnlParser p, int id )
39      {
40          super( p, id );
41      }
42  
43      public abstract String getComparisonFunction();
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public String toGetSourceString( OgnlContext context, Object target )
50      {
51          if ( target == null )
52          {
53              throw new UnsupportedCompilationException( "Current target is null, can't compile." );
54          }
55  
56          try
57          {
58  
59              Object value = getValueBody( context, target );
60  
61              if ( value != null && Boolean.class.isAssignableFrom( value.getClass() ) )
62              {
63                  getterClass = Boolean.TYPE;
64              }
65              else if ( value != null )
66              {
67                  getterClass = value.getClass();
68              }
69              else
70              {
71                  getterClass = Boolean.TYPE;
72              }
73  
74              // iterate over children to make numeric type detection work properly
75  
76              OgnlRuntime.getChildSource( context, target, children[0] );
77              OgnlRuntime.getChildSource( context, target, children[1] );
78  
79              // System.out.println("comparison expression currentType: " + context.getCurrentType() + " previousType: " +
80              // context.getPreviousType());
81  
82              boolean conversion = OgnlRuntime.shouldConvertNumericTypes( context );
83  
84              StringBuilder result = new StringBuilder( "(" );
85              if ( conversion )
86              {
87                  result.append( getComparisonFunction() ).append( "( ($w) (" );
88              }
89  
90              result.append( OgnlRuntime.getChildSource( context, target, children[0], conversion ) )
91                      .append( " " );
92  
93              if ( conversion )
94              {
95                  result.append( "), ($w) " );
96              }
97              else
98              {
99                  result.append( getExpressionOperator( 0 ) );
100             }
101 
102             result.append( "" ).append( OgnlRuntime.getChildSource( context, target, children[1], conversion ) );
103 
104             if ( conversion )
105             {
106                 result.append( ")" );
107             }
108 
109             context.setCurrentType( Boolean.TYPE );
110 
111             result.append( ")" );
112 
113             return result.toString();
114         }
115         catch ( NullPointerException e )
116         {
117 
118             // expected to happen in some instances
119 
120             throw new UnsupportedCompilationException( "evaluation resulted in null expression." );
121         }
122         catch ( Throwable t )
123         {
124             throw OgnlOps.castToRuntime( t );
125         }
126     }
127 }