Coverage Report - org.codehaus.plexus.compiler.CompilerMessage
 
Classes in this File Line Coverage Branch Coverage Complexity
CompilerMessage
0%
0/52
0%
0/22
1,812
CompilerMessage$Kind
0%
0/9
N/A
1,812
 
 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  0
     {
 110  0
         this.file = file;
 111  0
         this.kind = error ? Kind.ERROR : Kind.WARNING;
 112  0
         this.startline = startline;
 113  0
         this.startcolumn = startcolumn;
 114  0
         this.endline = endline;
 115  0
         this.endcolumn = endcolumn;
 116  
 
 117  0
         this.message = cleanupMessage( message );
 118  0
     }
 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  0
     {
 134  0
         this.file = file;
 135  0
         this.kind = kind;
 136  0
         this.startline = startline;
 137  0
         this.startcolumn = startcolumn;
 138  0
         this.endline = endline;
 139  0
         this.endcolumn = endcolumn;
 140  0
         this.message = cleanupMessage( message );
 141  0
     }
 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  0
     {
 152  0
         this.kind = Kind.WARNING;
 153  0
         this.message = cleanupMessage( message );
 154  0
     }
 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  0
     {
 167  0
         this.kind = error ? Kind.ERROR : Kind.WARNING;
 168  0
         this.message = cleanupMessage( message );
 169  0
     }
 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  0
     {
 180  0
         this.kind = kind;
 181  0
         this.message = cleanupMessage( message );
 182  0
     }
 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  0
         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  0
         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  0
         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  0
         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  0
         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  0
         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  0
         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  0
         return kind;
 269  
     }
 270  
 
 271  
     @Override
 272  
     public String toString()
 273  
     {
 274  0
         if ( file != null )
 275  
         {
 276  0
             if ( startline != 0 )
 277  
             {
 278  0
                 if ( startcolumn != 0 )
 279  
                 {
 280  0
                     return file + ":" + "[" + startline + "," + startcolumn + "] " + message;
 281  
                 }
 282  
                 else
 283  
                 {
 284  0
                     return file + ":" + "[" + startline + "] " + message;
 285  
                 }
 286  
             }
 287  
             else
 288  
             {
 289  0
                 return file + ": " + message;
 290  
             }
 291  
         }
 292  
         else
 293  
         {
 294  0
             return message;
 295  
         }
 296  
     }
 297  
 
 298  
     private String cleanupMessage( String msg )
 299  
     {
 300  0
         if ( kind == Kind.NOTE && msg.toLowerCase()
 301  0
                                      .startsWith( JDK_6_NOTE_PREFIX ) )
 302  
         {
 303  0
             msg = msg.substring( JDK_6_NOTE_PREFIX.length() );
 304  
         }
 305  0
         else if ( ( kind == Kind.WARNING || kind == Kind.MANDATORY_WARNING ) && msg.toLowerCase()
 306  0
                                                                                    .startsWith( JDK_6_WARNING_PREFIX ) )
 307  
         {
 308  0
             msg = msg.substring( JDK_6_WARNING_PREFIX.length() );
 309  
         }
 310  
 
 311  0
         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  0
     public enum Kind
 320  
     {
 321  
         /**
 322  
          * Problem which prevents the tool's normal completion.
 323  
          */
 324  0
         ERROR( "error" ),
 325  
         /**
 326  
          * Problem similar to a warning, but is mandated by the tool's specification.
 327  
          */
 328  0
         MANDATORY_WARNING( "mandatory_warning" ),
 329  
         /**
 330  
          * Informative message from the tool.
 331  
          */
 332  0
         NOTE( "note" ),
 333  
         /**
 334  
          * Diagnostic which does not fit within the other kinds.
 335  
          */
 336  0
         OTHER( "other" ),
 337  
         /**
 338  
          * Problem which does not usually prevent the tool from completing normally.
 339  
          */
 340  0
         WARNING( "warning" );
 341  
 
 342  
         private String type;
 343  
 
 344  
         private Kind( final String type )
 345  0
         {
 346  0
             this.type = type;
 347  0
         }
 348  
     }
 349  
 }