001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es.parser;
030:
031: import com.caucho.es.ESException;
032:
033: import java.io.IOException;
034:
035: /**
036: * CastExpr represents casting.
037: */
038: class CastExpr extends Expr {
039: private Expr lhs;
040: private TypeExpr typeExpr;
041:
042: private CastExpr(Block block, Expr lhs, TypeExpr typeExpr)
043: throws ESException {
044: super (block);
045:
046: this .lhs = lhs;
047: this .typeExpr = typeExpr;
048: }
049:
050: static CastExpr create(Block block, Expr lhs, TypeExpr typeExpr)
051: throws ESException {
052: return new CastExpr(block, lhs, typeExpr);
053: }
054:
055: /**
056: * Returns the JavaScript type class of the casted type.
057: */
058: int getType() {
059: return typeExpr.getType();
060: }
061:
062: /**
063: * Returns the actual type expression.
064: */
065: Expr getTypeExpr() {
066: return typeExpr;
067: }
068:
069: /**
070: * Prints the casted expression when the result is a JavaScript object.
071: */
072: void printImpl() throws IOException {
073: lhs.print();
074: }
075:
076: /**
077: * Prints the casted expression when the result is a boolean
078: */
079: void printBooleanImpl() throws IOException {
080: lhs.printBoolean();
081: }
082:
083: /**
084: * Prints the casted expression when the result is a 32-bit integer
085: */
086: void printInt32Impl() throws IOException {
087: if (!typeExpr.getTypeName().equals("int"))
088: cl.print("(" + typeExpr.getTypeName() + ") ");
089:
090: lhs.printInt32();
091: }
092:
093: /**
094: * Prints the casted expression when the result is a number (double)
095: */
096: void printNumImpl() throws IOException {
097: if (!typeExpr.getTypeName().equals("double"))
098: cl.print("(" + typeExpr.getTypeName() + ") ");
099:
100: lhs.printNum();
101: }
102:
103: /**
104: * Prints the casted expression when the result is a string
105: */
106: void printStringImpl() throws IOException {
107: if (lhs.getJavaClass().equals(String.class))
108: lhs.printStringImpl();
109: else {
110: cl.print("String.valueOf(");
111: lhs.printString();
112: cl.print(")");
113: }
114: }
115:
116: /**
117: * Prints the casted expression when the result is a JavaExpression
118: */
119: void printJavaImpl() throws IOException {
120: if (!typeExpr.getJavaClass().isAssignableFrom(
121: lhs.getJavaClass())) {
122: cl.print("(");
123: printJavaClass(typeExpr.getJavaClass());
124: cl.print(") ");
125: }
126:
127: lhs.printJava();
128: }
129:
130: /**
131: * Marks the value as being used.
132: */
133: void setUsed() {
134: lhs.setUsed();
135: }
136: }
|