View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.bcel.classfile;
18  
19  import org.apache.bcel.Const;
20  
21  /**
22   * Super class for all objects that have modifiers like private, final, ... I.e. classes, fields, and methods.
23   */
24  public abstract class AccessFlags {
25  
26      /**
27       * Access flags.
28       *
29       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter.
30       */
31      @java.lang.Deprecated
32      protected int access_flags; // TODO not used externally at present
33  
34      /**
35       * Constructs a new instance.
36       */
37      public AccessFlags() {
38      }
39  
40      /**
41       * Constructs a new instance.
42       *
43       * @param accessFlags initial access flags.
44       */
45      public AccessFlags(final int accessFlags) {
46          access_flags = accessFlags;
47      }
48  
49      /**
50       * Gets access flags.
51       *
52       * @return Access flags of the object aka. "modifiers".
53       */
54      public final int getAccessFlags() {
55          return access_flags;
56      }
57  
58      /**
59       * Gets access flags.
60       *
61       * @return Access flags of the object also known as modifiers.
62       */
63      public final int getModifiers() {
64          return access_flags;
65      }
66  
67      /**
68       * Tests whether the abstract bit is on.
69       *
70       * @return whether the abstract bit is on.
71       */
72      public final boolean isAbstract() {
73          return test(Const.ACC_ABSTRACT);
74      }
75  
76      /**
77       * Sets the abstract bit.
78       *
79       * @param flag The new value.
80       */
81      public final void isAbstract(final boolean flag) {
82          setFlag(Const.ACC_ABSTRACT, flag);
83      }
84  
85      /**
86       * Tests whether the annotation bit is on.
87       *
88       * @return whether the annotation bit is on.
89       */
90      public final boolean isAnnotation() {
91          return test(Const.ACC_ANNOTATION);
92      }
93  
94      /**
95       * Sets the annotation bit.
96       *
97       * @param flag The new value.
98       */
99      public final void isAnnotation(final boolean flag) {
100         setFlag(Const.ACC_ANNOTATION, flag);
101     }
102     /**
103      * Tests whether the enum bit is on.
104      *
105      * @return whether the enum bit is on.
106      */
107     public final boolean isEnum() {
108         return test(Const.ACC_ENUM);
109     }
110 
111     /**
112      * Sets the enum bit.
113      *
114      * @param flag The new value.
115      */
116     public final void isEnum(final boolean flag) {
117         setFlag(Const.ACC_ENUM, flag);
118     }
119 
120     /**
121      * Tests whether the final bit is on.
122      *
123      * @return whether the final bit is on.
124      */
125     public final boolean isFinal() {
126         return test(Const.ACC_FINAL);
127     }
128 
129     /**
130      * Sets the final bit.
131      *
132      * @param flag The new value.
133      */
134     public final void isFinal(final boolean flag) {
135         setFlag(Const.ACC_FINAL, flag);
136     }
137 
138     /**
139      * Tests whether the interface bit is on.
140      *
141      * @return whether the interface bit is on.
142      */
143     public final boolean isInterface() {
144         return test(Const.ACC_INTERFACE);
145     }
146 
147     /**
148      * Sets the interface bit.
149      *
150      * @param flag The new value.
151      */
152     public final void isInterface(final boolean flag) {
153         setFlag(Const.ACC_INTERFACE, flag);
154     }
155 
156     /**
157      * Tests whether the native bit is on.
158      *
159      * @return whether the native bit is on.
160      */
161     public final boolean isNative() {
162         return test(Const.ACC_NATIVE);
163     }
164 
165     /**
166      * Sets the native bit.
167      *
168      * @param flag The new value.
169      */
170     public final void isNative(final boolean flag) {
171         setFlag(Const.ACC_NATIVE, flag);
172     }
173 
174     /**
175      * Tests whether the private bit is on.
176      *
177      * @return whether the private bit is on.
178      */
179     public final boolean isPrivate() {
180         return test(Const.ACC_PRIVATE);
181     }
182 
183     /**
184      * Sets the private bit.
185      *
186      * @param flag The new value.
187      */
188     public final void isPrivate(final boolean flag) {
189         setFlag(Const.ACC_PRIVATE, flag);
190     }
191 
192     /**
193      * Tests whether the protected bit is on.
194      *
195      * @return whether the protected bit is on.
196      */
197     public final boolean isProtected() {
198         return test(Const.ACC_PROTECTED);
199     }
200 
201     /**
202      * Sets the protected bit.
203      *
204      * @param flag The new value.
205      */
206     public final void isProtected(final boolean flag) {
207         setFlag(Const.ACC_PROTECTED, flag);
208     }
209 
210     /**
211      * Tests whether the public bit is on.
212      *
213      * @return whether the public bit is on.
214      */
215     public final boolean isPublic() {
216         return test(Const.ACC_PUBLIC);
217     }
218 
219     /**
220      * Sets the public bit.
221      *
222      * @param flag The new value.
223      */
224     public final void isPublic(final boolean flag) {
225         setFlag(Const.ACC_PUBLIC, flag);
226     }
227 
228     /**
229      * Tests whether the static bit is on.
230      *
231      * @return whether the static bit is on.
232      */
233     public final boolean isStatic() {
234         return test(Const.ACC_STATIC);
235     }
236 
237     /**
238      * Sets the static bit.
239      *
240      * @param flag The new value.
241      */
242     public final void isStatic(final boolean flag) {
243         setFlag(Const.ACC_STATIC, flag);
244     }
245 
246     /**
247      * Tests whether the strict bit is on.
248      *
249      * @return whether the strict bit is on.
250      */
251     public final boolean isStrictfp() {
252         return test(Const.ACC_STRICT);
253     }
254 
255     /**
256      * Sets the strict bit.
257      *
258      * @param flag The new value.
259      */
260     public final void isStrictfp(final boolean flag) {
261         setFlag(Const.ACC_STRICT, flag);
262     }
263 
264     /**
265      * Tests whether the synchronized bit is on.
266      *
267      * @return whether the synchronized bit is on.
268      */
269     public final boolean isSynchronized() {
270         return test(Const.ACC_SYNCHRONIZED);
271     }
272 
273     /**
274      * Sets the synchronized bit.
275      *
276      * @param flag The new value.
277      */
278     public final void isSynchronized(final boolean flag) {
279         setFlag(Const.ACC_SYNCHRONIZED, flag);
280     }
281 
282     /**
283      * Tests whether the synthetic bit is on.
284      *
285      * @return whether the synthetic bit is on.
286      */
287     public final boolean isSynthetic() {
288         return test(Const.ACC_SYNTHETIC);
289     }
290 
291     /**
292      * Sets the synthetic bit.
293      *
294      * @param flag The new value.
295      */
296     public final void isSynthetic(final boolean flag) {
297         setFlag(Const.ACC_SYNTHETIC, flag);
298     }
299 
300     /**
301      * Tests whether the transient bit is on.
302      *
303      * @return whether the varargs bit is on.
304      */
305     public final boolean isTransient() {
306         return test(Const.ACC_TRANSIENT);
307     }
308 
309     /**
310      * Sets the varargs bit.
311      *
312      * @param flag The new value.
313      */
314     public final void isTransient(final boolean flag) {
315         setFlag(Const.ACC_TRANSIENT, flag);
316     }
317 
318     /**
319      * Tests whether the varargs bit is on.
320      *
321      * @return whether the varargs bit is on.
322      */
323     public final boolean isVarArgs() {
324         return test(Const.ACC_VARARGS);
325     }
326 
327     /**
328      * Sets the varargs bit.
329      *
330      * @param flag The new value.
331      */
332     public final void isVarArgs(final boolean flag) {
333         setFlag(Const.ACC_VARARGS, flag);
334     }
335 
336     /**
337      * Tests whether the volatile bit is on.
338      *
339      * @return whether the volatile bit is on.
340      */
341     public final boolean isVolatile() {
342         return test(Const.ACC_VOLATILE);
343     }
344 
345     /**
346      * Sets the volatile bit.
347      *
348      * @param flag The new value.
349      */
350     public final void isVolatile(final boolean flag) {
351         setFlag(Const.ACC_VOLATILE, flag);
352     }
353 
354     /**
355      * Sets access flags also known as modifiers.
356      *
357      * @param accessFlags Access flags of the object.
358      */
359     public final void setAccessFlags(final int accessFlags) {
360         this.access_flags = accessFlags;
361     }
362 
363     private void setFlag(final int flag, final boolean set) {
364         if ((access_flags & flag) != 0) { // Flag is set already
365             if (!set) {
366                 access_flags ^= flag;
367             }
368         } else if (set) {
369             access_flags |= flag;
370         }
371     }
372 
373     /**
374      * Sets access flags aka "modifiers".
375      *
376      * @param accessFlags Access flags of the object.
377      */
378     public final void setModifiers(final int accessFlags) {
379         setAccessFlags(accessFlags);
380     }
381 
382     /**
383      * Tests whether the bit is on.
384      *
385      * @param test the bit to test.
386      * @return whether the bit is on.
387      */
388     private boolean test(final short test) {
389         return (access_flags & test) != 0;
390     }
391 }