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