Coverage Report - org.apache.commons.ognl.NumericExpression
 
Classes in this File Line Coverage Branch Coverage Complexity
NumericExpression
89%
33/37
88%
30/34
4.5
 
 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.ExpressionCompiler;
 23  
 
 24  
 /**
 25  
  * Base class for numeric expressions.
 26  
  */
 27  
 public abstract class NumericExpression
 28  
     extends ExpressionNode
 29  
     implements NodeType
 30  
 {
 31  
 
 32  
     private static final long serialVersionUID = -174952564587478850L;
 33  
 
 34  
     protected Class<?> getterClass;
 35  
 
 36  
     public NumericExpression( int id )
 37  
     {
 38  326
         super( id );
 39  326
     }
 40  
 
 41  
     public NumericExpression( OgnlParser p, int id )
 42  
     {
 43  0
         super( p, id );
 44  0
     }
 45  
 
 46  
     /**
 47  
      * {@inheritDoc}
 48  
      */
 49  
     public Class<?> getGetterClass()
 50  
     {
 51  44
         if ( getterClass != null )
 52  
         {
 53  41
             return getterClass;
 54  
         }
 55  
 
 56  3
         return Double.TYPE;
 57  
     }
 58  
 
 59  
     /**
 60  
      * {@inheritDoc}
 61  
      */
 62  
     public Class<?> getSetterClass()
 63  
     {
 64  1
         return null;
 65  
     }
 66  
 
 67  
     /**
 68  
      * {@inheritDoc}
 69  
      */
 70  
     @Override
 71  
     public String toGetSourceString( OgnlContext context, Object target )
 72  
     {
 73  
         Object value;
 74  75
         StringBuilder result = new StringBuilder( "" );
 75  
 
 76  
         try
 77  
         {
 78  
 
 79  75
             value = getValueBody( context, target );
 80  
 
 81  75
             if ( value != null )
 82  
             {
 83  75
                 getterClass = value.getClass();
 84  
             }
 85  
 
 86  225
             for ( int i = 0; i < children.length; i++ )
 87  
             {
 88  150
                 if ( i > 0 )
 89  
                 {
 90  75
                     result.append( " " ).append( getExpressionOperator( i ) ).append( " " );
 91  
                 }
 92  150
                 String str = OgnlRuntime.getChildSource( context, target, children[i] );
 93  
 
 94  150
                 result.append( coerceToNumeric( str, context, children[i] ) );
 95  
             }
 96  
 
 97  
         }
 98  0
         catch ( Throwable t )
 99  
         {
 100  0
             throw OgnlOps.castToRuntime( t );
 101  75
         }
 102  
 
 103  75
         return result.toString();
 104  
     }
 105  
 
 106  
     public String coerceToNumeric( String source, OgnlContext context, Node child )
 107  
     {
 108  160
         StringBuilder ret = new StringBuilder( source );
 109  160
         Object value = context.getCurrentObject();
 110  
 
 111  160
         if ( ASTConst.class.isInstance( child ) && value != null )
 112  
         {
 113  115
             return value.toString();
 114  
         }
 115  
 
 116  45
         if ( context.getCurrentType() != null && !context.getCurrentType().isPrimitive()
 117  
             && context.getCurrentObject() != null && Number.class.isInstance( context.getCurrentObject() ) )
 118  
         {
 119  6
             ret = new StringBuilder( "((" ).append(
 120  
                 ExpressionCompiler.getCastString( context.getCurrentObject().getClass() ) ).append( ")" ).append(
 121  
                 ret ).append( ")." ).append(
 122  
                 OgnlRuntime.getNumericValueGetter( context.getCurrentObject().getClass() ) );
 123  
         }
 124  39
         else if ( context.getCurrentType() != null && context.getCurrentType().isPrimitive()
 125  
             && ( ASTConst.class.isInstance( child ) || NumericExpression.class.isInstance( child ) ) )
 126  
         {
 127  
             @SuppressWarnings( "unchecked" ) // checked by the condition in the if clause
 128  10
             Class<? extends Number> numberClass = (Class<? extends Number>) context.getCurrentType();
 129  10
             ret.append( OgnlRuntime.getNumericLiteral( numberClass ) );
 130  10
         }
 131  29
         else if ( context.getCurrentType() != null && String.class.isAssignableFrom( context.getCurrentType() ) )
 132  
         {
 133  2
             ret = new StringBuilder( "Double.parseDouble(" )
 134  
                 .append( ret )
 135  
                 .append( ")" );
 136  2
             context.setCurrentType( Double.TYPE );
 137  
         }
 138  
 
 139  45
         if ( NumericExpression.class.isInstance( child ) )
 140  
         {
 141  18
             ret = new StringBuilder( "(" ).append( ret ).append( ")" );
 142  
         }
 143  
 
 144  45
         return ret.toString();
 145  
     }
 146  
 }