Introduce Gerrit gwtui common project

The new project hosts code that is reused between Gerrit core and GWT
based plugins.

Change-Id: I1a6e4352f681266c2ce664881a64d35bd2639299
diff --git a/gerrit-gwtui-common/BUCK b/gerrit-gwtui-common/BUCK
new file mode 100644
index 0000000..76f2db8
--- /dev/null
+++ b/gerrit-gwtui-common/BUCK
@@ -0,0 +1,30 @@
+SRC = 'src/main/java/com/google/gerrit/'
+
+gwt_module(
+  name = 'client',
+  srcs = glob([SRC + 'client/**/*.java']),
+  gwtxml = SRC + 'GerritGwtUICommon.gwt.xml',
+  compile_deps = ['//lib/gwt:user'],
+  visibility = ['PUBLIC'],
+)
+
+java_library(
+  name = 'client-lib',
+  exported_deps = [':client-lib2'],
+  visibility = ['PUBLIC'],
+)
+
+java_library2(
+  name = 'client-lib2',
+  srcs = glob(['src/main/**/*.java']),
+  resources = glob(['src/main/**/*']),
+  compile_deps = ['//lib/gwt:user'],
+  visibility = ['PUBLIC'],
+)
+
+java_library(
+  name = 'client-src-lib',
+  srcs = [],
+  resources = glob(['src/main/**/*']),
+  visibility = ['PUBLIC'],
+)
diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/GerritGwtUICommon.gwt.xml b/gerrit-gwtui-common/src/main/java/com/google/gerrit/GerritGwtUICommon.gwt.xml
new file mode 100644
index 0000000..eb551c4
--- /dev/null
+++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/GerritGwtUICommon.gwt.xml
@@ -0,0 +1,18 @@
+<!--
+ Copyright (C) 2014 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.
+-->
+<module>
+  <source path='client' />
+</module>
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/rpc/NativeMap.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/NativeMap.java
similarity index 100%
rename from gerrit-gwtui/src/main/java/com/google/gerrit/client/rpc/NativeMap.java
rename to gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/NativeMap.java
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/rpc/NativeString.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/NativeString.java
similarity index 86%
rename from gerrit-gwtui/src/main/java/com/google/gerrit/client/rpc/NativeString.java
rename to gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/NativeString.java
index 8cc5735..0e16dc0 100644
--- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/rpc/NativeString.java
+++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/NativeString.java
@@ -21,7 +21,14 @@
 public final class NativeString extends JavaScriptObject {
   public static final JavaScriptObject TYPE = init();
 
-  private static final native JavaScriptObject init() /*-{ return function(s){this.s=s}; }-*/;
+  // Used from core and plugins
+  private static final native JavaScriptObject init() /*-{
+    if ($wnd.Gerrit === undefined || $wnd.Gerrit.JsonString === undefined) {
+      return function(s){this.s=s};
+    } else {
+      return $wnd.Gerrit.JsonString;
+    }
+  }-*/;
 
   static final NativeString wrap(String s) {
     return wrap0(TYPE, s);
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/rpc/Natives.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/Natives.java
similarity index 100%
rename from gerrit-gwtui/src/main/java/com/google/gerrit/client/rpc/Natives.java
rename to gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/Natives.java
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/rpc/TransformCallback.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/TransformCallback.java
similarity index 100%
rename from gerrit-gwtui/src/main/java/com/google/gerrit/client/rpc/TransformCallback.java
rename to gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/TransformCallback.java
diff --git a/gerrit-gwtui/BUCK b/gerrit-gwtui/BUCK
index b99bcfa..2e6510e 100644
--- a/gerrit-gwtui/BUCK
+++ b/gerrit-gwtui/BUCK
@@ -61,6 +61,7 @@
     '//gerrit-gwtexpui:Progress',
     '//gerrit-gwtexpui:SafeHtml',
     '//gerrit-gwtexpui:UserAgent',
+    '//gerrit-gwtui-common:client',
     '//gerrit-common:client',
     '//gerrit-extension-api:client',
     '//gerrit-patch-jgit:client',
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/GerritGwtUI.gwt.xml b/gerrit-gwtui/src/main/java/com/google/gerrit/GerritGwtUI.gwt.xml
index 788f7cd..76a5ca8 100644
--- a/gerrit-gwtui/src/main/java/com/google/gerrit/GerritGwtUI.gwt.xml
+++ b/gerrit-gwtui/src/main/java/com/google/gerrit/GerritGwtUI.gwt.xml
@@ -27,6 +27,7 @@
   <inherits name='com.google.gerrit.extensions.Extensions'/>
   <inherits name='com.google.gerrit.prettify.PrettyFormatter'/>
   <inherits name='com.google.gerrit.Common'/>
+  <inherits name='com.google.gerrit.GerritGwtUICommon'/>
   <inherits name='com.google.gerrit.UserAgent'/>
   <inherits name='org.eclipse.jgit.JGit'/>
   <inherits name='net.codemirror.CodeMirror'/>
diff --git a/gerrit-plugin-gwtui/BUCK b/gerrit-plugin-gwtui/BUCK
index b036d5e..8022bd1 100644
--- a/gerrit-plugin-gwtui/BUCK
+++ b/gerrit-plugin-gwtui/BUCK
@@ -1,3 +1,4 @@
+COMMON = ['gerrit-gwtui-common/src/main/java/']
 SRC = 'src/main/java/com/google/gerrit/'
 SRCS = glob([SRC + '**/*.java'])
 
@@ -6,16 +7,41 @@
   '//lib/gwt:dev',
 ]
 
-java_library2(
+java_binary(
   name = 'gwtui-api',
-  srcs = SRCS,
-  resources = glob(['src/main/**/*']),
-  compile_deps = DEPS,
+  deps = [
+    ':gwtui-api-lib',
+    '//gerrit-gwtui-common:client-lib',
+  ],
   visibility = ['PUBLIC'],
 )
 
 java_library(
+  name = 'gwtui-api-lib',
+  exported_deps = [':gwtui-api-lib2'],
+  visibility = ['PUBLIC'],
+)
+
+java_library2(
+  name = 'gwtui-api-lib2',
+  srcs = SRCS,
+  resources = glob(['src/main/**/*']),
+  deps = ['//gerrit-gwtui-common:client-lib2'],
+  compile_deps = DEPS,
+  visibility = ['PUBLIC'],
+)
+
+java_binary(
   name = 'gwtui-api-src',
+  deps = [
+    ':gwtui-api-src-lib',
+    '//gerrit-gwtui-common:client-src-lib',
+  ],
+  visibility = ['PUBLIC'],
+)
+
+java_library(
+  name = 'gwtui-api-src-lib',
   srcs = [],
   resources = glob(['src/main/**/*']),
   visibility = ['PUBLIC'],
@@ -24,9 +50,9 @@
 java_doc(
   name = 'gwtui-api-javadoc',
   title = 'Gerrit Review GWT Extension API Documentation',
-  pkg = 'com.google.gerrit.plugin',
-  paths = ['$SRCDIR/src/main/java'],
-  srcs = SRCS,
-  deps = DEPS,
+  pkg = 'com.google.gerrit',
+  paths = ['$SRCDIR/src/main/java'] + COMMON,
+  srcs = SRCS + glob(COMMON),
+  deps = DEPS + ['//gerrit-gwtui-common:client-src-lib'],
   visibility = ['PUBLIC'],
 )
diff --git a/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/Plugin.gwt.xml b/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/Plugin.gwt.xml
index 81adcac..e0b0833 100644
--- a/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/Plugin.gwt.xml
+++ b/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/Plugin.gwt.xml
@@ -15,6 +15,7 @@
 -->
 <module>
   <inherits name="com.google.gwt.json.JSON"/>
+  <inherits name='com.google.gerrit.GerritGwtUICommon'/>
 
   <define-linker name="gerrit_plugin" class="com.google.gerrit.plugin.linker.GerritPluginLinker"/>
   <add-linker name="gerrit_plugin"/>
diff --git a/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/plugin/client/rpc/NativeString.java b/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/plugin/client/rpc/NativeString.java
deleted file mode 100644
index 13c8b1d..0000000
--- a/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/plugin/client/rpc/NativeString.java
+++ /dev/null
@@ -1,54 +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 com.google.gerrit.plugin.client.rpc;
-
-import com.google.gwt.core.client.JavaScriptObject;
-import com.google.gwt.user.client.rpc.AsyncCallback;
-
-/** Wraps a String that was returned from a JSON API. */
-public class NativeString extends JavaScriptObject {
-  private static final JavaScriptObject TYPE = init();
-
-  private static final native JavaScriptObject init()
-  /*-{ return $wnd.Gerrit.JsonString }-*/;
-
-  public final native String asString()
-  /*-{ return this; }-*/;
-
-  public static final
-  AsyncCallback<NativeString> unwrap(final AsyncCallback<String> cb) {
-    return new AsyncCallback<NativeString>() {
-      @Override
-      public void onSuccess(NativeString result) {
-        cb.onSuccess(result != null ? result.asString() : null);
-      }
-
-      @Override
-      public void onFailure(Throwable caught) {
-        cb.onFailure(caught);
-      }
-    };
-  }
-
-  public static final boolean is(JavaScriptObject o) {
-    return is(TYPE, o);
-  }
-
-  private static final native boolean is(JavaScriptObject T, JavaScriptObject o)
-  /*-{ return o instanceof T }-*/;
-
-  protected NativeString() {
-  }
-}
diff --git a/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/plugin/client/rpc/Natives.java b/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/plugin/client/rpc/Natives.java
deleted file mode 100644
index 6f5b136..0000000
--- a/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/plugin/client/rpc/Natives.java
+++ /dev/null
@@ -1,71 +0,0 @@
-// Copyright (C) 2012 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 com.google.gerrit.plugin.client.rpc;
-
-import com.google.gwt.core.client.JavaScriptObject;
-import com.google.gwt.core.client.JsArray;
-import com.google.gwt.json.client.JSONObject;
-
-import java.util.AbstractList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Set;
-
-public class Natives {
-  /**
-   * Get the names of defined properties on the object. The returned set
-   * iterates in the native iteration order, which may match the source order.
-   */
-  public static Set<String> keys(JavaScriptObject obj) {
-    if (obj != null) {
-      return new JSONObject(obj).keySet();
-    }
-    return Collections.emptySet();
-  }
-
-  public static <T extends JavaScriptObject> List<T> asList(
-      final JsArray<T> arr) {
-    if (arr == null) {
-      return null;
-    }
-    return new AbstractList<T>() {
-      @Override
-      public T set(int index, T element) {
-        T old = arr.get(index);
-        arr.set(index, element);
-        return old;
-      }
-
-      @Override
-      public T get(int index) {
-        return arr.get(index);
-      }
-
-      @Override
-      public int size() {
-        return arr.length();
-      }
-    };
-  }
-
-  public static <T extends JavaScriptObject> JsArray<T> arrayOf(T element) {
-    JsArray<T> arr = JavaScriptObject.createArray().cast();
-    arr.push(element);
-    return arr;
-  }
-
-  private Natives() {
-  }
-}
diff --git a/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/plugin/client/rpc/RestApi.java b/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/plugin/client/rpc/RestApi.java
index 66f099f..cfbc8a5 100644
--- a/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/plugin/client/rpc/RestApi.java
+++ b/gerrit-plugin-gwtui/src/main/java/com/google/gerrit/plugin/client/rpc/RestApi.java
@@ -14,6 +14,7 @@
 
 package com.google.gerrit.plugin.client.rpc;
 
+import com.google.gerrit.client.rpc.NativeString;
 import com.google.gwt.core.client.JavaScriptObject;
 import com.google.gwt.http.client.URL;
 import com.google.gwt.user.client.rpc.AsyncCallback;