美文网首页JavaAndroid
Java suppress warning

Java suppress warning

作者: JaedenKil | 来源:发表于2019-02-18 14:51 被阅读0次

Sometimes Java editor warns you something, but you're pretty sure that is exactly what you want.

        try {
            UiObject2 systemScoreObj = systemParent.getChildren().get(1);
            return false;
        } catch (java.lang.IndexOutOfBoundsException ex) {
            return true;
        }

I simply want to return "true" or "false", don't care about the "UiObject2", so add suppress warning to the method.

    @SuppressWarnings("unused")
    private boolean blankResult(int round) throws IOException {
        ...
        UiObject2 systemParent = system.getParent();
        try {
            UiObject2 systemScoreObj = systemParent.getChildren().get(1);
            return false;
        } catch (java.lang.IndexOutOfBoundsException ex) {
            return true;
        }
    }

Typically we can type javac -X to get a list of useful suppress warning messages:

> javac -X
  -Xlint                     Enable recommended warnings
  -Xlint:{all,auxiliaryclass,cast,classfile,deprecation,dep-ann,divzero,empty,fallthrough,finally,options,overloads,overrides,path,processing,rawtypes,serial,static,try,unchecked,varargs,-auxiliaryclass,-cast,-classfile,-deprecation,-dep-ann,-divzero,-empty,-fallthrough,-finally,-options,-overloads,-overrides,-path,-processing,-rawtypes,-serial,-static,-try,-unchecked,-varargs,none} Enable or disable specific warnings

But there are some special suppress warning messages:

  • @SuppressWarnings("FieldCanBeLocal")
    Original warning: Field can be converted to a local variable
  • @SuppressWarnings("unused")
    Original warning: Variable xx is never used
  • @SuppressWarnings("unchecked")
    Original warning: Unchecked call ... or Unchecked assignment...
  • @SuppressWarnings("SameParameterValue")
    Original warning: Actual value of parameter xx is always yy
  • @SuppressWarnings("unused")
    Original warning: Parameter xx is never used
  • @SuppressWarnings({"unused", "SameParameterValue"})
    Combination of the two above
But be aware, most of the time, when it warns you, you should follow the editor's advice rather than add @SuppressWarnings.

相关文章

网友评论

    本文标题:Java suppress warning

    本文链接:https://www.haomeiwen.com/subject/nspaeqtx.html