Java tutorial
/** * * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see <http://www.gnu.org/licenses/>. * **/ package lucee.transformer.bytecode.cast; import lucee.runtime.exp.TemplateException; import lucee.transformer.bytecode.BytecodeContext; import lucee.transformer.bytecode.BytecodeException; import lucee.transformer.bytecode.Literal; import lucee.transformer.bytecode.expression.ExprBoolean; import lucee.transformer.bytecode.expression.ExprDouble; import lucee.transformer.bytecode.expression.ExprFloat; import lucee.transformer.bytecode.expression.ExprString; import lucee.transformer.bytecode.expression.Expression; import lucee.transformer.bytecode.expression.ExpressionBase; import lucee.transformer.bytecode.literal.LitFloat; import lucee.transformer.bytecode.op.OpDouble; import lucee.transformer.bytecode.util.Methods; import lucee.transformer.bytecode.util.Types; import org.objectweb.asm.Type; import org.objectweb.asm.commons.GeneratorAdapter; import org.objectweb.asm.commons.Method; /** * cast a Expression to a Double */ public final class CastFloat extends ExpressionBase implements ExprFloat, Cast { private Expression expr; private CastFloat(Expression expr) { super(expr.getStart(), expr.getEnd()); this.expr = expr; } /** * Create a String expression from a Expression * @param expr * @return String expression * @throws TemplateException */ public static ExprFloat toExprFloat(Expression expr) { if (expr instanceof ExprFloat) return (ExprFloat) expr; if (expr instanceof Literal) { Double dbl = ((Literal) expr).getDouble(null); if (dbl != null) return new LitFloat((float) dbl.doubleValue(), expr.getStart(), expr.getEnd()); } return new CastFloat(expr); } /** * @see lucee.transformer.bytecode.expression.Expression#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int) */ public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException { GeneratorAdapter adapter = bc.getAdapter(); if (expr instanceof OpDouble) { ((OpDouble) expr).writeOutDouble(bc, MODE_VALUE); if (mode == MODE_VALUE) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE_FROM_DOUBLE); else adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_DOUBLE); } else if (expr instanceof ExprBoolean) { expr.writeOut(bc, MODE_VALUE); if (mode == MODE_VALUE) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE_FROM_BOOLEAN); else adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_BOOLEAN); } else if (expr instanceof ExprFloat) { expr.writeOut(bc, mode); } else if (expr instanceof ExprDouble) { expr.writeOut(bc, MODE_VALUE); if (mode == MODE_VALUE) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE_FROM_DOUBLE); else adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_DOUBLE); } else if (expr instanceof ExprString) { expr.writeOut(bc, MODE_REF); if (mode == MODE_VALUE) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE_FROM_STRING); else adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_STRING); } else { Type rtn = expr.writeOut(bc, mode); if (mode == MODE_VALUE) { if (!Types.isPrimitiveType(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE); } else if (Types.DOUBLE_VALUE.equals(rtn)) { adapter.cast(Types.DOUBLE_VALUE, Types.FLOAT_VALUE); } else if (Types.FLOAT_VALUE.equals(rtn)) { } else if (Types.BOOLEAN_VALUE.equals(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE_FROM_BOOLEAN); } else { adapter.invokeStatic(Types.CASTER, new Method("toRef", Types.toRefType(rtn), new Type[] { rtn })); adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE); } return Types.FLOAT_VALUE; } else if (Types.isPrimitiveType(rtn)) { if (Types.DOUBLE_VALUE.equals(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_DOUBLE); } else if (Types.FLOAT_VALUE.equals(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_FLOAT); } else if (Types.BOOLEAN_VALUE.equals(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_BOOLEAN); } else { adapter.invokeStatic(Types.CASTER, new Method("toRef", Types.toRefType(rtn), new Type[] { rtn })); adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT); } return Types.FLOAT; } //else { if (!Types.FLOAT.equals(rtn)) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT); return Types.FLOAT; //} } if (mode == MODE_VALUE) return Types.FLOAT_VALUE; return Types.FLOAT; } @Override public Expression getExpr() { return expr; } }