KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > expr > TryExp


1 package gnu.expr;
2 import gnu.bytecode.*;
3 import gnu.mapping.*;
4
5 /**
6   * This class represents try/catch/finally.
7   * @author Per Bothner
8   */

9
10 public class TryExp extends Expression
11 {
12   Expression try_clause;
13
14   CatchClause catch_clauses;
15
16   Expression finally_clause;
17   
18   public final CatchClause getCatchClauses () { return catch_clauses; }
19   public final void setCatchClauses (CatchClause catch_clauses)
20   {
21     this.catch_clauses = catch_clauses;
22   }
23
24   public TryExp (Expression try_clause, Expression finally_clause)
25   {
26     this.try_clause = try_clause;
27     this.finally_clause = finally_clause;
28   }
29
30   protected boolean mustCompile () { return catch_clauses != null; }
31
32   public void apply (CallContext ctx) throws Throwable JavaDoc
33   {
34     if (catch_clauses != null)
35       throw new RuntimeException JavaDoc("internal error - TryExp.eval called");
36     try
37       {
38     try_clause.apply(ctx);
39         ctx.runUntilDone();
40       }
41     catch (Throwable JavaDoc ex)
42       {
43         for (CatchClause clause = catch_clauses; clause != null;
44              clause = clause.next)
45           {
46             // Declaration decl = clause.firstDecl();
47
// FIXME
48
}
49         throw ex;
50       }
51     finally
52       {
53     finally_clause.eval(ctx);
54       }
55   }
56
57   public void compile (Compilation comp, Target target)
58   {
59     CodeAttr code = comp.getCode();
60     boolean has_finally = finally_clause != null;
61     Type result_type = target instanceof IgnoreTarget ? null
62     : getType();
63     Target ttarg;
64     if (result_type == null)
65       ttarg = Target.Ignore;
66     else if (result_type == Type.pointer_type)
67       ttarg = Target.pushObject;
68     else
69       ttarg = new StackTarget(result_type);
70     code.emitTryStart(has_finally, result_type);
71     try_clause.compileWithPosition(comp, ttarg);
72     code.emitTryEnd();
73
74     CatchClause catch_clause = catch_clauses;
75     for (; catch_clause != null; catch_clause = catch_clause.getNext())
76       {
77     catch_clause.compile(comp, ttarg);
78       }
79
80     if (finally_clause != null)
81       {
82     code.emitFinallyStart();
83     finally_clause.compileWithPosition(comp, Target.Ignore);
84     code.emitFinallyEnd();
85       }
86     code.emitTryCatchEnd();
87     if (result_type != null)
88       target.compileFromStack(comp, result_type);
89   }
90
91   protected Expression walk (ExpWalker walker)
92   {
93     return walker.walkTryExp(this);
94   }
95
96   protected void walkChildren(ExpWalker walker)
97   {
98     try_clause = walker.walk(try_clause);
99     CatchClause catch_clause = catch_clauses;
100     while (walker.exitValue == null && catch_clause != null)
101       {
102     walker.walk(catch_clause);
103     catch_clause = catch_clause.getNext();
104       }
105
106     if (walker.exitValue == null && finally_clause != null)
107       finally_clause =walker.walk( finally_clause);
108   }
109
110   public void print (OutPort ps)
111   {
112     ps.startLogicalBlock("(Try", ")", 2);
113     ps.writeSpaceFill();
114     try_clause.print(ps);
115     CatchClause catch_clause = catch_clauses;
116     for (; catch_clause != null; catch_clause = catch_clause.getNext())
117       {
118     catch_clause.print(ps);
119       }
120     if (finally_clause != null)
121       {
122     ps.writeSpaceLinear();
123     ps.print(" finally: ");
124     finally_clause.print(ps);
125       }
126     ps.endLogicalBlock(")");
127   }
128 }
129
Popular Tags