001    package org.apache.commons.ognl;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.commons.ognl.enhance.UnsupportedCompilationException;
023    
024    /**
025     * Base class for boolean expressions.
026     * 
027     * @author jkuhnert
028     */
029    public abstract class BooleanExpression
030        extends ExpressionNode
031        implements NodeType
032    {
033    
034        private static final long serialVersionUID = 8630306635724834872L;
035    
036        protected Class<?> getterClass;
037    
038        public BooleanExpression( int id )
039        {
040            super( id );
041        }
042    
043        public BooleanExpression( OgnlParser p, int id )
044        {
045            super( p, id );
046        }
047    
048        /**
049         * {@inheritDoc}
050         */
051        public Class<?> getGetterClass()
052        {
053            return getterClass;
054        }
055    
056        /**
057         * {@inheritDoc}
058         */
059        public Class<?> getSetterClass()
060        {
061            return null;
062        }
063    
064        /**
065         * {@inheritDoc}
066         */
067        @Override
068        public String toGetSourceString( OgnlContext context, Object target )
069        {
070            try
071            {
072    
073                Object value = getValueBody( context, target );
074    
075                if ( value != null && Boolean.class.isAssignableFrom( value.getClass() ) )
076                {
077                    getterClass = Boolean.TYPE;
078                }
079                else if ( value != null )
080                {
081                    getterClass = value.getClass();
082                }
083                else
084                {
085                    getterClass = Boolean.TYPE;
086                }
087    
088                String ret = super.toGetSourceString( context, target );
089    
090                if ( "(false)".equals( ret ) )
091                {
092                    return "false";
093                }
094                else if ( "(true)".equals( ret ) )
095                {
096                    return "true";
097                }
098                return ret;
099    
100            }
101            catch ( NullPointerException e )
102            {
103    
104                // expected to happen in some instances
105                e.printStackTrace();
106    
107                throw new UnsupportedCompilationException( "evaluation resulted in null expression." );
108            }
109            catch ( Throwable t )
110            {
111                throw OgnlOps.castToRuntime( t );
112            }
113        }
114    }