Coverage Report - org.apache.commons.ognl.ASTConst
 
Classes in this File Line Coverage Branch Coverage Complexity
ASTConst
92%
47/51
91%
31/34
3.364
 
 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  
  * $Id: ASTConst.java 1194869 2011-10-29 11:10:16Z mcucchiara $
 26  
  * @author Luke Blanshard (blanshlu@netscape.net)
 27  
  * @author Drew Davidson (drew@ognl.org)
 28  
  */
 29  
 public class ASTConst
 30  
     extends SimpleNode
 31  
     implements NodeType
 32  
 {
 33  
 
 34  
     private Object value;
 35  
 
 36  
     private Class getterClass;
 37  
 
 38  
     public ASTConst( int id )
 39  
     {
 40  2318
         super( id );
 41  2318
     }
 42  
 
 43  
     public ASTConst( OgnlParser p, int id )
 44  
     {
 45  0
         super( p, id );
 46  0
     }
 47  
 
 48  
     /** 
 49  
      * Called from parser actions.
 50  
      * @param value the value to set 
 51  
      */
 52  
     public void setValue( Object value )
 53  
     {
 54  2252
         this.value = value;
 55  2252
     }
 56  
 
 57  
     public Object getValue()
 58  
     {
 59  808
         return value;
 60  
     }
 61  
 
 62  
     protected Object getValueBody( OgnlContext context, Object source )
 63  
         throws OgnlException
 64  
     {
 65  1114
         return this.value;
 66  
     }
 67  
 
 68  
     public boolean isNodeConstant( OgnlContext context )
 69  
         throws OgnlException
 70  
     {
 71  1284
         return true;
 72  
     }
 73  
 
 74  
     public Class getGetterClass()
 75  
     {
 76  675
         if ( getterClass == null )
 77  
         {
 78  4
             return null;
 79  
         }
 80  
         
 81  671
         return getterClass;
 82  
     }
 83  
 
 84  
     public Class getSetterClass()
 85  
     {
 86  0
         return null;
 87  
     }
 88  
 
 89  
     public String toGetSourceString( OgnlContext context, Object target )
 90  
     {
 91  1020
         if ( value == null && parent != null && ExpressionNode.class.isInstance( parent ) )
 92  
         {
 93  22
             context.setCurrentType( null );
 94  22
             return "null";
 95  
         }
 96  998
         else if ( value == null )
 97  
         {
 98  15
             context.setCurrentType( null );
 99  15
             return "";
 100  
         }
 101  
 
 102  983
         getterClass = value.getClass();
 103  
 
 104  983
         Object retval = value;
 105  983
         if ( parent != null && ASTProperty.class.isInstance( parent ) )
 106  
         {
 107  390
             context.setCurrentObject( value );
 108  
 
 109  390
             return value.toString();
 110  
         }
 111  593
         else if ( value != null && Number.class.isAssignableFrom( value.getClass() ) )
 112  
         {
 113  302
             context.setCurrentType( OgnlRuntime.getPrimitiveWrapperClass( value.getClass() ) );
 114  302
             context.setCurrentObject( value );
 115  
 
 116  302
             return value.toString();
 117  
         }
 118  291
         else if ( !( parent != null
 119  
                         && value != null 
 120  
                         && NumericExpression.class.isAssignableFrom( parent.getClass() ) )
 121  
             && String.class.isAssignableFrom( value.getClass() ) )
 122  
         {
 123  159
             context.setCurrentType( String.class );
 124  
 
 125  159
             retval = '\"' + OgnlOps.getEscapeString( value.toString() ) + '\"';
 126  
 
 127  159
             context.setCurrentObject( retval.toString() );
 128  
 
 129  159
             return retval.toString();
 130  
         }
 131  132
         else if ( Character.class.isInstance( value ) )
 132  
         {
 133  32
             Character val = (Character) value;
 134  
 
 135  32
             context.setCurrentType( Character.class );
 136  
 
 137  32
             if ( Character.isLetterOrDigit( val.charValue() ) )
 138  
             {
 139  14
                 retval = "'" + ( (Character) value ).charValue() + "'";
 140  
             }
 141  
             else
 142  
             {
 143  18
                 retval = "'" + OgnlOps.getEscapedChar( ( (Character) value ).charValue() ) + "'";
 144  
             }
 145  
             
 146  32
             context.setCurrentObject( retval );
 147  32
             return retval.toString();
 148  
         }
 149  
 
 150  100
         if ( Boolean.class.isAssignableFrom( value.getClass() ) )
 151  
         {
 152  38
             getterClass = Boolean.TYPE;
 153  
 
 154  38
             context.setCurrentType( Boolean.TYPE );
 155  38
             context.setCurrentObject( value );
 156  
 
 157  38
             return value.toString();
 158  
         }
 159  
 
 160  62
         return value.toString();
 161  
     }
 162  
 
 163  
     public String toSetSourceString( OgnlContext context, Object target )
 164  
     {
 165  13
         if ( parent == null )
 166  
         {
 167  0
             throw new UnsupportedCompilationException( "Can't modify constant values." );
 168  
         }
 169  
         
 170  13
         return toGetSourceString( context, target );
 171  
     }
 172  
     
 173  
     public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data )
 174  
         throws OgnlException
 175  
     {
 176  9
         return visitor.visit( this, data );
 177  
     }
 178  
 }