Projekt

Obecné

Profil

Stáhnout (3.64 KB) Statistiky
| Větev: | Tag: | Revize:
1
package jdeserialize;
2
import java.io.*;
3

    
4
/**
5
 * This class represents a field within a class description/declaration (classdesc).  It
6
 * contains information about the type and name of the field.  Fields themselves don't
7
 * have a handle; inside the stream, they exist only as part of a class description.
8
 */
9
public class field {
10
    /**
11
     * The type of the field.
12
     */
13
    public fieldtype type;
14

    
15
    /**
16
     * The name of the field.
17
     */
18
    public String name; 
19

    
20
    /**
21
     * The string object representing the class name.
22
     */
23
    public stringobj classname; 
24

    
25
    private boolean isInnerClassReference = false;
26

    
27
    /**
28
     * Tells whether or not this class is an inner class reference.  This value is set by
29
     * connectMemberClasses() -- if this hasn't been called, or if the field hasn't been
30
     * otherwise set by setIsInnerClassReference(), it will be false;
31
     *
32
     * @return true if the class is an inner class reference
33
     */
34
    public boolean isInnerClassReference() {
35
        return isInnerClassReference;
36
    }
37

    
38
    /**
39
     * Sets the flag that denotes whether this class is an inner class reference.
40
     *
41
     * @param nis the value to set; true iff the class is an inner class reference.
42
     */
43
    public void setIsInnerClassReference(boolean nis) {
44
        this.isInnerClassReference = nis;
45
    }
46

    
47
    /**
48
     * Constructor.  
49
     *
50
     * @param type the field type
51
     * @param name the field name
52
     * @param classname the class name
53
     */
54
    public field(fieldtype type, String name, stringobj classname) throws ValidityException {
55
        this.type = type;
56
        this.name = name;
57
        this.classname = classname;
58
        if(classname != null) {
59
            validate(classname.value);
60
        }
61
    }
62

    
63
    /**
64
     * Constructor for simple fields.
65
     * 
66
     * @param type the field type
67
     * @param name the field name
68
     */
69
    public field(fieldtype type, String name) throws ValidityException {
70
        this(type, name, null);
71
    }
72

    
73
    /**
74
     * Get a string representing the type for this field in Java (the language)
75
     * format.
76
     * @return a string representing the fully-qualified type of the field
77
     * @throws IOException if a validity or I/O error occurs
78
     */
79
    public String getJavaType() throws IOException {
80
        return jdeserialize.resolveJavaType(this.type, this.classname == null ? null : this.classname.value, true, false);
81
    }
82
    
83
    /**
84
     * Changes the name of an object reference to the name specified.  This is used by
85
     * the inner-class-connection code to fix up field references.
86
     * @param newname the fully-qualified class 
87
     * @throws ValidityException if the field isn't a reference type, or another
88
     * validity error occurs
89
     */
90
    public void setReferenceTypeName(String newname) throws ValidityException {
91
        if(this.type != fieldtype.OBJECT) {
92
            throw new ValidityException("can't fix up a non-reference field!");
93
        }
94
        String nname = "L" + newname.replace('.', '/') + ";";
95
        this.classname.value = nname;
96
    }
97
    public void validate(String jt) throws ValidityException {
98
        if(this.type == fieldtype.OBJECT) {
99
            if(jt == null) {
100
                throw new ValidityException("classname can't be null");
101
            }
102
            if(jt.charAt(0) != 'L') {
103
                throw new ValidityException("invalid object field type descriptor: " + classname.value);
104
            }
105
            int end = jt.indexOf(';');
106
            if(end == -1 || end != (jt.length()-1)) {
107
                throw new ValidityException("invalid object field type descriptor (must end with semicolon): " + classname.value);
108
            }
109
        }
110
    }
111
}
(16-16/20)