1
我有一個奇怪的問題。這是我的枚舉:爲什麼android studio告訴我enum.valueOf總是正確的?
enum ErrorInByte{
ERROR_BIT0(3),
ERROR_BIT2(4),
ERROR_BIT3(5),
ERROR_BIT4(7),
ERROR_BIT5(13),
ERROR_BIT7(15),
private int value;
ErrorInByte(int value) {
this.value = value;
}
public static ErrorInByte valueOf(int value){
return intToErrorInByte.get(value);
}
private static final Map<Integer, ErrorInByte> intToErrorInByte= new HashMap<>();
static {
for (ErrorInByte type : ErrorInByte.values()) {
intToErrorInByte.put(type.value, type);
}
}
}
在我的班級我有一個如果:
int n;
//Code that changes n to a value
if(n > 0 && ErrorInByte.valueOf(n) != null){
//do stuff...
}
爲什麼機器人工作室告訴我,ErrorInByte.valueOf(n)將永遠是真的嗎?我測試了它,併爲ErrorInByte.valueOf(326)
它等於空。
警告消息:
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)
有什麼辦法去除警告?我討厭警告...
因爲你沒有得到的地圖元素具有與價值326 –
確保值存在與否 –
你的問題,你的解釋沒有任何意義。首先你說它總是空的,然後你說它總是真的...... –