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