Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 84937a3f

Přidáno uživatelem Jan Havlíček před asi 4 roky(ů)

re #7893 comments into code and Javadoc
Added for jdeserialize and its usage in Converter.java
Not sure if this level of refactoring is enough - better check

Zobrazit rozdíly:

project/Deserializer/src/jdeserialize/classdesc.java
1 1
package jdeserialize;
2

  
2 3
import java.io.*;
3 4
import java.util.*;
4 5

  
5 6
/**
6 7
 * <p>
7
 * Represents the entire serialized prototype of the class, including all fields,
8
 * inner classes, class annotations, and inheritance hierarchy.  This includes proxy class
9
 * descriptions.
8
 * Represents the entire serialized prototype of the class, including all
9
 * fields, inner classes, class annotations, and inheritance hierarchy. This
10
 * includes proxy class descriptions.
10 11
 * </p>
11 12
 *
12 13
 * <p>
13
 * Generally, this class is used to represent the type of an instance written to an
14
 * ObjectOutputStream with its writeObject() method, or of a related array or field type.
15
 * However, there's a notable exception: when instances of type java.io.ObjectStreamClass
16
 * are written with writeObject(), only their class description is written (cf. Object
17
 * Serialization Specification, 4.3).  They will be represented with an instance of
18
 * classdesc as well.
14
 * Generally, this class is used to represent the type of an instance written to
15
 * an ObjectOutputStream with its writeObject() method, or of a related array or
16
 * field type. However, there's a notable exception: when instances of type
17
 * java.io.ObjectStreamClass are written with writeObject(), only their class
18
 * description is written (cf. Object Serialization Specification, 4.3). They
19
 * will be represented with an instance of classdesc as well.
19 20
 * </p>
20 21
 */
21 22
public class classdesc extends contentbase {
......
35 36
    public long serialVersionUID;
36 37

  
37 38
    /**
38
     * Description flags byte; this should be a mask of values from the ObjectStreamContants 
39
     * class.  Refer to chapter 6 of the Object Stream Serialization Protocol for details.
39
     * Description flags byte; this should be a mask of values from the
40
     * ObjectStreamContants class. Refer to chapter 6 of the Object Stream
41
     * Serialization Protocol for details.
40 42
     */
41 43
    public byte descflags;
42 44

  
......
49 51
     * List of inner classes, in the order serialized by the stream writer.
50 52
     */
51 53
    public List<classdesc> innerclasses;
52
    
54

  
53 55
    /**
54
     * List of annotation objects; these are *not* Java annotations, but data written by
55
     * the <pre>annotateClass(Class<?>)<pre> and <pre>annotateProxyClass(Class<?>)</pre> methods of an
56
     * ObjectOutputStream.  
56
     * List of annotation objects; these are *not* Java annotations, but data
57
     * written by the
58
     * 
59
     * <pre>
60
     * annotateClass(Class<?>)<pre> and <pre>annotateProxyClass(Class<?>)
61
     * </pre>
62
     * 
63
     * methods of an ObjectOutputStream.
57 64
     */
58 65
    public List<content> annotations;
59 66

  
......
73 80
    public Set<String> enumconstants;
74 81

  
75 82
    private boolean isInnerClass = false;
83

  
76 84
    /**
77
     * True if this class has been determined to be an inner class; this determination is
78
     * generally made by connectMemberClasses().
85
     * True if this class has been determined to be an inner class; this
86
     * determination is generally made by connectMemberClasses().
79 87
     * 
80 88
     * @return true if the class is an inner class
81 89
     */
82 90
    public boolean isInnerClass() {
83 91
        return isInnerClass;
84 92
    }
93

  
85 94
    /**
86 95
     * Sets the value that denotes that the class is an inner class.
87 96
     *
......
92 101
    }
93 102

  
94 103
    private boolean isLocalInnerClass = false;
104

  
95 105
    /**
96 106
     * True if this class has been determined to be a local inner class; this
97 107
     * determination is generally made by connectMemberClasses().
......
101 111
    public boolean isLocalInnerClass() {
102 112
        return isLocalInnerClass;
103 113
    }
114

  
104 115
    /**
105 116
     * Sets the flag that denotes whether this class is a local inner class.
106 117
     * 
......
111 122
    }
112 123

  
113 124
    private boolean isStaticMemberClass = false;
125

  
114 126
    /**
115 127
     * <p>
116 128
     * True if this class has been determined to be a static member class; this
......
118 130
     * </p>
119 131
     *
120 132
     * <p>
121
     * Note that in some cases, static member classes' descriptions will be serialized
122
     * even though their enclosing class is not.  In these cases, this may return false.
123
     * See connectMemberClasses() for details.
133
     * Note that in some cases, static member classes' descriptions will be
134
     * serialized even though their enclosing class is not. In these cases, this may
135
     * return false. See connectMemberClasses() for details.
124 136
     * </p>
125 137
     *
126 138
     * @return true if this is a static member class
......
128 140
    public boolean isStaticMemberClass() {
129 141
        return isStaticMemberClass;
130 142
    }
143

  
131 144
    /**
132 145
     * Sets the flag that denotes whether this class is a static member class.
133 146
     *
......
151 164

  
152 165
    /**
153 166
     * Add an inner class to the description's list.
167
     * 
154 168
     * @param cd inner class to add
155 169
     */
156 170
    public void addInnerClass(classdesc cd) {
......
167 181
    }
168 182

  
169 183
    /**
170
     * Determines whether this is an array type. 
184
     * Determines whether this is an array type.
185
     * 
171 186
     * @return true if this is an array type.
172 187
     */
173 188
    public boolean isArrayClass() {
174
        if(name != null && name.length() > 1 && name.charAt(0) == '[') {
189
        if (name != null && name.length() > 1 && name.charAt(0) == '[') {
175 190
            return true;
176 191
        } else {
177 192
            return false;
178 193
        }
179 194
    }
195

  
180 196
    public String toString() {
181 197
        StringBuffer sb = new StringBuffer();
182 198
        sb.append("[cd ").append(jdeserialize.hex(handle)).append(": name ").append(name);
......
186 202
    }
187 203

  
188 204
    /**
189
     * Generates a list of all class descriptions in this class's hierarchy, in the order
190
     * described by the Object Stream Serialization Protocol.  This is the order in which
191
     * fields are read from the stream.
205
     * Generates a list of all class descriptions in this class's hierarchy, in the
206
     * order described by the Object Stream Serialization Protocol. This is the
207
     * order in which fields are read from the stream.
192 208
     * 
193 209
     * @param classes a list to be filled in with the hierarchy
194 210
     */
195 211
    public void getHierarchy(ArrayList<classdesc> classes) {
196
        if(superclass != null) {
197
            if(superclass.classtype == classdesctype.PROXYCLASS) {
212
        if (superclass != null) {
213
            if (superclass.classtype == classdesctype.PROXYCLASS) {
198 214
                jdeserialize.debugerr("warning: hit a proxy class in superclass hierarchy");
199 215
            } else {
200 216
                superclass.getHierarchy(classes);
201 217
            }
202
        } 
218
        }
203 219
        classes.add(this);
204 220
    }
221

  
205 222
    public void validate() throws ValidityException {
206 223
        // If neither SC_SERIALIZABLE nor SC_EXTERNALIZABLE is set, then the number of
207
        // fields is always zero.  (spec section 4.3)
208
        if((descflags & (ObjectStreamConstants.SC_SERIALIZABLE | ObjectStreamConstants.SC_EXTERNALIZABLE)) == 0 && fields != null && fields.length > 0) {
224
        // fields is always zero. (spec section 4.3)
225
        if ((descflags & (ObjectStreamConstants.SC_SERIALIZABLE | ObjectStreamConstants.SC_EXTERNALIZABLE)) == 0
226
                && fields != null && fields.length > 0) {
209 227
            throw new ValidityException("non-serializable, non-externalizable class has fields!");
210 228
        }
211
        if((descflags & (ObjectStreamConstants.SC_SERIALIZABLE | ObjectStreamConstants.SC_EXTERNALIZABLE)) == (ObjectStreamConstants.SC_SERIALIZABLE | ObjectStreamConstants.SC_EXTERNALIZABLE)) {
229
        if ((descflags & (ObjectStreamConstants.SC_SERIALIZABLE
230
                | ObjectStreamConstants.SC_EXTERNALIZABLE)) == (ObjectStreamConstants.SC_SERIALIZABLE
231
                        | ObjectStreamConstants.SC_EXTERNALIZABLE)) {
212 232
            throw new ValidityException("both Serializable and Externalizable are set!");
213 233
        }
214
        if((descflags & ObjectStreamConstants.SC_ENUM) != 0) {
234
        if ((descflags & ObjectStreamConstants.SC_ENUM) != 0) {
215 235
            // we're an enum; shouldn't have any fields/superinterfaces
216
            if((fields != null && fields.length > 0) || interfaces != null) {
236
            if ((fields != null && fields.length > 0) || interfaces != null) {
217 237
                throw new ValidityException("enums shouldn't implement interfaces or have non-constant fields!");
218 238
            }
219 239
        } else {
220
            // non-enums shouldn't have enum constant fields.  
221
            if(enumconstants != null && enumconstants.size() > 0) {
240
            // non-enums shouldn't have enum constant fields.
241
            if (enumconstants != null && enumconstants.size() > 0) {
222 242
                throw new ValidityException("non-enum classes shouldn't have enum constants!");
223 243
            }
224 244
        }
225 245
    }
226 246

  
227
    public @Override String toJson(String indentation, Map<classdesc, Map<field, Object>> fielddata, boolean child)
228
    {
247
    public @Override String toJson(String indentation, Map<classdesc, Map<field, Object>> fielddata, boolean child) {
229 248
        StringBuffer sb = new StringBuffer();
230
        String val = "";
249
        String fieldJsonValue = "";
231 250

  
232
        if(!child)
233
        {
251
        if (!child) {
234 252
            sb.append(indentation);
235 253
        }
236
        
237
        sb.append("{\n");
238 254

  
255
        sb.append("{\n");
239 256
        indentation += "\t";
240 257

  
241
        // ClassName
242
        sb.append(indentation).append("class ");
243
        sb.append(this.name);
244
        // extends
245
        if (this.superclass != null) {
246
            sb.append(" extends ");
247
            sb.append(this.superclass.name);
248
        }
249
        // implements
250
        if (this.interfaces != null) {
251
            sb.append(" implements ");
252
            for (String str : this.interfaces) {
253
                sb.append(str);
254
                if(this.interfaces[this.interfaces.length-1] != str)
255
                {
256
                    sb.append(", ");
257
                }
258
            }
259
        }
258
        sb.append(indentation).append("class ").append(this.name);
259

  
260
        this.extendsToJson(sb, this.superclass);
261
        this.interfacesToJson(sb, this.interfaces);
262

  
260 263
        sb.append(" : {"); // ending of first line
261
        
262 264
        indentation += "\t";
263 265

  
264
        for (field f : this.fields) {
265
            // v this.fielddata najit element, jehoz key == classdesc
266
            // v tomto prvku fielddata najdu value (element) podle key == f
267
            // jeho value je chtena hodnota
268
            Object locVal = fielddata.get(this).get(f);
269
            
270
            // Sometimes locVal may be a null.
266
        //iterating through fields and converts them into json string
267
        for (field locField : this.fields) {
268
            Object locVal = fielddata.get(this).get(locField);
269

  
270
            // field could be null, but then, nothing to show
271 271
            if (locVal == null)
272
            	continue;
272
                continue;
273 273

  
274
            if(locVal instanceof content)
275
            {
276
                val = ((content)locVal).toJson(indentation, fielddata, true);
277
            }
278
            else {
279
                val = locVal.toString();
274
            if (locVal instanceof content) {
275
                fieldJsonValue = ((content) locVal).toJson(indentation, fielddata, true);
276
            } else {
277
                fieldJsonValue = locVal.toString();
280 278
            }
281 279

  
282
            String locStringTest = "\n" + indentation + "\"" + f.name + "\"" + " : " + val;
283
            sb.append(locStringTest);
284
            /*sb.append(formatting.get("lineBreak")).append(indentation);
285
            sb.append(formatting.get("fieldCol")).append("\"").append(f.name).append("\"").append(formatting.get("closeTagCol"));
286
            sb.append(" : ");
287
            sb.append(val);*/
280
            String locString = "\n" + indentation + "\"" + locField.name + "\"" + " : " + fieldJsonValue;
281
            sb.append(locString);
288 282

  
289
            if (!f.equals(this.fields[this.fields.length - 1])) {
283
            if (!locField.equals(this.fields[this.fields.length - 1])) {
290 284
                sb.append(", ");
291 285
            }
292 286
        }
293
        indentation = indentation.replaceFirst("\t", "");
294 287

  
288
        indentation = indentation.replaceFirst("\t", "");
295 289
        sb.append("\n").append(indentation).append("}");
296 290

  
297
        if(this.superclass != null)
298
        {
291
        //recursively call toJson for superclass if presented
292
        if (this.superclass != null) {
299 293
            indentation = indentation.replaceFirst("\t", "");
300 294
            sb.append("\n").append(indentation).append("}");
301 295
            sb.append("," + "\n");
302 296
            sb.append(this.superclass.toJson(indentation, fielddata, false));
303
        }
304
        else 
305
        {
297
        } else {
306 298
            indentation = indentation.replaceFirst("\t", "");
307 299
            sb.append("\n").append(indentation).append("}");
308 300
        }
309 301

  
310 302
        return sb.toString();
311 303
    }
312
}
313 304

  
305
    /**
306
     * Appends the extends clausule to the input StringBuffer.
307
     * Only if superclass presented
308
     * 
309
     * @param sb            StringBuffer to append the string to
310
     * @param superclass    superclass instance which name will be appended
311
     */
312
    private void extendsToJson(StringBuffer sb, classdesc superclass) {
313
        if (this.superclass != null) {
314
            sb.append(" extends ");
315
            sb.append(this.superclass.name);
316
        }
317
    }
318

  
319
    /**
320
     * Appends the implements clausule to the input StringBuffer.
321
     * Only if any interface implementation presented
322
     * 
323
     * @param sb            StringBuffer to append the string to
324
     * @param interfaces    Array of implemented interface names
325
     */
326
    private void interfacesToJson(StringBuffer sb, String[] interfaces) {
327
        if (interfaces != null) {
328
            sb.append(" implements ");
329
            for (String str : this.interfaces) {
330
                sb.append(str);
331
                if (this.interfaces[this.interfaces.length - 1] != str) {
332
                    sb.append(", ");
333
                }
334
            }
335
        }
336
    }
337
}

Také k dispozici: Unified diff