KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ErrorHandler


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base;
26
27
28 import org.aspectj.compiler.base.ast.*;
29
30
31 import java.util.*;
32 import java.io.*;
33
34 public class ErrorHandler {
35     private PrintWriter errorWriter;
36     private JavaCompiler compiler;
37
38     private static final String JavaDoc PREFIX = "";
39
40     public void setCompiler(JavaCompiler compiler) {
41         this.compiler = compiler;
42     }
43     
44     public JavaCompiler getCompiler() { return compiler; }
45
46     public void cleanupErrorWarningCounters() {
47         errors = 0;
48         warnings = 0;
49     }
50
51     public ErrorHandler() {
52         this(null);
53     }
54
55     public ErrorHandler(Writer errorWriter) {
56         if (errorWriter == null) {
57             errorWriter = makePrintWriter();
58         }
59         if (!(errorWriter instanceof PrintWriter)) {
60             errorWriter = new PrintWriter(errorWriter, true);
61         }
62         this.errorWriter = (PrintWriter)errorWriter;
63     }
64
65     protected PrintWriter makePrintWriter() {
66         return new PrintWriter(System.err, true);
67     }
68
69
70     /* This section of code handles errors that occur during compilation */
71     static final String JavaDoc internalErrorMessage =
72     "Please copy the following text into an email message and send it,\n" +
73     "along with any additional information you can add to: \n" +
74     " \n" +
75     " support@aspectj.org \n" +
76     " \n";
77
78     static final String JavaDoc specificCodeRequest =
79     "If you could send us at least that fragment of code, provided it isn't\n"+
80     "confidential, that would be helpful to us in finding the bug.\n";
81
82     static final String JavaDoc generalCodeRequest =
83     "(If possible, please include a small fragment of code that can \n"+
84     "reproduce the error, provided that code isn't confidential.) \n";
85
86
87     public synchronized void internalError(Throwable JavaDoc uncaughtException, ASTObject where) {
88         throw new InternalCompilerError(compiler, uncaughtException, where);
89     }
90
91     //public synchronized void internalError(Throwable uncaughtException) {
92
// internalError(uncaughtException, compiler.getCurrentNode());
93
//}
94

95     private int printLine(File source, int beginLine) {
96         return printLines(source, beginLine, beginLine);
97     }
98
99     private static int count(String JavaDoc s, char ch) {
100         int index = s.indexOf(ch);
101         int n = 0;
102         while (index != -1) {
103             n += 1;
104             index = s.indexOf(ch, index+1);
105         }
106         return n;
107     }
108
109
110     private int printLines(File source, int beginLine, int endLine) {
111         int tabCount = 0;
112         try {
113             BufferedReader reader = new BufferedReader(new FileReader(source));
114
115             try {
116                 int i;
117
118                 for(i=1; i<beginLine; i++) reader.readLine();
119
120                 for(;i<=endLine; i++) {
121                     String JavaDoc line = reader.readLine();
122                     if (line == null) {
123                         errorWriter.println(PREFIX + "<at end of file>");
124                         return -1;
125                     }
126                     errorWriter.println(PREFIX + line);
127                     tabCount += count(line, '\t');
128                 }
129             } finally {
130                 if (reader != null) reader.close();
131             }
132         } catch (IOException e) {
133             errorWriter.println(PREFIX + "can't read text from "+source.getPath());
134         }
135
136         return tabCount;
137     }
138
139     private void printCaretLine(int pos) {
140         errorWriter.print(PREFIX);
141         for(int i=1; i<pos; i++) errorWriter.print(" ");
142         errorWriter.println("^");
143     }
144
145     public synchronized boolean showMessage(String JavaDoc message) {
146         return showMessage(null, message, false, false);
147     }
148
149     public synchronized boolean showMessage(ASTObject where, String JavaDoc message) {
150         return showMessage(where, message, false, false);
151     }
152
153
154     private Set firstLines = new HashSet();
155     public synchronized boolean showMessage(File source, int line, int endLine, int column, String JavaDoc message, boolean isError) {
156         if (isError && compiler.getOptionDumpStack()) {
157             Thread.dumpStack();
158         }
159
160         if (source == null) {
161             errorWriter.println(PREFIX + message);
162             return true;
163         }
164
165         String JavaDoc head = source.getPath() + ":" + line + ":" + column + ": ";
166         String JavaDoc firstLine = head+message;
167         if (firstLines.contains(firstLine)) return false;
168         firstLines.add(firstLine);
169         errorWriter.println(PREFIX + firstLine);
170         if (endLine > line) {
171             printLines(source, line, endLine);
172         } else {
173             int tabcount = printLine(source, line);
174             if (tabcount != -1) {
175                column += tabcount * 7;
176                printCaretLine(column);
177             }
178         }
179         return true;
180     }
181
182     public synchronized boolean showMessage(ASTObject where, String JavaDoc message, boolean showAll, boolean isError) {
183         if (where == null) {
184             return showMessage(null, -1, -1, -1, message, isError);
185         }
186         if (where.getSourceFile() == null || where.getBeginLine() == -1) {
187             
188             ASTObject newWhere = where.getSourceLocation().getSourceObject();
189             if (newWhere != null) {
190                 
191                 if (showMessage(newWhere, message, showAll, isError)) {
192                     if (newWhere.getBeginLine() == -1 || compiler.getOptions().developer) {
193                         errorWriter.println(" in generated code: " + where.unparse());
194                     }
195                     return true;
196                 } else {
197                     return false;
198                 }
199             }
200         }
201         int endLine = showAll ? where.getEndLine() : -1;
202         return showMessage(where.getSourceFile(), where.getBeginLine(), endLine,
203                     where.getBeginColumn(), message, isError);
204     }
205
206
207     protected int errors = 0;
208     public synchronized void showError(ASTObject where, String JavaDoc message) {
209         if (showMessage(where, message, false, true)) errors += 1;
210     }
211
212     public synchronized void showError(File source, int line, int column, String JavaDoc message) {
213         if (showMessage(source, line, -1, column, message, true)) errors += 1;
214     }
215
216     protected int warnings = 0;
217     public synchronized void showWarning(ASTObject where, String JavaDoc message) {
218         // we only show warnings when there are no pending errors
219
if (errors > 0) return;
220         if (showMessage(where, message+" (warning)")) warnings += 1;
221     }
222
223
224     public synchronized void showWarning(ASTObject where, Message message) {
225         if (message.shouldDisplay(getCompiler())) {
226             showWarning(where, message.getString());
227         }
228     }
229             
230
231
232     public synchronized void showWarnings() {
233         if (warnings > 0) {
234             errorWriter.println(PREFIX + Integer.toString(warnings)+" warnings");
235         }
236     }
237
238     public synchronized boolean willExitWithErrors() {
239         return errors > 0;
240     }
241
242     public synchronized void exitOnErrors() {
243         if (errors > 0) {
244             showWarnings();
245             errorWriter.println(PREFIX + Integer.toString(errors)+" errors");
246             if (compiler.getOptionIgnoreErrors()) return;
247
248             throw new CompilerErrors(errors);
249         }
250     }
251
252     public static class Message {
253         private String JavaDoc message;
254         public Message(String JavaDoc message) {
255             this.message = message;
256         }
257         
258         public boolean shouldDisplay(JavaCompiler compiler) {
259             return compiler.getOptions().Xlint;
260         }
261         public String JavaDoc getString() { return message; }
262     }
263
264
265     public static final Message invalidAbsoluteTypeName =
266         new Message("No match for this type");
267         
268     public static final Message invalidWildcardTypeName =
269         new Message("No matches for this type pattern");
270         
271
272 }
273
Popular Tags