You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by jw...@apache.org on 2015/12/31 23:25:22 UTC

groovy git commit: GROOVY-7716 - groovy.json.internal.FastStringUtils.StringImplementation#toCharArray fails on jdk9 (closes #225)

Repository: groovy
Updated Branches:
  refs/heads/master 38d681ca4 -> a843a40ed


GROOVY-7716 - groovy.json.internal.FastStringUtils.StringImplementation#toCharArray fails on jdk9 (closes #225)

JDK 9 Compat Strings enhancement changed the internal representation of the value field from a char[] to a combination of a byte[] and an encoding flag field (see http://openjdk.java.net/jeps/254).  As a workaround this change allows fallback to the normal String#toCharArray method and bypasses using direct access.


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/a843a40e
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/a843a40e
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/a843a40e

Branch: refs/heads/master
Commit: a843a40ed64ebd09b017880e7a43709124094cb7
Parents: 38d681c
Author: John Wagenleitner <jw...@apache.org>
Authored: Sat Dec 26 13:35:01 2015 -0800
Committer: John Wagenleitner <jw...@apache.org>
Committed: Thu Dec 31 14:20:55 2015 -0800

----------------------------------------------------------------------
 .../java/groovy/json/internal/FastStringUtils.java  | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/a843a40e/subprojects/groovy-json/src/main/java/groovy/json/internal/FastStringUtils.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-json/src/main/java/groovy/json/internal/FastStringUtils.java b/subprojects/groovy-json/src/main/java/groovy/json/internal/FastStringUtils.java
index 417e95a..f43c0ff 100644
--- a/subprojects/groovy-json/src/main/java/groovy/json/internal/FastStringUtils.java
+++ b/subprojects/groovy-json/src/main/java/groovy/json/internal/FastStringUtils.java
@@ -156,10 +156,11 @@ public class FastStringUtils {
         if (STRING_VALUE_FIELD_OFFSET != -1L) {
             if (STRING_OFFSET_FIELD_OFFSET != -1L && STRING_COUNT_FIELD_OFFSET != -1L) {
                 return StringImplementation.OFFSET;
-            } else if (STRING_OFFSET_FIELD_OFFSET == -1L && STRING_COUNT_FIELD_OFFSET == -1L) {
+            } else if (STRING_OFFSET_FIELD_OFFSET == -1L && STRING_COUNT_FIELD_OFFSET == -1L && valueFieldIsCharArray()) {
                 return StringImplementation.DIRECT_CHARS;
             } else {
-                // WTF this is a French abbreviation for unknown.
+                // JDK 9
+                // TODO: GROOVY-7716 workaround - find way to optimize JDK9 String (or rethink need for Unsafe usage)
                 return StringImplementation.UNKNOWN;
             }
         } else {
@@ -168,6 +169,17 @@ public class FastStringUtils {
     }
 
     /**
+     * JDK9 Compat Strings enhancement changed the internal representation of the value field from a char[]
+     * to a byte[] (see http://openjdk.java.net/jeps/254).
+     *
+     * @return true if internal String value field is a char[], otherwise false
+     */
+    private static boolean valueFieldIsCharArray() {
+        Object o = UNSAFE.getObject("", STRING_VALUE_FIELD_OFFSET);
+        return (o instanceof char[]);
+    }
+
+    /**
      * @param string string to grab array from.
      * @return char array from string
      */