[ApiModel] Fix stub modeling options check

Bug: b/259076765
Change-Id: I64b224ebe65efeb0004e8020604939578f508a8e
diff --git a/src/main/java/com/android/tools/r8/androidapi/ApiReferenceStubber.java b/src/main/java/com/android/tools/r8/androidapi/ApiReferenceStubber.java
index 4864e0b..c6d8571 100644
--- a/src/main/java/com/android/tools/r8/androidapi/ApiReferenceStubber.java
+++ b/src/main/java/com/android/tools/r8/androidapi/ApiReferenceStubber.java
@@ -226,21 +226,7 @@
       DexLibraryClass libraryClass,
       ThrowExceptionCode throwExceptionCode) {
     DexItemFactory factory = appView.dexItemFactory();
-    if (libraryClass.getType() == factory.objectType
-        || libraryClass.getType().toDescriptorString().startsWith("Ljava/")) {
-      return;
-    }
-    // We cannot reliably create a stub that will have the same throwing
-    // behavior for all VMs. We only create stubs for exceptions to allow them being present in
-    // catch handlers. See b/b/258270051 for more information.
-    if (!isThrowable(libraryClass)
-        || appView.options().apiModelingOptions().stubNonThrowableClasses) {
-      return;
-    }
-    if (appView
-        .options()
-        .machineDesugaredLibrarySpecification
-        .isSupported(libraryClass.getType())) {
+    if (!appView.options().apiModelingOptions().stubbingEnabledFor(appView, libraryClass)) {
       return;
     }
     Set<ProgramDefinition> contexts = referencingContexts.get(libraryClass);
@@ -277,16 +263,4 @@
             },
             ignored -> {});
   }
-
-  private boolean isThrowable(DexLibraryClass libraryClass) {
-    DexClass current = libraryClass;
-    while (current.getSuperType() != null) {
-      DexType superType = current.getSuperType();
-      if (superType == factory.throwableType) {
-        return true;
-      }
-      current = appView.definitionFor(current.getSuperType());
-    }
-    return false;
-  }
 }
diff --git a/src/main/java/com/android/tools/r8/graph/DexItemFactory.java b/src/main/java/com/android/tools/r8/graph/DexItemFactory.java
index d838596..411e5ee 100644
--- a/src/main/java/com/android/tools/r8/graph/DexItemFactory.java
+++ b/src/main/java/com/android/tools/r8/graph/DexItemFactory.java
@@ -142,6 +142,7 @@
   public final DexString shortDescriptor = createString("S");
   public final DexString voidDescriptor = createString("V");
   public final DexString descriptorSeparator = createString("/");
+  public final DexString javaDescriptorPrefix = createString("Ljava/");
 
   private final DexString booleanArrayDescriptor = createString("[Z");
   private final DexString byteArrayDescriptor = createString("[B");
diff --git a/src/main/java/com/android/tools/r8/utils/InternalOptions.java b/src/main/java/com/android/tools/r8/utils/InternalOptions.java
index b726531..c7370be 100644
--- a/src/main/java/com/android/tools/r8/utils/InternalOptions.java
+++ b/src/main/java/com/android/tools/r8/utils/InternalOptions.java
@@ -828,7 +828,8 @@
   private final ProtoShrinkingOptions protoShrinking = new ProtoShrinkingOptions();
   private final KotlinOptimizationOptions kotlinOptimizationOptions =
       new KotlinOptimizationOptions();
-  private final ApiModelTestingOptions apiModelTestingOptions = new ApiModelTestingOptions();
+  private final ApiModelTestingOptions apiModelTestingOptions =
+      new ApiModelTestingOptions(dexItemFactory());
   private final DesugarSpecificOptions desugarSpecificOptions = new DesugarSpecificOptions();
   private final MappingComposeOptions mappingComposeOptions = new MappingComposeOptions();
   private final ArtProfileOptions artProfileOptions = new ArtProfileOptions();
@@ -1832,6 +1833,40 @@
     public void disableStubbingOfClasses() {
       enableStubbingOfClasses = false;
     }
+
+    public boolean stubbingEnabledFor(AppView<?> appView, DexLibraryClass libraryClass) {
+      if (libraryClass.getType() == appView.dexItemFactory().objectType
+          || libraryClass
+              .getType()
+              .getDescriptor()
+              .startsWith(appView.dexItemFactory().javaDescriptorPrefix)) {
+        return false;
+      }
+      // Check if desugared library will bridge the type.
+      if (appView
+          .options()
+          .machineDesugaredLibrarySpecification
+          .isSupported(libraryClass.getType())) {
+        return false;
+      }
+      // We cannot reliably create a stub that will have the same throwing behavior for all VMs. We
+      // only create stubs for exceptions to allow them being present in catch handlers. See
+      // b/258270051 for more information. Note that throwables in the java namespace are not
+      // stubbed (bailout above).
+      return isThrowable(appView, libraryClass) || stubNonThrowableClasses;
+    }
+
+    private boolean isThrowable(AppView<?> appView, DexLibraryClass libraryClass) {
+      DexClass current = libraryClass;
+      while (current.getSuperType() != null) {
+        DexType superType = current.getSuperType();
+        if (superType == appView.dexItemFactory().throwableType) {
+          return true;
+        }
+        current = appView.definitionFor(current.getSuperType());
+      }
+      return false;
+    }
   }
 
   public static class ProtoShrinkingOptions {