gpt4 book ai didi

android-studio - 防止 Android Studio Inspection 将 Java Stream 链标记为包含可能的 NullPointerException

转载 作者:行者123 更新时间:2023-12-03 17:54:08 26 4
gpt4 key购买 nike

我正在编写一个实用程序方法来提取一组包装的不可打包对象:

public interface UnparcelableHolder<U> {
@Nullable U getUnparcelable();
}

public final class FragmentUtil {
@Nullable
public static <U> List<U> getUnparcelableHolderListArgument(
@Nonnull Fragment fragment,
@Nonnull Class<UnparcelableHolder<U>> unparcelableHolderClass,
@Nonnull String key
) {
@Nullable final Bundle arguments = fragment.getArguments();
if (arguments == null) {
return null;
} else {
@Nullable final Parcelable[] parcelableArray = arguments.getParcelableArray(key);
if (parcelableArray == null) {
return null;
} else {
return Arrays
.stream(parcelableArray)
.filter(unparcelableHolderClass::isInstance)
.map(unparcelableHolderClass::cast)
.filter(Objects::nonNull)
.map(UnparcelableHolder::getUnparcelable)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
if (unparcelableHolderClass.isInstance(parcelable)) {
@Nonnull final UnparcelableHolder<U> unparcelableHolder =
Objects.requireNonNull(unparcelableHolderClass.cast(parcelable));
return unparcelableHolder.getUnparcelable();
} else {
return null;
}
}
}
}

Android Studio 警告我,我的 .map(UnparcelableHolder::getUnparcelable) 调用可能会导致 NullPointerException。由于我之前的 filter(Objects::nonNull) 调用,这应该是不可能的。我如何告诉 Android Studio 的检查器我的代码是干净的?

这是一个 MCVE is available on github使用 Android Studio 3.4 beta 2 构建:

build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.github.hborders.streamsnonnulljsr305"
minSdkVersion 28
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.code.findbugs:jsr305:3.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

MainActivity.java:

package com.github.hborders.streamsnonnulljsr305;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class MainActivity extends AppCompatActivity {

class Foo {
@Nonnull
private final String string;

Foo(@Nonnull String string) {
this.string = string;
}

@Nonnull
String getString() {
return string;
}
}

class Bar {
@Nullable
private final Foo foo;

Bar(@Nullable Foo foo) {
this.foo = foo;
}

@Nullable
Foo getFoo() {
return foo;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final Bar bar1 = new Bar(new Foo("foo"));
final Bar bar2 = new Bar(null);
final Bar[] bars = new Bar[]{
null,
bar1,
bar2,
};
final List<String> strings = Arrays
.stream(bars)
.map(Bar::getFoo)
.filter(Objects::nonNull)
.map(Foo::getString)
.collect(Collectors.toList());
System.out.println("strings: " + strings);

}
}

同样的问题发生在 .map(Foo::getString) 调用上。具有讽刺意味的是,尽管肯定会抛出 NullPointerException,Android Studio 并没有提示我的 .map(Bar::getFoo) 调用。

最佳答案

这是一个 Android Studios 错误,因为 Android Studios 的建议在这里都不起作用。

没有警告:

No Warning

应用建议并得到警告:

Warning

它还建议在 .filter(Objects::nonNull) 步骤已经存在时插入。

Insert step

所以这绝对是一个 AS 错误。

对于此问题,这是一个真正的 MCVE:

import android.support.annotation.Nullable; // or any nullable you care to use

import java.util.Arrays;
import java.util.Objects;

public class MCVE {

class Foo {
}

class Bar {
@Nullable
private final Foo foo;

Bar(@Nullable Foo foo) {
this.foo = foo;
}

@Nullable
Foo getFoo() {
return foo;
}
}

public void mcve() {
final Bar[] bars = new Bar[]{
new Bar(new Foo()),
};
Arrays.stream(bars)
.map(Bar::getFoo)
.filter(Objects::nonNull)
.map(Foo::toString);
}
}

关于android-studio - 防止 Android Studio Inspection 将 Java Stream 链标记为包含可能的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54360011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com