Move some default-methods tests to Java from Smali.

Move all smali tests for default method behavior in
non-source-incompatible contexts to java. Also move some of the
simpler tests for source and binary incompatibilities into java as
well when possible.

Bug: 27310767

Change-Id: I753196f19849494825953c1bf06f15b7132f459b
diff --git a/test/utils/python/generate_smali_main.py b/test/utils/python/generate_java_main.py
similarity index 67%
rename from test/utils/python/generate_smali_main.py
rename to test/utils/python/generate_java_main.py
index d796d31..f66d0dd 100755
--- a/test/utils/python/generate_smali_main.py
+++ b/test/utils/python/generate_java_main.py
@@ -15,7 +15,7 @@
 # limitations under the License.
 
 """
-Generate Smali Main file from a classes.xml file.
+Generate Java Main file from a classes.xml file.
 """
 
 import os
@@ -38,48 +38,27 @@
 import functools
 import xml.etree.ElementTree as ET
 
-class MainClass(mixins.DumpMixin, mixins.Named, mixins.SmaliFileMixin):
+class MainClass(mixins.DumpMixin, mixins.Named, mixins.JavaFileMixin):
   """
   A mainclass and main method for this test.
   """
 
   MAIN_CLASS_TEMPLATE = """{copyright}
-.class public LMain;
-.super Ljava/lang/Object;
-
-# class Main {{
-
-.method public constructor <init>()V
-    .registers 1
-    invoke-direct {{p0}}, Ljava/lang/Object;-><init>()V
-    return-void
-.end method
-
+class Main {{
 {test_groups}
-
 {test_funcs}
-
 {main_func}
-
-# }}
+}}
 """
 
   MAIN_FUNCTION_TEMPLATE = """
-#   public static void main(String[] args) {{
-.method public static main([Ljava/lang/String;)V
-    .locals 2
-    sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
-
+  public static void main(String[] args) {{
     {test_group_invoke}
-
-    return-void
-.end method
-#   }}
+  }}
 """
 
   TEST_GROUP_INVOKE_TEMPLATE = """
-#     {test_name}();
-    invoke-static {{}}, {test_name}()V
+    {test_name}();
 """
 
   def __init__(self):
@@ -123,7 +102,7 @@
     funcs = ""
     for f in self.global_funcs:
       funcs += str(f)
-    return self.MAIN_CLASS_TEMPLATE.format(copyright = get_copyright('smali'),
+    return self.MAIN_CLASS_TEMPLATE.format(copyright = get_copyright('java'),
                                            test_groups=test_groups,
                                            main_func=main_func, test_funcs=funcs)
 
@@ -135,33 +114,19 @@
   """
 
   INSTANCE_TEST_TEMPLATE = """
-#   public static void {test_name}() {{
-#     System.out.println("Testing for type {ty}");
-#     String s = "{ty}";
-#     {ty} v = new {ty}();
-.method public static {test_name}()V
-    .locals 3
-    sget-object v2, Ljava/lang/System;->out:Ljava/io/PrintStream;
-    const-string v0, "Testing for type {ty}"
-    invoke-virtual {{v2,v0}}, Ljava/io/PrintStream;->println(Ljava/lang/Object;)V
-
-    const-string v0, "{ty}"
-    new-instance v1, L{ty};
-    invoke-direct {{v1}}, L{ty};-><init>()V
+  public static void {test_name}() {{
+    System.out.println("Testing for type {ty}");
+    String s = "{ty}";
+    {ty} v = new {ty}();
 
     {invokes}
 
-    const-string v0, "End testing for type {ty}"
-    invoke-virtual {{v2,v0}}, Ljava/io/PrintStream;->println(Ljava/lang/Object;)V
-    return-void
-.end method
-#     System.out.println("End testing for type {ty}");
-#   }}
+    System.out.println("End testing for type {ty}");
+  }}
 """
 
   TEST_INVOKE_TEMPLATE = """
-#     {fname}(s, v);
-    invoke-static {{v0, v1}}, {fname}(Ljava/lang/String;L{farg};)V
+    {fname}(s, v);
 """
 
   def __init__(self, main, ty):
@@ -188,7 +153,7 @@
 
   def __str__(self):
     """
-    Returns the smali code for this function
+    Returns the java code for this function
     """
     func_invokes = ""
     for f in sorted(self.funcs, key=lambda a: (a.func, a.farg)):
@@ -204,47 +169,15 @@
   """
 
   TEST_FUNCTION_TEMPLATE = """
-#   public static void {fname}(String s, {farg} v) {{
-#     try {{
-#       System.out.printf("%s-{invoke_type:<9} {farg:>9}.{callfunc}()='%s'\\n", s, v.{callfunc}());
-#       return;
-#     }} catch (Error e) {{
-#       System.out.printf("%s-{invoke_type} on {farg}: {callfunc}() threw exception!\\n", s);
-#       e.printStackTrace(System.out);
-#     }}
-#   }}
-.method public static {fname}(Ljava/lang/String;L{farg};)V
-    .locals 7
-    :call_{fname}_try_start
-      const/4 v0, 2
-      new-array v1,v0, [Ljava/lang/Object;
-      const/4 v0, 0
-      aput-object p0,v1,v0
-
-      sget-object v2, Ljava/lang/System;->out:Ljava/io/PrintStream;
-      const-string v3, "%s-{invoke_type:<9} {farg:>9}.{callfunc}()='%s'\\n"
-
-      invoke-{invoke_type} {{p1}}, L{farg};->{callfunc}()Ljava/lang/String;
-      move-result-object v4
-      const/4 v0, 1
-      aput-object v4, v1, v0
-
-      invoke-virtual {{v2,v3,v1}}, Ljava/io/PrintStream;->printf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;
-      return-void
-    :call_{fname}_try_end
-    .catch Ljava/lang/Error; {{:call_{fname}_try_start .. :call_{fname}_try_end}} :error_{fname}_start
-    :error_{fname}_start
-      move-exception v3
-      const/4 v0, 1
-      new-array v1,v0, [Ljava/lang/Object;
-      const/4 v0, 0
-      aput-object p0, v1, v0
-      sget-object v2, Ljava/lang/System;->out:Ljava/io/PrintStream;
-      const-string v4, "%s-{invoke_type} on {farg}: {callfunc}() threw exception!\\n"
-      invoke-virtual {{v2,v4,v1}}, Ljava/io/PrintStream;->printf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;
-      invoke-virtual {{v3,v2}}, Ljava/lang/Error;->printStackTrace(Ljava/io/PrintStream;)V
-      return-void
-.end method
+  public static void {fname}(String s, {farg} v) {{
+    try {{
+      System.out.printf("%s-{invoke_type:<9} {farg:>9}.{callfunc}()='%s'\\n", s, v.{callfunc}());
+      return;
+    }} catch (Error e) {{
+      System.out.printf("%s-{invoke_type} on {farg}: {callfunc}() threw exception!\\n", s);
+      e.printStackTrace(System.out);
+    }}
+  }}
 """
 
   def __init__(self, func, farg, invoke):
@@ -263,7 +196,7 @@
 
   def __str__(self):
     """
-    Get the smali code for this test function
+    Get the java code for this test function
     """
     return self.TEST_FUNCTION_TEMPLATE.format(fname=self.get_name(),
                                               farg=self.farg,
@@ -307,7 +240,7 @@
 
 def make_main_class(dat):
   """
-  Creates a Main.smali file that runs all the tests
+  Creates a Main.java file that runs all the tests
   """
   m = MainClass()
   for c in dat.classes.values():
@@ -365,12 +298,12 @@
   return TestData(classes, ifaces)
 
 def main(argv):
-  smali_dir = Path(argv[1])
-  if not smali_dir.exists() or not smali_dir.is_dir():
-    print("{} is not a valid smali dir".format(smali_dir), file=sys.stderr)
+  java_dir = Path(argv[1])
+  if not java_dir.exists() or not java_dir.is_dir():
+    print("{} is not a valid java dir".format(java_dir), file=sys.stderr)
     sys.exit(1)
-  class_data = parse_xml((smali_dir / "classes.xml").open().read())
-  make_main_class(class_data).dump(smali_dir)
+  class_data = parse_xml((java_dir / "classes.xml").open().read())
+  make_main_class(class_data).dump(java_dir)
 
 if __name__ == '__main__':
   main(sys.argv)