Remove Transliterator wrapper class.

This change removes the wrapper class in Java which was used for
proxying requests to ICU4c. Now ICU4J is called directly at the
call sites. See: https://android-review.googlesource.com/#/c/162002/

This is done by replacing instantiations of the old Transliterator
class with calls to a static factory in ICU4J which returns a cached
transliterator if available, otherwise creates it on demand.

This change requires that the transliterator resource directories
and source files are added to our java libraries, which increases
the size of core-libart.jar from 2.289 MB to 2.332 MB. Maybe it's
possible to wait until dependencies on ICU4c have been removed to
see if we can save space there.

Change-Id: I2b2f84a051ba9815ba6d346dfe7fb764e39032c4
diff --git a/JavaLibrary.mk b/JavaLibrary.mk
index be3b4b2..8720170 100644
--- a/JavaLibrary.mk
+++ b/JavaLibrary.mk
@@ -77,10 +77,9 @@
 icu4j_src_files := $(call all-java-files-under,$(icu4j_root)/main/classes)
 
 # Filter out bits of ICU4J we don't use yet : the SPIs (which we have limited support for),
-# the charset encoders and the transliterators.
+# and the charset encoders
 icu4j_src_files := $(filter-out $(icu4j_root)/main/classes/localespi/%, $(icu4j_src_files))
 icu4j_src_files := $(filter-out $(icu4j_root)/main/classes/charset/%, $(icu4j_src_files))
-icu4j_src_files := $(filter-out $(icu4j_root)/main/classes/translit/%, $(icu4j_src_files))
 
 # Not all src dirs contain resources, some instead contain other random files
 # that should not be included as resources. The ones that should be included
@@ -93,7 +92,6 @@
 icu4j_resource_dirs := $(call all-icu-subdir-with-subdir,$(icu4j_root)/main/classes/*/src,com/ibm/icu)
 icu4j_resource_dirs := $(filter-out $(icu4j_root)/main/classes/localespi/%, $(icu4j_resource_dirs))
 icu4j_resource_dirs := $(filter-out $(icu4j_root)/main/classes/charset/%, $(icu4j_resource_dirs))
-icu4j_resource_dirs := $(filter-out $(icu4j_root)/main/classes/translit/%, $(icu4j_resource_dirs))
 
 
 
diff --git a/libart/src/main/java/java/lang/CaseMapper.java b/libart/src/main/java/java/lang/CaseMapper.java
index f23a4ef..5cf4ecf 100644
--- a/libart/src/main/java/java/lang/CaseMapper.java
+++ b/libart/src/main/java/java/lang/CaseMapper.java
@@ -18,7 +18,7 @@
 
 import java.util.Locale;
 import libcore.icu.ICU;
-import libcore.icu.Transliterator;
+import com.ibm.icu.text.Transliterator;
 
 /**
  * Performs case operations as described by http://unicode.org/reports/tr21/tr21-5.html.
@@ -138,7 +138,7 @@
 
     private static final ThreadLocal<Transliterator> EL_UPPER = new ThreadLocal<Transliterator>() {
         @Override protected Transliterator initialValue() {
-            return new Transliterator("el-Upper");
+            return Transliterator.getInstance("el-Upper");
         }
     };
 
diff --git a/luni/src/main/java/libcore/icu/Transliterator.java b/luni/src/main/java/libcore/icu/Transliterator.java
deleted file mode 100644
index 77b5ba7..0000000
--- a/luni/src/main/java/libcore/icu/Transliterator.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package libcore.icu;
-
-/**
- * Exposes icu4c's Transliterator.
- */
-public final class Transliterator {
-  private long peer;
-
-  /**
-   * Creates a new Transliterator for the given id.
-   */
-  public Transliterator(String id) {
-    peer = create(id);
-  }
-
-  @Override protected synchronized void finalize() throws Throwable {
-    try {
-      destroy(peer);
-      peer = 0;
-    } finally {
-      super.finalize();
-    }
-  }
-
-  /**
-   * Returns the ids of all known transliterators.
-   */
-  public static native String[] getAvailableIDs();
-
-  /**
-   * Transliterates the specified string.
-   */
-  public String transliterate(String s) {
-    return transliterate(peer, s);
-  }
-
-  private static native long create(String id);
-  private static native void destroy(long peer);
-  private static native String transliterate(long peer, String s);
-}
diff --git a/luni/src/main/native/Register.cpp b/luni/src/main/native/Register.cpp
index d0230ee..0e30da7 100644
--- a/luni/src/main/native/Register.cpp
+++ b/luni/src/main/native/Register.cpp
@@ -62,7 +62,6 @@
     REGISTER(register_libcore_icu_NativeDecimalFormat);
     REGISTER(register_libcore_icu_NativeIDN);
     REGISTER(register_libcore_icu_TimeZoneNames);
-    REGISTER(register_libcore_icu_Transliterator);
     REGISTER(register_libcore_io_AsynchronousCloseMonitor);
     REGISTER(register_libcore_io_Memory);
     REGISTER(register_libcore_io_Posix);
diff --git a/luni/src/main/native/libcore_icu_Transliterator.cpp b/luni/src/main/native/libcore_icu_Transliterator.cpp
deleted file mode 100644
index ae21565..0000000
--- a/luni/src/main/native/libcore_icu_Transliterator.cpp
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_TAG "Transliterator"
-
-#include "IcuUtilities.h"
-#include "JNIHelp.h"
-#include "JniConstants.h"
-#include "JniException.h"
-#include "ScopedJavaUnicodeString.h"
-#include "unicode/translit.h"
-
-static icu::Transliterator* fromPeer(jlong peer) {
-  return reinterpret_cast<icu::Transliterator*>(static_cast<uintptr_t>(peer));
-}
-
-static jlong Transliterator_create(JNIEnv* env, jclass, jstring javaId) {
-  ScopedJavaUnicodeString id(env, javaId);
-  if (!id.valid()) {
-    return 0;
-  }
-  UErrorCode status = U_ZERO_ERROR;
-  icu::Transliterator* t = icu::Transliterator::createInstance(id.unicodeString(), UTRANS_FORWARD, status);
-  if (maybeThrowIcuException(env, "Transliterator::createInstance", status)) {
-    return 0;
-  }
-  return reinterpret_cast<uintptr_t>(t);
-}
-
-static void Transliterator_destroy(JNIEnv*, jclass, jlong peer) {
-  delete fromPeer(peer);
-}
-
-static jobjectArray Transliterator_getAvailableIDs(JNIEnv* env, jclass) {
-  UErrorCode status = U_ZERO_ERROR;
-  icu::StringEnumeration* e = icu::Transliterator::getAvailableIDs(status);
-  return fromStringEnumeration(env, status, "Transliterator::getAvailableIDs", e);
-}
-
-static jstring Transliterator_transliterate(JNIEnv* env, jclass, jlong peer, jstring javaString) {
-  icu::Transliterator* t = fromPeer(peer);
-  ScopedJavaUnicodeString string(env, javaString);
-  if (!string.valid()) {
-    return NULL;
-  }
-
-  icu::UnicodeString& s(string.unicodeString());
-  t->transliterate(s);
-  return env->NewString(s.getBuffer(), s.length());
-}
-
-static JNINativeMethod gMethods[] = {
-  NATIVE_METHOD(Transliterator, create, "(Ljava/lang/String;)J"),
-  NATIVE_METHOD(Transliterator, destroy, "(J)V"),
-  NATIVE_METHOD(Transliterator, getAvailableIDs, "()[Ljava/lang/String;"),
-  NATIVE_METHOD(Transliterator, transliterate, "(JLjava/lang/String;)Ljava/lang/String;"),
-};
-void register_libcore_icu_Transliterator(JNIEnv* env) {
-  jniRegisterNativeMethods(env, "libcore/icu/Transliterator", gMethods, NELEM(gMethods));
-}
diff --git a/luni/src/main/native/sub.mk b/luni/src/main/native/sub.mk
index 1906185..ec39596 100644
--- a/luni/src/main/native/sub.mk
+++ b/luni/src/main/native/sub.mk
@@ -42,7 +42,6 @@
     libcore_icu_NativeDecimalFormat.cpp \
     libcore_icu_NativeIDN.cpp \
     libcore_icu_TimeZoneNames.cpp \
-    libcore_icu_Transliterator.cpp \
     libcore_io_AsynchronousCloseMonitor.cpp \
     libcore_io_Memory.cpp \
     libcore_io_Posix.cpp \
diff --git a/luni/src/test/java/libcore/icu/TransliteratorTest.java b/luni/src/test/java/libcore/icu/TransliteratorTest.java
index 9ced2de..626d951 100644
--- a/luni/src/test/java/libcore/icu/TransliteratorTest.java
+++ b/luni/src/test/java/libcore/icu/TransliteratorTest.java
@@ -16,18 +16,24 @@
 
 package libcore.icu;
 
+import android.icu.text.Transliterator;
+
+import java.util.Enumeration;
+
 public class TransliteratorTest extends junit.framework.TestCase {
   public void testAll() throws Exception {
-    for (String id : Transliterator.getAvailableIDs()) {
+    Enumeration<String> ids = Transliterator.getAvailableIDs();
+    while (ids.hasMoreElements()) {
+      String id = ids.nextElement();
       System.err.println(id);
-      Transliterator t = new Transliterator(id);
+      Transliterator t = Transliterator.getInstance(id);
       t.transliterate("hello");
     }
   }
 
   public void test_Unknown() throws Exception {
     try {
-      Transliterator t = new Transliterator("Unknown");
+      Transliterator t = Transliterator.getInstance("Unknown");
       fail();
     } catch (RuntimeException expected) {
     }
@@ -35,29 +41,20 @@
 
   public void test_null_id() throws Exception {
     try {
-      Transliterator t = new Transliterator(null);
-      fail();
-    } catch (NullPointerException expected) {
-    }
-  }
-
-  public void test_null_string() throws Exception {
-    try {
-      Transliterator t = new Transliterator("Any-Upper");
-      t.transliterate(null);
+      Transliterator t = Transliterator.getInstance(null);
       fail();
     } catch (NullPointerException expected) {
     }
   }
 
   public void test_Any_Upper() throws Exception {
-    Transliterator t = new Transliterator("Any-Upper");
+    Transliterator t = Transliterator.getInstance("Any-Upper");
     assertEquals("HELLO WORLD!", t.transliterate("HeLlO WoRlD!"));
     assertEquals("STRASSE", t.transliterate("Straße"));
   }
 
   public void test_Any_Lower() throws Exception {
-    Transliterator t = new Transliterator("Any-Lower");
+    Transliterator t = Transliterator.getInstance("Any-Lower");
     assertEquals("hello world!", t.transliterate("HeLlO WoRlD!"));
   }
 
@@ -65,34 +62,34 @@
     String greek = "Καλημέρα κόσμε!";
 
     // Transliterate Greek to Latin, then to plain ASCII.
-    Transliterator t = new Transliterator("Greek-Latin");
+    Transliterator t = Transliterator.getInstance("Greek-Latin");
     String latin = t.transliterate(greek);
-    t = new Transliterator("Latin-Ascii");
+    t = Transliterator.getInstance("Latin-Ascii");
     String ascii = t.transliterate(latin);
     assertEquals("Kalēméra kósme!", latin);
     assertEquals("Kalemera kosme!", ascii);
 
     // Use alternative transliteration variants.
-    t = new Transliterator("Greek-Latin/BGN");
+    t = Transliterator.getInstance("Greek-Latin/BGN");
     assertEquals("Kaliméra kósme!", t.transliterate(greek));
-    t = new Transliterator("Greek-Latin/UNGEGN");
+    t = Transliterator.getInstance("Greek-Latin/UNGEGN");
     assertEquals("Kali̱méra kósme!",t.transliterate(greek));
   }
 
   public void test_Han_Latin() throws Exception {
-    Transliterator t = new Transliterator("Han-Latin");
+    Transliterator t = Transliterator.getInstance("Han-Latin");
     assertEquals("hàn zì/hàn zì", t.transliterate("汉字/漢字"));
 
     assertEquals("chén", t.transliterate("\u6c88"));
     assertEquals("shěn", t.transliterate("\u700b"));
     assertEquals("jiǎ", t.transliterate("\u8d3e"));
 
-    t = new Transliterator("Han-Latin/Names");
+    t = Transliterator.getInstance("Han-Latin/Names");
     assertEquals("shěn", t.transliterate("\u6c88"));
     assertEquals("shěn", t.transliterate("\u700b"));
     assertEquals("jiǎ", t.transliterate("\u8d3e"));
 
-    t = new Transliterator("Han-Latin/Names; Latin-Ascii; Any-Upper");
+    t = Transliterator.getInstance("Han-Latin/Names; Latin-Ascii; Any-Upper");
     assertEquals("SHEN", t.transliterate("\u6c88"));
     assertEquals("SHEN", t.transliterate("\u700b"));
     assertEquals("JIA", t.transliterate("\u8d3e"));