KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > jjs > InternalCompilerException


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.jjs;
17
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * Indicates the compiler encountered an unexpected and unsupported state of
24  * operation.
25  */

26 public class InternalCompilerException extends RuntimeException JavaDoc {
27
28   /**
29    * Information regarding a node that was being processed when an
30    * InternalCompilerException was thrown.
31    */

32   public static final class NodeInfo {
33
34     private final String JavaDoc className;
35     private final String JavaDoc description;
36     private final SourceInfo sourceInfo;
37
38     private NodeInfo(String JavaDoc className, String JavaDoc description, SourceInfo sourceInfo) {
39       this.className = className;
40       this.description = description;
41       this.sourceInfo = sourceInfo;
42     }
43
44     /**
45      * Returns the name of the Java class of the node.
46      */

47     public String JavaDoc getClassName() {
48       return className;
49     }
50
51     /**
52      * Returns a text description of the node; typically toString().
53      */

54     public String JavaDoc getDescription() {
55       return description;
56     }
57
58     /**
59      * Returns the node's source info, if available; otherwise <code>null</code>.
60      */

61     public SourceInfo getSourceInfo() {
62       return sourceInfo;
63     }
64   }
65
66   /**
67    * Tracks if there's a pending addNode() to avoid recursion sickness.
68    */

69   private static final ThreadLocal JavaDoc pendingICE = new ThreadLocal JavaDoc();
70
71   private final List JavaDoc nodeTrace = new ArrayList JavaDoc();
72
73   /**
74    * Constructs a new exception with the specified node, message, and cause.
75    */

76   public InternalCompilerException(HasSourceInfo node, String JavaDoc message,
77       Throwable JavaDoc cause) {
78     this(message, cause);
79     addNode(node);
80   }
81
82   /**
83    * Constructs a new exception with the specified message.
84    */

85   public InternalCompilerException(String JavaDoc message) {
86     super(message);
87   }
88
89   /**
90    * Constructs a new exception with the specified message and cause.
91    */

92   public InternalCompilerException(String JavaDoc message, Throwable JavaDoc cause) {
93     super(message, cause);
94   }
95
96   /**
97    * Adds a node to the end of the node trace. This is similar to how a stack
98    * trace works.
99    */

100   public void addNode(HasSourceInfo node) {
101     InternalCompilerException other = (InternalCompilerException) pendingICE.get();
102     if (other != null) {
103       // Avoiding recursion sickness: Yet Another ICE must have occured while
104
// generating info for a prior ICE. Just bail!
105
return;
106     }
107
108     String JavaDoc className = null;
109     String JavaDoc description = null;
110     SourceInfo sourceInfo = null;
111     try {
112       pendingICE.set(this);
113       className = node.getClass().getName();
114       sourceInfo = node.getSourceInfo();
115       description = node.toString();
116     } catch (Throwable JavaDoc e) {
117       // ignore any exceptions
118
if (description == null) {
119         description = "<source info not available>";
120       }
121     } finally {
122       pendingICE.set(null);
123     }
124     addNode(className, description, sourceInfo);
125   }
126
127   /**
128    * Adds information about a a node to the end of the node trace. This is
129    * similar to how a stack trace works.
130    */

131   public void addNode(String JavaDoc className, String JavaDoc description,
132       SourceInfo sourceInfo) {
133     nodeTrace.add(new NodeInfo(className, description, sourceInfo));
134   }
135
136   /**
137    * Returns a list of nodes that were being processed when this exception was
138    * thrown. The list reflects the parent-child relationships of the AST and is
139    * is in order from children to parents. The first element of the returned
140    * list is the node that was most specifically being visited when the
141    * exception was thrown.
142    */

143   public List JavaDoc getNodeTrace() {
144     return nodeTrace;
145   }
146
147 }
148
Popular Tags