001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.classfile;
020
021import org.apache.bcel.Const;
022
023/**
024 * Super class for all objects that have modifiers like private, final, ... I.e. classes, fields, and methods.
025 */
026public abstract class AccessFlags {
027
028    /**
029     * Access flags.
030     *
031     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter.
032     */
033    @java.lang.Deprecated
034    protected int access_flags; // TODO not used externally at present
035
036    /**
037     * Constructs a new instance.
038     */
039    public AccessFlags() {
040    }
041
042    /**
043     * Constructs a new instance.
044     *
045     * @param accessFlags initial access flags.
046     */
047    public AccessFlags(final int accessFlags) {
048        access_flags = accessFlags;
049    }
050
051    /**
052     * Gets access flags.
053     *
054     * @return Access flags of the object aka. "modifiers".
055     */
056    public final int getAccessFlags() {
057        return access_flags;
058    }
059
060    /**
061     * Gets access flags.
062     *
063     * @return Access flags of the object also known as modifiers.
064     */
065    public final int getModifiers() {
066        return access_flags;
067    }
068
069    /**
070     * Tests whether the abstract bit is on.
071     *
072     * @return whether the abstract bit is on.
073     */
074    public final boolean isAbstract() {
075        return test(Const.ACC_ABSTRACT);
076    }
077
078    /**
079     * Sets the abstract bit.
080     *
081     * @param flag The new value.
082     */
083    public final void isAbstract(final boolean flag) {
084        setFlag(Const.ACC_ABSTRACT, flag);
085    }
086
087    /**
088     * Tests whether the annotation bit is on.
089     *
090     * @return whether the annotation bit is on.
091     */
092    public final boolean isAnnotation() {
093        return test(Const.ACC_ANNOTATION);
094    }
095
096    /**
097     * Sets the annotation bit.
098     *
099     * @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}