KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > parser > CastExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es.parser;
30
31 import com.caucho.es.ESException;
32
33 import java.io.IOException JavaDoc;
34
35 /**
36  * CastExpr represents casting.
37  */

38 class CastExpr extends Expr {
39   private Expr lhs;
40   private TypeExpr typeExpr;
41
42   private CastExpr(Block block, Expr lhs, TypeExpr typeExpr)
43     throws ESException
44   {
45     super(block);
46     
47     this.lhs = lhs;
48     this.typeExpr = typeExpr;
49   }
50
51   static CastExpr create(Block block, Expr lhs, TypeExpr typeExpr)
52     throws ESException
53   {
54     return new CastExpr(block, lhs, typeExpr);
55   }
56     
57   /**
58    * Returns the JavaScript type class of the casted type.
59    */

60   int getType()
61   {
62     return typeExpr.getType();
63   }
64
65   /**
66    * Returns the actual type expression.
67    */

68   Expr getTypeExpr()
69   {
70     return typeExpr;
71   }
72
73   /**
74    * Prints the casted expression when the result is a JavaScript object.
75    */

76   void printImpl() throws IOException JavaDoc
77   {
78     lhs.print();
79   }
80   
81   /**
82    * Prints the casted expression when the result is a boolean
83    */

84   void printBooleanImpl() throws IOException JavaDoc
85   {
86     lhs.printBoolean();
87   }
88   
89   /**
90    * Prints the casted expression when the result is a 32-bit integer
91    */

92   void printInt32Impl() throws IOException JavaDoc
93   {
94     if (! typeExpr.getTypeName().equals("int"))
95       cl.print("(" + typeExpr.getTypeName() + ") ");
96     
97     lhs.printInt32();
98   }
99   
100   /**
101    * Prints the casted expression when the result is a number (double)
102    */

103   void printNumImpl() throws IOException JavaDoc
104   {
105     if (! typeExpr.getTypeName().equals("double"))
106       cl.print("(" + typeExpr.getTypeName() + ") ");
107     
108     lhs.printNum();
109   }
110   
111   /**
112    * Prints the casted expression when the result is a string
113    */

114   void printStringImpl() throws IOException JavaDoc
115   {
116     if (lhs.getJavaClass().equals(String JavaDoc.class))
117       lhs.printStringImpl();
118     else {
119       cl.print("String.valueOf(");
120       lhs.printString();
121       cl.print(")");
122     }
123   }
124   
125   /**
126    * Prints the casted expression when the result is a JavaExpression
127    */

128   void printJavaImpl() throws IOException JavaDoc
129   {
130     if (! typeExpr.getJavaClass().isAssignableFrom(lhs.getJavaClass())) {
131       cl.print("(");
132       printJavaClass(typeExpr.getJavaClass());
133       cl.print(") ");
134     }
135
136     lhs.printJava();
137   }
138     
139
140   /**
141    * Marks the value as being used.
142    */

143   void setUsed()
144   {
145     lhs.setUsed();
146   }
147 }
148
Popular Tags