Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ClazzModifiers |
|
| 1.7586206896551724;1.759 |
1 | /* | |
2 | * Copyright 2002-2004 The Apache Software Foundation | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.apache.commons.clazz; | |
17 | ||
18 | import java.lang.reflect.Modifier; | |
19 | ||
20 | /** | |
21 | * <code>ClazzModifiers</code> defines the modifiers in a manipulable way. | |
22 | * | |
23 | * @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a> | |
24 | * @version $Id: ClazzModifiers.java 155436 2005-02-26 13:17:48Z dirkv $ | |
25 | */ | |
26 | public class ClazzModifiers { | |
27 | ||
28 | /** | |
29 | * The <code>int</code> value representing the <code>public</code> | |
30 | * modifier. | |
31 | */ | |
32 | public static final int PUBLIC = Modifier.PUBLIC; | |
33 | ||
34 | /** | |
35 | * The <code>int</code> value representing the <code>private</code> | |
36 | * modifier. | |
37 | */ | |
38 | public static final int PRIVATE = Modifier.PRIVATE; | |
39 | ||
40 | /** | |
41 | * The <code>int</code> value representing the <code>protected</code> | |
42 | * modifier. | |
43 | */ | |
44 | public static final int PROTECTED = Modifier.PROTECTED; | |
45 | ||
46 | /** | |
47 | * The <code>int</code> value representing the <code>static</code> | |
48 | * modifier. | |
49 | */ | |
50 | public static final int STATIC = Modifier.STATIC; | |
51 | ||
52 | /** | |
53 | * The <code>int</code> value representing the <code>final</code> | |
54 | * modifier. | |
55 | */ | |
56 | public static final int FINAL = Modifier.FINAL; | |
57 | ||
58 | /** | |
59 | * The <code>int</code> value representing the <code>synchronized</code> | |
60 | * modifier. | |
61 | */ | |
62 | public static final int SYNCHRONIZED = Modifier.SYNCHRONIZED; | |
63 | ||
64 | /** | |
65 | * The <code>int</code> value representing the <code>volatile</code> | |
66 | * modifier. | |
67 | */ | |
68 | public static final int VOLATILE = Modifier.VOLATILE; | |
69 | ||
70 | /** | |
71 | * The <code>int</code> value representing the <code>transient</code> | |
72 | * modifier. | |
73 | */ | |
74 | public static final int TRANSIENT = Modifier.TRANSIENT; | |
75 | ||
76 | /** | |
77 | * The <code>int</code> value representing the <code>native</code> | |
78 | * modifier. | |
79 | */ | |
80 | public static final int NATIVE = Modifier.NATIVE; | |
81 | ||
82 | /** | |
83 | * The <code>int</code> value representing the <code>interface</code> | |
84 | * modifier. | |
85 | */ | |
86 | public static final int INTERFACE = Modifier.INTERFACE; | |
87 | ||
88 | /** | |
89 | * The <code>int</code> value representing the <code>abstract</code> | |
90 | * modifier. | |
91 | */ | |
92 | public static final int ABSTRACT = Modifier.ABSTRACT; | |
93 | ||
94 | /** | |
95 | * The <code>int</code> value representing the <code>strictfp</code> | |
96 | * modifier. | |
97 | */ | |
98 | public static final int STRICT = Modifier.STRICT; | |
99 | ||
100 | /** The modifiers */ | |
101 | 0 | private int modifiers = 0; |
102 | ||
103 | /** | |
104 | * Constructor. | |
105 | * | |
106 | * @param modifiers the modifiers | |
107 | */ | |
108 | public ClazzModifiers(int modifiers) { | |
109 | 0 | super(); |
110 | 0 | setFlags(modifiers); |
111 | 0 | } |
112 | ||
113 | /** | |
114 | * Copy constructor. | |
115 | * | |
116 | * @param modifiers the modifiers object | |
117 | * @throws IllegalArgumentException if the modifiers object is null | |
118 | */ | |
119 | public ClazzModifiers(ClazzModifiers modifiers) { | |
120 | 0 | super(); |
121 | 0 | if (modifiers == null) { |
122 | 0 | throw new IllegalArgumentException( |
123 | "The modifiers must not be null"); | |
124 | } | |
125 | 0 | setFlags(modifiers.getFlags()); |
126 | 0 | } |
127 | ||
128 | //-------------------------------------------------------------------------- | |
129 | ||
130 | /** | |
131 | * Gets the modifier flags, which can be accessed via java.lang.reflect. | |
132 | * Modifier. | |
133 | * | |
134 | * @return the modifier flags | |
135 | */ | |
136 | public int getFlags() { | |
137 | 0 | return modifiers; |
138 | } | |
139 | ||
140 | /** | |
141 | * Sets the modifier flags. | |
142 | * | |
143 | * @param modifiers the modifier flags to update to | |
144 | */ | |
145 | public void setFlags(int modifiers) { | |
146 | 0 | this.modifiers = modifiers; |
147 | 0 | } |
148 | ||
149 | //-------------------------------------------------------------------------- | |
150 | ||
151 | /** | |
152 | * Is the object public scope. | |
153 | * | |
154 | * @return true if public scope | |
155 | */ | |
156 | public boolean isPublicScope() { | |
157 | 0 | return (getFlags() & PUBLIC) != 0; |
158 | } | |
159 | ||
160 | /** | |
161 | * Set the object to be public scope. | |
162 | */ | |
163 | public void setPublicScope() { | |
164 | 0 | setFlags( |
165 | getFlags() | |
166 | & ~Modifier.PRIVATE | |
167 | & ~Modifier.PROTECTED | |
168 | & Modifier.PUBLIC); | |
169 | 0 | } |
170 | ||
171 | //-------------------------------------------------------------------------- | |
172 | ||
173 | /** | |
174 | * Is the object protected scope. | |
175 | * | |
176 | * @return true if protected scope | |
177 | */ | |
178 | public boolean isProtectedScope() { | |
179 | 0 | return (getFlags() & PROTECTED) != 0; |
180 | } | |
181 | ||
182 | /** | |
183 | * Set the object to be protected scope. | |
184 | */ | |
185 | public void setProtectedScope() { | |
186 | 0 | setFlags( |
187 | getFlags() | |
188 | & ~Modifier.PRIVATE | |
189 | & Modifier.PROTECTED | |
190 | & ~Modifier.PUBLIC); | |
191 | 0 | } |
192 | ||
193 | //-------------------------------------------------------------------------- | |
194 | ||
195 | /** | |
196 | * Is the object package scope. | |
197 | * | |
198 | * @return true if package scope | |
199 | */ | |
200 | public boolean isPackageScope() { | |
201 | 0 | return ( |
202 | (isPublicScope() == false) | |
203 | && (isProtectedScope() == false) | |
204 | && (isPrivateScope() == false)); | |
205 | } | |
206 | ||
207 | /** | |
208 | * Set the object to be package scope. | |
209 | */ | |
210 | public void setPackageScope() { | |
211 | 0 | setFlags( |
212 | getFlags() | |
213 | & ~Modifier.PRIVATE | |
214 | & ~Modifier.PROTECTED | |
215 | & ~Modifier.PUBLIC); | |
216 | 0 | } |
217 | ||
218 | //-------------------------------------------------------------------------- | |
219 | ||
220 | /** | |
221 | * Is the object private scope. | |
222 | * | |
223 | * @return true if private scope | |
224 | */ | |
225 | public boolean isPrivateScope() { | |
226 | 0 | return (getFlags() & PRIVATE) != 0; |
227 | } | |
228 | ||
229 | /** | |
230 | * Set the object to be private scope. | |
231 | */ | |
232 | public void setPrivateScope() { | |
233 | 0 | setFlags( |
234 | getFlags() | |
235 | & Modifier.PRIVATE | |
236 | & ~Modifier.PROTECTED | |
237 | & ~Modifier.PUBLIC); | |
238 | 0 | } |
239 | ||
240 | //-------------------------------------------------------------------------- | |
241 | ||
242 | /** | |
243 | * Is the object static. | |
244 | * | |
245 | * @return true if static | |
246 | */ | |
247 | public boolean isStatic() { | |
248 | 0 | return (getFlags() & STATIC) != 0; |
249 | } | |
250 | ||
251 | /** | |
252 | * Set the object to be static. | |
253 | * | |
254 | * @param state true to make static | |
255 | */ | |
256 | public void setStatic(boolean state) { | |
257 | 0 | if (state) { |
258 | 0 | setFlags(getFlags() & Modifier.STATIC); |
259 | } | |
260 | else { | |
261 | 0 | setFlags(getFlags() & ~Modifier.STATIC); |
262 | } | |
263 | 0 | } |
264 | ||
265 | //-------------------------------------------------------------------------- | |
266 | ||
267 | /** | |
268 | * Is the object final. | |
269 | * | |
270 | * @return true if final | |
271 | */ | |
272 | public boolean isFinal() { | |
273 | 0 | return (getFlags() & FINAL) != 0; |
274 | } | |
275 | ||
276 | /** | |
277 | * Set the object to be final. | |
278 | * | |
279 | * @param state true to make final | |
280 | */ | |
281 | public void setFinal(boolean state) { | |
282 | 0 | if (state) { |
283 | 0 | setFlags(getFlags() & Modifier.FINAL); |
284 | } | |
285 | else { | |
286 | 0 | setFlags(getFlags() & ~Modifier.FINAL); |
287 | } | |
288 | 0 | } |
289 | ||
290 | //-------------------------------------------------------------------------- | |
291 | ||
292 | /** | |
293 | * Is the object synchronized. | |
294 | * | |
295 | * @return true if synchronized | |
296 | */ | |
297 | public boolean isSynchronized() { | |
298 | 0 | return (getFlags() & SYNCHRONIZED) != 0; |
299 | } | |
300 | ||
301 | /** | |
302 | * Set the object to be synchronized. | |
303 | * | |
304 | * @param state true to make synchronized | |
305 | */ | |
306 | public void setSynchronized(boolean state) { | |
307 | 0 | if (state) { |
308 | 0 | setFlags(getFlags() & Modifier.SYNCHRONIZED); |
309 | } | |
310 | else { | |
311 | 0 | setFlags(getFlags() & ~Modifier.SYNCHRONIZED); |
312 | } | |
313 | 0 | } |
314 | ||
315 | //-------------------------------------------------------------------------- | |
316 | ||
317 | /** | |
318 | * Is the object volatile. | |
319 | * | |
320 | * @return true if volatile | |
321 | */ | |
322 | public boolean isVolatile() { | |
323 | 0 | return (getFlags() & VOLATILE) != 0; |
324 | } | |
325 | ||
326 | /** | |
327 | * Set the object to be volatile. | |
328 | * | |
329 | * @param state true to make volatile | |
330 | */ | |
331 | public void setVolatile(boolean state) { | |
332 | 0 | if (state) { |
333 | 0 | setFlags(getFlags() & Modifier.VOLATILE); |
334 | } | |
335 | else { | |
336 | 0 | setFlags(getFlags() & ~Modifier.VOLATILE); |
337 | } | |
338 | 0 | } |
339 | ||
340 | //-------------------------------------------------------------------------- | |
341 | ||
342 | /** | |
343 | * Is the object transient. | |
344 | * | |
345 | * @return true if transient | |
346 | */ | |
347 | public boolean isTransient() { | |
348 | 0 | return (getFlags() & TRANSIENT) != 0; |
349 | } | |
350 | ||
351 | /** | |
352 | * Set the object to be transient. | |
353 | * | |
354 | * @param state true to make transient | |
355 | */ | |
356 | public void setTransient(boolean state) { | |
357 | 0 | if (state) { |
358 | 0 | setFlags(getFlags() & TRANSIENT); |
359 | } | |
360 | else { | |
361 | 0 | setFlags(getFlags() & ~TRANSIENT); |
362 | } | |
363 | 0 | } |
364 | ||
365 | //-------------------------------------------------------------------------- | |
366 | ||
367 | /** | |
368 | * Is the object native. | |
369 | * | |
370 | * @return true if native | |
371 | */ | |
372 | public boolean isNative() { | |
373 | 0 | return (getFlags() & NATIVE) != 0; |
374 | } | |
375 | ||
376 | /** | |
377 | * Set the object to be native. | |
378 | * | |
379 | * @param state true to make native | |
380 | */ | |
381 | public void setNative(boolean state) { | |
382 | 0 | if (state) { |
383 | 0 | setFlags(getFlags() & NATIVE); |
384 | } | |
385 | else { | |
386 | 0 | setFlags(getFlags() & ~NATIVE); |
387 | } | |
388 | 0 | } |
389 | ||
390 | //-------------------------------------------------------------------------- | |
391 | ||
392 | /** | |
393 | * Is the object abstract. | |
394 | * | |
395 | * @return true if abstract | |
396 | */ | |
397 | public boolean isAbstract() { | |
398 | 0 | return (getFlags() & ABSTRACT) != 0; |
399 | } | |
400 | ||
401 | /** | |
402 | * Set the object to be abstract. | |
403 | * | |
404 | * @param state true to make abstract | |
405 | */ | |
406 | public void setAbstract(boolean state) { | |
407 | 0 | if (state) { |
408 | 0 | setFlags(getFlags() & ABSTRACT); |
409 | } | |
410 | else { | |
411 | 0 | setFlags(getFlags() & ~ABSTRACT); |
412 | } | |
413 | 0 | } |
414 | ||
415 | //-------------------------------------------------------------------------- | |
416 | ||
417 | /** | |
418 | * Is the object strictfp. | |
419 | * | |
420 | * @return true if strictfp | |
421 | */ | |
422 | public boolean isStrictFP() { | |
423 | 0 | return (getFlags() & STRICT) != 0; |
424 | } | |
425 | ||
426 | /** | |
427 | * Set the object to be strictfp. | |
428 | * | |
429 | * @param state true to make strictfp | |
430 | */ | |
431 | public void setStrictFP(boolean state) { | |
432 | 0 | if (state) { |
433 | 0 | setFlags(getFlags() & STRICT); |
434 | } | |
435 | else { | |
436 | 0 | setFlags(getFlags() & ~STRICT); |
437 | } | |
438 | 0 | } |
439 | ||
440 | //-------------------------------------------------------------------------- | |
441 | ||
442 | /** | |
443 | * Geta string describing the access modifier flags in | |
444 | * the specified modifier. For example: | |
445 | * <blockquote><pre> | |
446 | * AModifiers[public final synchronized] | |
447 | * AModifiers[private transient volatile] | |
448 | * </pre></blockquote> | |
449 | * The modifier names are return in canonical order, as | |
450 | * specified by <em>The Java Language Specification</em>. | |
451 | * | |
452 | * @return a debug string | |
453 | */ | |
454 | public String toString() { | |
455 | 0 | StringBuffer sb = new StringBuffer("Modifiers["); |
456 | ||
457 | 0 | if (isPublicScope()) { |
458 | 0 | sb.append("public "); |
459 | } | |
460 | 0 | if (isPrivateScope()) { |
461 | 0 | sb.append("private "); |
462 | } | |
463 | 0 | if (isProtectedScope()) { |
464 | 0 | sb.append("protected "); |
465 | } | |
466 | ||
467 | // Canonical order | |
468 | 0 | if (isAbstract()) { |
469 | 0 | sb.append("abstract "); |
470 | } | |
471 | 0 | if (isStatic()) { |
472 | 0 | sb.append("static "); |
473 | } | |
474 | 0 | if (isFinal()) { |
475 | 0 | sb.append("final "); |
476 | } | |
477 | 0 | if (isTransient()) { |
478 | 0 | sb.append("transient "); |
479 | } | |
480 | 0 | if (isVolatile()) { |
481 | 0 | sb.append("volatile "); |
482 | } | |
483 | 0 | if (isNative()) { |
484 | 0 | sb.append("native "); |
485 | } | |
486 | 0 | if (isSynchronized()) { |
487 | 0 | sb.append("synchronized "); |
488 | } | |
489 | // if (isInterface()) { | |
490 | // sb.append("interface "); | |
491 | // } | |
492 | 0 | if (isStrictFP()) { |
493 | 0 | sb.append("strictfp "); |
494 | } | |
495 | ||
496 | 0 | if (sb.charAt(sb.length() - 1) == ' ') { |
497 | 0 | sb.setCharAt(sb.length() - 1, ']'); |
498 | } | |
499 | else { | |
500 | 0 | sb.append(']'); |
501 | } | |
502 | 0 | return sb.toString(); |
503 | } | |
504 | ||
505 | } |