Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Improve nullability and java docs
  • Loading branch information
markushi committed Jul 14, 2023
commit 5f44d963fb514090d6c5fcfe7663b3dbc967cb7e
9 changes: 2 additions & 7 deletions sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,13 @@ public final class io/sentry/android/core/CurrentActivityIntegration : android/a
public fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V
}

public class io/sentry/android/core/DeviceInfoUtil {
public final class io/sentry/android/core/DeviceInfoUtil {
public fun <init> (Landroid/content/Context;Lio/sentry/android/core/SentryAndroidOptions;)V
public fun collectDeviceInformation (ZZ)Lio/sentry/protocol/Device;
public static fun getInstance (Landroid/content/Context;Lio/sentry/android/core/SentryAndroidOptions;)Lio/sentry/android/core/DeviceInfoUtil;
public final fun getOperatingSystem ()Lio/sentry/protocol/OperatingSystem;
public fun getOperatingSystem ()Lio/sentry/protocol/OperatingSystem;
public fun getSideLoadedInfo ()Lio/sentry/android/core/ContextUtils$SideLoadedInfo;
public static fun resetInstance ()V
protected fun retrieveOperatingSystemInformation ()Lio/sentry/protocol/OperatingSystem;
}

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

public final class io/sentry/android/core/Installation {
public static fun id (Landroid/content/Context;)Ljava/lang/String;
}

public final class io/sentry/android/core/InternalSentrySdk {
public fun <init> ()V
public static fun captureEnvelope ([B)Lio/sentry/protocol/SentryId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.jetbrains.annotations.TestOnly;

@ApiStatus.Internal
public class DeviceInfoUtil {
public final class DeviceInfoUtil {

@SuppressLint("StaticFieldLeak")
private static volatile DeviceInfoUtil instance;
Expand All @@ -61,7 +61,8 @@ public DeviceInfoUtil(
ContextUtils.retrieveSideLoadedInfo(context, options.getLogger(), buildInfoProvider);
}

public static @NotNull DeviceInfoUtil getInstance(
@NotNull
public static DeviceInfoUtil getInstance(
final @NotNull Context context, final @NotNull SentryAndroidOptions options) {
if (instance == null) {
synchronized (DeviceInfoUtil.class) {
Expand Down Expand Up @@ -139,7 +140,8 @@ public Device collectDeviceInformation(
return device;
}

public final @NotNull OperatingSystem getOperatingSystem() {
@NotNull
public OperatingSystem getOperatingSystem() {
return os;
}

Expand All @@ -164,7 +166,8 @@ protected OperatingSystem retrieveOperatingSystemInformation() {
return os;
}

public @Nullable ContextUtils.SideLoadedInfo getSideLoadedInfo() {
@Nullable
public ContextUtils.SideLoadedInfo getSideLoadedInfo() {
return sideLoadedInfo;
}

Expand Down Expand Up @@ -226,6 +229,7 @@ private void setDeviceIO(final @NotNull Device device, final boolean includeDyna
}

@SuppressWarnings("NewApi")
@NotNull
private TimeZone getTimeZone() {
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.N) {
LocaleList locales = context.getResources().getConfiguration().getLocales();
Expand All @@ -238,7 +242,8 @@ private TimeZone getTimeZone() {
}

@SuppressWarnings("JdkObsolete")
private @Nullable Date getBootTime() {
@Nullable
private Date getBootTime() {
try {
// if user changes the clock, will give a wrong answer, consider ACTION_TIME_CHANGED.
// currentTimeMillis returns UTC already
Expand All @@ -249,7 +254,8 @@ private TimeZone getTimeZone() {
return null;
}

private @Nullable Intent getBatteryIntent() {
@Nullable
private Intent getBatteryIntent() {
return context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

Expand All @@ -258,7 +264,8 @@ private TimeZone getTimeZone() {
*
* @return the device's current battery level (as a percentage of total), or null if unknown
*/
private @Nullable Float getBatteryLevel(final @NotNull Intent batteryIntent) {
@Nullable
private Float getBatteryLevel(final @NotNull Intent batteryIntent) {
try {
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
Expand All @@ -281,7 +288,8 @@ private TimeZone getTimeZone() {
*
* @return whether or not the device is currently plugged in and charging, or null if unknown
*/
private @Nullable Boolean isCharging(final @NotNull Intent batteryIntent) {
@Nullable
private Boolean isCharging(final @NotNull Intent batteryIntent) {
try {
int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
return plugged == BatteryManager.BATTERY_PLUGGED_AC
Expand All @@ -292,7 +300,8 @@ private TimeZone getTimeZone() {
}
}

private @Nullable Float getBatteryTemperature(final @NotNull Intent batteryIntent) {
@Nullable
private Float getBatteryTemperature(final @NotNull Intent batteryIntent) {
try {
int temperature = batteryIntent.getIntExtra(EXTRA_TEMPERATURE, -1);
if (temperature != -1) {
Expand All @@ -309,7 +318,8 @@ private TimeZone getTimeZone() {
*
* @return the device's current screen orientation, or null if unknown
*/
private @Nullable Device.DeviceOrientation getOrientation() {
@Nullable
private Device.DeviceOrientation getOrientation() {
Device.DeviceOrientation deviceOrientation = null;
try {
deviceOrientation =
Expand All @@ -329,7 +339,8 @@ private TimeZone getTimeZone() {
}

@SuppressWarnings({"ObsoleteSdkInt", "NewApi"})
private @NotNull Long getMemorySize(final @NotNull ActivityManager.MemoryInfo memInfo) {
@NotNull
private Long getMemorySize(final @NotNull ActivityManager.MemoryInfo memInfo) {
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.JELLY_BEAN) {
return memInfo.totalMem;
}
Expand All @@ -342,7 +353,8 @@ private TimeZone getTimeZone() {
*
* @return the total amount of internal storage, in bytes
*/
private @Nullable Long getTotalInternalStorage(final @NotNull StatFs stat) {
@Nullable
private Long getTotalInternalStorage(final @NotNull StatFs stat) {
try {
long blockSize = getBlockSizeLong(stat);
long totalBlocks = getBlockCountLong(stat);
Expand Down Expand Up @@ -397,7 +409,8 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
*
* @return the unused amount of internal storage, in bytes
*/
private @Nullable Long getUnusedInternalStorage(final @NotNull StatFs stat) {
@Nullable
private Long getUnusedInternalStorage(final @NotNull StatFs stat) {
try {
long blockSize = getBlockSizeLong(stat);
long availableBlocks = getAvailableBlocksLong(stat);
Expand All @@ -410,7 +423,8 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
}
}

private @Nullable StatFs getExternalStorageStat(final @Nullable File internalStorage) {
@Nullable
private StatFs getExternalStorageStat(final @Nullable File internalStorage) {
if (!isExternalStorageMounted()) {
File path = getExternalStorageDep(internalStorage);
if (path != null) { // && path.canRead()) { canRead() will read return false
Expand All @@ -424,7 +438,8 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
}

@SuppressWarnings({"ObsoleteSdkInt", "NewApi"})
private @Nullable File[] getExternalFilesDirs() {
@Nullable
private File[] getExternalFilesDirs() {
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.KITKAT) {
return context.getExternalFilesDirs(null);
} else {
Expand All @@ -436,7 +451,8 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
return null;
}

private @Nullable File getExternalStorageDep(final @Nullable File internalStorage) {
@Nullable
private File getExternalStorageDep(final @Nullable File internalStorage) {
final @Nullable File[] externalFilesDirs = getExternalFilesDirs();

if (externalFilesDirs != null) {
Expand Down Expand Up @@ -471,7 +487,8 @@ private int getAvailableBlocksDep(final @NotNull StatFs stat) {
* @return the total amount of external storage, in bytes, or null if no external storage is
* mounted
*/
private @Nullable Long getTotalExternalStorage(final @NotNull StatFs stat) {
@Nullable
private Long getTotalExternalStorage(final @NotNull StatFs stat) {
try {
final long blockSize = getBlockSizeLong(stat);
final long totalBlocks = getBlockCountLong(stat);
Expand All @@ -495,7 +512,8 @@ private boolean isExternalStorageMounted() {
* @return the unused amount of external storage, in bytes, or null if no external storage is
* mounted
*/
private @Nullable Long getUnusedExternalStorage(final @NotNull StatFs stat) {
@Nullable
private Long getUnusedExternalStorage(final @NotNull StatFs stat) {
try {
final long blockSize = getBlockSizeLong(stat);
final long availableBlocks = getAvailableBlocksLong(stat);
Expand All @@ -508,7 +526,8 @@ private boolean isExternalStorageMounted() {
}
}

private @Nullable String getDeviceId() {
@Nullable
private String getDeviceId() {
try {
return Installation.id(context);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.util.UUID;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

@ApiStatus.Internal
public final class Installation {
final class Installation {
@TestOnly static @Nullable String deviceId = null;

@TestOnly static final String INSTALLATION = "INSTALLATION";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,21 @@ public static Scope getCurrentScope() {
return scopeRef.get();
}

/**
* Serializes the provided scope. Specific data may be back-filled (e.g. device context) if the
* scope itself does not provide it.
*
* @param context Android context
* @param options Sentry Options
* @param scope the scope
* @return a map containing all relevant scope data (user, contexts, tags, extras, fingerprint,
* level, breadcrumbs)
*/
@NotNull
public static Map<String, Object> serializeScope(
@NotNull Context context, @NotNull SentryAndroidOptions options, @Nullable Scope scope) {
final @NotNull Context context,
final @NotNull SentryAndroidOptions options,
final @Nullable Scope scope) {
final @NotNull Map<String, Object> data = new HashMap<>();
if (scope == null) {
return data;
Expand All @@ -70,7 +82,11 @@ public static Map<String, Object> serializeScope(
scope.setUser(user);
}
if (user.getId() == null) {
user.setId(Installation.id(context));
try {
user.setId(Installation.id(context));
} catch (RuntimeException e) {
logger.log(SentryLevel.ERROR, "Could not retrieve installation ID", e);
}
}

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

@Nullable
private static Session updateSession(
@NotNull IHub hub,
@NotNull SentryOptions options,
final @NotNull IHub hub,
final @NotNull SentryOptions options,
final @Nullable Session.State status,
final boolean crashedOrErrored) {
final @NotNull AtomicReference<Session> sessionRef = new AtomicReference<>();
Expand Down
3 changes: 2 additions & 1 deletion sentry/src/main/java/io/sentry/Breadcrumb.java
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ public static final class JsonKeys {
}

@Override
public void serialize(@NotNull ObjectWriter writer, @NotNull ILogger logger) throws IOException {
public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger logger)
throws IOException {
writer.beginObject();
writer.name(JsonKeys.TIMESTAMP).value(logger, timestamp);
if (message != null) {
Expand Down
24 changes: 12 additions & 12 deletions sentry/src/main/java/io/sentry/JsonObjectWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

public final class JsonObjectWriter implements ObjectWriter {
Copy link
Contributor

@marandaneto marandaneto Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is public and not annotated as internal, technically a breaking change, was that intentional?
What's the motivation for the renaming?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a required change in order to serialize protocol classes to either Json or Map<>. IMHO it's an oversight that this class is not marked as apistatus.internal. If we consider this a breaking change we should talk to Karl/Krystof as this PR is blocking the RN profiling feature.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree its an oversight, but the class/interface was used in pretty much every class of the protocol.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


private final JsonWriter jsonWriter;
private final JsonObjectSerializer jsonObjectSerializer;
private final @NotNull JsonWriter jsonWriter;
private final @NotNull JsonObjectSerializer jsonObjectSerializer;

public JsonObjectWriter(Writer out, int maxDepth) {
public JsonObjectWriter(final @NotNull Writer out, final int maxDepth) {
jsonWriter = new JsonWriter(out);
jsonObjectSerializer = new JsonObjectSerializer(maxDepth);
}
Expand Down Expand Up @@ -41,13 +41,13 @@ public JsonObjectWriter endObject() throws IOException {
}

@Override
public JsonObjectWriter name(String name) throws IOException {
public JsonObjectWriter name(final @NotNull String name) throws IOException {
jsonWriter.name(name);
return this;
}

@Override
public JsonObjectWriter value(String value) throws IOException {
public JsonObjectWriter value(final @Nullable String value) throws IOException {
jsonWriter.value(value);
return this;
}
Expand All @@ -59,31 +59,31 @@ public JsonObjectWriter nullValue() throws IOException {
}

@Override
public JsonObjectWriter value(boolean value) throws IOException {
public JsonObjectWriter value(final boolean value) throws IOException {
jsonWriter.value(value);
return this;
}

@Override
public JsonObjectWriter value(Boolean value) throws IOException {
public JsonObjectWriter value(final @Nullable Boolean value) throws IOException {
jsonWriter.value(value);
return this;
}

@Override
public JsonObjectWriter value(double value) throws IOException {
public JsonObjectWriter value(final double value) throws IOException {
jsonWriter.value(value);
return this;
}

@Override
public JsonObjectWriter value(long value) throws IOException {
public JsonObjectWriter value(final long value) throws IOException {
jsonWriter.value(value);
return this;
}

@Override
public JsonObjectWriter value(Number value) throws IOException {
public JsonObjectWriter value(final @Nullable Number value) throws IOException {
jsonWriter.value(value);
return this;
}
Expand All @@ -97,13 +97,13 @@ public JsonObjectWriter value(Number value) throws IOException {
* @return this writer.
*/
@Override
public JsonObjectWriter value(@NotNull ILogger logger, @Nullable Object object)
public JsonObjectWriter value(final @NotNull ILogger logger, final @Nullable Object object)
throws IOException {
jsonObjectSerializer.serialize(this, logger, object);
return this;
}

public void setIndent(@NotNull final String indent) {
public void setIndent(final @NotNull String indent) {
jsonWriter.setIndent(indent);
}
}
3 changes: 2 additions & 1 deletion sentry/src/main/java/io/sentry/JsonSerializable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
import org.jetbrains.annotations.NotNull;

public interface JsonSerializable {
void serialize(@NotNull ObjectWriter writer, @NotNull ILogger logger) throws IOException;
void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger logger)
throws IOException;
}
Loading