changeset 14044:c82dff06b0d6

Arrays implement the Arrayish interface
author dsimms
date Wed, 27 Apr 2016 14:49:23 +0200
parents 64db0365ed01
children 836efde938c1 ca246d44ad08
files src/java.base/share/classes/java/lang/Arrayish.java src/java.base/share/classes/valhalla/model3/Model3Converter.java
diffstat 2 files changed, 35 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/Arrayish.java	Thu Apr 21 17:57:46 2016 +0100
+++ b/src/java.base/share/classes/java/lang/Arrayish.java	Wed Apr 27 14:49:23 2016 +0200
@@ -30,8 +30,23 @@
  *
  * @author Brian Goetz
  */
-public interface Arrayish<any T> {
-    int arraySize();
-    T arrayGet(int index);
-    void arraySet(int index, T element);
+public interface Arrayish<any T> extends Cloneable, java.io.Serializable {
+
+    static final long serialVersionUID = 292L + 4711L;
+
+    default int arraySize() {
+        @SuppressWarnings("unchecked")
+        T[] array = (T[]) (Object) this;
+        return array.length;
+    }
+    default T arrayGet(int index) {
+        @SuppressWarnings("unchecked")
+        T[] array = (T[]) (Object) this;
+        return array[index];
+    }
+    default void arraySet(int index, T element) {
+        @SuppressWarnings("unchecked")
+        T[] array = (T[]) (Object) this;
+        array[index] = element;
+    }
 }
--- a/src/java.base/share/classes/valhalla/model3/Model3Converter.java	Thu Apr 21 17:57:46 2016 +0100
+++ b/src/java.base/share/classes/valhalla/model3/Model3Converter.java	Wed Apr 27 14:49:23 2016 +0200
@@ -29,6 +29,7 @@
 import java.io.IOException;
 import java.nio.file.FileSystems;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.List;
@@ -657,18 +658,30 @@
 
 
     public static void main(String[] args) throws Exception {
+        String types = args.length > 1 ? args[1] : "IZ";
+        String outputPath = args.length > 2 ? args[2] : "out";
+
         byte[] bytes = Files.readAllBytes(FileSystems.getDefault().getPath(args[0]));
         Model3Converter converter = new Model3Converter();
         TemplateClass template = converter.register(bytes);
         byte[] erasedBytes = template.getErasedBytes();
-        Files.write(FileSystems.getDefault().getPath("out", template.className + ".class"), erasedBytes);
+        Path outputFile = FileSystems.getDefault().getPath(outputPath,
+            template.className.replace('.', '/') + ".class");
+        if (Files.notExists(outputFile.getParent())) {
+            try {
+                Files.createDirectories(outputFile.getParent());
+            } catch (Exception e) {
+                e.printStackTrace();
+                System.exit(2);
+            }
+        }
+        Files.write(outputFile, erasedBytes);
 
-        String types = args.length > 1 ? args[1] : "IZ";
         for (int i=0; i<types.length(); i++) {
             String type = types.substring(i, i+1);
             ParameterizedType pt = new ParameterizedType(template.className, type);
             byte[] intBytes = template.getBytes(pt);
-            Files.write(FileSystems.getDefault().getPath("out", pt.encode() + ".class"), intBytes);
+            Files.write(FileSystems.getDefault().getPath(outputPath, pt.encode() + ".class"), intBytes);
         }
     }
 }