AccessFlags.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.classfile;

  18. import org.apache.bcel.Const;

  19. /**
  20.  * Super class for all objects that have modifiers like private, final, ... I.e. classes, fields, and methods.
  21.  */
  22. public abstract class AccessFlags {

  23.     /**
  24.      * Access flags.
  25.      *
  26.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter.
  27.      */
  28.     @java.lang.Deprecated
  29.     protected int access_flags; // TODO not used externally at present

  30.     /**
  31.      * Constructs a new instance.
  32.      */
  33.     public AccessFlags() {
  34.     }

  35.     /**
  36.      * Constructs a new instance.
  37.      *
  38.      * @param accessFlags initial access flags.
  39.      */
  40.     public AccessFlags(final int accessFlags) {
  41.         access_flags = accessFlags;
  42.     }

  43.     /**
  44.      * Gets access flags.
  45.      *
  46.      * @return Access flags of the object aka. "modifiers".
  47.      */
  48.     public final int getAccessFlags() {
  49.         return access_flags;
  50.     }

  51.     /**
  52.      * Gets access flags.
  53.      *
  54.      * @return Access flags of the object also known as modifiers.
  55.      */
  56.     public final int getModifiers() {
  57.         return access_flags;
  58.     }

  59.     /**
  60.      * Tests whether the abstract bit is on.
  61.      *
  62.      * @return whether the abstract bit is on.
  63.      */
  64.     public final boolean isAbstract() {
  65.         return test(Const.ACC_ABSTRACT);
  66.     }

  67.     /**
  68.      * Sets the abstract bit.
  69.      *
  70.      * @param flag The new value.
  71.      */
  72.     public final void isAbstract(final boolean flag) {
  73.         setFlag(Const.ACC_ABSTRACT, flag);
  74.     }

  75.     /**
  76.      * Tests whether the annotation bit is on.
  77.      *
  78.      * @return whether the annotation bit is on.
  79.      */
  80.     public final boolean isAnnotation() {
  81.         return test(Const.ACC_ANNOTATION);
  82.     }

  83.     /**
  84.      * Sets the annotation bit.
  85.      *
  86.      * @param flag The new value.
  87.      */
  88.     public final void isAnnotation(final boolean flag) {
  89.         setFlag(Const.ACC_ANNOTATION, flag);
  90.     }
  91.     /**
  92.      * Tests whether the enum bit is on.
  93.      *
  94.      * @return whether the enum bit is on.
  95.      */
  96.     public final boolean isEnum() {
  97.         return test(Const.ACC_ENUM);
  98.     }

  99.     /**
  100.      * Sets the enum bit.
  101.      *
  102.      * @param flag The new value.
  103.      */
  104.     public final void isEnum(final boolean flag) {
  105.         setFlag(Const.ACC_ENUM, flag);
  106.     }

  107.     /**
  108.      * Tests whether the final bit is on.
  109.      *
  110.      * @return whether the final bit is on.
  111.      */
  112.     public final boolean isFinal() {
  113.         return test(Const.ACC_FINAL);
  114.     }

  115.     /**
  116.      * Sets the final bit.
  117.      *
  118.      * @param flag The new value.
  119.      */
  120.     public final void isFinal(final boolean flag) {
  121.         setFlag(Const.ACC_FINAL, flag);
  122.     }

  123.     /**
  124.      * Tests whether the interface bit is on.
  125.      *
  126.      * @return whether the interface bit is on.
  127.      */
  128.     public final boolean isInterface() {
  129.         return test(Const.ACC_INTERFACE);
  130.     }

  131.     /**
  132.      * Sets the interface bit.
  133.      *
  134.      * @param flag The new value.
  135.      */
  136.     public final void isInterface(final boolean flag) {
  137.         setFlag(Const.ACC_INTERFACE, flag);
  138.     }

  139.     /**
  140.      * Tests whether the native bit is on.
  141.      *
  142.      * @return whether the native bit is on.
  143.      */
  144.     public final boolean isNative() {
  145.         return test(Const.ACC_NATIVE);
  146.     }

  147.     /**
  148.      * Sets the native bit.
  149.      *
  150.      * @param flag The new value.
  151.      */
  152.     public final void isNative(final boolean flag) {
  153.         setFlag(Const.ACC_NATIVE, flag);
  154.     }

  155.     /**
  156.      * Tests whether the private bit is on.
  157.      *
  158.      * @return whether the private bit is on.
  159.      */
  160.     public final boolean isPrivate() {
  161.         return test(Const.ACC_PRIVATE);
  162.     }

  163.     /**
  164.      * Sets the private bit.
  165.      *
  166.      * @param flag The new value.
  167.      */
  168.     public final void isPrivate(final boolean flag) {
  169.         setFlag(Const.ACC_PRIVATE, flag);
  170.     }

  171.     /**
  172.      * Tests whether the protected bit is on.
  173.      *
  174.      * @return whether the protected bit is on.
  175.      */
  176.     public final boolean isProtected() {
  177.         return test(Const.ACC_PROTECTED);
  178.     }

  179.     /**
  180.      * Sets the protected bit.
  181.      *
  182.      * @param flag The new value.
  183.      */
  184.     public final void isProtected(final boolean flag) {
  185.         setFlag(Const.ACC_PROTECTED, flag);
  186.     }

  187.     /**
  188.      * Tests whether the public bit is on.
  189.      *
  190.      * @return whether the public bit is on.
  191.      */
  192.     public final boolean isPublic() {
  193.         return test(Const.ACC_PUBLIC);
  194.     }

  195.     /**
  196.      * Sets the public bit.
  197.      *
  198.      * @param flag The new value.
  199.      */
  200.     public final void isPublic(final boolean flag) {
  201.         setFlag(Const.ACC_PUBLIC, flag);
  202.     }

  203.     /**
  204.      * Tests whether the static bit is on.
  205.      *
  206.      * @return whether the static bit is on.
  207.      */
  208.     public final boolean isStatic() {
  209.         return test(Const.ACC_STATIC);
  210.     }

  211.     /**
  212.      * Sets the static bit.
  213.      *
  214.      * @param flag The new value.
  215.      */
  216.     public final void isStatic(final boolean flag) {
  217.         setFlag(Const.ACC_STATIC, flag);
  218.     }

  219.     /**
  220.      * Tests whether the strict bit is on.
  221.      *
  222.      * @return whether the strict bit is on.
  223.      */
  224.     public final boolean isStrictfp() {
  225.         return test(Const.ACC_STRICT);
  226.     }

  227.     /**
  228.      * Sets the strict bit.
  229.      *
  230.      * @param flag The new value.
  231.      */
  232.     public final void isStrictfp(final boolean flag) {
  233.         setFlag(Const.ACC_STRICT, flag);
  234.     }

  235.     /**
  236.      * Tests whether the synchronized bit is on.
  237.      *
  238.      * @return whether the synchronized bit is on.
  239.      */
  240.     public final boolean isSynchronized() {
  241.         return test(Const.ACC_SYNCHRONIZED);
  242.     }

  243.     /**
  244.      * Sets the synchronized bit.
  245.      *
  246.      * @param flag The new value.
  247.      */
  248.     public final void isSynchronized(final boolean flag) {
  249.         setFlag(Const.ACC_SYNCHRONIZED, flag);
  250.     }

  251.     /**
  252.      * Tests whether the synthetic bit is on.
  253.      *
  254.      * @return whether the synthetic bit is on.
  255.      */
  256.     public final boolean isSynthetic() {
  257.         return test(Const.ACC_SYNTHETIC);
  258.     }

  259.     /**
  260.      * Sets the synthetic bit.
  261.      *
  262.      * @param flag The new value.
  263.      */
  264.     public final void isSynthetic(final boolean flag) {
  265.         setFlag(Const.ACC_SYNTHETIC, flag);
  266.     }

  267.     /**
  268.      * Tests whether the transient bit is on.
  269.      *
  270.      * @return whether the varargs bit is on.
  271.      */
  272.     public final boolean isTransient() {
  273.         return test(Const.ACC_TRANSIENT);
  274.     }

  275.     /**
  276.      * Sets the varargs bit.
  277.      *
  278.      * @param flag The new value.
  279.      */
  280.     public final void isTransient(final boolean flag) {
  281.         setFlag(Const.ACC_TRANSIENT, flag);
  282.     }

  283.     /**
  284.      * Tests whether the varargs bit is on.
  285.      *
  286.      * @return whether the varargs bit is on.
  287.      */
  288.     public final boolean isVarArgs() {
  289.         return test(Const.ACC_VARARGS);
  290.     }

  291.     /**
  292.      * Sets the varargs bit.
  293.      *
  294.      * @param flag The new value.
  295.      */
  296.     public final void isVarArgs(final boolean flag) {
  297.         setFlag(Const.ACC_VARARGS, flag);
  298.     }

  299.     /**
  300.      * Tests whether the volatile bit is on.
  301.      *
  302.      * @return whether the volatile bit is on.
  303.      */
  304.     public final boolean isVolatile() {
  305.         return test(Const.ACC_VOLATILE);
  306.     }

  307.     /**
  308.      * Sets the volatile bit.
  309.      *
  310.      * @param flag The new value.
  311.      */
  312.     public final void isVolatile(final boolean flag) {
  313.         setFlag(Const.ACC_VOLATILE, flag);
  314.     }

  315.     /**
  316.      * Sets access flags also known as modifiers.
  317.      *
  318.      * @param accessFlags Access flags of the object.
  319.      */
  320.     public final void setAccessFlags(final int accessFlags) {
  321.         this.access_flags = accessFlags;
  322.     }

  323.     private void setFlag(final int flag, final boolean set) {
  324.         if ((access_flags & flag) != 0) { // Flag is set already
  325.             if (!set) {
  326.                 access_flags ^= flag;
  327.             }
  328.         } else if (set) {
  329.             access_flags |= flag;
  330.         }
  331.     }

  332.     /**
  333.      * Sets access flags aka "modifiers".
  334.      *
  335.      * @param accessFlags Access flags of the object.
  336.      */
  337.     public final void setModifiers(final int accessFlags) {
  338.         setAccessFlags(accessFlags);
  339.     }

  340.     /**
  341.      * Tests whether the bit is on.
  342.      *
  343.      * @param test the bit to test.
  344.      * @return whether the bit is on.
  345.      */
  346.     private boolean test(final short test) {
  347.         return (access_flags & test) != 0;
  348.     }
  349. }