<small id='DAVQC'></small><noframes id='DAVQC'>

    1. <legend id='DAVQC'><style id='DAVQC'><dir id='DAVQC'><q id='DAVQC'></q></dir></style></legend>

      1. <i id='DAVQC'><tr id='DAVQC'><dt id='DAVQC'><q id='DAVQC'><span id='DAVQC'><b id='DAVQC'><form id='DAVQC'><ins id='DAVQC'></ins><ul id='DAVQC'></ul><sub id='DAVQC'></sub></form><legend id='DAVQC'></legend><bdo id='DAVQC'><pre id='DAVQC'><center id='DAVQC'></center></pre></bdo></b><th id='DAVQC'></th></span></q></dt></tr></i><div id='DAVQC'><tfoot id='DAVQC'></tfoot><dl id='DAVQC'><fieldset id='DAVQC'></fieldset></dl></div>
          <bdo id='DAVQC'></bdo><ul id='DAVQC'></ul>
        <tfoot id='DAVQC'></tfoot>
      2. 在 Android Studio 中使用 @NonNull 注解的正确方法

        时间:2023-09-28

          1. <i id='4Tvpr'><tr id='4Tvpr'><dt id='4Tvpr'><q id='4Tvpr'><span id='4Tvpr'><b id='4Tvpr'><form id='4Tvpr'><ins id='4Tvpr'></ins><ul id='4Tvpr'></ul><sub id='4Tvpr'></sub></form><legend id='4Tvpr'></legend><bdo id='4Tvpr'><pre id='4Tvpr'><center id='4Tvpr'></center></pre></bdo></b><th id='4Tvpr'></th></span></q></dt></tr></i><div id='4Tvpr'><tfoot id='4Tvpr'></tfoot><dl id='4Tvpr'><fieldset id='4Tvpr'></fieldset></dl></div>
          2. <legend id='4Tvpr'><style id='4Tvpr'><dir id='4Tvpr'><q id='4Tvpr'></q></dir></style></legend>

            <small id='4Tvpr'></small><noframes id='4Tvpr'>

          3. <tfoot id='4Tvpr'></tfoot>

                <tbody id='4Tvpr'></tbody>
                <bdo id='4Tvpr'></bdo><ul id='4Tvpr'></ul>
                  本文介绍了在 Android Studio 中使用 @NonNull 注解的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想在 Android 中使用 @NonNull 注释,但我想不出正确的方法.我建议你这个例子:

                  I'd like to use the @NonNull annotation in Android, but I can't figure out just the right way to do it. I propose you this example:

                  public void doStuff(@NonNull String s){
                       //do work with s...    
                  }
                  

                  所以当我调用 doStuff(null) 时,IDE 会给我一个警告.问题是我不能依赖这个注释,因为像 this 问题指出,它们不会传播很远.所以我想对我的方法进行空检查,如下所示:

                  So when i call doStuff(null) the IDE will give me a warning. The problem is that I cannot rely on this annotation since, like this question points out, they don't propagate very far. So I'd like to put a null check on my method, like this:

                   if(s==null) throw new IllegalAgrumentException();
                  

                  但假设 s!=null 的 IDE 会警告我 s==null 始终为假.我想知道最好的方法是什么.

                  But the IDE, assuming that s!=null, will warn me that s==null is always false. I'd like to know what is the best way to do this.

                  我个人认为应该有一个像 @ShouldntBeNull 这样的注解 only 检查并警告 null 没有传递给它,但 没有 在检查值为 null 时会报错.

                  I personally think that there should be an annotation like @ShouldntBeNull that only checks and warns that null isn't passed to it, but doesn't complains when the value is null checked.

                  推荐答案

                  你可以使用Objects.requireNonNull 为此.它将在内部进行检查(因此 IDE 不会在您的函数上显示警告)并引发 NullPointerException 当参数为null:

                  You can use Objects.requireNonNull for that. It will do the check internally (so the IDE will not show a warning on your function) and raise a NullPointerException when the parameter is null:

                  public MyMethod(@NonNull Context pContext) {
                      Objects.requireNonNull(pContext);
                      ...
                  }
                  

                  如果你想抛出另一个异常或使用 API 级别 <19,然后你可以只做你自己的助手类来实现同样的检查.例如

                  If you want to throw another exception or use API level < 19, then you can just make your own helper-class to implement the same check. e.g.

                  public class Check {
                      public static <T> T requireNonNull(T obj) {
                          if (obj == null)
                              throw new IllegalArgumentException();
                          return obj;
                      }
                  }
                  

                  并像这样使用它:

                  public MyMethod(@NonNull Context pContext) {
                      Check.requireNonNull(pContext);
                      ...
                  }
                  

                  这篇关于在 Android Studio 中使用 @NonNull 注解的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为特定注释禁用 Jackson 映射器 下一篇:与 xml 配置文件相比,注释(非编译器)的优点/缺点

                  相关文章

                  最新文章

                  <i id='TVaYY'><tr id='TVaYY'><dt id='TVaYY'><q id='TVaYY'><span id='TVaYY'><b id='TVaYY'><form id='TVaYY'><ins id='TVaYY'></ins><ul id='TVaYY'></ul><sub id='TVaYY'></sub></form><legend id='TVaYY'></legend><bdo id='TVaYY'><pre id='TVaYY'><center id='TVaYY'></center></pre></bdo></b><th id='TVaYY'></th></span></q></dt></tr></i><div id='TVaYY'><tfoot id='TVaYY'></tfoot><dl id='TVaYY'><fieldset id='TVaYY'></fieldset></dl></div>
                  <legend id='TVaYY'><style id='TVaYY'><dir id='TVaYY'><q id='TVaYY'></q></dir></style></legend>
                1. <tfoot id='TVaYY'></tfoot>

                      • <bdo id='TVaYY'></bdo><ul id='TVaYY'></ul>

                      <small id='TVaYY'></small><noframes id='TVaYY'>