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     /**
106      * Tests whether the enum bit is on.
107      *
108      * @return whether the enum bit is on.
109      */
110     public final boolean isEnum() {
111         return test(Const.ACC_ENUM);
112     }
113 
114     /**
115      * Sets the enum bit.
116      *
117      * @param flag The new value.
118      */
119     public final void isEnum(final boolean flag) {
120         setFlag(Const.ACC_ENUM, flag);
121     }
122 
123     /**
124      * Tests whether the final bit is on.
125      *
126      * @return whether the final bit is on.
127      */
128     public final boolean isFinal() {
129         return test(Const.ACC_FINAL);
130     }
131 
132     /**
133      * Sets the final bit.
134      *
135      * @param flag The new value.
136      */
137     public final void isFinal(final boolean flag) {
138         setFlag(Const.ACC_FINAL, flag);
139     }
140 
141     /**
142      * Tests whether the interface bit is on.
143      *
144      * @return whether the interface bit is on.
145      */
146     public final boolean isInterface() {
147         return test(Const.ACC_INTERFACE);
148     }
149 
150     /**
151      * Sets the interface bit.
152      *
153      * @param flag The new value.
154      */
155     public final void isInterface(final boolean flag) {
156         setFlag(Const.ACC_INTERFACE, flag);
157     }
158 
159     /**
160      * Tests whether the native bit is on.
161      *
162      * @return whether the native bit is on.
163      */
164     public final boolean isNative() {
165         return test(Const.ACC_NATIVE);
166     }
167 
168     /**
169      * Sets the native bit.
170      *
171      * @param flag The new value.
172      */
173     public final void isNative(final boolean flag) {
174         setFlag(Const.ACC_NATIVE, flag);
175     }
176 
177     /**
178      * Tests whether the private bit is on.
179      *
180      * @return whether the private bit is on.
181      */
182     public final boolean isPrivate() {
183         return test(Const.ACC_PRIVATE);
184     }
185 
186     /**
187      * Sets the private bit.
188      *
189      * @param flag The new value.
190      */
191     public final void isPrivate(final boolean flag) {
192         setFlag(Const.ACC_PRIVATE, flag);
193     }
194 
195     /**
196      * Tests whether the protected bit is on.
197      *
198      * @return whether the protected bit is on.
199      */
200     public final boolean isProtected() {
201         return test(Const.ACC_PROTECTED);
202     }
203 
204     /**
205      * Sets the protected bit.
206      *
207      * @param flag The new value.
208      */
209     public final void isProtected(final boolean flag) {
210         setFlag(Const.ACC_PROTECTED, flag);
211     }
212 
213     /**
214      * Tests whether the public bit is on.
215      *
216      * @return whether the public bit is on.
217      */
218     public final boolean isPublic() {
219         return test(Const.ACC_PUBLIC);
220     }
221 
222     /**
223      * Sets the public bit.
224      *
225      * @param flag The new value.
226      */
227     public final void isPublic(final boolean flag) {
228         setFlag(Const.ACC_PUBLIC, flag);
229     }
230 
231     /**
232      * Tests whether the static bit is on.
233      *
234      * @return whether the static bit is on.
235      */
236     public final boolean isStatic() {
237         return test(Const.ACC_STATIC);
238     }
239 
240     /**
241      * Sets the static bit.
242      *
243      * @param flag The new value.
244      */
245     public final void isStatic(final boolean flag) {
246         setFlag(Const.ACC_STATIC, flag);
247     }
248 
249     /**
250      * Tests whether the strict bit is on.
251      *
252      * @return whether the strict bit is on.
253      */
254     public final boolean isStrictfp() {
255         return test(Const.ACC_STRICT);
256     }
257 
258     /**
259      * Sets the strict bit.
260      *
261      * @param flag The new value.
262      */
263     public final void isStrictfp(final boolean flag) {
264         setFlag(Const.ACC_STRICT, flag);
265     }
266 
267     /**
268      * Tests whether the synchronized bit is on.
269      *
270      * @return whether the synchronized bit is on.
271      */
272     public final boolean isSynchronized() {
273         return test(Const.ACC_SYNCHRONIZED);
274     }
275 
276     /**
277      * Sets the synchronized bit.
278      *
279      * @param flag The new value.
280      */
281     public final void isSynchronized(final boolean flag) {
282         setFlag(Const.ACC_SYNCHRONIZED, flag);
283     }
284 
285     /**
286      * Tests whether the synthetic bit is on.
287      *
288      * @return whether the synthetic bit is on.
289      */
290     public final boolean isSynthetic() {
291         return test(Const.ACC_SYNTHETIC);
292     }
293 
294     /**
295      * Sets the synthetic bit.
296      *
297      * @param flag The new value.
298      */
299     public final void isSynthetic(final boolean flag) {
300         setFlag(Const.ACC_SYNTHETIC, flag);
301     }
302 
303     /**
304      * Tests whether the transient bit is on.
305      *
306      * @return whether the varargs bit is on.
307      */
308     public final boolean isTransient() {
309         return test(Const.ACC_TRANSIENT);
310     }
311 
312     /**
313      * Sets the varargs bit.
314      *
315      * @param flag The new value.
316      */
317     public final void isTransient(final boolean flag) {
318         setFlag(Const.ACC_TRANSIENT, flag);
319     }
320 
321     /**
322      * Tests whether the varargs bit is on.
323      *
324      * @return whether the varargs bit is on.
325      */
326     public final boolean isVarArgs() {
327         return test(Const.ACC_VARARGS);
328     }
329 
330     /**
331      * Sets the varargs bit.
332      *
333      * @param flag The new value.
334      */
335     public final void isVarArgs(final boolean flag) {
336         setFlag(Const.ACC_VARARGS, flag);
337     }
338 
339     /**
340      * Tests whether the volatile bit is on.
341      *
342      * @return whether the volatile bit is on.
343      */
344     public final boolean isVolatile() {
345         return test(Const.ACC_VOLATILE);
346     }
347 
348     /**
349      * Sets the volatile bit.
350      *
351      * @param flag The new value.
352      */
353     public final void isVolatile(final boolean flag) {
354         setFlag(Const.ACC_VOLATILE, flag);
355     }
356 
357     /**
358      * Sets access flags also known as modifiers.
359      *
360      * @param accessFlags Access flags of the object.
361      */
362     public final void setAccessFlags(final int accessFlags) {
363         this.access_flags = accessFlags;
364     }
365 
366     private void setFlag(final int flag, final boolean set) {
367         if ((access_flags & flag) != 0) { // Flag is set already
368             if (!set) {
369                 access_flags ^= flag;
370             }
371         } else if (set) {
372             access_flags |= flag;
373         }
374     }
375 
376     /**
377      * Sets access flags aka "modifiers".
378      *
379      * @param accessFlags Access flags of the object.
380      */
381     public final void setModifiers(final int accessFlags) {
382         setAccessFlags(accessFlags);
383     }
384 
385     /**
386      * Tests whether the bit is on.
387      *
388      * @param test the bit to test.
389      * @return whether the bit is on.
390      */
391     private boolean test(final short test) {
392         return (access_flags & test) != 0;
393     }
394 }