Minimize
Table
Bug Overview
line | bug priority | bug | bug description | existing since | author |
---|
297 | high | OBL_UNSATISFIED_OBLIGATION | Öffnet eine Methode einen Stream oder eine andere Ressource muss diese mit einem try/finally-Block geschlossen werden. Bitte Code auf eine entsprechende cleanup-Operation hin überprüfen! Details zum Fehler... | 18.06.2012-10:31 | Unbekannt |
298 | medium | SystemPrintln | Bitte keine System.(out|err).print-Ausgaben verwenden, stattdessen Logger-Klassen nutzen. Details zum Fehler... | 18.06.2012-10:31 | Unbekannt |
300 | medium | SystemPrintln | Bitte keine System.(out|err).print-Ausgaben verwenden, stattdessen Logger-Klassen nutzen. Details zum Fehler... | 18.06.2012-10:31 | Unbekannt |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.groovy.control;
18
19 import groovy.lang.GroovyClassLoader;
20
21 import java.io.File;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.io.Reader;
25 import java.net.URL;
26 import java.security.AccessController;
27 import java.security.PrivilegedAction;
28
29 import org.codehaus.groovy.GroovyBugError;
30 import org.codehaus.groovy.ast.ModuleNode;
31 import org.codehaus.groovy.control.io.FileReaderSource;
32 import org.codehaus.groovy.control.io.ReaderSource;
33 import org.codehaus.groovy.control.io.StringReaderSource;
34 import org.codehaus.groovy.control.io.URLReaderSource;
35 import org.codehaus.groovy.control.messages.Message;
36 import org.codehaus.groovy.control.messages.SimpleMessage;
37 import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
38 import org.codehaus.groovy.syntax.*;
39 import org.codehaus.groovy.tools.Utilities;
40
41 import antlr.CharScanner;
42 import antlr.MismatchedTokenException;
43 import antlr.MismatchedCharException;
44 import antlr.NoViableAltException;
45 import antlr.NoViableAltForCharException;
46
47 import com.thoughtworks.xstream.XStream;
48
49
50
51
52
53
54
55
56
57
58 public class SourceUnit extends ProcessingUnit {
59
60
61
62
63
64 private ParserPlugin parserPlugin;
65
66
67
68
69 protected ReaderSource source;
70
71
72
73
74
75
76
77 protected String name;
78
79
80
81
82 protected Reduction cst;
83
84
85
86
87 protected ModuleNode ast;
88
89
90
91
92 public SourceUnit(String name, ReaderSource source, CompilerConfiguration flags,
93 GroovyClassLoader loader, ErrorCollector er) {
94 super(flags, loader, er);
95
96 this.name = name;
97 this.source = source;
98 }
99
100
101
102
103 public SourceUnit(File source, CompilerConfiguration configuration, GroovyClassLoader loader, ErrorCollector er) {
104 this(source.getPath(), new FileReaderSource(source, configuration), configuration, loader, er);
105 }
106
107
108
109
110 public SourceUnit(URL source, CompilerConfiguration configuration, GroovyClassLoader loader, ErrorCollector er) {
111 this(source.getPath(), new URLReaderSource(source, configuration), configuration, loader, er);
112 }
113
114
115
116
117 public SourceUnit(String name, String source, CompilerConfiguration configuration,
118 GroovyClassLoader loader, ErrorCollector er) {
119 this(name, new StringReaderSource(source, configuration), configuration, loader, er);
120 }
121
122
123
124
125
126
127 public String getName() {
128 return name;
129 }
130
131
132
133
134
135 public Reduction getCST() {
136 return this.cst;
137 }
138
139
140
141
142
143 public ModuleNode getAST() {
144 return this.ast;
145 }
146
147
148
149
150
151
152 public boolean failedWithUnexpectedEOF() {
153
154
155
156 if (getErrorCollector().hasErrors()) {
157 Message last = (Message) getErrorCollector().getLastError();
158 Throwable cause = null;
159 if (last instanceof SyntaxErrorMessage) {
160 cause = ((SyntaxErrorMessage) last).getCause().getCause();
161 }
162 if (cause != null) {
163 if (cause instanceof NoViableAltException) {
164 return isEofToken(((NoViableAltException) cause).token);
165 } else if (cause instanceof NoViableAltForCharException) {
166 char badChar = ((NoViableAltForCharException) cause).foundChar;
167 return badChar == CharScanner.EOF_CHAR;
168 } else if (cause instanceof MismatchedCharException) {
169 char badChar = (char) ((MismatchedCharException) cause).foundChar;
170 return badChar == CharScanner.EOF_CHAR;
171 } else if (cause instanceof MismatchedTokenException) {
172 return isEofToken(((MismatchedTokenException) cause).token);
173 }
174 }
175 }
176 return false;
177 }
178
179 protected boolean isEofToken(antlr.Token token) {
180 return token.getType() == antlr.Token.EOF_TYPE;
181 }
182
183
184
185
186
187
188
189
190
191
192 public static SourceUnit create(String name, String source) {
193 CompilerConfiguration configuration = new CompilerConfiguration();
194 configuration.setTolerance(1);
195
196 return new SourceUnit(name, source, configuration, null, new ErrorCollector(configuration));
197 }
198
199
200
201
202
203
204 public static SourceUnit create(String name, String source, int tolerance) {
205 CompilerConfiguration configuration = new CompilerConfiguration();
206 configuration.setTolerance(tolerance);
207
208 return new SourceUnit(name, source, configuration, null, new ErrorCollector(configuration));
209 }
210
211
212
213
214
215
216
217 public void parse() throws CompilationFailedException {
218 if (this.phase > Phases.PARSING) {
219 throw new GroovyBugError("parsing is already complete");
220 }
221
222 if (this.phase == Phases.INITIALIZATION) {
223 nextPhase();
224 }
225
226
227
228
229 Reader reader = null;
230 try {
231 reader = source.getReader();
232
233
234 parserPlugin = getConfiguration().getPluginFactory().createParserPlugin();
235
236 cst = parserPlugin.parseCST(this, reader);
237
238 reader.close();
239
240 }
241 catch (IOException e) {
242 getErrorCollector().addFatalError(new SimpleMessage(e.getMessage(), this));
243 }
244 finally {
245 if (reader != null) {
246 try {
247 reader.close();
248 }
249 catch (IOException e) {
250
251 }
252 }
253 }
254 }
255
256
257
258
259 public void convert() throws CompilationFailedException {
260 if (this.phase == Phases.PARSING && this.phaseComplete) {
261 gotoPhase(Phases.CONVERSION);
262 }
263
264 if (this.phase != Phases.CONVERSION) {
265 throw new GroovyBugError("SourceUnit not ready for convert()");
266 }
267
268
269
270
271 try {
272 this.ast = parserPlugin.buildAST(this, this.classLoader, this.cst);
273 this.ast.setDescription(this.name);
274 }
275 catch (SyntaxException e) {
276 if (this.ast == null) {
277
278 this.ast = new ModuleNode(this);
279 }
280 getErrorCollector().addError(new SyntaxErrorMessage(e, this));
281 }
282
283 String property = (String) AccessController.doPrivileged(new PrivilegedAction() {
284 public Object run() {
285 return System.getProperty("groovy.ast");
286 }
287 });
288
289 if ("xml".equals(property)) {
290 saveAsXML(name, ast);
291 }
292 }
293
294 private void saveAsXML(String name, ModuleNode ast) {
295 XStream xstream = new XStream();
296 try {
OBL_UNSATISFIED_OBLIGATION
SystemPrintln
299 } catch (Exception e) {
SystemPrintln
301 e.printStackTrace();
302 }
303 }
304
305
306
307
308
309
310
311 public String getSample(int line, int column, Janitor janitor) {
312 String sample = null;
313 String text = source.getLine(line, janitor);
314
315 if (text != null) {
316 if (column > 0) {
317 String marker = Utilities.repeatString(" ", column - 1) + "^";
318
319 if (column > 40) {
320 int start = column - 30 - 1;
321 int end = (column + 10 > text.length() ? text.length() : column + 10 - 1);
322 sample = " " + text.substring(start, end) + Utilities.eol() + " " +
323 marker.substring(start, marker.length());
324 } else {
325 sample = " " + text + Utilities.eol() + " " + marker;
326 }
327 } else {
328 sample = text;
329 }
330 }
331
332 return sample;
333 }
334
335
336
337
338
339
340
341
342
343
344 public void addException(Exception e) throws CompilationFailedException {
345 getErrorCollector().addException(e, this);
346 }
347
348
349
350
351
352
353
354
355
356
357
358 public void addError(SyntaxException se) throws CompilationFailedException {
359 getErrorCollector().addError(se, this);
360 }
361
362 public ReaderSource getSource() { return source; }
363 }