1 package org.catacomb.act;
2
3 import java.util.ArrayList;
4
5 public class FieldSignature {
6
7 public final static int PRIVATE = 1;
8 public final static int PUBLIC = 2;
9 public final static int REFLECT_PUBLIC = 3;
10
11
12 public int type;
13
14 String preComment;
15 ArrayList<EnumOption> enumOptions;
16
17
18 String vtype;
19 String name;
20
21 boolean getter;
22 boolean setter;
23
24 boolean visible = false;
25
26 public FieldSignature(String typ, String nm) {
27 vtype = typ;
28 name = nm;
29 type = PRIVATE;
30 getter = false;
31 setter = false;
32 }
33
34
35 public void addGetter() {
36 getter = true;
37 }
38
39 public void addSetter() {
40 setter = true;
41 }
42
43 public void setPrivate() {
44 type = PRIVATE;
45 }
46
47 public void setPublic() {
48 type = PUBLIC;
49 }
50
51 public void setVisible() {
52 visible = true;
53 }
54
55
56 public void setReflectable() {
57
58
59 type = REFLECT_PUBLIC;
60 }
61
62 public String getJavaSource() {
63 StringBuffer sb = new StringBuffer();
64
65 if (preComment != null) {
66 sb.append("// " + preComment + "\n");
67 }
68 if (enumOptions != null) {
69 for (EnumOption eno : enumOptions) {
70 sb.append(eno.toJavaDeclaration());
71 }
72 }
73
74
75 sb.append(" ");
76 if (type == PRIVATE) {
77 sb.append("private ");
78 } else if (type == PUBLIC || type == REFLECT_PUBLIC) {
79 sb.append("public ");
80 }
81 sb.append(vtype);
82 sb.append(" ");
83 sb.append(name);
84 sb.append(";\n");
85
86 return sb.toString();
87 }
88
89
90
91 public void addEnumOption(String nm, int val) {
92 if (enumOptions == null) {
93 enumOptions = new ArrayList<EnumOption>();
94 }
95 enumOptions.add(new EnumOption(nm, val));
96 }
97
98
99 public String getPrefixedInfo(String pfx) {
100 StringBuffer sb = new StringBuffer();
101 String ufnm = getUName();
102 if (getter) {
103 sb.append(" " + vtype + " " + pfx + ".get" + ufnm + "() ");
104
105 if (enumOptions != null && enumOptions.size() > 0) {
106 sb.append("\n possible values: ");
107 int ict = 0;
108 for (EnumOption eno : enumOptions) {
109 ict++;
110 if (ict % 4 == 0) {
111 sb.append("\n ");
112 }
113 sb.append(eno.toPrefixedCodeComment(pfx));
114 }
115 }
116 } else {
117 sb.append(" " + vtype + " " + pfx + "." + name);
118 }
119
120 return sb.toString();
121 }
122
123
124 public String getLocalInfo() {
125 return " " + vtype + " " + name;
126 }
127
128
129
130
131 private String getUName() {
132 String ufnm = name;
133 if (name.length() == 1) {
134 ufnm = name.toUpperCase();
135 } else {
136 ufnm = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
137 }
138 return ufnm;
139 }
140
141
142
143 public String getJavaAccessors() {
144 StringBuffer sb = new StringBuffer();
145
146 String ufnm = getUName();
147 if (setter) {
148 sb.append(" public void set" + ufnm + "(" + vtype + " v) {\n");
149 sb.append(" this." + name + " = v;\n");
150 sb.append(" }\n\n");
151 }
152
153 if (getter) {
154 sb.append(" public " + vtype + " get" + ufnm + "() {\n");
155 sb.append(" return " + name + ";\n");
156 sb.append(" }\n\n");
157 }
158 return sb.toString();
159 }
160
161
162 public void addPreComment(String str) {
163 preComment = str;
164
165 }
166
167
168 public boolean isPublic() {
169 boolean ret = false;
170 if (type == PUBLIC) {
171 ret = true;
172 }
173 return ret;
174 }
175
176 public boolean isVisible() {
177 return visible;
178 }
179
180
181 }