Merge "Add a comment about the leaks in JniConstants"
diff --git a/JavaLibrary.bp b/JavaLibrary.bp
index afba8e9..528db8f 100644
--- a/JavaLibrary.bp
+++ b/JavaLibrary.bp
@@ -503,6 +503,8 @@
         "android-support-test",
     ],
     platform_apis: true,
+    // This builds classes that are in the java.base Java module:
+    patch_module: "java.base",
 }
 
 // Builds the core-tests-support library used by various tests.
@@ -744,7 +746,7 @@
 //
 // Only for use by art.module.public.api.stubs target below.
 droidstubs {
-    name: "art-module-public-api-stubs-gen",
+    name: "art-module-public-api-stubs-source",
     srcs: [
         ":core_oj_api_files",
         ":core_libart_api_files",
@@ -752,16 +754,13 @@
     java_version: "1.9",
     installable: false,
     sdk_version: "none",
+    system_modules: "none",
 }
 
 // A stubs target containing the parts of the public SDK API provided by the ART module.
 java_library {
     name: "art.module.public.api.stubs",
-    visibility: [
-        "//external/conscrypt",
-        "//external/icu/android_icu4j",
-    ],
-    srcs: [":art-module-public-api-stubs-gen"],
+    srcs: [":art-module-public-api-stubs-source"],
     errorprone: {
         javacflags: [
             "-Xep:MissingOverride:OFF",
@@ -853,6 +852,7 @@
     srcs: [":art_module_api_files"],
     installable: false,
     sdk_version: "none",
+    system_modules: "none",
     annotations_enabled: true,
     args: "--hide-annotation libcore.api.Hide " +
         "--validate-nullability-from-merged-stubs ",
@@ -917,5 +917,6 @@
     srcs: [
         ":api_surface_annotation_files",
     ],
+    java_version: "1.9",
     sdk_version: "core_current",
 }
diff --git a/dalvik/src/main/java/dalvik/annotation/compat/UnsupportedAppUsage.java b/dalvik/src/main/java/dalvik/annotation/compat/UnsupportedAppUsage.java
index aaebbb8..888ad38 100644
--- a/dalvik/src/main/java/dalvik/annotation/compat/UnsupportedAppUsage.java
+++ b/dalvik/src/main/java/dalvik/annotation/compat/UnsupportedAppUsage.java
@@ -21,7 +21,6 @@
 import static java.lang.annotation.ElementType.TYPE;
 import static java.lang.annotation.RetentionPolicy.CLASS;
 
-import dalvik.system.VersionCodes;
 import java.lang.annotation.Repeatable;
 import java.lang.annotation.Retention;
 import java.lang.annotation.Target;
diff --git a/dalvik/src/main/java/dalvik/system/VersionCodes.java b/dalvik/src/main/java/dalvik/annotation/compat/VersionCodes.java
similarity index 97%
rename from dalvik/src/main/java/dalvik/system/VersionCodes.java
rename to dalvik/src/main/java/dalvik/annotation/compat/VersionCodes.java
index b20af83..8f7bcd2 100644
--- a/dalvik/src/main/java/dalvik/system/VersionCodes.java
+++ b/dalvik/src/main/java/dalvik/annotation/compat/VersionCodes.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package dalvik.system;
+package dalvik.annotation.compat;
 
 import libcore.api.CorePlatformApi;
 import libcore.api.IntraCoreApi;
diff --git a/luni/src/main/java/android/compat/Compatibility.java b/luni/src/main/java/android/compat/Compatibility.java
index a0cc0a0..1fbdf76 100644
--- a/luni/src/main/java/android/compat/Compatibility.java
+++ b/luni/src/main/java/android/compat/Compatibility.java
@@ -154,6 +154,10 @@
             }
         }
 
+        public boolean isEmpty() {
+            return enabled.isEmpty() && disabled.isEmpty();
+        }
+
         private static long[] toLongArray(Set<Long> values) {
             long[] result = new long[values.size()];
             int idx = 0;
diff --git a/luni/src/main/java/libcore/util/FP16.java b/luni/src/main/java/libcore/util/FP16.java
index 88fe43a..9157f85 100644
--- a/luni/src/main/java/libcore/util/FP16.java
+++ b/luni/src/main/java/libcore/util/FP16.java
@@ -626,30 +626,42 @@
         } else {
             e = e - FP32_EXPONENT_BIAS + EXPONENT_BIAS;
             if (e >= 0x1f) { // Overflow
-                outE = 0x31;
+                outE = 0x1f;
             } else if (e <= 0) { // Underflow
                 if (e < -10) {
                     // The absolute fp32 value is less than MIN_VALUE, flush to +/-0
                 } else {
                     // The fp32 value is a normalized float less than MIN_NORMAL,
                     // we convert to a denorm fp16
-                    m = (m | 0x800000) >> (1 - e);
-                    if ((m & 0x1000) != 0) m += 0x2000;
-                    outM = m >> 13;
+                    m = m | 0x800000;
+                    int shift = 14 - e;
+                    outM = m >> shift;
+
+                    int lowm = m & ((1 << shift) - 1);
+                    int hway = 1 << (shift - 1);
+                    // if above halfway or exactly halfway and outM is odd
+                    if (lowm + (outM & 1) > hway){
+                        // Round to nearest even
+                        // Can overflow into exponent bit, which surprisingly is OK.
+                        // This increment relies on the +outM in the return statement below
+                        outM++;
+                    }
                 }
             } else {
                 outE = e;
                 outM = m >> 13;
-                if ((m & 0x1000) != 0) {
-                    // Round to nearest "0.5" up
-                    int out = (outE << EXPONENT_SHIFT) | outM;
-                    out++;
-                    return (short) (out | (s << SIGN_SHIFT));
+                // if above halfway or exactly halfway and outM is odd
+                if ((m & 0x1fff) + (outM & 0x1) > 0x1000) {
+                    // Round to nearest even
+                    // Can overflow into exponent bit, which surprisingly is OK.
+                    // This increment relies on the +outM in the return statement below
+                    outM++;
                 }
             }
         }
-
-        return (short) ((s << SIGN_SHIFT) | (outE << EXPONENT_SHIFT) | outM);
+        // The outM is added here as the +1 increments for outM above can
+        // cause an overflow in the exponent bit which is OK.
+        return (short) ((s << SIGN_SHIFT) | (outE << EXPONENT_SHIFT) + outM);
     }
 
     /**
diff --git a/mmodules/core_platform_api/Android.bp b/mmodules/core_platform_api/Android.bp
index b82cc13..adf7d28 100644
--- a/mmodules/core_platform_api/Android.bp
+++ b/mmodules/core_platform_api/Android.bp
@@ -25,6 +25,7 @@
         ":art_module_api_files",
     ],
     sdk_version: "none",
+    system_modules: "none",
     libs: [
         // Needed to break the cycle in the platform api caused by
         // b/141747409.
@@ -69,10 +70,6 @@
 // core library modules.
 java_library {
     name: "art.module.platform.api.stubs",
-    visibility: [
-        "//external/conscrypt",
-        "//external/icu/android_icu4j",
-    ],
     srcs: [
         ":art-module-platform-api-stubs-source",
     ],
diff --git a/mmodules/core_platform_api/api/platform/current-api.txt b/mmodules/core_platform_api/api/platform/current-api.txt
index 14d96eb..5181f94 100644
--- a/mmodules/core_platform_api/api/platform/current-api.txt
+++ b/mmodules/core_platform_api/api/platform/current-api.txt
@@ -21,6 +21,7 @@
     method public java.util.Set<java.lang.Long> forceDisabledSet();
     method public long[] forceEnabledChangesArray();
     method public java.util.Set<java.lang.Long> forceEnabledSet();
+    method public boolean isEmpty();
     method public boolean isForceDisabled(long);
     method public boolean isForceEnabled(long);
   }
@@ -528,6 +529,12 @@
     method public abstract dalvik.annotation.compat.UnsupportedAppUsage[] value();
   }
 
+  public class VersionCodes {
+    field public static final int O = 26; // 0x1a
+    field public static final int P = 28; // 0x1c
+    field public static final int Q = 29; // 0x1d
+  }
+
 }
 
 package dalvik.annotation.optimization {
@@ -752,12 +759,6 @@
     method @dalvik.annotation.optimization.FastNative public static dalvik.system.AnnotatedStackTraceElement[] getAnnotatedThreadStackTrace(Thread);
   }
 
-  public class VersionCodes {
-    field public static final int O = 26; // 0x1a
-    field public static final int P = 28; // 0x1c
-    field public static final int Q = 29; // 0x1d
-  }
-
   public final class ZygoteHooks {
     method public static void gcAndFinalize();
     method public static void onBeginPreload();
diff --git a/mmodules/intracoreapi/Android.bp b/mmodules/intracoreapi/Android.bp
index 4212f7f..f8454bb 100644
--- a/mmodules/intracoreapi/Android.bp
+++ b/mmodules/intracoreapi/Android.bp
@@ -25,6 +25,7 @@
         ":art_module_api_files",
     ],
     sdk_version: "none",
+    system_modules: "none",
 
     installable: false,
     args: "--hide-annotation libcore.api.Hide " +
@@ -53,8 +54,6 @@
 java_library {
     name: "art.module.intra.core.api.stubs",
     visibility: [
-        "//external/conscrypt",
-        "//external/icu/android_icu4j",
         "//libcore/mmodules/core_platform_api",
     ],
     srcs: [
diff --git a/mmodules/intracoreapi/api/intra/current-api.txt b/mmodules/intracoreapi/api/intra/current-api.txt
index c2024d6..46a1298 100644
--- a/mmodules/intracoreapi/api/intra/current-api.txt
+++ b/mmodules/intracoreapi/api/intra/current-api.txt
@@ -12,6 +12,7 @@
     method public java.util.Set<java.lang.Long> forceDisabledSet();
     method public long[] forceEnabledChangesArray();
     method public java.util.Set<java.lang.Long> forceEnabledSet();
+    method public boolean isEmpty();
     method public boolean isForceDisabled(long);
     method public boolean isForceEnabled(long);
   }
@@ -46,6 +47,12 @@
     method @libcore.api.CorePlatformApi @libcore.api.IntraCoreApi public abstract dalvik.annotation.compat.UnsupportedAppUsage[] value();
   }
 
+  @libcore.api.CorePlatformApi @libcore.api.IntraCoreApi public class VersionCodes {
+    field @libcore.api.CorePlatformApi @libcore.api.IntraCoreApi public static final int O = 26; // 0x1a
+    field @libcore.api.CorePlatformApi @libcore.api.IntraCoreApi public static final int P = 28; // 0x1c
+    field @libcore.api.CorePlatformApi @libcore.api.IntraCoreApi public static final int Q = 29; // 0x1d
+  }
+
 }
 
 package dalvik.annotation.optimization {
@@ -72,12 +79,6 @@
     method @dalvik.annotation.compat.UnsupportedAppUsage(trackingBug=111170242) @libcore.api.CorePlatformApi @libcore.api.IntraCoreApi public void warnIfOpen();
   }
 
-  @libcore.api.CorePlatformApi @libcore.api.IntraCoreApi public class VersionCodes {
-    field @libcore.api.CorePlatformApi @libcore.api.IntraCoreApi public static final int O = 26; // 0x1a
-    field @libcore.api.CorePlatformApi @libcore.api.IntraCoreApi public static final int P = 28; // 0x1c
-    field @libcore.api.CorePlatformApi @libcore.api.IntraCoreApi public static final int Q = 29; // 0x1d
-  }
-
 }
 
 package java.net {
diff --git a/non_openjdk_java_files.bp b/non_openjdk_java_files.bp
index 4f7a33d..f889637 100644
--- a/non_openjdk_java_files.bp
+++ b/non_openjdk_java_files.bp
@@ -113,7 +113,7 @@
     name: "hidden_api_surface_annotation_files",
     srcs: [
         "dalvik/src/main/java/dalvik/annotation/compat/UnsupportedAppUsage.java",
-        "dalvik/src/main/java/dalvik/system/VersionCodes.java",
+        "dalvik/src/main/java/dalvik/annotation/compat/VersionCodes.java",
     ],
 }
 
@@ -405,8 +405,6 @@
         "luni/src/main/java/libcore/reflect/WildcardTypeImpl.java",
         "luni/src/main/java/libcore/util/CharsetUtils.java",
         "luni/src/main/java/libcore/util/CollectionUtils.java",
-        "luni/src/main/java/libcore/util/NonNull.java",
-        "luni/src/main/java/libcore/util/Nullable.java",
         "luni/src/main/java/libcore/util/NullFromTypeParam.java",
         "luni/src/main/java/libcore/util/Objects.java",
         "luni/src/main/java/org/apache/harmony/xml/ExpatAttributes.java",
diff --git a/ojluni/annotations/hiddenapi/java/lang/Boolean.java b/ojluni/annotations/hiddenapi/java/lang/Boolean.java
index 14f0255..d5b749c 100644
--- a/ojluni/annotations/hiddenapi/java/lang/Boolean.java
+++ b/ojluni/annotations/hiddenapi/java/lang/Boolean.java
@@ -26,7 +26,7 @@
 package java.lang;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public final class Boolean
diff --git a/ojluni/annotations/hiddenapi/java/lang/Byte.java b/ojluni/annotations/hiddenapi/java/lang/Byte.java
index 6938eb7..4e58f2b 100644
--- a/ojluni/annotations/hiddenapi/java/lang/Byte.java
+++ b/ojluni/annotations/hiddenapi/java/lang/Byte.java
@@ -26,7 +26,7 @@
 package java.lang;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public final class Byte extends java.lang.Number implements java.lang.Comparable<java.lang.Byte> {
diff --git a/ojluni/annotations/hiddenapi/java/lang/Character.java b/ojluni/annotations/hiddenapi/java/lang/Character.java
index 6ac842d..d6c68e3 100644
--- a/ojluni/annotations/hiddenapi/java/lang/Character.java
+++ b/ojluni/annotations/hiddenapi/java/lang/Character.java
@@ -26,7 +26,7 @@
 package java.lang;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public final class Character
diff --git a/ojluni/annotations/hiddenapi/java/lang/Double.java b/ojluni/annotations/hiddenapi/java/lang/Double.java
index 187b7b4..d58289f 100644
--- a/ojluni/annotations/hiddenapi/java/lang/Double.java
+++ b/ojluni/annotations/hiddenapi/java/lang/Double.java
@@ -26,7 +26,7 @@
 package java.lang;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public final class Double extends java.lang.Number
diff --git a/ojluni/annotations/hiddenapi/java/lang/Float.java b/ojluni/annotations/hiddenapi/java/lang/Float.java
index 1081764..da45b52 100644
--- a/ojluni/annotations/hiddenapi/java/lang/Float.java
+++ b/ojluni/annotations/hiddenapi/java/lang/Float.java
@@ -26,7 +26,7 @@
 package java.lang;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public final class Float extends java.lang.Number implements java.lang.Comparable<java.lang.Float> {
diff --git a/ojluni/annotations/hiddenapi/java/lang/Integer.java b/ojluni/annotations/hiddenapi/java/lang/Integer.java
index adb8c9c..49bec24 100644
--- a/ojluni/annotations/hiddenapi/java/lang/Integer.java
+++ b/ojluni/annotations/hiddenapi/java/lang/Integer.java
@@ -27,7 +27,7 @@
 package java.lang;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public final class Integer extends java.lang.Number
diff --git a/ojluni/annotations/hiddenapi/java/lang/Long.java b/ojluni/annotations/hiddenapi/java/lang/Long.java
index 2f32b27..a8ae869 100644
--- a/ojluni/annotations/hiddenapi/java/lang/Long.java
+++ b/ojluni/annotations/hiddenapi/java/lang/Long.java
@@ -27,7 +27,7 @@
 package java.lang;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public final class Long extends java.lang.Number implements java.lang.Comparable<java.lang.Long> {
diff --git a/ojluni/annotations/hiddenapi/java/lang/Short.java b/ojluni/annotations/hiddenapi/java/lang/Short.java
index f1f0641..00583c9 100644
--- a/ojluni/annotations/hiddenapi/java/lang/Short.java
+++ b/ojluni/annotations/hiddenapi/java/lang/Short.java
@@ -26,7 +26,7 @@
 package java.lang;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public final class Short extends java.lang.Number implements java.lang.Comparable<java.lang.Short> {
diff --git a/ojluni/annotations/hiddenapi/java/net/InetAddress.java b/ojluni/annotations/hiddenapi/java/net/InetAddress.java
index e1a1deb..6436a6b 100644
--- a/ojluni/annotations/hiddenapi/java/net/InetAddress.java
+++ b/ojluni/annotations/hiddenapi/java/net/InetAddress.java
@@ -27,7 +27,7 @@
 package java.net;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public class InetAddress implements java.io.Serializable {
diff --git a/ojluni/annotations/hiddenapi/javax/net/ssl/SSLServerSocketFactory.java b/ojluni/annotations/hiddenapi/javax/net/ssl/SSLServerSocketFactory.java
index 61c720b..8226c75 100644
--- a/ojluni/annotations/hiddenapi/javax/net/ssl/SSLServerSocketFactory.java
+++ b/ojluni/annotations/hiddenapi/javax/net/ssl/SSLServerSocketFactory.java
@@ -27,7 +27,7 @@
 package javax.net.ssl;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public abstract class SSLServerSocketFactory extends javax.net.ServerSocketFactory {
diff --git a/ojluni/annotations/hiddenapi/javax/net/ssl/SSLSocketFactory.java b/ojluni/annotations/hiddenapi/javax/net/ssl/SSLSocketFactory.java
index 70a11bd..e79418d 100644
--- a/ojluni/annotations/hiddenapi/javax/net/ssl/SSLSocketFactory.java
+++ b/ojluni/annotations/hiddenapi/javax/net/ssl/SSLSocketFactory.java
@@ -27,7 +27,7 @@
 package javax.net.ssl;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public abstract class SSLSocketFactory extends javax.net.SocketFactory {
diff --git a/ojluni/annotations/hiddenapi/sun/misc/Unsafe.java b/ojluni/annotations/hiddenapi/sun/misc/Unsafe.java
index 54308e4..f560d96 100644
--- a/ojluni/annotations/hiddenapi/sun/misc/Unsafe.java
+++ b/ojluni/annotations/hiddenapi/sun/misc/Unsafe.java
@@ -26,7 +26,7 @@
 package sun.misc;
 
 import dalvik.annotation.compat.UnsupportedAppUsage;
-import dalvik.system.VersionCodes;
+import dalvik.annotation.compat.VersionCodes;
 
 @SuppressWarnings({"unchecked", "deprecation", "all"})
 public final class Unsafe {
diff --git a/openjdk_java_files.bp b/openjdk_java_files.bp
index 885517b..57f6251 100644
--- a/openjdk_java_files.bp
+++ b/openjdk_java_files.bp
@@ -1834,6 +1834,7 @@
     srcs: [":openjdk_javadoc_files"],
     installable: false,
     sdk_version: "none",
+    system_modules: "none",
     args: "--exclude-documentation-from-stubs",
 }
 
@@ -1849,6 +1850,7 @@
     ],
     installable: false,
     sdk_version: "none",
+    system_modules: "none",
     args: "--exclude-documentation-from-stubs " +
         "--hide-annotation libcore.api.Hide ",
     merge_inclusion_annotations_dirs: ["ojluni-annotated-mmodule-stubs"],
diff --git a/test-rules/src/platform_compat/java/android/compat/CompatChangeRule.java b/test-rules/src/platform_compat/java/android/compat/CompatChangeRule.java
index 2586f9c..1bae1a2 100644
--- a/test-rules/src/platform_compat/java/android/compat/CompatChangeRule.java
+++ b/test-rules/src/platform_compat/java/android/compat/CompatChangeRule.java
@@ -86,17 +86,12 @@
         if (disableCompatChanges != null) {
             disabled.addAll(Longs.asList(disableCompatChanges.value()));
         }
-        ArraySet<Long> intersection = new ArraySet<>(enabled);
-        intersection.retainAll(disabled);
-        if (!intersection.isEmpty()) {
-            throw new IllegalArgumentException(
-                    "Changes " + intersection + " are both enabled and disabled.");
-        }
-        if (enabled.isEmpty() && disabled.isEmpty()) {
-            throw new IllegalStateException("Added a CompatChangeRule without specifying any "
+        ChangeConfig config = new ChangeConfig(enabled, disabled);
+        if (config.isEmpty()) {
+            throw new IllegalArgumentException("Added a CompatChangeRule without specifying any "
                 + "@EnableCompatChanges or @DisableCompatChanges !");
         }
-        return new CompatChangeStatement(statement, new ChangeConfig(enabled, disabled));
+        return new CompatChangeStatement(statement, config);
     }
 
     private static class CompatChangeStatement extends Statement {