View Javadoc
1   package org.codehaus.plexus.compiler;
2   
3   /**
4    * The MIT License
5    *
6    * Copyright (c) 2005, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  /**
28   *
29   * Copyright 2004 The Apache Software Foundation
30   *
31   *  Licensed under the Apache License, Version 2.0 (the "License");
32   *  you may not use this file except in compliance with the License.
33   *  You may obtain a copy of the License at
34   *
35   *     http://www.apache.org/licenses/LICENSE-2.0
36   *
37   *  Unless required by applicable law or agreed to in writing, software
38   *  distributed under the License is distributed on an "AS IS" BASIS,
39   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40   *  See the License for the specific language governing permissions and
41   *  limitations under the License.
42   */
43  
44  /**
45   * This class encapsulates a message produced by a programming language
46   * processor (whether interpreted or compiled).
47   *
48   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
49   * @since 2.0
50   */
51  public class CompilerMessage {
52      private static final String JDK_6_NOTE_PREFIX = "note: ";
53  
54      private static final String JDK_6_WARNING_PREFIX = "warning: ";
55  
56      /**
57       * The kind of message.
58       */
59      private final Kind kind;
60  
61      /**
62       * The start line number of the offending program text
63       */
64      private int startline;
65  
66      /**
67       * The start column number of the offending program text
68       */
69      private int startcolumn;
70  
71      /**
72       * The end line number of the offending program text
73       */
74      private int endline;
75  
76      /**
77       * The end column number of the offending program text
78       */
79      private int endcolumn;
80  
81      /**
82       * The name of the file containing the offending program text
83       */
84      private String file;
85  
86      /**
87       * The actual message text produced by the language processor
88       */
89      private final String message;
90  
91      /**
92       * Constructs a compiler message.
93       *
94       * @param file        The name of the file containing the offending program text
95       * @param error       <code>true</code> if this is a error message, or <code>false</code> if it
96       *                    is a warning message
97       * @param startline   The start line number of the offending program text
98       * @param startcolumn The start column number of the offending program text
99       * @param endline     The end line number of the offending program text
100      * @param endcolumn   The end column number of the offending program text
101      * @param message     The actual message text produced by the language processor
102      * @deprecated Use {@link #CompilerMessage(String, Kind, int, int, int, int, String)} instead
103      */
104     @Deprecated
105     public CompilerMessage(
106             final String file,
107             final boolean error,
108             final int startline,
109             final int startcolumn,
110             final int endline,
111             final int endcolumn,
112             final String message) {
113         this.file = file;
114         this.kind = error ? Kind.ERROR : Kind.WARNING;
115         this.startline = startline;
116         this.startcolumn = startcolumn;
117         this.endline = endline;
118         this.endcolumn = endcolumn;
119 
120         this.message = cleanupMessage(message);
121     }
122 
123     /**
124      * Constructs a compiler message.
125      *
126      * @param file        The name of the file containing the offending program text
127      * @param kind        The kind of message
128      * @param startline   The start line number of the offending program text
129      * @param startcolumn The start column number of the offending program text
130      * @param endline     The end line number of the offending program text
131      * @param endcolumn   The end column number of the offending program text
132      * @param message     The actual message text produced by the language processor
133      */
134     public CompilerMessage(
135             final String file,
136             final Kind kind,
137             final int startline,
138             final int startcolumn,
139             final int endline,
140             final int endcolumn,
141             final String message) {
142         this.file = file;
143         this.kind = kind;
144         this.startline = startline;
145         this.startcolumn = startcolumn;
146         this.endline = endline;
147         this.endcolumn = endcolumn;
148         this.message = cleanupMessage(message);
149     }
150 
151     /**
152      * The warning message constructor.
153      *
154      * @param message The actual message text produced by the language processor
155      * @deprecated Use {@link #CompilerMessage(String, Kind)} instead
156      */
157     @Deprecated
158     public CompilerMessage(final String message) {
159         this.kind = Kind.WARNING;
160         this.message = cleanupMessage(message);
161     }
162 
163     /**
164      * Constructs a compiler message.
165      *
166      * @param message The actual message text produced by the language processor
167      * @param error   <code>true</code> if this is a error message, or <code>false</code> if it
168      *                is a warning message
169      * @deprecated Use {@link #CompilerMessage(String, Kind)} instead
170      */
171     @Deprecated
172     public CompilerMessage(final String message, final boolean error) {
173         this.kind = error ? Kind.ERROR : Kind.WARNING;
174         this.message = cleanupMessage(message);
175     }
176 
177     /**
178      * Constructs a compiler message.
179      *
180      * @param message The actual message text produced by the language processor
181      * @param kind    The kind of message
182      * @since 2.0
183      */
184     public CompilerMessage(final String message, final Kind kind) {
185         this.kind = kind;
186         this.message = cleanupMessage(message);
187     }
188 
189     /**
190      * Returns the filename associated with this compiler message.
191      *
192      * @return The filename associated with this compiler message
193      */
194     public String getFile() {
195         return file;
196     }
197 
198     /**
199      * Asserts whether this is an error message or not.
200      *
201      * @return Whether the message is an error message
202      */
203     public boolean isError() {
204         return kind == Kind.ERROR;
205     }
206 
207     /**
208      * Returns the starting line number of the program text originating this compiler
209      * message.
210      *
211      * @return The starting line number of the program text originating this message
212      */
213     public int getStartLine() {
214         return startline;
215     }
216 
217     /**
218      * Returns the starting column number of the program text originating this
219      * compiler message.
220      *
221      * @return The starting column number of the program text originating this
222      *         message
223      */
224     public int getStartColumn() {
225         return startcolumn;
226     }
227 
228     /**
229      * Return the ending line number of the program text originating this compiler
230      * message.
231      *
232      * @return The ending line number of the program text originating this message
233      */
234     public int getEndLine() {
235         return endline;
236     }
237 
238     /**
239      * Returns the ending column number of the program text originating this
240      * compiler message.
241      *
242      * @return The ending column number of the program text originating this
243      *         message
244      */
245     public int getEndColumn() {
246         return endcolumn;
247     }
248 
249     /**
250      * Returns the message produced by the language processor.
251      *
252      * @return The message produced by the language processor
253      */
254     public String getMessage() {
255         return message;
256     }
257 
258     /**
259      * Returns the kind of the compiler message.
260      *
261      * @return the kind of the message
262      * @since 2.0
263      */
264     public Kind getKind() {
265         return kind;
266     }
267 
268     @Override
269     public String toString() {
270         if (file != null) {
271             if (startline != 0) {
272                 if (startcolumn != 0) {
273                     return file + ":" + "[" + startline + "," + startcolumn + "] " + message;
274                 } else {
275                     return file + ":" + "[" + startline + "] " + message;
276                 }
277             } else {
278                 return file + ": " + message;
279             }
280         } else {
281             return message;
282         }
283     }
284 
285     private String cleanupMessage(String msg) {
286         if (kind == Kind.NOTE && msg.toLowerCase().startsWith(JDK_6_NOTE_PREFIX)) {
287             msg = msg.substring(JDK_6_NOTE_PREFIX.length());
288         } else if ((kind == Kind.WARNING || kind == Kind.MANDATORY_WARNING)
289                 && msg.toLowerCase().startsWith(JDK_6_WARNING_PREFIX)) {
290             msg = msg.substring(JDK_6_WARNING_PREFIX.length());
291         }
292 
293         return msg;
294     }
295 
296     /**
297      * As we are still 1.5 required we use a wrapper to Diagnostic.Kind and some compilers don't know jdk constants.
298      *
299      * @since 2.0
300      */
301     public enum Kind {
302         /**
303          * Problem which prevents the tool's normal completion.
304          */
305         ERROR("error"),
306         /**
307          * Problem similar to a warning, but is mandated by the tool's specification.
308          */
309         MANDATORY_WARNING("mandatory_warning"),
310         /**
311          * Informative message from the tool.
312          */
313         NOTE("note"),
314         /**
315          * Diagnostic which does not fit within the other kinds.
316          */
317         OTHER("other"),
318         /**
319          * Problem which does not usually prevent the tool from completing normally.
320          */
321         WARNING("warning");
322 
323         Kind(String type) {}
324     }
325 }