    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    /**
    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}