summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/ValueWriter.java
blob: 61c0f6da13d0191372347f68369f3562aa93ac7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// **********************************************************************
//
// Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package IceInternal;

public final class ValueWriter
{
    public static void
    write(java.lang.Object obj, IceUtilInternal.OutputBase out)
    {
        writeValue(null, obj, null, out);
    }

    private static void
    writeValue(String name, java.lang.Object value, java.util.Map<java.lang.Object, java.lang.Object> objectTable,
               IceUtilInternal.OutputBase out)
    {
        if(value == null)
        {
            writeName(name, out);
            out.print("(null)");
        }
        else
        {
            Class<?> c = value.getClass();
            if(c.equals(Byte.class) || c.equals(Short.class) || c.equals(Integer.class) || c.equals(Long.class) ||
               c.equals(Double.class) || c.equals(Float.class) || c.equals(Boolean.class))
            {
                writeName(name, out);
                out.print(value.toString());
            }
            else if(c.equals(String.class))
            {
                //
                // Indent the lines of a string value.
                //
                writeName(name, out);
                out.print("\"");
                out.useCurrentPosAsIndent();
                String str = value.toString();
                int start = 0, pos;
                while(start < str.length() && (pos = str.indexOf('\n', start)) != -1)
                {
                    out.print(str.substring(start, pos));
                    out.nl();
                    start = pos + 1;
                }
                if(start < str.length())
                {
                    out.print(str.substring(start));
                }
                out.print("\"");
                out.restoreIndent();
            }
            else if(c.isArray())
            {
                int n = java.lang.reflect.Array.getLength(value);
                for(int i = 0; i < n; i++)
                {
                    String elem = (name != null ? name : "");
                    elem += "[" + i + "]";
                    writeValue(elem, java.lang.reflect.Array.get(value, i), objectTable, out);
                }
            }
            else if(value instanceof java.util.Map)
            {
                java.util.Map map = (java.util.Map)value;
                java.util.Iterator i = map.entrySet().iterator();
                while(i.hasNext())
                {
                    java.util.Map.Entry entry = (java.util.Map.Entry)i.next();
                    String elem = (name != null ? name + "." : "");
                    writeValue(elem + "key", entry.getKey(), objectTable, out);
                    writeValue(elem + "value", entry.getValue(), objectTable, out);
                }
            }
            else if(value instanceof Ice.ObjectPrxHelperBase)
            {
                writeName(name, out);
                Ice.ObjectPrxHelperBase proxy = (Ice.ObjectPrxHelperBase)value;
                out.print(proxy.__reference().toString());
            }
            else if(value instanceof Ice.Object)
            {
                //
                // Check for recursion.
                //
                if(objectTable != null && objectTable.containsKey(value))
                {
                    writeName(name, out);
                    out.print("(recursive)");
                }
                else
                {
                    if(objectTable == null)
                    {
                        objectTable = new java.util.IdentityHashMap<java.lang.Object, java.lang.Object>();
                    }
                    objectTable.put(value, null);
                    writeFields(name, value, c, objectTable, out);
                }
            }
            else if(value instanceof java.lang.Enum)
            {
                writeName(name, out);
                out.print(((java.lang.Enum)value).name());
            }
            else
            {
                //
                // Must be struct.
                //
                writeFields(name, value, c, objectTable, out);
            }
        }
    }

    private static void
    writeFields(String name, java.lang.Object obj, Class<?> c,
                java.util.Map<java.lang.Object, java.lang.Object> objectTable, IceUtilInternal.OutputBase out)
    {
        if(!c.equals(java.lang.Object.class))
        {
            //
            // Write the superclass first.
            //
            writeFields(name, obj, c.getSuperclass(), objectTable, out);

            //
            // Write the declared fields of the given class. We prefer to use the declared
            // fields because it includes protected fields that may have been defined using
            // the Slice "protected" metadata. However, if a security manager prevents us
            // from obtaining the declared fields, we will fall back to using the public ones.
            //
            java.lang.reflect.Field[] fields = null;
            try
            {
                fields = c.getDeclaredFields();
            }
            catch(java.lang.SecurityException ex)
            {
                try
                {
                    fields = c.getFields();
                }
                catch(java.lang.SecurityException e)
                {
                    return; // Nothing else we can do.
                }
            }
            assert(fields != null);
            for(java.lang.reflect.Field field : fields)
            {
                //
                // Only write public, non-static fields.
                //
                int mods = field.getModifiers();
                if(java.lang.reflect.Modifier.isPublic(mods) && !java.lang.reflect.Modifier.isStatic(mods))
                {
                    String fieldName = (name != null ? name + '.' + field.getName() : field.getName());

                    try
                    {
                        java.lang.Object value = field.get(obj);
                        writeValue(fieldName, value, objectTable, out);
                    }
                    catch(IllegalAccessException ex)
                    {
                        assert(false);
                    }
                }
            }
        }
    }

    private static void
    writeName(String name, IceUtilInternal.OutputBase out)
    {
        if(name != null)
        {
            out.nl();
            out.print(name + " = ");
        }
    }
}