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