Migrate benchmark to use JSpecify annotations
CL created by running `development/jspecify_update.py benchmark`
We are updating all AndroidX libraries to use the Jspecify nullness annotations (https://docs.google.com/document/d/1XjainD032hUdCfgXIzRThHvP9FJANYuocKaTDXWuLXo/edit?usp=sharing)
Bug: 326456246
Test: `checkApi`, `lint`, `runErrorProne`
Relnote: "This library now uses [JSpecify nullness annotations](https://jspecify.dev/), which are type-use. Kotlin developers should use the following compiler arguments to enforce correct usage: -Xjspecify-annotations=strict, -Xtype-enhancement-improvements-strict-mode"
Change-Id: I7104f0ca68a72a7e996b79f1609cde685e6c61e6
diff --git a/benchmark/benchmark-common/build.gradle b/benchmark/benchmark-common/build.gradle
index 8b806f0..f109adb 100644
--- a/benchmark/benchmark-common/build.gradle
+++ b/benchmark/benchmark-common/build.gradle
@@ -77,6 +77,7 @@
}
dependencies {
+ api(libs.jspecify)
implementation(libs.kotlinStdlib)
api("androidx.annotation:annotation:1.8.1")
api("androidx.annotation:annotation-experimental:1.4.1")
diff --git a/benchmark/benchmark-common/src/main/java/androidx/benchmark/simpleperf/ProfileSession.java b/benchmark/benchmark-common/src/main/java/androidx/benchmark/simpleperf/ProfileSession.java
index 2e33e53..11fe2f0 100644
--- a/benchmark/benchmark-common/src/main/java/androidx/benchmark/simpleperf/ProfileSession.java
+++ b/benchmark/benchmark-common/src/main/java/androidx/benchmark/simpleperf/ProfileSession.java
@@ -21,11 +21,12 @@
import android.system.Os;
import android.system.OsConstants;
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
+import org.jspecify.annotations.NonNull;
+import org.jspecify.annotations.Nullable;
+
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
@@ -247,8 +248,7 @@
return file.canExecute();
}
- @Nullable
- private String findSimpleperfInTempDir() {
+ private @Nullable String findSimpleperfInTempDir() {
String path = "/data/local/tmp/simpleperf";
File file = new File(path);
if (!file.isFile()) {
@@ -408,8 +408,7 @@
}
}
- @NonNull
- private String readReply() {
+ private @NonNull String readReply() {
// Read one byte at a time to stop at line break or EOF. BufferedReader will try to read
// more than available and make us blocking, so don't use it.
String s = "";
diff --git a/benchmark/benchmark-common/src/main/java/androidx/benchmark/simpleperf/RecordOptions.java b/benchmark/benchmark-common/src/main/java/androidx/benchmark/simpleperf/RecordOptions.java
index 4a23dba..e538e454f 100644
--- a/benchmark/benchmark-common/src/main/java/androidx/benchmark/simpleperf/RecordOptions.java
+++ b/benchmark/benchmark-common/src/main/java/androidx/benchmark/simpleperf/RecordOptions.java
@@ -18,11 +18,12 @@
import android.system.Os;
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
+import org.jspecify.annotations.NonNull;
+import org.jspecify.annotations.Nullable;
+
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
@@ -59,8 +60,7 @@
* Set output filename. Default is perf-<month>-<day>-<hour>-<minute>-<second>.data.
* The file will be generated under simpleperf_data/.
*/
- @NonNull
- public RecordOptions setOutputFilename(@NonNull String filename) {
+ public @NonNull RecordOptions setOutputFilename(@NonNull String filename) {
mOutputFilename = filename;
return this;
}
@@ -68,8 +68,7 @@
/**
* Set event to record. Default is cpu-cycles. See `simpleperf list` for all available events.
*/
- @NonNull
- public RecordOptions setEvent(@NonNull String event) {
+ public @NonNull RecordOptions setEvent(@NonNull String event) {
mEvent = event;
return this;
}
@@ -77,8 +76,7 @@
/**
* Set how many samples to generate each second running. Default is 4000.
*/
- @NonNull
- public RecordOptions setSampleFrequency(int freq) {
+ public @NonNull RecordOptions setSampleFrequency(int freq) {
mFreq = freq;
return this;
}
@@ -87,8 +85,7 @@
* Set record duration. The record stops after `durationInSecond` seconds. By default,
* record stops only when stopRecording() is called.
*/
- @NonNull
- public RecordOptions setDuration(double durationInSecond) {
+ public @NonNull RecordOptions setDuration(double durationInSecond) {
mDurationInSeconds = durationInSecond;
return this;
}
@@ -96,8 +93,7 @@
/**
* Record some threads in the app process. By default, record all threads in the process.
*/
- @NonNull
- public RecordOptions setSampleThreads(@NonNull List<Integer> threads) {
+ public @NonNull RecordOptions setSampleThreads(@NonNull List<Integer> threads) {
mThreads.addAll(threads);
return this;
}
@@ -105,15 +101,13 @@
/**
* Record current thread in the app process. By default, record all threads in the process.
*/
- @NonNull
- public RecordOptions setSampleCurrentThread() {
+ public @NonNull RecordOptions setSampleCurrentThread() {
return setSampleThreads(Collections.singletonList(Os.gettid()));
}
/**
* Record dwarf based call graph. It is needed to get Java callstacks.
*/
- @NonNull
- public RecordOptions recordDwarfCallGraph() {
+ public @NonNull RecordOptions recordDwarfCallGraph() {
mDwarfCallGraph = true;
mFpCallGraph = false;
return this;
@@ -123,8 +117,7 @@
* Record frame pointer based call graph. It is suitable to get C++ callstacks on 64bit devices.
*/
@SuppressWarnings("unused")
- @NonNull
- public RecordOptions recordFramePointerCallGraph() {
+ public @NonNull RecordOptions recordFramePointerCallGraph() {
mFpCallGraph = true;
mDwarfCallGraph = false;
return this;
@@ -133,8 +126,7 @@
/**
* Trace context switch info to show where threads spend time off cpu.
*/
- @NonNull
- public RecordOptions traceOffCpu() {
+ public @NonNull RecordOptions traceOffCpu() {
mTraceOffCpu = true;
return this;
}
@@ -142,8 +134,7 @@
/**
* Translate record options into arguments for `simpleperf record` cmd.
*/
- @NonNull
- public List<String> toRecordArgs() {
+ public @NonNull List<String> toRecordArgs() {
ArrayList<String> args = new ArrayList<>();
String filename = mOutputFilename;
@@ -192,18 +183,15 @@
return time.format(formatter);
}
- @Nullable
- private String mOutputFilename;
+ private @Nullable String mOutputFilename;
- @NonNull
- private String mEvent = "cpu-cycles";
+ private @NonNull String mEvent = "cpu-cycles";
private int mFreq = 4000;
private double mDurationInSeconds = 0.0;
- @NonNull
- private final ArrayList<Integer> mThreads = new ArrayList<>();
+ private final @NonNull ArrayList<Integer> mThreads = new ArrayList<>();
private boolean mDwarfCallGraph = false;
diff --git a/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/ByteBufferUtil.java b/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/ByteBufferUtil.java
index f968b6f..785dc77 100644
--- a/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/ByteBufferUtil.java
+++ b/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/ByteBufferUtil.java
@@ -15,7 +15,7 @@
*/
package androidx.benchmark.vmtrace;
-import androidx.annotation.NonNull;
+import org.jspecify.annotations.NonNull;
import java.io.File;
import java.io.FileInputStream;
@@ -30,9 +30,8 @@
private ByteBufferUtil() {
}
- @NonNull
- public static ByteBuffer mapFile(@NonNull File f, long offset, @NonNull ByteOrder byteOrder)
- throws IOException {
+ public static @NonNull ByteBuffer mapFile(
+ @NonNull File f, long offset, @NonNull ByteOrder byteOrder) throws IOException {
FileInputStream dataFile = new FileInputStream(f);
try {
FileChannel fc = dataFile.getChannel();
diff --git a/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/MethodInfo.java b/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/MethodInfo.java
index edd3552..ba51ac8 100644
--- a/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/MethodInfo.java
+++ b/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/MethodInfo.java
@@ -16,7 +16,7 @@
package androidx.benchmark.vmtrace;
-import androidx.annotation.NonNull;
+import org.jspecify.annotations.NonNull;
import java.util.Locale;
@@ -46,24 +46,21 @@
this.srcLineNumber = srcLineNumber;
}
- @NonNull
- public String getFullName() {
+ public @NonNull String getFullName() {
if (mFullName == null) {
mFullName = String.format(Locale.US, "%s.%s: %s", className, methodName, signature);
}
return mFullName;
}
- @NonNull
- public String getShortName() {
+ public @NonNull String getShortName() {
if (mShortName == null) {
mShortName = String.format(Locale.US, "%s.%s", getUnqualifiedClassName(), methodName);
}
return mShortName;
}
- @NonNull
- private String getUnqualifiedClassName() {
+ private @NonNull String getUnqualifiedClassName() {
String cn = className;
int i = cn.lastIndexOf('/');
if (i > 0) {
diff --git a/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/VmTraceHandler.java b/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/VmTraceHandler.java
index 55ebfa4..fdd35f8 100644
--- a/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/VmTraceHandler.java
+++ b/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/VmTraceHandler.java
@@ -15,7 +15,7 @@
*/
package androidx.benchmark.vmtrace;
-import androidx.annotation.NonNull;
+import org.jspecify.annotations.NonNull;
/**
* This interface used by {@link VmTraceParser}. {@link VmTraceParser} parses a trace file and
diff --git a/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/VmTraceParser.java b/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/VmTraceParser.java
index f7c8a0b..3a7df15 100644
--- a/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/VmTraceParser.java
+++ b/benchmark/benchmark-common/src/main/java/androidx/benchmark/vmtrace/VmTraceParser.java
@@ -16,7 +16,7 @@
package androidx.benchmark.vmtrace;
-import androidx.annotation.NonNull;
+import org.jspecify.annotations.NonNull;
import java.io.BufferedReader;
import java.io.File;
diff --git a/benchmark/benchmark-macro/build.gradle b/benchmark/benchmark-macro/build.gradle
index 8c0b606..2a2cfa0 100644
--- a/benchmark/benchmark-macro/build.gradle
+++ b/benchmark/benchmark-macro/build.gradle
@@ -63,6 +63,7 @@
}
dependencies {
+ api(libs.jspecify)
api(project(":benchmark:benchmark-common"))
api(libs.junit)
api(libs.kotlinStdlib)
diff --git a/benchmark/benchmark-macro/src/main/java/androidx/benchmark/macro/JankCollectionHelper.java b/benchmark/benchmark-macro/src/main/java/androidx/benchmark/macro/JankCollectionHelper.java
index fff2766..17993bd 100644
--- a/benchmark/benchmark-macro/src/main/java/androidx/benchmark/macro/JankCollectionHelper.java
+++ b/benchmark/benchmark-macro/src/main/java/androidx/benchmark/macro/JankCollectionHelper.java
@@ -19,12 +19,13 @@
import android.text.TextUtils;
import android.util.Log;
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;
+import org.jspecify.annotations.NonNull;
+import org.jspecify.annotations.Nullable;
+
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
@@ -170,8 +171,7 @@
mMetricId = metricId;
}
- @Nullable
- public Double parse(@NonNull String lines) {
+ public @Nullable Double parse(@NonNull String lines) {
Matcher matcher = mPattern.matcher(lines);
if (matcher.matches()) {
return Double.valueOf(matcher.group(mGroupIndex));
@@ -180,8 +180,7 @@
}
}
- @NonNull
- public String getMetricId() {
+ public @NonNull String getMetricId() {
return mMetricId;
}
}
@@ -221,8 +220,7 @@
}
/** Collect the {@code gfxinfo} metrics for tracked processes (or all, if unspecified). */
- @NonNull
- public Map<String, Double> getMetrics() {
+ public @NonNull Map<String, Double> getMetrics() {
Map<String, Double> result = new HashMap<>();
if (mTrackedPackages.isEmpty()) {
result.putAll(getGfxInfoMetrics());
@@ -258,7 +256,7 @@
}
/** Add a package or list of packages to be tracked. */
- public void addTrackedPackages(@NonNull String... packages) {
+ public void addTrackedPackages(String @NonNull ... packages) {
Collections.addAll(mTrackedPackages, packages);
}
@@ -348,7 +346,7 @@
return results;
}
- private String constructKey(@NonNull String ...tokens) {
+ private String constructKey(String @NonNull ...tokens) {
return TextUtils.join("_", tokens);
}
@@ -371,9 +369,8 @@
}
/** Returns the {@link UiDevice} under test. */
- @NonNull
@VisibleForTesting
- protected UiDevice getDevice() {
+ protected @NonNull UiDevice getDevice() {
if (mDevice == null) {
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}