为什么 AS 告诉我 `(ob instanceof Button)` 总是 `false`?
Why is AS telling me that `(ob instanceof Button)` is always `false`?
为什么 AS 告诉我 (ob instanceof Button)
总是 false
?
另外,它给了"hint"到'Cast to Button'
。如果我接受了暗示,什么都不会改变。
void setTextSizeForField(int textSize, Object ... obs){
for(Object ob: obs)
if (ob instanceof EditText) ((EditText) ob).setTextSize ( textSize );
else if (ob instanceof TextView) ((TextView) ob).setTextSize ( textSize );
else if (ob instanceof Button) ((Button) ob).setTextSize ( textSize/10 );
}
邮件全文为:
Condition 'ob instanceof Button' is always 'false' less... (Ctrl+F1)
This inspection analyzes method control and data flow to report possible conditions that are
always true or false, expressions whose value is statically proven to be constant, and
situations that can lead to nullability contract violations.
Variables, method parameters and return values marked as @Nullable or @NotNull are
treated as nullable (or not-null, respectively) and used during the analysis to check
nullability contracts, e.g. report possible NullPointerException errors.
More complex contracts can be defined using @Contract annotation, for example:
@Contract("_, null -> null") — method returns null if its second argument is null
@Contract("_, null -> null; _, !null -> !null") — method returns null if
its second argument is null and not-null otherwise
@Contract("true -> fail") — a typical assertFalse method which throws an
exception if true is passed to it
The inspection can be configured to use custom @Nullable
@NotNull annotations (by default the ones from annotations.jar will be used)
我没有使用注释。
* 编辑 *
如果我交换两个 else if
语句,我不会收到任何警告并且代码运行正常。
所以我想 "never mind" 可能是正常的,但你认为 AS 在做什么?
Documentation 表明 Button
扩展了 TextView
,因此永远不会采用该分支。如果您反转 Button
和 TextView
的检查,那么将采用不同的路径。
为什么 AS 告诉我 (ob instanceof Button)
总是 false
?
另外,它给了"hint"到'Cast to Button'
。如果我接受了暗示,什么都不会改变。
void setTextSizeForField(int textSize, Object ... obs){
for(Object ob: obs)
if (ob instanceof EditText) ((EditText) ob).setTextSize ( textSize );
else if (ob instanceof TextView) ((TextView) ob).setTextSize ( textSize );
else if (ob instanceof Button) ((Button) ob).setTextSize ( textSize/10 );
}
邮件全文为:
Condition 'ob instanceof Button' is always 'false' less... (Ctrl+F1)
This inspection analyzes method control and data flow to report possible conditions that are
always true or false, expressions whose value is statically proven to be constant, and
situations that can lead to nullability contract violations.
Variables, method parameters and return values marked as @Nullable or @NotNull are
treated as nullable (or not-null, respectively) and used during the analysis to check
nullability contracts, e.g. report possible NullPointerException errors.
More complex contracts can be defined using @Contract annotation, for example:
@Contract("_, null -> null") — method returns null if its second argument is null
@Contract("_, null -> null; _, !null -> !null") — method returns null if
its second argument is null and not-null otherwise
@Contract("true -> fail") — a typical assertFalse method which throws an
exception if true is passed to it
The inspection can be configured to use custom @Nullable
@NotNull annotations (by default the ones from annotations.jar will be used)
我没有使用注释。
* 编辑 *
如果我交换两个 else if
语句,我不会收到任何警告并且代码运行正常。
所以我想 "never mind" 可能是正常的,但你认为 AS 在做什么?
Documentation 表明 Button
扩展了 TextView
,因此永远不会采用该分支。如果您反转 Button
和 TextView
的检查,那么将采用不同的路径。