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