AccessFlags.java 9.0 KB
Newer Older
1
/*
X
xdono 已提交
2
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package com.sun.tools.classfile;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * See JVMS3, sections 4.2, 4.6, 4.7.
 *
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
 *  you write code that depends on this, you do so at your own risk.
 *  This code and its internal interfaces are subject to change or
 *  deletion without notice.</b>
 */
public class AccessFlags {
    public static final int ACC_PUBLIC        = 0x0001; // class, inner, field, method
    public static final int ACC_PRIVATE       = 0x0002; //        inner, field, method
    public static final int ACC_PROTECTED     = 0x0004; //        inner, field, method
    public static final int ACC_STATIC        = 0x0008; //        inner, field, method
    public static final int ACC_FINAL         = 0x0010; // class, inner, field, method
    public static final int ACC_SUPER         = 0x0020; // class
    public static final int ACC_SYNCHRONIZED  = 0x0020; //                      method
    public static final int ACC_VOLATILE      = 0x0040; //               field
    public static final int ACC_BRIDGE        = 0x0040; //                      method
    public static final int ACC_TRANSIENT     = 0x0080; //               field
    public static final int ACC_VARARGS       = 0x0080; //                      method
    public static final int ACC_NATIVE        = 0x0100; //                      method
    public static final int ACC_INTERFACE     = 0x0200; // class, inner
    public static final int ACC_ABSTRACT      = 0x0400; // class, inner,        method
    public static final int ACC_STRICT        = 0x0800; //                      method
    public static final int ACC_SYNTHETIC     = 0x1000; // class, inner, field, method
    public static final int ACC_ANNOTATION    = 0x2000; // class, inner
    public static final int ACC_ENUM          = 0x4000; // class, inner, field
    public static final int ACC_MODULE        = 0x8000; // class, inner, field, method

61
    public static enum Kind { Class, InnerClass, Field, Method};
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    AccessFlags(ClassReader cr) throws IOException {
        this(cr.readUnsignedShort());
    }

    public AccessFlags(int flags) {
        this.flags = flags;
    }

    public AccessFlags ignore(int mask) {
        return new AccessFlags(flags & ~mask);
    }

    public boolean is(int mask) {
        return (flags & mask) != 0;
    }

    private static final int[] classModifiers = {
        ACC_PUBLIC, ACC_FINAL, ACC_ABSTRACT, ACC_MODULE
    };

    private static final int[] classFlags = {
        ACC_PUBLIC, ACC_FINAL, ACC_SUPER, ACC_INTERFACE, ACC_ABSTRACT,
        ACC_SYNTHETIC, ACC_ANNOTATION, ACC_ENUM, ACC_MODULE
    };

    public Set<String> getClassModifiers() {
        int f = ((flags & ACC_INTERFACE) != 0 ? flags & ~ACC_ABSTRACT : flags);
90
        return getModifiers(f, classModifiers, Kind.Class);
91 92 93
    }

    public Set<String> getClassFlags() {
94
        return getFlags(classFlags, Kind.Class);
95 96 97 98 99 100 101 102 103 104 105 106 107 108
    }

    private static final int[] innerClassModifiers = {
        ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL,
        ACC_ABSTRACT, ACC_MODULE
    };

    private static final int[] innerClassFlags = {
        ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL, ACC_SUPER,
        ACC_INTERFACE, ACC_ABSTRACT, ACC_SYNTHETIC, ACC_ANNOTATION, ACC_ENUM, ACC_MODULE
    };

    public Set<String> getInnerClassModifiers() {
        int f = ((flags & ACC_INTERFACE) != 0 ? flags & ~ACC_ABSTRACT : flags);
109
        return getModifiers(f, innerClassModifiers, Kind.InnerClass);
110 111 112
    }

    public Set<String> getInnerClassFlags() {
113
        return getFlags(innerClassFlags, Kind.InnerClass);
114 115 116 117 118 119 120 121 122 123 124 125 126
    }

    private static final int[] fieldModifiers = {
        ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL,
        ACC_VOLATILE, ACC_TRANSIENT, ACC_MODULE
    };

    private static final int[] fieldFlags = {
        ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL,
        ACC_VOLATILE, ACC_TRANSIENT, ACC_SYNTHETIC, ACC_ENUM, ACC_MODULE
    };

    public Set<String> getFieldModifiers() {
127
        return getModifiers(fieldModifiers, Kind.Field);
128 129 130
    }

    public Set<String> getFieldFlags() {
131
        return getFlags(fieldFlags, Kind.Field);
132 133 134 135 136 137 138 139 140 141 142 143 144 145
    }

    private static final int[] methodModifiers = {
        ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL,
        ACC_SYNCHRONIZED, ACC_NATIVE, ACC_ABSTRACT, ACC_STRICT, ACC_MODULE
    };

    private static final int[] methodFlags = {
        ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL,
        ACC_SYNCHRONIZED, ACC_BRIDGE, ACC_VARARGS, ACC_NATIVE, ACC_ABSTRACT,
        ACC_STRICT, ACC_SYNTHETIC, ACC_MODULE
    };

    public Set<String> getMethodModifiers() {
146
        return getModifiers(methodModifiers, Kind.Method);
147 148 149
    }

    public Set<String> getMethodFlags() {
150
        return getFlags(methodFlags, Kind.Method);
151 152
    }

153
    private Set<String> getModifiers(int[] modifierFlags, Kind t) {
154 155 156
        return getModifiers(flags, modifierFlags, t);
    }

157
    private static Set<String> getModifiers(int flags, int[] modifierFlags, Kind t) {
158 159 160 161 162 163 164 165
        Set<String> s = new LinkedHashSet<String>();
        for (int m: modifierFlags) {
            if ((flags & m) != 0)
                s.add(flagToModifier(m, t));
        }
        return s;
    }

166
    private Set<String> getFlags(int[] expectedFlags, Kind t) {
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        Set<String> s = new LinkedHashSet<String>();
        int f = flags;
        for (int e: expectedFlags) {
            if ((f & e) != 0) {
                s.add(flagToName(e, t));
                f = f & ~e;
            }
        }
        while (f != 0) {
            int bit = Integer.highestOneBit(f);
            s.add("0x" + Integer.toHexString(bit));
            f = f & ~bit;
        }
        return s;
    }

183
    private static String flagToModifier(int flag, Kind t) {
184 185 186 187 188 189 190 191 192 193 194 195 196 197
        switch (flag) {
            case ACC_PUBLIC:
                return "public";
            case ACC_PRIVATE:
                return "private";
            case ACC_PROTECTED:
                return "protected";
            case ACC_STATIC:
                return "static";
            case ACC_FINAL:
                return "final";
            case ACC_SYNCHRONIZED:
                return "synchronized";
            case 0x80:
198
                return (t == Kind.Field ? "transient" : null);
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
            case ACC_VOLATILE:
                return "volatile";
            case ACC_NATIVE:
                return "native";
            case ACC_ABSTRACT:
                return "abstract";
            case ACC_STRICT:
                return "strictfp";
            case ACC_MODULE:
                return "module";
            default:
                return null;
        }
    }

214
    private static String flagToName(int flag, Kind t) {
215 216 217 218 219 220 221 222 223 224 225 226
        switch (flag) {
        case ACC_PUBLIC:
            return "ACC_PUBLIC";
        case ACC_PRIVATE:
            return "ACC_PRIVATE";
        case ACC_PROTECTED:
            return "ACC_PROTECTED";
        case ACC_STATIC:
            return "ACC_STATIC";
        case ACC_FINAL:
            return "ACC_FINAL";
        case 0x20:
227
            return (t == Kind.Class ? "ACC_SUPER" : "ACC_SYNCHRONIZED");
228
        case 0x40:
229
            return (t == Kind.Field ? "ACC_VOLATILE" : "ACC_BRIDGE");
230
        case 0x80:
231
            return (t == Kind.Field ? "ACC_TRANSIENT" : "ACC_VARARGS");
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        case ACC_NATIVE:
            return "ACC_NATIVE";
        case ACC_INTERFACE:
            return "ACC_INTERFACE";
        case ACC_ABSTRACT:
            return "ACC_ABSTRACT";
        case ACC_STRICT:
            return "ACC_STRICT";
        case ACC_SYNTHETIC:
            return "ACC_SYNTHETIC";
        case ACC_ANNOTATION:
            return "ACC_ANNOTATION";
        case ACC_ENUM:
            return "ACC_ENUM";
        case ACC_MODULE:
            return "ACC_MODULE";
        default:
            return null;
        }
    }

253
    public final int flags;
254
}