Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -7869,7 +7869,6 @@ public final class io/sentry/util/Pair {
public final class io/sentry/util/Platform {
public fun <init> ()V
public static fun isAndroid ()Z
public static fun isJavaNinePlus ()Z
public static fun isJvm ()Z
}

Expand Down
13 changes: 2 additions & 11 deletions sentry/src/main/java/io/sentry/SentryAutoDateProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public final class SentryAutoDateProvider implements SentryDateProvider {
private final @NotNull SentryDateProvider dateProvider;

public SentryAutoDateProvider() {
if (checkInstantAvailabilityAndPrecision()) {
// Instant is sub-millisecond on every supported JVM, but only millisecond-granular on Android.
if (Platform.isJvm()) {
dateProvider = new SentryInstantDateProvider();
} else {
dateProvider = new SentryNanotimeDateProvider();
Expand All @@ -22,14 +23,4 @@ public SentryAutoDateProvider() {
public SentryDate now() {
return dateProvider.now();
}

/**
* Check for Java 9+ because older versions only offer ms precision for {@link java.time.Instant}.
* This class should not be used for Android as it cannot check Android API level.
*
* @return true if Instant offers high precision and this is not Android
*/
private static boolean checkInstantAvailabilityAndPrecision() {
return Platform.isJvm() && Platform.isJavaNinePlus();
}
}
4 changes: 2 additions & 2 deletions sentry/src/main/java/io/sentry/SentryNanotimeDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* precision leading to an end timestamp with ns precision that can be used to gain ns precision
* transaction durations.
*
* <p>This is a workaround for older versions of Java (before 9) and Android API (lower than 26)
* that allows for higher precision than a millisecond timestamp alone would.
* <p>This is a workaround for Android, where {@link java.time.Instant} offers only millisecond
* precision, that allows for higher precision than a millisecond timestamp alone would.
*/
@ApiStatus.Internal
public final class SentryNanotimeDate extends SentryDate {
Expand Down
3 changes: 1 addition & 2 deletions sentry/src/main/java/io/sentry/time/SystemEpochClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
@ApiStatus.Internal
public final class SystemEpochClock implements EpochClock {

private static final boolean INSTANT_IS_SUB_MILLISECOND =
Platform.isJvm() && Platform.isJavaNinePlus();
private static final boolean INSTANT_IS_SUB_MILLISECOND = Platform.isJvm();

private static final SystemEpochClock instance = new SystemEpochClock();

Expand Down
23 changes: 0 additions & 23 deletions sentry/src/main/java/io/sentry/util/Platform.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.sentry.util;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

@ApiStatus.Internal
public final class Platform {
static boolean isAndroid;
static boolean isJavaNinePlus;

static {
// System#getProperty can throw an exception if there is a security manager is configured and
Expand All @@ -18,23 +16,6 @@ public final class Platform {
} catch (Throwable e) {
isAndroid = false;
}

if (isAndroid) {
// Android is never Java 9+, skip the system property lookup + parse on the startup path.
isJavaNinePlus = false;
} else {
try {
final @Nullable String javaStringVersion = System.getProperty("java.specification.version");
if (javaStringVersion != null) {
final double javaVersion = Double.parseDouble(javaStringVersion);
isJavaNinePlus = javaVersion >= 9.0;
} else {
isJavaNinePlus = false;
}
} catch (Throwable e) {
isJavaNinePlus = false;
}
}
}

public static boolean isAndroid() {
Expand All @@ -44,8 +25,4 @@ public static boolean isAndroid() {
public static boolean isJvm() {
return !isAndroid;
}

public static boolean isJavaNinePlus() {
return isJavaNinePlus;
}
}
12 changes: 6 additions & 6 deletions sentry/src/test/java/io/sentry/SentryAutoDateProviderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import kotlin.test.Test
import kotlin.test.assertTrue

class SentryAutoDateProviderTest {
val javaNinePlus = Platform.isJavaNinePlus()
val isAndroid = Platform.isAndroid()

@AfterTest
fun restorePlatform() {
PlatformTestManipulator.pretendJavaNinePlus(javaNinePlus)
PlatformTestManipulator.pretendIsAndroid(isAndroid)
}

@Test
fun `uses SentryInstantDate on Java9+`() {
PlatformTestManipulator.pretendJavaNinePlus(true)
fun `uses SentryInstantDate on the JVM`() {
PlatformTestManipulator.pretendIsAndroid(false)
val now = SentryAutoDateProvider().now()
assertTrue(now is SentryInstantDate)
}

@Test
fun `uses SentryNanotimeDate on Java8`() {
PlatformTestManipulator.pretendJavaNinePlus(false)
fun `uses SentryNanotimeDate on Android`() {
PlatformTestManipulator.pretendIsAndroid(true)
val now = SentryAutoDateProvider().now()
assertTrue(now is SentryNanotimeDate)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package io.sentry.util

class PlatformTestManipulator {
companion object {
fun pretendJavaNinePlus(isJavaNinePlus: Boolean) {
Platform.isJavaNinePlus = isJavaNinePlus
}

fun pretendIsAndroid(isAndroid: Boolean) {
Platform.isAndroid = isAndroid
}
Expand Down
Loading