Merge "Export FlaggedApi.java to libcore/ and art/" into main am: 7f58004434 am: 7cb7ba9651

Original change: https://android-review.googlesource.com/c/platform/frameworks/libs/modules-utils/+/2855670

Change-Id: Ied43a53fc984ffdd8eab0570ba16889b9d47b298
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/java/Android.bp b/java/Android.bp
index 326dc51..e31f878 100644
--- a/java/Android.bp
+++ b/java/Android.bp
@@ -70,6 +70,7 @@
     host_supported: true,
     optimize: {
         proguard_flags_files: ["aconfig_proguard.flags"],
+        export_proguard_flags_files: true,
     },
     visibility: [
         "//visibility:public",
diff --git a/java/android/annotation/NonNull.java b/java/android/annotation/NonNull.java
index 20472ba..635959b 100644
--- a/java/android/annotation/NonNull.java
+++ b/java/android/annotation/NonNull.java
@@ -18,7 +18,7 @@
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
 import static java.lang.annotation.ElementType.PARAMETER;
-import static java.lang.annotation.RetentionPolicy.SOURCE;
+import static java.lang.annotation.RetentionPolicy.CLASS;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.Target;
@@ -32,7 +32,7 @@
  * @returnDoc This value cannot be {@code null}.
  * @hide
  */
-@Retention(SOURCE)
+@Retention(CLASS)
 @Target({METHOD, PARAMETER, FIELD})
 public @interface NonNull {
 }
diff --git a/java/android/annotation/Nullable.java b/java/android/annotation/Nullable.java
index b8473e7..11b6511 100644
--- a/java/android/annotation/Nullable.java
+++ b/java/android/annotation/Nullable.java
@@ -18,7 +18,7 @@
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
 import static java.lang.annotation.ElementType.PARAMETER;
-import static java.lang.annotation.RetentionPolicy.SOURCE;
+import static java.lang.annotation.RetentionPolicy.CLASS;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.Target;
@@ -39,7 +39,7 @@
  * @returnDoc This value may be {@code null}.
  * @hide
  */
-@Retention(SOURCE)
+@Retention(CLASS)
 @Target({METHOD, PARAMETER, FIELD})
 public @interface Nullable {
 }
diff --git a/java/com/android/internal/util/Preconditions.java b/java/com/android/internal/util/Preconditions.java
index d2d8220..bee0808 100644
--- a/java/com/android/internal/util/Preconditions.java
+++ b/java/com/android/internal/util/Preconditions.java
@@ -18,6 +18,7 @@
 
 import android.annotation.IntRange;
 import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.compat.annotation.UnsupportedAppUsage;
 import android.os.Build;
 import android.text.TextUtils;
@@ -768,4 +769,17 @@
 
         return value;
     }
+
+    /**
+     * Throws an exception that guides developers to configure a {@code RavenwoodRule} when the
+     * given argument is {@code null}.
+     */
+    public static <T> @NonNull T requireNonNullViaRavenwoodRule(@Nullable T t) {
+        if (t == null) {
+            throw new IllegalStateException("This operation requires that a RavenwoodRule be "
+                    + "configured to accurately define the expected test environment");
+        } else {
+            return t;
+        }
+    }
 }
diff --git a/java/com/android/modules/utils/FastDataInput.java b/java/com/android/modules/utils/FastDataInput.java
index daa86d5..318382b 100644
--- a/java/com/android/modules/utils/FastDataInput.java
+++ b/java/com/android/modules/utils/FastDataInput.java
@@ -18,8 +18,6 @@
 
 import android.annotation.NonNull;
 
-import dalvik.system.VMRuntime;
-
 import java.io.BufferedInputStream;
 import java.io.Closeable;
 import java.io.DataInput;
@@ -42,8 +40,6 @@
 
     protected static final int DEFAULT_BUFFER_SIZE = 32_768;
 
-    protected final VMRuntime mRuntime;
-
     protected final byte[] mBuffer;
     protected final int mBufferCap;
 
@@ -58,13 +54,12 @@
     private String[] mStringRefs = new String[32];
 
     public FastDataInput(@NonNull InputStream in, int bufferSize) {
-        mRuntime = VMRuntime.getRuntime();
         mIn = Objects.requireNonNull(in);
         if (bufferSize < 8) {
             throw new IllegalArgumentException();
         }
 
-        mBuffer = (byte[]) mRuntime.newNonMovableArray(byte.class, bufferSize);
+        mBuffer = newByteArray(bufferSize);
         mBufferCap = mBuffer.length;
     }
 
@@ -92,6 +87,10 @@
         mStringRefCount = 0;
     }
 
+    public byte[] newByteArray(int bufferSize) {
+        return new byte[bufferSize];
+    }
+
     /**
      * Re-initializes the object for the new input.
      */
@@ -173,7 +172,7 @@
             mBufferPos += len;
             return res;
         } else {
-            final byte[] tmp = (byte[]) mRuntime.newNonMovableArray(byte.class, len + 1);
+            final byte[] tmp = newByteArray(len + 1);
             readFully(tmp, 0, len);
             return ModifiedUtf8.decode(tmp, new char[len], 0, len);
         }
@@ -207,6 +206,10 @@
 
             return s;
         } else {
+            if (ref >= mStringRefs.length) {
+                throw new IOException("Invalid interned string reference " + ref + " for "
+                        + mStringRefs.length + " interned strings");
+            }
             return mStringRefs[ref];
         }
     }
diff --git a/java/com/android/modules/utils/FastDataOutput.java b/java/com/android/modules/utils/FastDataOutput.java
index 2098c2d..ae19573 100644
--- a/java/com/android/modules/utils/FastDataOutput.java
+++ b/java/com/android/modules/utils/FastDataOutput.java
@@ -18,8 +18,6 @@
 
 import android.annotation.NonNull;
 
-import dalvik.system.VMRuntime;
-
 import java.io.BufferedOutputStream;
 import java.io.Closeable;
 import java.io.DataOutput;
@@ -42,8 +40,6 @@
 
     protected static final int DEFAULT_BUFFER_SIZE = 32_768;
 
-    protected final VMRuntime mRuntime;
-
     protected final byte[] mBuffer;
     protected final int mBufferCap;
 
@@ -56,12 +52,11 @@
     private final HashMap<String, Integer> mStringRefs = new HashMap<>();
 
     public FastDataOutput(@NonNull OutputStream out, int bufferSize) {
-        mRuntime = VMRuntime.getRuntime();
         if (bufferSize < 8) {
             throw new IllegalArgumentException();
         }
 
-        mBuffer = (byte[]) mRuntime.newNonMovableArray(byte.class, bufferSize);
+        mBuffer = newByteArray(bufferSize);
         mBufferCap = mBuffer.length;
 
         setOutput(out);
@@ -94,6 +89,10 @@
         mStringRefs.clear();
     }
 
+    public byte[] newByteArray(int bufferSize) {
+        return new byte[bufferSize];
+    }
+
     /**
      * Re-initializes the object for the new output.
      */
@@ -163,7 +162,7 @@
             ModifiedUtf8.encode(mBuffer, mBufferPos, s);
             mBufferPos += len;
         } else {
-            final byte[] tmp = (byte[]) mRuntime.newNonMovableArray(byte.class, len + 1);
+            final byte[] tmp = newByteArray(len + 1);
             ModifiedUtf8.encode(tmp, 0, s);
             writeShort(len);
             write(tmp, 0, len);