View Javadoc
1   /*
2    * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package sun.tools.tree;
27  
28  import sun.tools.java.*;
29  import sun.tools.asm.Assembler;
30  import sun.tools.asm.LocalVariable;
31  import sun.tools.asm.Label;
32  import java.io.PrintStream;
33  import java.util.Hashtable;
34  
35  /**
36   * WARNING: The contents of this source file are not part of any
37   * supported API.  Code that depends on them does so at its own risk:
38   * they are subject to change or removal without notice.
39   */
40  public
41  class CatchStatement extends Statement {
42      int mod;
43      Expression texpr;
44      Identifier id;
45      Statement body;
46      LocalMember field;
47  
48      /**
49       * Constructor
50       */
51      public CatchStatement(long where, Expression texpr, IdentifierToken id, Statement body) {
52          super(CATCH, where);
53          this.mod = id.getModifiers();
54          this.texpr = texpr;
55          this.id = id.getName();
56          this.body = body;
57      }
58      /** @deprecated */
59      @Deprecated
60      public CatchStatement(long where, Expression texpr, Identifier id, Statement body) {
61          super(CATCH, where);
62          this.texpr = texpr;
63          this.id = id;
64          this.body = body;
65      }
66  
67      /**
68       * Check statement
69       */
70      Vset check(Environment env, Context ctx, Vset vset, Hashtable exp) {
71          vset = reach(env, vset);
72          ctx = new Context(ctx, this);
73          Type type = texpr.toType(env, ctx);
74  
75          try {
76              if (ctx.getLocalField(id) != null) {
77                  env.error(where, "local.redefined", id);
78              }
79  
80              if (type.isType(TC_ERROR)) {
81                  // error message printed out elsewhere
82              } else if (!type.isType(TC_CLASS)) {
83                  env.error(where, "catch.not.throwable", type);
84              } else {
85                  ClassDefinition def = env.getClassDefinition(type);
86                  if (!def.subClassOf(env,
87                                 env.getClassDeclaration(idJavaLangThrowable))) {
88                      env.error(where, "catch.not.throwable", def);
89                  }
90              }
91  
92              field = new LocalMember(where, ctx.field.getClassDefinition(), mod, type, id);
93              ctx.declare(env, field);
94              vset.addVar(field.number);
95  
96              return body.check(env, ctx, vset, exp);
97          } catch (ClassNotFound e) {
98              env.error(where, "class.not.found", e.name, opNames[op]);
99              return vset;
100         }
101     }
102 
103     /**
104      * Inline
105      */
106     public Statement inline(Environment env, Context ctx) {
107         ctx = new Context(ctx, this);
108         if (field.isUsed()) {
109             ctx.declare(env, field);
110         }
111         if (body != null) {
112             body = body.inline(env, ctx);
113         }
114         return this;
115     }
116 
117     /**
118      * Create a copy of the statement for method inlining
119      */
120     public Statement copyInline(Context ctx, boolean valNeeded) {
121         CatchStatement s = (CatchStatement)clone();
122         if (body != null) {
123             s.body = body.copyInline(ctx, valNeeded);
124         }
125         if (field != null) {
126             s.field = field.copyInline(ctx);
127         }
128         return s;
129     }
130 
131     /**
132      * Compute cost of inlining this statement
133      */
134     public int costInline(int thresh, Environment env, Context ctx){
135         int cost = 1;
136         if (body != null) {
137             cost += body.costInline(thresh, env,ctx);
138         }
139         return cost;
140     }
141 
142     /**
143      * Code
144      */
145     public void code(Environment env, Context ctx, Assembler asm) {
146         CodeContext newctx = new CodeContext(ctx, this);
147         if (field.isUsed()) {
148             newctx.declare(env, field);
149             asm.add(where, opc_astore, new LocalVariable(field, field.number));
150         } else {
151             asm.add(where, opc_pop);
152         }
153         if (body != null) {
154             body.code(env, newctx, asm);
155         }
156         //asm.add(newctx.breakLabel);
157     }
158 
159     /**
160      * Print
161      */
162     public void print(PrintStream out, int indent) {
163         super.print(out, indent);
164         out.print("catch (");
165         texpr.print(out);
166         out.print(" " + id + ") ");
167         if (body != null) {
168             body.print(out, indent);
169         } else {
170             out.print("<empty>");
171         }
172     }
173 }