001    package railo.transformer.bytecode.literal;
002    
003    import org.objectweb.asm.Opcodes;
004    import org.objectweb.asm.Type;
005    import org.objectweb.asm.commons.GeneratorAdapter;
006    
007    import railo.runtime.op.Caster;
008    import railo.transformer.bytecode.BytecodeContext;
009    import railo.transformer.bytecode.Literal;
010    import railo.transformer.bytecode.Position;
011    import railo.transformer.bytecode.expression.ExprBoolean;
012    import railo.transformer.bytecode.expression.ExpressionBase;
013    import railo.transformer.bytecode.util.Types;
014    
015    /**
016     * Literal Boolean
017     */
018    public final class LitBoolean extends ExpressionBase implements Literal,ExprBoolean {
019        
020            /**
021             * @see java.lang.Object#toString()
022             */
023            @Override
024            public String toString() {
025                    return b+"";
026            }
027    
028            private boolean b;
029    
030            public static final LitBoolean TRUE=new LitBoolean(true,null,null);
031            public static final LitBoolean FALSE=new LitBoolean(false,null,null);
032    
033            public static ExprBoolean toExprBoolean(boolean b, Position start,Position end) {
034                    return new LitBoolean(b,start,end);
035            }
036    
037            public static ExprBoolean toExprBoolean(boolean b) {
038                    return new LitBoolean(b,null,null);
039            }
040            
041        /**
042         * constructor of the class
043         * @param b 
044         * @param line 
045         */
046        public LitBoolean(boolean b, Position start,Position end) {
047            super(start,end);
048            this.b=b;
049        }
050    
051        /**
052         * @return return value as double value
053         */ 
054        public double getDoubleValue() {
055            return Caster.toDoubleValue(b);
056        }
057        
058        /**
059         * @return return value as Double Object
060         */
061        public Double getDouble() {
062            return Caster.toDouble(b);
063        }
064        
065        /**
066         * @see railo.transformer.bytecode.Literal#getString()
067         */
068        public String getString() {
069            return Caster.toString(b);
070        }
071        
072        /**
073         * @return return value as a Boolean Object
074         */
075        public Boolean getBoolean() {
076            return Caster.toBoolean(b);
077        }
078        
079        /**
080         * @return return value as a boolean value
081         */
082        public boolean getBooleanValue() {
083            return b;
084        }
085    
086        /**
087         *
088         * @see railo.transformer.bytecode.expression.ExpressionBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int)
089         */
090        public Type _writeOut(BytecodeContext bc, int mode) {
091            GeneratorAdapter adapter = bc.getAdapter();
092            
093            if(mode==MODE_REF) {
094                    adapter.getStatic(Types.BOOLEAN, b?"TRUE":"FALSE", Types.BOOLEAN);
095                    return Types.BOOLEAN;
096            }
097            adapter.visitInsn(b?Opcodes.ICONST_1:Opcodes.ICONST_0);
098            return Types.BOOLEAN_VALUE;
099        }
100    
101        /**
102         * @see railo.transformer.bytecode.Literal#getDouble(java.lang.Double)
103         */
104        public Double getDouble(Double defaultValue) {
105            return getDouble();
106        }
107    
108        /**
109         * @see railo.transformer.bytecode.Literal#getBoolean(java.lang.Boolean)
110         */
111        public Boolean getBoolean(Boolean defaultValue) {
112            return getBoolean();
113        }
114    
115        /* *
116         * @see railo.transformer.bytecode.expression.Expression#getType()
117         * /
118        public int getType() {
119            return Types._BOOLEAN;
120        }*/
121    }