Merge "Expand errors that can be returned from RPCs" into studio-master-dev
diff --git a/tools/codergen/template/embed.go b/tools/codergen/template/embed.go
index fa60938..35fc16c 100644
--- a/tools/codergen/template/embed.go
+++ b/tools/codergen/template/embed.go
@@ -1268,18 +1268,18 @@

   import org.jetbrains.annotations.NotNull;¶

-  import com.android.tools.rpclib.binary.BinaryClass;¶
-  import com.android.tools.rpclib.binary.BinaryID;¶
-  import com.android.tools.rpclib.binary.BinaryObject;¶
-  import com.android.tools.rpclib.binary.Decoder;¶
-  import com.android.tools.rpclib.binary.Encoder;¶
-  import com.android.tools.rpclib.binary.Namespace;¶
+  import com.android.tools.rpclib.binary.*;¶
+  import com.android.tools.rpclib.schema.*;¶
   {{range .Struct.Fields}}{{Call "Java.Import" .Type}}{{end}}

   import java.io.IOException;¶

   {{if .Struct.Exported}}public•{{end}}
-  final class {{File.ClassName .Struct}} implements BinaryObject {Ȧ
+  final class {{File.ClassName .Struct}}
+  {{range .Struct.Implements}}
+    {{if and (eq .Package "rpc") (eq .Name "Err")}}•extends RpcException{{end}}
+  {{end}}
+  •implements BinaryObject {»¶
     //{{Section "Java.ClassBody"}}¶
     public enum Klass implements BinaryClass {Ȧ
       //{{Section "Java.KlassBody"}}¶
@@ -1287,7 +1287,6 @@
   «}¶
 {{end}}
 
-
 {{define "Java.FactoryBody"}}
   ¶{{/*Newline after section marker*/}}
   {{range .Structs}}{{File.ClassName .}}.register();¶{{end}}
@@ -1467,7 +1466,7 @@
     {{range .Service.Methods}}
       private class {{.Name}}Callable implements {{Call "Java.Callable" .Result.Type}} {Ȧ
         private final {{File.ClassName .Call}} myCall;¶
-        private final Exception stack = new StackException();¶
+        private final Exception myStack = new StackException();¶

         private {{.Name}}Callable({{template "Java.Parameters" .}}) {Ȧ
           myCall = new {{File.ClassName .Call}}();¶
@@ -1485,7 +1484,8 @@
               return null;¶
             {{end}}
           «} catch (Exception e) {»¶
-            throw (Exception)stack.initCause(e);¶
+            e.initCause(myStack);¶
+            throw e;¶
           «}¶
         «}¶
       «}¶
diff --git a/tools/codergen/template/java_binary.tmpl b/tools/codergen/template/java_binary.tmpl
index 1e1269e..a70b444 100644
--- a/tools/codergen/template/java_binary.tmpl
+++ b/tools/codergen/template/java_binary.tmpl
@@ -195,18 +195,18 @@

   import org.jetbrains.annotations.NotNull;¶

-  import com.android.tools.rpclib.binary.BinaryClass;¶
-  import com.android.tools.rpclib.binary.BinaryID;¶
-  import com.android.tools.rpclib.binary.BinaryObject;¶
-  import com.android.tools.rpclib.binary.Decoder;¶
-  import com.android.tools.rpclib.binary.Encoder;¶
-  import com.android.tools.rpclib.binary.Namespace;¶
+  import com.android.tools.rpclib.binary.*;¶
+  import com.android.tools.rpclib.schema.*;¶
   {{range .Struct.Fields}}{{Call "Java.Import" .Type}}{{end}}

   import java.io.IOException;¶

   {{if .Struct.Exported}}public•{{end}}
-  final class {{File.ClassName .Struct}} implements BinaryObject {Ȧ
+  final class {{File.ClassName .Struct}}
+  {{range .Struct.Implements}}
+    {{if and (eq .Package "rpc") (eq .Name "Err")}}•extends RpcException{{end}}
+  {{end}}
+  •implements BinaryObject {»¶
     //{{Section "Java.ClassBody"}}¶
     public enum Klass implements BinaryClass {Ȧ
       //{{Section "Java.KlassBody"}}¶
@@ -214,7 +214,6 @@
   «}¶
 {{end}}
 
-
 {{define "Java.FactoryBody"}}
   ¶{{/*Newline after section marker*/}}
   {{range .Structs}}{{File.ClassName .}}.register();¶{{end}}
diff --git a/tools/codergen/template/java_client.tmpl b/tools/codergen/template/java_client.tmpl
index 66275e3..f11159e 100644
--- a/tools/codergen/template/java_client.tmpl
+++ b/tools/codergen/template/java_client.tmpl
@@ -83,7 +83,7 @@
     {{range .Service.Methods}}
       private class {{.Name}}Callable implements {{Call "Java.Callable" .Result.Type}} {Ȧ
         private final {{File.ClassName .Call}} myCall;¶
-        private final Exception stack = new StackException();¶
+        private final Exception myStack = new StackException();¶

         private {{.Name}}Callable({{template "Java.Parameters" .}}) {Ȧ
           myCall = new {{File.ClassName .Call}}();¶
@@ -101,7 +101,8 @@
               return null;¶
             {{end}}
           «} catch (Exception e) {»¶
-            throw (Exception)stack.initCause(e);¶
+            e.initCause(myStack);¶
+            throw e;¶
           «}¶
         «}¶
       «}¶