Skip to content

Commit 5f44d96

Browse files
committed
Improve nullability and java docs
1 parent 2860fa1 commit 5f44d96

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+201
-105
lines changed

sentry-android-core/api/sentry-android-core.api

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,13 @@ public final class io/sentry/android/core/CurrentActivityIntegration : android/a
151151
public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V
152152
}
153153

154-
public class io/sentry/android/core/DeviceInfoUtil {
154+
public final class io/sentry/android/core/DeviceInfoUtil {
155155
public fun <init> (Landroid/content/Context;Lio/sentry/android/core/SentryAndroidOptions;)V
156156
public fun collectDeviceInformation (ZZ)Lio/sentry/protocol/Device;
157157
public static fun getInstance (Landroid/content/Context;Lio/sentry/android/core/SentryAndroidOptions;)Lio/sentry/android/core/DeviceInfoUtil;
158-
public final fun getOperatingSystem ()Lio/sentry/protocol/OperatingSystem;
158+
public fun getOperatingSystem ()Lio/sentry/protocol/OperatingSystem;
159159
public fun getSideLoadedInfo ()Lio/sentry/android/core/ContextUtils$SideLoadedInfo;
160160
public static fun resetInstance ()V
161-
protected fun retrieveOperatingSystemInformation ()Lio/sentry/protocol/OperatingSystem;
162161
}
163162

164163
public abstract class io/sentry/android/core/EnvelopeFileObserverIntegration : io/sentry/Integration, java/io/Closeable {
@@ -173,10 +172,6 @@ public abstract interface class io/sentry/android/core/IDebugImagesLoader {
173172
public abstract fun loadDebugImages ()Ljava/util/List;
174173
}
175174

176-
public final class io/sentry/android/core/Installation {
177-
public static fun id (Landroid/content/Context;)Ljava/lang/String;
178-
}
179-
180175
public final class io/sentry/android/core/InternalSentrySdk {
181176
public fun <init> ()V
182177
public static fun captureEnvelope ([B)Lio/sentry/protocol/SentryId;

sentry-android-core/src/main/java/io/sentry/android/core/DeviceInfoUtil.java

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.jetbrains.annotations.TestOnly;
3636

3737
@ApiStatus.Internal
38-
public class DeviceInfoUtil {
38+
public final class DeviceInfoUtil {
3939

4040
@SuppressLint("StaticFieldLeak")
4141
private static volatile DeviceInfoUtil instance;
@@ -61,7 +61,8 @@ public DeviceInfoUtil(
6161
ContextUtils.retrieveSideLoadedInfo(context, options.getLogger(), buildInfoProvider);
6262
}
6363

64-
public static @NotNull DeviceInfoUtil getInstance(
64+
@NotNull
65+
public static DeviceInfoUtil getInstance(
6566
final @NotNull Context context, final @NotNull SentryAndroidOptions options) {
6667
if (instance == null) {
6768
synchronized (DeviceInfoUtil.class) {
@@ -139,7 +140,8 @@ public Device collectDeviceInformation(
139140
return device;
140141
}
141142

142-
public final @NotNull OperatingSystem getOperatingSystem() {
143+
@NotNull
144+
public OperatingSystem getOperatingSystem() {
143145
return os;
144146
}
145147

@@ -164,7 +166,8 @@ protected OperatingSystem retrieveOperatingSystemInformation() {
164166
return os;
165167
}
166168

167-
public @Nullable ContextUtils.SideLoadedInfo getSideLoadedInfo() {
169+
@Nullable
170+
public ContextUtils.SideLoadedInfo getSideLoadedInfo() {
168171
return sideLoadedInfo;
169172
}
170173

@@ -226,6 +229,7 @@ private void setDeviceIO(final @NotNull Device device, final boolean includeDyna
226229
}
227230

228231
@SuppressWarnings("NewApi")
232+
@NotNull
229233
private TimeZone getTimeZone() {
230234
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.N) {
231235
LocaleList locales = context.getResources().getConfiguration().getLocales();
@@ -238,7 +242,8 @@ private TimeZone getTimeZone() {
238242
}
239243

240244
@SuppressWarnings("JdkObsolete")
241-
private @Nullable Date getBootTime() {
245+
@Nullable
246+
private Date getBootTime() {
242247
try {
243248
// if user changes the clock, will give a wrong answer, consider ACTION_TIME_CHANGED.
244249
// currentTimeMillis returns UTC already
@@ -249,7 +254,8 @@ private TimeZone getTimeZone() {
249254
return null;
250255
}
251256

252-
private @Nullable Intent getBatteryIntent() {
257+
@Nullable
258+
private Intent getBatteryIntent() {
253259
return context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
254260
}
255261

@@ -258,7 +264,8 @@ private TimeZone getTimeZone() {
258264
*
259265
* @return the device's current battery level (as a percentage of total), or null if unknown
260266
*/
261-
private @Nullable Float getBatteryLevel(final @NotNull Intent batteryIntent) {
267+
@Nullable
268+
private Float getBatteryLevel(final @NotNull Intent batteryIntent) {
262269
try {
263270
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
264271
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
@@ -281,7 +288,8 @@ private TimeZone getTimeZone() {
281288
*
282289
* @return whether or not the device is currently plugged in and charging, or null if unknown
283290
*/
284-
private @Nullable Boolean isCharging(final @NotNull Intent batteryIntent) {
291+
@Nullable
292+
private Boolean isCharging(final @NotNull Intent batteryIntent) {
285293
try {
286294
int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
287295
return plugged == BatteryManager.BATTERY_PLUGGED_AC
@@ -292,7 +300,8 @@ private TimeZone getTimeZone() {
292300
}
293301
}
294302

295-
private @Nullable Float getBatteryTemperature(final @NotNull Intent batteryIntent) {
303+
@Nullable
304+
private Float getBatteryTemperature(final @NotNull Intent batteryIntent) {
296305
try {
297306
int temperature = batteryIntent.getIntExtra(EXTRA_TEMPERATURE, -1);
298307
if (temperature != -1) {
@@ -309,7 +318,8 @@ private TimeZone getTimeZone() {
309318
*
310319
* @return the device's current screen orientation, or null if unknown
311320
*/
312-
private @Nullable Device.DeviceOrientation getOrientation() {
321+
@Nullable
322+
private Device.DeviceOrientation getOrientation() {
313323
Device.DeviceOrientation deviceOrientation = null;
314324
try {
315325
deviceOrientation =
@@ -329,7 +339,8 @@ private TimeZone getTimeZone() {
329339
}
330340

331341
@SuppressWarnings({"ObsoleteSdkInt", "NewApi"})
332-
private @NotNull Long getMemorySize(final @NotNull ActivityManager.MemoryInfo memInfo) {
342+
@NotNull
343+
private Long getMemorySize(final @NotNull ActivityManager.MemoryInfo memInfo) {
333344
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.JELLY_BEAN) {
334345
return memInfo.totalMem;
335346
}
@@ -342,7 +353,8 @@ private TimeZone getTimeZone() {
342353
*
343354
* @return the total amount of internal storage, in bytes
344355
*/
345-
private @Nullable Long getTotalInternalStorage(final @NotNull StatFs stat) {
356+
@Nullable
357+
private Long getTotalInternalStorage(final @NotNull StatFs stat) {
346358
try {
347359
long blockSize = getBlockSizeLong(stat);
348360
long totalBlocks = getBlockCountLong(stat);
@@ -397,7 +409,8 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
397409
*
398410
* @return the unused amount of internal storage, in bytes
399411
*/
400-
private @Nullable Long getUnusedInternalStorage(final @NotNull StatFs stat) {
412+
@Nullable
413+
private Long getUnusedInternalStorage(final @NotNull StatFs stat) {
401414
try {
402415
long blockSize = getBlockSizeLong(stat);
403416
long availableBlocks = getAvailableBlocksLong(stat);
@@ -410,7 +423,8 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
410423
}
411424
}
412425

413-
private @Nullable StatFs getExternalStorageStat(final @Nullable File internalStorage) {
426+
@Nullable
427+
private StatFs getExternalStorageStat(final @Nullable File internalStorage) {
414428
if (!isExternalStorageMounted()) {
415429
File path = getExternalStorageDep(internalStorage);
416430
if (path != null) { // && path.canRead()) { canRead() will read return false
@@ -424,7 +438,8 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
424438
}
425439

426440
@SuppressWarnings({"ObsoleteSdkInt", "NewApi"})
427-
private @Nullable File[] getExternalFilesDirs() {
441+
@Nullable
442+
private File[] getExternalFilesDirs() {
428443
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.KITKAT) {
429444
return context.getExternalFilesDirs(null);
430445
} else {
@@ -436,7 +451,8 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
436451
return null;
437452
}
438453

439-
private @Nullable File getExternalStorageDep(final @Nullable File internalStorage) {
454+
@Nullable
455+
private File getExternalStorageDep(final @Nullable File internalStorage) {
440456
final @Nullable File[] externalFilesDirs = getExternalFilesDirs();
441457

442458
if (externalFilesDirs != null) {
@@ -471,7 +487,8 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
471487
* @return the total amount of external storage, in bytes, or null if no external storage is
472488
* mounted
473489
*/
474-
private @Nullable Long getTotalExternalStorage(final @NotNull StatFs stat) {
490+
@Nullable
491+
private Long getTotalExternalStorage(final @NotNull StatFs stat) {
475492
try {
476493
final long blockSize = getBlockSizeLong(stat);
477494
final long totalBlocks = getBlockCountLong(stat);
@@ -495,7 +512,8 @@ private boolean isExternalStorageMounted() {
495512
* @return the unused amount of external storage, in bytes, or null if no external storage is
496513
* mounted
497514
*/
498-
private @Nullable Long getUnusedExternalStorage(final @NotNull StatFs stat) {
515+
@Nullable
516+
private Long getUnusedExternalStorage(final @NotNull StatFs stat) {
499517
try {
500518
final long blockSize = getBlockSizeLong(stat);
501519
final long availableBlocks = getAvailableBlocksLong(stat);
@@ -508,7 +526,8 @@ private boolean isExternalStorageMounted() {
508526
}
509527
}
510528

511-
private @Nullable String getDeviceId() {
529+
@Nullable
530+
private String getDeviceId() {
512531
try {
513532
return Installation.id(context);
514533
} catch (Throwable e) {

sentry-android-core/src/main/java/io/sentry/android/core/Installation.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
import java.io.RandomAccessFile;
99
import java.nio.charset.Charset;
1010
import java.util.UUID;
11-
import org.jetbrains.annotations.ApiStatus;
1211
import org.jetbrains.annotations.NotNull;
1312
import org.jetbrains.annotations.Nullable;
1413
import org.jetbrains.annotations.TestOnly;
1514

16-
@ApiStatus.Internal
17-
public final class Installation {
15+
final class Installation {
1816
@TestOnly static @Nullable String deviceId = null;
1917

2018
@TestOnly static final String INSTALLATION = "INSTALLATION";

sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,21 @@ public static Scope getCurrentScope() {
4545
return scopeRef.get();
4646
}
4747

48+
/**
49+
* Serializes the provided scope. Specific data may be back-filled (e.g. device context) if the
50+
* scope itself does not provide it.
51+
*
52+
* @param context Android context
53+
* @param options Sentry Options
54+
* @param scope the scope
55+
* @return a map containing all relevant scope data (user, contexts, tags, extras, fingerprint,
56+
* level, breadcrumbs)
57+
*/
4858
@NotNull
4959
public static Map<String, Object> serializeScope(
50-
@NotNull Context context, @NotNull SentryAndroidOptions options, @Nullable Scope scope) {
60+
final @NotNull Context context,
61+
final @NotNull SentryAndroidOptions options,
62+
final @Nullable Scope scope) {
5163
final @NotNull Map<String, Object> data = new HashMap<>();
5264
if (scope == null) {
5365
return data;
@@ -70,7 +82,11 @@ public static Map<String, Object> serializeScope(
7082
scope.setUser(user);
7183
}
7284
if (user.getId() == null) {
73-
user.setId(Installation.id(context));
85+
try {
86+
user.setId(Installation.id(context));
87+
} catch (RuntimeException e) {
88+
logger.log(SentryLevel.ERROR, "Could not retrieve installation ID", e);
89+
}
7490
}
7591

7692
// app context
@@ -160,8 +176,8 @@ public static SentryId captureEnvelope(final @NotNull byte[] envelopeData) throw
160176

161177
@Nullable
162178
private static Session updateSession(
163-
@NotNull IHub hub,
164-
@NotNull SentryOptions options,
179+
final @NotNull IHub hub,
180+
final @NotNull SentryOptions options,
165181
final @Nullable Session.State status,
166182
final boolean crashedOrErrored) {
167183
final @NotNull AtomicReference<Session> sessionRef = new AtomicReference<>();

sentry/src/main/java/io/sentry/Breadcrumb.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ public static final class JsonKeys {
561561
}
562562

563563
@Override
564-
public void serialize(@NotNull ObjectWriter writer, @NotNull ILogger logger) throws IOException {
564+
public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger logger)
565+
throws IOException {
565566
writer.beginObject();
566567
writer.name(JsonKeys.TIMESTAMP).value(logger, timestamp);
567568
if (message != null) {

sentry/src/main/java/io/sentry/JsonObjectWriter.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
public final class JsonObjectWriter implements ObjectWriter {
1010

11-
private final JsonWriter jsonWriter;
12-
private final JsonObjectSerializer jsonObjectSerializer;
11+
private final @NotNull JsonWriter jsonWriter;
12+
private final @NotNull JsonObjectSerializer jsonObjectSerializer;
1313

14-
public JsonObjectWriter(Writer out, int maxDepth) {
14+
public JsonObjectWriter(final @NotNull Writer out, final int maxDepth) {
1515
jsonWriter = new JsonWriter(out);
1616
jsonObjectSerializer = new JsonObjectSerializer(maxDepth);
1717
}
@@ -41,13 +41,13 @@ public JsonObjectWriter endObject() throws IOException {
4141
}
4242

4343
@Override
44-
public JsonObjectWriter name(String name) throws IOException {
44+
public JsonObjectWriter name(final @NotNull String name) throws IOException {
4545
jsonWriter.name(name);
4646
return this;
4747
}
4848

4949
@Override
50-
public JsonObjectWriter value(String value) throws IOException {
50+
public JsonObjectWriter value(final @Nullable String value) throws IOException {
5151
jsonWriter.value(value);
5252
return this;
5353
}
@@ -59,31 +59,31 @@ public JsonObjectWriter nullValue() throws IOException {
5959
}
6060

6161
@Override
62-
public JsonObjectWriter value(boolean value) throws IOException {
62+
public JsonObjectWriter value(final boolean value) throws IOException {
6363
jsonWriter.value(value);
6464
return this;
6565
}
6666

6767
@Override
68-
public JsonObjectWriter value(Boolean value) throws IOException {
68+
public JsonObjectWriter value(final @Nullable Boolean value) throws IOException {
6969
jsonWriter.value(value);
7070
return this;
7171
}
7272

7373
@Override
74-
public JsonObjectWriter value(double value) throws IOException {
74+
public JsonObjectWriter value(final double value) throws IOException {
7575
jsonWriter.value(value);
7676
return this;
7777
}
7878

7979
@Override
80-
public JsonObjectWriter value(long value) throws IOException {
80+
public JsonObjectWriter value(final long value) throws IOException {
8181
jsonWriter.value(value);
8282
return this;
8383
}
8484

8585
@Override
86-
public JsonObjectWriter value(Number value) throws IOException {
86+
public JsonObjectWriter value(final @Nullable Number value) throws IOException {
8787
jsonWriter.value(value);
8888
return this;
8989
}
@@ -97,13 +97,13 @@ public JsonObjectWriter value(Number value) throws IOException {
9797
* @return this writer.
9898
*/
9999
@Override
100-
public JsonObjectWriter value(@NotNull ILogger logger, @Nullable Object object)
100+
public JsonObjectWriter value(final @NotNull ILogger logger, final @Nullable Object object)
101101
throws IOException {
102102
jsonObjectSerializer.serialize(this, logger, object);
103103
return this;
104104
}
105105

106-
public void setIndent(@NotNull final String indent) {
106+
public void setIndent(final @NotNull String indent) {
107107
jsonWriter.setIndent(indent);
108108
}
109109
}

sentry/src/main/java/io/sentry/JsonSerializable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
import org.jetbrains.annotations.NotNull;
55

66
public interface JsonSerializable {
7-
void serialize(@NotNull ObjectWriter writer, @NotNull ILogger logger) throws IOException;
7+
void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger logger)
8+
throws IOException;
89
}

0 commit comments

Comments
 (0)