package com.wizzardo.tools.reflection;

import java.lang.reflect.Field;

/**
 * @author: wizzardo
 * Date: 8/8/14
 */
public class StringReflection {
    private static FieldReflection value;
    private static FieldReflection hash;
    private static FieldReflection count;
    private static FieldReflection offset;

    static {
        try {
            Field value = String.class.getDeclaredField("value");
            value.setAccessible(true);
            StringReflection.value = new FieldReflection(value);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        try {
            Field hash = String.class.getDeclaredField("hash");
            hash.setAccessible(true);
            StringReflection.hash = new FieldReflection(hash);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        try {
            Field offset = String.class.getDeclaredField("offset");
            offset.setAccessible(true);
            StringReflection.offset = new FieldReflection(offset);
        } catch (NoSuchFieldException ignored) {
        }
        try {
            Field count = String.class.getDeclaredField("count");
            count.setAccessible(true);
            StringReflection.count = new FieldReflection(count);
        } catch (NoSuchFieldException ignored) {
        }
    }

    public static char[] chars(String s) {
        return (char[]) value.getObject(s);
    }

    public static int offset(String s) {
        if (offset != null)
            return offset.getInteger(s);
        return 0;
    }

    public static String createString(char[] chars) {
        String s = new String();

        value.setObject(s, chars);
        if (count != null)
            count.setInteger(s, chars.length);
        return s;
    }

    public static String createString(char[] chars, int hash) {
        String s = new String();
        value.setObject(s, chars);

        if (hash != 0)
            StringReflection.hash.setInteger(s, hash);

        if (count != null)
            count.setInteger(s, chars.length);
        return s;
    }
}
@@ -337,77 +337,62 @@
     
-try %7B%0A            Field value = String.class.getDeclaredField(
+value = getFieldReflection(String.class, 
 %22value%22
+, true
 );%0A 
@@ -402,100 +402,130 @@
     
-    value.setAccessible(true);%0A            StringReflection.value = new FieldReflection(valu
+offset = getFieldReflection(String.class, %22offset%22, false);%0A        count = getFieldReflection(String.class, %22count%22, fals
 e);%0A
@@ -536,99 +536,89 @@
     
-%7D catch (NoSuchFieldException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        try %7B
+hash = getFieldReflection(String.class, %22hash%22, false);%0A        if (hash == null)
 %0A   
@@ -630,91 +630,60 @@
     
-Field 
 hash = 
-String.class.getDeclaredField(%22hash%22);%0A            hash.setAccessible(
+getFieldReflection(String.class, %22hashCode%22, 
 true
@@ -693,124 +693,104 @@
     
- 
+%7D%0A%0A
     
-   StringReflection.hash = new FieldReflection(hash);%0A        %7D catch (NoSuchFieldException e) %7B%0A            e.
+private static FieldReflection getFieldReflection(Class clazz, String fieldName, boolean 
 prin
@@ -804,21 +804,11 @@
 race
-();%0A        %7D
+) %7B
 %0A   
@@ -840,29 +840,21 @@
 eld 
-offset = String.class
+count = clazz
 .get
@@ -867,24 +867,25 @@
 edField(
-%22offset%22
+fieldName
 );%0A     
@@ -891,21 +891,20 @@
         
-offse
+coun
 t.setAcc
@@ -934,33 +934,14 @@
     
-StringReflection.offset =
+return
  new
@@ -957,21 +957,20 @@
 lection(
-offse
+coun
 t);%0A    
@@ -995,39 +995,33 @@
 hFieldException 
-ignored
+e
 ) %7B%0A        %7D%0A  
@@ -1020,252 +1020,100 @@
     
-%7D%0A
     
-    try %7B%0A            Field count = String.class.getDeclaredField(%22count%22);%0A            count.setAccessible(true);%0A            StringReflection.count = new FieldReflection(count);%0A        %7D catch (NoSuchFieldException ignored) %7B%0A        %7D
+if (printStackTrace)%0A                e.printStackTrace();%0A        %7D%0A        return null;
 %0A   

fix hash for android