1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 package org.osjava.jardiff;
40
41 import org.objectweb.asm.Opcodes;
42
43 /***
44 * An abstract class representing information about a class, method or field.
45 *
46 * @author <a href="mailto:antony@cyberiantiger.org">Antony Riley</a>
47 */
48 public abstract class AbstractInfo
49 {
50 /***
51 * The string used to represent a class, method or field with public
52 * access.
53 */
54 public final String ACCESS_PUBLIC = "public";
55
56 /***
57 * The string used to represent a class, method or field with protected
58 * access.
59 */
60 public final String ACCESS_PROTECTED = "protected";
61
62 /***
63 * The string used to represent a class, method or field with package
64 * private access.
65 * Package private access is the default access level used by java when
66 * you do not specify one of public, protected or private.
67 */
68 public final String ACCESS_PACKAGE = "package";
69
70 /***
71 * The string used to represent a class, method or field with private
72 * access.
73 */
74 public final String ACCESS_PRIVATE = "private";
75
76 /***
77 * The access flags for this class, method or field.
78 */
79 private final int access;
80
81 /***
82 * The internal name of this class, method or field.
83 */
84 private final String name;
85
86 /***
87 * Construct a new AbstractInfo with the specified access and name.
88 *
89 * @param access The access flags for this class, method or field.
90 * @param name The internal name of this class, method or field.
91 */
92 public AbstractInfo(int access, String name) {
93 this.access = access;
94 this.name = name;
95 }
96
97 /***
98 * Get the access flags for this class, method or field.
99 *
100 * @return the access flags.
101 */
102 public final int getAccess() {
103 return access;
104 }
105
106 /***
107 * Get the internal name of this class, method or field.
108 *
109 * @return the name
110 */
111 public final String getName() {
112 return name;
113 }
114
115 /***
116 * Test if this class, method or field is public.
117 *
118 * @return true if it is public.
119 */
120 public final boolean isPublic() {
121 return (access & Opcodes.ACC_PUBLIC) != 0;
122 }
123
124 /***
125 * Test if this class, method or field is protected.
126 *
127 * @return true if it is protected.
128 */
129 public final boolean isProtected() {
130 return (access & Opcodes.ACC_PROTECTED) != 0;
131 }
132
133 /***
134 * Test if this class, method or field is package private.
135 *
136 * @return true if it is package private.
137 */
138 public final boolean isPackagePrivate() {
139 return (access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED |
140 Opcodes.ACC_PRIVATE)) == 0;
141 }
142
143 /***
144 * Test if this class, method or field is private.
145 *
146 * @return true if it is private.
147 */
148 public final boolean isPrivate() {
149 return (access & Opcodes.ACC_PRIVATE) != 0;
150 }
151
152 /***
153 * Test if this class, method or field is abstract.
154 *
155 * @return true if it is abstract.
156 */
157 public final boolean isAbstract() {
158 return (access & Opcodes.ACC_ABSTRACT) != 0;
159 }
160
161 /***
162 * Test if this class, method or field is annotation
163 *
164 * @return true if it is annotation.
165 */
166 public final boolean isAnnotation() {
167 return (access & Opcodes.ACC_ANNOTATION) != 0;
168 }
169
170 /***
171 * Test if this class, method or field is a bridge
172 *
173 * @return true if it is a bridge.
174 */
175 public final boolean isBridge() {
176 return (access & Opcodes.ACC_BRIDGE) != 0;
177 }
178
179 /***
180 * Test if this class, method or field is deprecated.
181 *
182 * @return true if it is deprecated.
183 */
184 public final boolean isDeprecated() {
185 return (access & Opcodes.ACC_DEPRECATED) != 0;
186 }
187
188 /***
189 * Test if this class, method or field is an enum.
190 *
191 * @return true if it is an enum.
192 */
193 public final boolean isEnum() {
194 return (access & Opcodes.ACC_ENUM) != 0;
195 }
196
197 /***
198 * Test if this class, method or field is final.
199 *
200 * @return true if it is final.
201 */
202 public final boolean isFinal() {
203 return (access & Opcodes.ACC_FINAL) != 0;
204 }
205
206 /***
207 * Test if this class, method or field is an interface.
208 *
209 * @return true if it is an interface.
210 */
211 public final boolean isInterface() {
212 return (access & Opcodes.ACC_INTERFACE) != 0;
213 }
214
215 /***
216 * Test if this class, method or field is native.
217 *
218 * @return true if it is native.
219 */
220 public final boolean isNative() {
221 return (access & Opcodes.ACC_NATIVE) != 0;
222 }
223
224 /***
225 * Test if this class, method or field is static.
226 *
227 * @return true if it is static.
228 */
229 public final boolean isStatic() {
230 return (access & Opcodes.ACC_STATIC) != 0;
231 }
232
233 /***
234 * Test if this class, method or field is string.
235 *
236 * @return true if it is strict.
237 */
238 public final boolean isStrict() {
239 return (access & Opcodes.ACC_STRICT) != 0;
240 }
241
242 /***
243 * Test if this class, method or field is super.
244 *
245 * @return true if it is super.
246 */
247 public final boolean isSuper() {
248 return (access & Opcodes.ACC_SUPER) != 0;
249 }
250
251 /***
252 * Test if this class, method or field is synchronized.
253 *
254 * @return true if it is synchronized
255 */
256 public final boolean isSynchronized() {
257 return (access & Opcodes.ACC_SYNCHRONIZED) != 0;
258 }
259
260 /***
261 * Test if this class, method or field is synthetic.
262 *
263 * @return true if it is synchronized.
264 */
265 public final boolean isSynthetic() {
266 return (access & Opcodes.ACC_SYNTHETIC) != 0;
267 }
268
269 /***
270 * Test if this class or field is transient.
271 * If this flag is set on a method it means something different.
272 *
273 * @return true if it is transient.
274 */
275 public final boolean isTransient() {
276 return !(this instanceof MethodInfo) &&
277 ((access & Opcodes.ACC_TRANSIENT) != 0);
278 }
279
280 /***
281 * Test if this method is varargs.
282 * If this flag is set on a class or field it means something different.
283 * Well, it probably shouldn't be set on a class as it would make
284 * no sense, it only really makes sense on fields and methods.
285 *
286 * @return true if it is vargargs.
287 */
288 public final boolean isVarargs() {
289 return (this instanceof MethodInfo) &&
290 ((access & Opcodes.ACC_VARARGS) != 0);
291 }
292
293 /***
294 * Test if this class, method or field is volatile.
295 *
296 * @return true if it is volatile.
297 */
298 public final boolean isVolatile() {
299 return (access & Opcodes.ACC_VOLATILE) != 0;
300 }
301
302 /***
303 * Retrivie the access level for this class, method or field.
304 *
305 * @return the access level
306 */
307 public final String getAccessType() {
308 if (isPublic())
309 return ACCESS_PUBLIC;
310 if (isProtected())
311 return ACCESS_PROTECTED;
312 if (isPrivate())
313 return ACCESS_PRIVATE;
314 return ACCESS_PACKAGE;
315 }
316 }