当前位置 博文首页 > xzh_blog:Java忽略警告注解@SuppressWarnings

    xzh_blog:Java忽略警告注解@SuppressWarnings

    作者:[db:作者] 时间:2021-08-07 19:00

    ?

    // 忽略全部类型的警告
    @SuppressWarnings("all")    
    
    // 忽略未检查的转化,例如集合没有指定类型的警告
    @SuppressWarnings("unchecked") 
      
    // 忽略未使用的变量的警告
    @SuppressWarnings("unused") 
    
    // 忽略与使用Closeable类型资源相关的警告
    @SuppressWarnings("resource")  
    
    // 忽略在类路径,原文件路径中有不存在的路径的警告
    @SuppressWarnings("path")   
    
    // 忽略使用了某些不赞成使用的类和方法的警告
    @SuppressWarnings("deprecation") 
    
    // 忽略switch语句执行到底没有break关键字的警告
    @SuppressWarnings("fallthrough") 
    
    // 忽略某类实现Serializable,但是没有定义serialVersionUID的警告
    @SuppressWarnings("serial") 
    
    // 忽略没有传递带有泛型的参数的警告
    @SuppressWarnings("rawtypes") 

    源码?

    package java.lang;
    
    import java.lang.annotation.*;
    import static java.lang.annotation.ElementType.*;
    
    /**
     * Indicates that the named compiler warnings should be suppressed in the
     * annotated element (and in all program elements contained in the annotated
     * element).  Note that the set of warnings suppressed in a given element is
     * a superset of the warnings suppressed in all containing elements.  For
     * example, if you annotate a class to suppress one warning and annotate a
     * method to suppress another, both warnings will be suppressed in the method.
     *
     * <p>As a matter of style, programmers should always use this annotation
     * on the most deeply nested element where it is effective.  If you want to
     * suppress a warning in a particular method, you should annotate that
     * method rather than its class.
     *
     * @author Josh Bloch
     * @since 1.5
     * @jls 4.8 Raw Types
     * @jls 4.12.2 Variables of Reference Type
     * @jls 5.1.9 Unchecked Conversion
     * @jls 5.5.2 Checked Casts and Unchecked Casts
     * @jls 9.6.3.5 @SuppressWarnings
     */
    @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface SuppressWarnings {
        /**
         * The set of warnings that are to be suppressed by the compiler in the
         * annotated element.  Duplicate names are permitted.  The second and
         * successive occurrences of a name are ignored.  The presence of
         * unrecognized warning names is <i>not</i> an error: Compilers must
         * ignore any warning names they do not recognize.  They are, however,
         * free to emit a warning if an annotation contains an unrecognized
         * warning name.
         *
         * <p> The string {@code "unchecked"} is used to suppress
         * unchecked warnings. Compiler vendors should document the
         * additional warning names they support in conjunction with this
         * annotation type. They are encouraged to cooperate to ensure
         * that the same names work across multiple compilers.
         * @return the set of warnings to be suppressed
         */
        String[] value();
    }
    

    cs