Projekt

Obecné

Profil

Stáhnout (2.68 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
	public Variable nextVariable() {
25
		if (++actVariableIndex < variables.size()) {
26
			return variables.get(actVariableIndex);
27
		} else {
28
			return null;
29
		}
30
	}
31
	
32
	public void addParent(ClassDescription parent) {
33
		parents.add(parent);
34
		actParentIndex++;
35
	}
36
	
37
	public Variable getActVariable() {
38
		for (; actParentIndex >= 0; actParentIndex--) {
39
			Variable var = parents.get(actParentIndex).nextVariable();
40
			if (var != null) {
41
				return var;
42
			}
43
		}
44
		return null;
45
	}
46
	
47
	public void setVariables(List<Variable> variables) {
48
		this.variables = variables;
49
	}
50
	
51
	@SuppressWarnings("unchecked")
52
	public JSONObject getJSONObject() {
53
		int actIndex = ++actParentIndex;
54
		JSONObject obj = new JSONObject();
55
		
56
		JSONObject vars = new JSONObject();
57
		for (int j = 0; j < variables.size(); j++) {
58
			variables.get(j).putToJSON(vars);
59
		}
60
		obj.put("variables", vars);
61
		
62
		String i = "";
63
		if (flags == SC_SERIALIZABLE) {
64
			i = "java.io.Serializable";
65
		} else if (flags == SC_EXTERNALIZABLE) {
66
			i = "java.io.Externalizable";
67
		}
68
		obj.put("interface", i);
69
		
70
		String name;
71
		if (isClass) {
72
			name = "class";
73
		} else {
74
			name = "enum";
75
		}
76
		name += " " + className;
77
		obj.put("name", name);
78
		
79
		if ((actIndex + 1) < parents.size()) {
80
			obj.put("parent", parents.get(actIndex + 1).getJSONObject());
81
		}
82
		
83
		return obj;
84
	}
85
	
86
	@Override
87
	public String toString() {
88
		actParentIndex++;
89
		String retValue = "";
90
		
91
		if (isClass) {
92
			retValue = "class";
93
		} else {
94
			retValue = "enum";
95
		}
96
		
97
		retValue += " " + className;
98
		
99
		if ((actParentIndex + 1) < parents.size()) {
100
			retValue += " extends " + parents.get(actParentIndex + 1).className;
101
		}
102
		
103
		if (flags == SC_SERIALIZABLE) {
104
			retValue += " implements java.io.Serializable";
105
		} else if (flags == SC_EXTERNALIZABLE) {
106
			retValue += " implements java.io.Externalizable";
107
		}
108
		
109
		retValue += "\n{\n";
110
		for (int i = 0; i < variables.size(); i++) {
111
			if (i > 0) {
112
				retValue += "\n";
113
			}
114
			retValue += "    " + variables.get(i).toString() + ";";
115
		}
116
		retValue += "\n}\n";
117
		
118
		if ((actParentIndex + 1) < parents.size()) {
119
			retValue += parents.get(actParentIndex + 1).toString();
120
		}
121
		
122
		return retValue;
123
	}
124
	
125
}
(1-1/8)