Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import io.sentry.IScopes;
import io.sentry.ISentryLifecycleToken;
import io.sentry.Integration;
import io.sentry.OptionsContainer;
import io.sentry.Sentry;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
Expand Down Expand Up @@ -98,7 +97,7 @@ public static void init(
@NotNull Sentry.OptionsConfiguration<SentryAndroidOptions> configuration) {
try (final @NotNull ISentryLifecycleToken ignored = staticLock.acquire()) {
Sentry.init(
OptionsContainer.create(SentryAndroidOptions.class),
new SentryAndroidOptionsContainer(),
options -> {
final io.sentry.util.LoadClass classLoader = new io.sentry.util.LoadClass();
final boolean isTimberUpstreamAvailable =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.sentry.android.core;

import io.sentry.OptionsContainer;
import org.jetbrains.annotations.NotNull;

/**
* Direct OptionsContainer for SentryAndroidOptions that avoids reflective
* getDeclaredConstructor().newInstance() on the Android startup path.
*/
final class SentryAndroidOptionsContainer extends OptionsContainer<SentryAndroidOptions> {

@Override
public @NotNull SentryAndroidOptions createInstance() {
return new SentryAndroidOptions();
}
}
3 changes: 2 additions & 1 deletion sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -2068,7 +2068,8 @@ public abstract interface class io/sentry/ObjectWriter {
public abstract fun value (Z)Lio/sentry/ObjectWriter;
}

public final class io/sentry/OptionsContainer {
public class io/sentry/OptionsContainer {
protected fun <init> ()V
public static fun create (Ljava/lang/Class;)Lio/sentry/OptionsContainer;
public fun createInstance ()Ljava/lang/Object;
}
Expand Down
18 changes: 15 additions & 3 deletions sentry/src/main/java/io/sentry/OptionsContainer.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
package io.sentry;

import com.jakewharton.nopen.annotation.Open;
import io.sentry.util.Objects;
import java.lang.reflect.InvocationTargetException;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@ApiStatus.Internal
public final class OptionsContainer<T> {
@Open
public class OptionsContainer<T> {

public @NotNull static <T> OptionsContainer<T> create(final @NotNull Class<T> clazz) {
return new OptionsContainer<>(clazz);
}

private final @NotNull Class<T> clazz;
private final @Nullable Class<T> clazz;

private OptionsContainer(final @NotNull Class<T> clazz) {
super();
this.clazz = clazz;
}

/** Constructor for subclasses that create the instance directly without reflection. */
protected OptionsContainer() {
super();
this.clazz = null;
}

public @NotNull T createInstance()
throws InstantiationException,
IllegalAccessException,
NoSuchMethodException,
InvocationTargetException {
return clazz.getDeclaredConstructor().newInstance();
return Objects.requireNonNull(clazz, "OptionsContainer clazz is required")
.getDeclaredConstructor()
.newInstance();
}
}
Loading