Coverage Report - org.apache.commons.ognl.ASTMethodUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ASTMethodUtil
91%
32/35
83%
30/36
5.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  
 import org.apache.commons.ognl.enhance.OgnlExpressionCompiler;
 24  
 
 25  
 
 26  
 /**
 27  
  * $Id: ASTMethodUtil.java 1194866 2011-10-29 10:51:32Z mcucchiara $
 28  
  */
 29  
 class ASTMethodUtil
 30  
 {
 31  
 
 32  
     private ASTMethodUtil()
 33  0
     {
 34  0
     }
 35  
 
 36  
     static String getParmString( OgnlContext context, Object root, Node child, Class prevType )
 37  
         throws OgnlException
 38  
     {
 39  75
         String parmString = child.toGetSourceString( context, root );
 40  
 
 41  75
         if ( parmString == null || parmString.trim().length() < 1 )
 42  
         {
 43  1
             parmString = "null";
 44  
         }
 45  
 
 46  
         // to undo type setting of constants when used as method parameters
 47  75
         if ( ASTConst.class.isInstance( child ) )
 48  
         {
 49  48
             context.setCurrentType( prevType );
 50  
         }
 51  
 
 52  75
         parmString = ExpressionCompiler.getRootExpression( child, root, context ) + parmString;
 53  
 
 54  75
         String cast = "";
 55  75
         if ( ExpressionCompiler.shouldCast( child ) )
 56  
         {
 57  25
             cast = (String) context.remove( ExpressionCompiler.PRE_CAST );
 58  
         }
 59  
 
 60  75
         if ( cast == null )
 61  
         {
 62  22
             cast = "";
 63  
         }
 64  
 
 65  75
         if ( !ASTConst.class.isInstance( child ) )
 66  
         {
 67  27
             parmString = cast + parmString;
 68  
         }
 69  75
         return parmString;
 70  
     }
 71  
 
 72  
     static Class getValueClass( OgnlContext context, Object root, Node child )
 73  
         throws OgnlException
 74  
     {
 75  75
         Object value = child.getValue( context, root );
 76  75
         Class valueClass = value != null ? value.getClass() : null;
 77  75
         if ( NodeType.class.isAssignableFrom( child.getClass() ) )
 78  
         {
 79  74
             valueClass = ( (NodeType) child ).getGetterClass();
 80  
         }
 81  75
         return valueClass;
 82  
     }
 83  
 
 84  
     static String getParmString( OgnlContext context, Class parm, String parmString, Node child, Class valueClass,
 85  
                                  String endParam )
 86  
     {
 87  34
         OgnlExpressionCompiler compiler = OgnlRuntime.getCompiler( context );
 88  34
         if ( parm.isArray() )
 89  
         {
 90  1
             parmString = compiler.createLocalReference( context, "(" + ExpressionCompiler.getCastString( parm )
 91  
                 + ")org.apache.commons.ognl.OgnlOps#toArray(" + parmString + ", " + parm.getComponentType().getName()
 92  
                 + endParam, parm );
 93  
 
 94  
         }
 95  33
         else if ( parm.isPrimitive() )
 96  
         {
 97  6
             Class wrapClass = OgnlRuntime.getPrimitiveWrapperClass( parm );
 98  
 
 99  6
             parmString = compiler.createLocalReference( context, "((" + wrapClass.getName()
 100  
                 + ")org.apache.commons.ognl.OgnlOps#convertValue(" + parmString + "," + wrapClass.getName()
 101  
                 + ".class, true))." + OgnlRuntime.getNumericValueGetter( wrapClass ), parm );
 102  
 
 103  6
         }
 104  27
         else if ( parm != Object.class )
 105  
         {
 106  10
             parmString = compiler.createLocalReference( context, "(" + parm.getName()
 107  
                 + ")org.apache.commons.ognl.OgnlOps#convertValue(" + parmString + "," + parm.getName() + ".class)",
 108  
                                                         parm );
 109  
 
 110  
         }
 111  17
         else if ( ( NodeType.class.isInstance( child ) && ( (NodeType) child ).getGetterClass() != null
 112  
             && Number.class.isAssignableFrom( ( (NodeType) child ).getGetterClass() ) ) || ( valueClass != null
 113  
             && valueClass.isPrimitive() ) )
 114  
         {
 115  9
             parmString = " ($w) " + parmString;
 116  
         }
 117  8
         else if ( valueClass != null && valueClass.isPrimitive() )
 118  
         {
 119  0
             parmString = "($w) " + parmString;
 120  
         }
 121  34
         return parmString;
 122  
     }
 123  
 }