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  {
53      private static final String JDK_6_NOTE_PREFIX = "note: ";
54  
55      private static final String JDK_6_WARNING_PREFIX = "warning: ";
56  
57      /**
58       * The kind of message.
59       */
60      private final Kind kind;
61  
62      /**
63       * The start line number of the offending program text
64       */
65      private int startline;
66  
67      /**
68       * The start column number of the offending program text
69       */
70      private int startcolumn;
71  
72      /**
73       * The end line number of the offending program text
74       */
75      private int endline;
76  
77      /**
78       * The end column number of the offending program text
79       */
80      private int endcolumn;
81  
82      /**
83       * The name of the file containing the offending program text
84       */
85      private String file;
86  
87      /**
88       * The actual message text produced by the language processor
89       */
90      private final String message;
91  
92  
93      /**
94       * Constructs a compiler message.
95       *
96       * @param file        The name of the file containing the offending program text
97       * @param error       <code>true</code> if this is a error message, or <code>false</code> if it
98       *                    is a warning message
99       * @param startline   The start line number of the offending program text
100      * @param startcolumn The start column number of the offending program text
101      * @param endline     The end line number of the offending program text
102      * @param endcolumn   The end column number of the offending program text
103      * @param message     The actual message text produced by the language processor
104      * @deprecated Use {@link #CompilerMessage(String, Kind, int, int, int, int, String)} instead
105      */
106     @Deprecated
107     public CompilerMessage( final String file, final boolean error, final int startline, final int startcolumn, final int endline, final int endcolumn,
108                             final String message )
109     {
110         this.file = file;
111         this.kind = error ? Kind.ERROR : Kind.WARNING;
112         this.startline = startline;
113         this.startcolumn = startcolumn;
114         this.endline = endline;
115         this.endcolumn = endcolumn;
116 
117         this.message = cleanupMessage( message );
118     }
119 
120     /**
121      * Constructs a compiler message.
122      *
123      * @param file        The name of the file containing the offending program text
124      * @param kind        The kind of message
125      * @param startline   The start line number of the offending program text
126      * @param startcolumn The start column number of the offending program text
127      * @param endline     The end line number of the offending program text
128      * @param endcolumn   The end column number of the offending program text
129      * @param message     The actual message text produced by the language processor
130      */
131     public CompilerMessage( final String file, final Kind kind, final int startline, final int startcolumn, final int endline, final int endcolumn,
132                             final String message )
133     {
134         this.file = file;
135         this.kind = kind;
136         this.startline = startline;
137         this.startcolumn = startcolumn;
138         this.endline = endline;
139         this.endcolumn = endcolumn;
140         this.message = cleanupMessage( message );
141     }
142 
143     /**
144      * The warning message constructor.
145      *
146      * @param message The actual message text produced by the language processor
147      * @deprecated Use {@link #CompilerMessage(String, Kind)} instead
148      */
149     @Deprecated
150     public CompilerMessage( final String message )
151     {
152         this.kind = Kind.WARNING;
153         this.message = cleanupMessage( message );
154     }
155 
156     /**
157      * Constructs a compiler message.
158      *
159      * @param message The actual message text produced by the language processor
160      * @param error   <code>true</code> if this is a error message, or <code>false</code> if it
161      *                is a warning message
162      * @deprecated Use {@link #CompilerMessage(String, Kind)} instead
163      */
164     @Deprecated
165     public CompilerMessage( final String message, final boolean error )
166     {
167         this.kind = error ? Kind.ERROR : Kind.WARNING;
168         this.message = cleanupMessage( message );
169     }
170 
171     /**
172      * Constructs a compiler message.
173      *
174      * @param message The actual message text produced by the language processor
175      * @param kind    The kind of message
176      * @since 2.0
177      */
178     public CompilerMessage( final String message, final Kind kind )
179     {
180         this.kind = kind;
181         this.message = cleanupMessage( message );
182     }
183 
184     /**
185      * Returns the filename associated with this compiler message.
186      *
187      * @return The filename associated with this compiler message
188      */
189     public String getFile()
190     {
191         return file;
192     }
193 
194     /**
195      * Asserts whether this is an error message or not.
196      *
197      * @return Whether the message is an error message
198      */
199     public boolean isError()
200     {
201         return kind == Kind.ERROR;
202     }
203 
204     /**
205      * Returns the starting line number of the program text originating this compiler
206      * message.
207      *
208      * @return The starting line number of the program text originating this message
209      */
210     public int getStartLine()
211     {
212         return startline;
213     }
214 
215     /**
216      * Returns the starting column number of the program text originating this
217      * compiler message.
218      *
219      * @return The starting column number of the program text originating this
220      *         message
221      */
222     public int getStartColumn()
223     {
224         return startcolumn;
225     }
226 
227     /**
228      * Return the ending line number of the program text originating this compiler
229      * message.
230      *
231      * @return The ending line number of the program text originating this message
232      */
233     public int getEndLine()
234     {
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     {
247         return endcolumn;
248     }
249 
250     /**
251      * Returns the message produced by the language processor.
252      *
253      * @return The message produced by the language processor
254      */
255     public String getMessage()
256     {
257         return message;
258     }
259 
260     /**
261      * Returns the kind of the compiler message.
262      *
263      * @return the kind of the message
264      * @since 2.0
265      */
266     public Kind getKind()
267     {
268         return kind;
269     }
270 
271     @Override
272     public String toString()
273     {
274         if ( file != null )
275         {
276             if ( startline != 0 )
277             {
278                 if ( startcolumn != 0 )
279                 {
280                     return file + ":" + "[" + startline + "," + startcolumn + "] " + message;
281                 }
282                 else
283                 {
284                     return file + ":" + "[" + startline + "] " + message;
285                 }
286             }
287             else
288             {
289                 return file + ": " + message;
290             }
291         }
292         else
293         {
294             return message;
295         }
296     }
297 
298     private String cleanupMessage( String msg )
299     {
300         if ( kind == Kind.NOTE && msg.toLowerCase()
301                                      .startsWith( JDK_6_NOTE_PREFIX ) )
302         {
303             msg = msg.substring( JDK_6_NOTE_PREFIX.length() );
304         }
305         else if ( ( kind == Kind.WARNING || kind == Kind.MANDATORY_WARNING ) && msg.toLowerCase()
306                                                                                    .startsWith( JDK_6_WARNING_PREFIX ) )
307         {
308             msg = msg.substring( JDK_6_WARNING_PREFIX.length() );
309         }
310 
311         return msg;
312     }
313 
314     /**
315      * As we are still 1.5 required we use a wrapper to Diagnostic.Kind and some compilers don't know jdk constants.
316      *
317      * @since 2.0
318      */
319     public enum Kind
320     {
321         /**
322          * Problem which prevents the tool's normal completion.
323          */
324         ERROR( "error" ),
325         /**
326          * Problem similar to a warning, but is mandated by the tool's specification.
327          */
328         MANDATORY_WARNING( "mandatory_warning" ),
329         /**
330          * Informative message from the tool.
331          */
332         NOTE( "note" ),
333         /**
334          * Diagnostic which does not fit within the other kinds.
335          */
336         OTHER( "other" ),
337         /**
338          * Problem which does not usually prevent the tool from completing normally.
339          */
340         WARNING( "warning" );
341 
342         private String type;
343 
344         private Kind( final String type )
345         {
346             this.type = type;
347         }
348     }
349 }