Projekt

Obecné

Profil

Stáhnout (2.82 KB) Statistiky
| Větev: | Tag: | Revize:
1
package deserialize;
2

    
3
import java.util.ArrayList;
4
import java.util.List;
5

    
6
import org.json.simple.JSONObject;
7

    
8
public class ClassDescription {
9

    
10
	private final static byte SC_SERIALIZABLE = (byte) 0x02;
11
	private final static byte SC_EXTERNALIZABLE = (byte) 0x04;
12
	
13
	String className;
14
	long serialVersionUID;
15
	byte flags;
16
	boolean isClass; // or enum
17
	
18
	private List<ClassDescription> parents = new ArrayList<ClassDescription>();
19
	private int actParentIndex = -1;
20
	
21
	private List<Variable> variables = new ArrayList<Variable>();
22
	private int actVariableIndex = -1;
23
	
24
	
25
	
26
	public List<ClassDescription> getParents() {
27
		return parents;
28
	}
29
	
30
	public List<Variable> getVariables() {
31
		return variables;
32
	}
33
	
34
	
35
	
36
	public Variable nextVariable() {
37
		if (++actVariableIndex < variables.size()) {
38
			return variables.get(actVariableIndex);
39
		} else {
40
			return null;
41
		}
42
	}
43
	
44
	public void addParent(ClassDescription parent) {
45
		parents.add(parent);
46
		actParentIndex++;
47
	}
48
	
49
	public Variable getActVariable() {
50
		for (; actParentIndex >= 0; actParentIndex--) {
51
			Variable var = parents.get(actParentIndex).nextVariable();
52
			if (var != null) {
53
				return var;
54
			}
55
		}
56
		return null;
57
	}
58
	
59
	public void setVariables(List<Variable> variables) {
60
		this.variables = variables;
61
	}
62
	
63
	@SuppressWarnings("unchecked")
64
	public JSONObject getJSONObject() {
65
		int actIndex = ++actParentIndex;
66
		JSONObject obj = new JSONObject();
67
		
68
		JSONObject vars = new JSONObject();
69
		for (int j = 0; j < variables.size(); j++) {
70
			variables.get(j).putToJSON(vars);
71
		}
72
		obj.put("variables", vars);
73
		
74
		String i = "";
75
		if (flags == SC_SERIALIZABLE) {
76
			i = "java.io.Serializable";
77
		} else if (flags == SC_EXTERNALIZABLE) {
78
			i = "java.io.Externalizable";
79
		}
80
		obj.put("interface", i);
81
		
82
		String name;
83
		if (isClass) {
84
			name = "class";
85
		} else {
86
			name = "enum";
87
		}
88
		name += " " + className;
89
		obj.put("name", name);
90
		
91
		if ((actIndex + 1) < parents.size()) {
92
			obj.put("parent", parents.get(actIndex + 1).getJSONObject());
93
		}
94
		
95
		return obj;
96
	}
97
	
98
	@Override
99
	public String toString() {
100
		actParentIndex++;
101
		String retValue = "";
102
		
103
		if (isClass) {
104
			retValue = "class";
105
		} else {
106
			retValue = "enum";
107
		}
108
		
109
		retValue += " " + className;
110
		
111
		if ((actParentIndex + 1) < parents.size()) {
112
			retValue += " extends " + parents.get(actParentIndex + 1).className;
113
		}
114
		
115
		if (flags == SC_SERIALIZABLE) {
116
			retValue += " implements java.io.Serializable";
117
		} else if (flags == SC_EXTERNALIZABLE) {
118
			retValue += " implements java.io.Externalizable";
119
		}
120
		
121
		retValue += "\n{\n";
122
		for (int i = 0; i < variables.size(); i++) {
123
			if (i > 0) {
124
				retValue += "\n";
125
			}
126
			retValue += "    " + variables.get(i).toString() + ";";
127
		}
128
		retValue += "\n}\n";
129
		
130
		if ((actParentIndex + 1) < parents.size()) {
131
			retValue += parents.get(actParentIndex + 1).toString();
132
		}
133
		
134
		return retValue;
135
	}
136
	
137
}
(2-2/9)