    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     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
    028     */
    029    @java.lang.Deprecated
    030    protected int access_flags; // TODO not used externally at present
    031
    032    public AccessFlags() {
    033    }
    034
    035    /**
    036     * @param a initial access flags
    037     */
    038    public AccessFlags(final int a) {
    039        access_flags = a;
    040    }
    041
    042    /**
    043     * @return Access flags of the object aka. "modifiers".
    044     */
    045    public final int getAccessFlags() {
    046        return access_flags;
    047    }
    048
    049    /**
    050     * @return Access flags of the object aka. "modifiers".
    051     */
    052    public final int getModifiers() {
    053        return access_flags;
    054    }
    055
    056    public final boolean isAbstract() {
    057        return (access_flags & Const.ACC_ABSTRACT) != 0;
    058    }
    059
    060    public final void isAbstract(final boolean flag) {
    061        setFlag(Const.ACC_ABSTRACT, flag);
    062    }
    063
    064    public final boolean isAnnotation() {
    065        return (access_flags & Const.ACC_ANNOTATION) != 0;
    066    }
    067
    068    public final void isAnnotation(final boolean flag) {
    069        setFlag(Const.ACC_ANNOTATION, flag);
    070    }
    071
    072    public final boolean isEnum() {
    073        return (access_flags & Const.ACC_ENUM) != 0;
    074    }
    075
    076    public final void isEnum(final boolean flag) {
    077        setFlag(Const.ACC_ENUM, flag);
    078    }
    079
    080    public final boolean isFinal() {
    081        return (access_flags & Const.ACC_FINAL) != 0;
    082    }
    083
    084    public final void isFinal(final boolean flag) {
    085        setFlag(Const.ACC_FINAL, flag);
    086    }
    087
    088    public final boolean isInterface() {
    089        return (access_flags & Const.ACC_INTERFACE) != 0;
    090    }
    091
    092    public final void isInterface(final boolean flag) {
    093        setFlag(Const.ACC_INTERFACE, flag);
    094    }
    095
    096    public final boolean isNative() {
    097        return (access_flags & Const.ACC_NATIVE) != 0;
    098    }
    099
    100    public final void isNative(final boolean flag) {
    101        setFlag(Const.ACC_NATIVE, flag);
    102    }
    103
    104    public final boolean isPrivate() {
    105        return (access_flags & Const.ACC_PRIVATE) != 0;
    106    }
    107
    108    public final void isPrivate(final boolean flag) {
    109        setFlag(Const.ACC_PRIVATE, flag);
    110    }
    111
    112    public final boolean isProtected() {
    113        return (access_flags & Const.ACC_PROTECTED) != 0;
    114    }
    115
    116    public final void isProtected(final boolean flag) {
    117        setFlag(Const.ACC_PROTECTED, flag);
    118    }
    119
    120    public final boolean isPublic() {
    121        return (access_flags & Const.ACC_PUBLIC) != 0;
    122    }
    123
    124    public final void isPublic(final boolean flag) {
    125        setFlag(Const.ACC_PUBLIC, flag);
    126    }
    127
    128    public final boolean isStatic() {
    129        return (access_flags & Const.ACC_STATIC) != 0;
    130    }
    131
    132    public final void isStatic(final boolean flag) {
    133        setFlag(Const.ACC_STATIC, flag);
    134    }
    135
    136    public final boolean isStrictfp() {
    137        return (access_flags & Const.ACC_STRICT) != 0;
    138    }
    139
    140    public final void isStrictfp(final boolean flag) {
    141        setFlag(Const.ACC_STRICT, flag);
    142    }
    143
    144    public final boolean isSynchronized() {
    145        return (access_flags & Const.ACC_SYNCHRONIZED) != 0;
    146    }
    147
    148    public final void isSynchronized(final boolean flag) {
    149        setFlag(Const.ACC_SYNCHRONIZED, flag);
    150    }
    151
    152    public final boolean isSynthetic() {
    153        return (access_flags & Const.ACC_SYNTHETIC) != 0;
    154    }
    155
    156    public final void isSynthetic(final boolean flag) {
    157        setFlag(Const.ACC_SYNTHETIC, flag);
    158    }
    159
    160    public final boolean isTransient() {
    161        return (access_flags & Const.ACC_TRANSIENT) != 0;
    162    }
    163
    164    public final void isTransient(final boolean flag) {
    165        setFlag(Const.ACC_TRANSIENT, flag);
    166    }
    167
    168    public final boolean isVarArgs() {
    169        return (access_flags & Const.ACC_VARARGS) != 0;
    170    }
    171
    172    public final void isVarArgs(final boolean flag) {
    173        setFlag(Const.ACC_VARARGS, flag);
    174    }
    175
    176    public final boolean isVolatile() {
    177        return (access_flags & Const.ACC_VOLATILE) != 0;
    178    }
    179
    180    public final void isVolatile(final boolean flag) {
    181        setFlag(Const.ACC_VOLATILE, flag);
    182    }
    183
    184    /**
    185     * Set access flags aka "modifiers".
    186     *
    187     * @param accessFlags Access flags of the object.
    188     */
    189    public final void setAccessFlags(final int accessFlags) {
    190        this.access_flags = accessFlags;
    191    }
    192
    193    private void setFlag(final int flag, final boolean set) {
    194        if ((access_flags & flag) != 0) { // Flag is set already
    195            if (!set) {
    196                access_flags ^= flag;
    197            }
    198        } else if (set) {
    199            access_flags |= flag;
    200        }
    201    }
    202
    203    /**
    204     * Set access flags aka "modifiers".
    205     *
    206     * @param accessFlags Access flags of the object.
    207     */
    208    public final void setModifiers(final int accessFlags) {
    209        setAccessFlags(accessFlags);
    210    }
    211}