diff truffle/com.oracle.truffle.api.interop/src/com/oracle/truffle/api/interop/Message.java @ 22135:e70b20f4bb00

Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 07 Sep 2015 17:07:20 +0200
parents f84a7663966d
children 5857f5ee9486
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.interop/src/com/oracle/truffle/api/interop/Message.java	Fri Sep 04 16:41:38 2015 +0200
+++ b/truffle/com.oracle.truffle.api.interop/src/com/oracle/truffle/api/interop/Message.java	Mon Sep 07 17:07:20 2015 +0200
@@ -178,4 +178,68 @@
         return new ForeignObjectAccessHeadNode(this);
     }
 
+    /**
+     * Converts the message into canonical string representation. The converted string can be
+     * stored, persisted, transfered and later passed to {@link #valueOf(java.lang.String)} to
+     * construct the message again.
+     * 
+     * @param message the message to convert
+     * @return canonical string representation
+     */
+    public static String toString(Message message) {
+        if (Message.READ == message) {
+            return "READ"; // NOI18N
+        }
+        if (Message.WRITE == message) {
+            return "WRITE"; // NOI18N
+        }
+        if (Message.UNBOX == message) {
+            return "UNBOX"; // NOI18N
+        }
+        if (Message.GET_SIZE == message) {
+            return "GET_SIZE"; // NOI18N
+        }
+        if (Message.HAS_SIZE == message) {
+            return "HAS_SIZE"; // NOI18N
+        }
+        if (Message.IS_NULL == message) {
+            return "IS_NULL"; // NOI18N
+        }
+        if (Message.IS_BOXED == message) {
+            return "IS_BOXED"; // NOI18N
+        }
+        if (Message.IS_EXECUTABLE == message) {
+            return "IS_EXECUTABLE"; // NOI18N
+        }
+        if (message instanceof Execute) {
+            return ((Execute) message).name();
+        }
+        return message.getClass().getName();
+    }
+
+    /**
+     * Converts string representation into real message. If the string was obtained by
+     * {@link #toString(com.oracle.truffle.api.interop.Message)} method, it is guaranteed to be
+     * successfully recognized (if the classpath of the system remains the same).
+     * 
+     * @param message canonical string representation of a message
+     * @return the message
+     * @throws IllegalArgumentException if the string does not represent known message
+     */
+    public static Message valueOf(String message) {
+        try {
+            return (Message) Message.class.getField(message).get(null);
+        } catch (Exception ex) {
+            try {
+                String factory = "create" + message.charAt(0) + message.substring(1).toLowerCase();
+                return (Message) Message.class.getMethod(factory, int.class).invoke(null, 0);
+            } catch (Exception ex2) {
+                try {
+                    return (Message) Class.forName(message).newInstance();
+                } catch (Exception ex1) {
+                    throw new IllegalArgumentException("Cannot find message for " + message, ex);
+                }
+            }
+        }
+    }
 }