Source code: openjava/ptree/CastExpression.java
1 /*
2 * CastExpression.java 1.0
3 *
4 *
5 * Jun 20, 1997 by mich
6 * Sep 29, 1997 by bv
7 * Oct 10, 1997 by mich
8 *
9 * @see openjava.ptree.ParseTree
10 * @version 1.0 last updated: Oct 10, 1997
11 * @author Michiaki Tatsubori
12 */
13 package openjava.ptree;
14
15 import openjava.mop.Environment;
16 import openjava.mop.OJClass;
17 import openjava.ptree.util.ParseTreeVisitor;
18
19 /**
20 * The <code>CastExpression</code> class represents
21 * a cast expression of parse tree.
22 * <br>
23 * If the operator in the expression of the right operand has week unity,
24 * this automatically produces the code in which the right operand
25 * is enclosed by parenthesises.
26 * <br>
27 * In the case the caster is <code>int</code> and
28 * the right operand to be casted is <code>p + q</code>,
29 * this produces the code :
30 * <br><blockquote><pre>
31 * (int) (p + q)
32 * </pre></blockquote><br>
33 *
34 * @see openjava.ptree.NonLeaf
35 * @see openjava.ptree.Expression
36 * @see openjava.ptree.TypeName
37 */
38 public class CastExpression extends NonLeaf implements Expression {
39 /**
40 * Allocates a new object.
41 *
42 * @param ts the type specifier to cast in this expression.
43 * @param expr the expression to be casted in this expression.
44 */
45 public CastExpression(TypeName ts, Expression expr) {
46 super();
47 set((ParseTree) ts, (ParseTree) expr);
48 }
49
50 public CastExpression(OJClass type, Expression expr) {
51 this(TypeName.forOJClass(type), expr);
52 }
53
54 CastExpression() {
55 super();
56 }
57
58 /**
59 * Gets the type specifier to cast in this expression.
60 *
61 * @return the type specifier.
62 */
63 public TypeName getTypeSpecifier() {
64 return (TypeName) elementAt(0);
65 }
66
67 /**
68 * Sets the type specifier to cast in this expression.
69 *
70 * @param tspec the type specifier.
71 */
72 public void setTypeSpecifier(TypeName tspec) {
73 setElementAt(tspec, 0);
74 }
75
76 /**
77 * Gets the expression of the operand to be casted in this expression.
78 *
79 * @return the expression.
80 */
81 public Expression getExpression() {
82 return (Expression) elementAt(1);
83 }
84
85 /**
86 * Sets the expression of the operand to be casted in this expression.
87 *
88 * @param expr the expression.
89 */
90 public void setExpression(Expression expr) {
91 setElementAt(expr, 1);
92 }
93
94 public void accept(ParseTreeVisitor v) throws ParseTreeException {
95 v.visit(this);
96 }
97
98 public OJClass getType(Environment env) throws Exception {
99 String qname = env.toQualifiedName(getTypeSpecifier().toString());
100 return env.lookupClass(qname);
101 }
102
103 }