KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > classfile > Access


1 /*
2  * Access.java
3  *
4  * The contents of this file are subject to the terms of the Common Development
5  * and Distribution License (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
9  * or http://www.netbeans.org/cddl.txt.
10  *
11  * When distributing Covered Code, include this CDDL Header Notice in each file
12  * and include the License file at http://www.netbeans.org/cddl.txt.
13  * If applicable, add the following below the CDDL Header, with the fields
14  * enclosed by brackets [] replaced by your own identifying information:
15  * "Portions Copyrighted [year] [name of copyright owner]"
16  *
17  * The Original Software is NetBeans. The Initial Developer of the Original
18  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
19  * Microsystems, Inc. All Rights Reserved.
20  *
21  * Contributor(s): Thomas Ball
22  *
23  * Version: $Revision: 1.3 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 /**
29  * A utility class defining access flags and access utility methods.
30  * Access flags are as defined by the Java Virtual Machine Specification
31  * Second Edition, tables 4.1, 4.4, 4.5 and 4.7.
32  *
33  * @author Thomas Ball
34  */

35 public class Access {
36
37     /** Declared public, may be accessed from outside its package. */
38     public static final int PUBLIC = 0x0001;
39
40     /** Declared private, usable only within the defining class. */
41     public static final int PRIVATE = 0x0002;
42
43     /** Declared protected, may be accessed within subclasses. */
44     public static final int PROTECTED = 0x0004;
45
46     /** Declared static. */
47     public static final int STATIC = 0x0008;
48
49     /**
50      * Declared final. For classes this means no subclassing allowed.
51      * For fields it means no further assignment allowed after initialization.
52      * For methods it means that the method cannot be overridden.
53      */

54     public static final int FINAL = 0x0010;
55
56     /** Declared synchronized; invocation is wrapped in a monitor lock. */
57     public static final int SYNCHRONIZED = 0x0020;
58
59     /**
60      * Treat superclass methods specially when invoked by the
61      * <i>invokespecial</i> instruction. This access only applies to
62      * classes, and shares the same value as SYNCHRONIZED.
63      */

64     public static final int SUPER = 0x0020;
65
66     /** Declared volatile; cannot be cached. */
67     public static final int VOLATILE = 0x0040;
68
69     /** A bridge method, generated by the compiler. */
70     public static final int BRIDGE = 0x0040;
71
72     /**
73      * Declared transient; not written or read by a persistent object
74      * manager
75      */

76     public static final int TRANSIENT = 0x0080;
77
78     /** Declared with a variable number of arguments. */
79     public static final int VARARGS = 0x0080;
80
81     /** Declared native; implemented in a language other than Java. */
82     public static final int NATIVE = 0x0100;
83
84     /** Is an interface, not a class. */
85     public static final int INTERFACE = 0x0200;
86
87     /** Declared abstract; must not be instantiated. */
88     public static final int ABSTRACT = 0x0400;
89
90     /** Declared strictfp; floating point mode is FP-strict. */
91     public static final int STRICT = 0x0800;
92
93     /** Declared synthetic, not present in the source file. */
94     public static final int SYNTHETIC = 0x1000;
95
96     /** Declared as an annotation type. */
97     public static final int ANNOTATION = 0x2000;
98
99     /**
100      * For classes, declared as an enum type. For fields, declared as
101      * an element of an enum.
102      */

103     public static final int ENUM = 0x4000;
104
105     /**
106      * Return a text representation for a given set of access flags.
107      * Here are some examples:
108      * <DL>
109      * <DD><CODE>"public static final"</CODE>,</DD>
110      * <DD><CODE>"package private"</CODE>, or</DD>
111      * <DD><CODE>"protected transient"</CODE>.</DD>
112      * </DL>
113      * Note: only access flags that map to Java modifier keywords are returned.
114      * @param access the mask of flags denoting access permission.
115      * @return a text representation of the access flags.
116      */

117     public static String JavaDoc toString(int access) {
118         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
119         if ((access & PUBLIC) == PUBLIC)
120             sb.append("public "); //NOI18N
121
if ((access & PRIVATE) == PRIVATE)
122             sb.append("private "); //NOI18N
123
if ((access & PROTECTED) == PROTECTED)
124             sb.append("protected "); //NOI18N
125
if ((access & (PUBLIC | PRIVATE | PROTECTED)) == 0)
126             sb.append("package private "); //NOI18N
127
if ((access & STATIC) == STATIC)
128             sb.append("static "); //NOI18N
129
if ((access & FINAL) == FINAL)
130             sb.append("final "); //NOI18N
131
if ((access & SYNCHRONIZED) == SYNCHRONIZED)
132             sb.append("synchronized "); //NOI18N
133
if ((access & VOLATILE) == VOLATILE)
134             sb.append("volatile "); //NOI18N
135
if ((access & TRANSIENT) == TRANSIENT)
136             sb.append("transient "); //NOI18N
137
if ((access & NATIVE) == NATIVE)
138             sb.append("native "); //NOI18N
139
if ((access & ABSTRACT) == ABSTRACT)
140             sb.append("abstract "); //NOI18N
141
if ((access & STRICT) == STRICT)
142             sb.append("strict "); //NOI18N
143

144         // trim trailing space
145
return sb.substring(0, sb.length()-1);
146     }
147
148     public static boolean isStatic(int access) {
149         return ((access & STATIC) == STATIC);
150     }
151
152     public static final boolean isPublic(int access) {
153         return ((access & PUBLIC) == PUBLIC);
154     }
155
156     public static final boolean isProtected(int access) {
157         return ((access & PROTECTED) == PROTECTED);
158     }
159
160     public static final boolean isPackagePrivate(int access) {
161         return ((access & (PUBLIC | PRIVATE | PROTECTED)) == 0);
162     }
163
164     public static final boolean isPrivate(int access) {
165         return ((access & PRIVATE) == PRIVATE);
166     }
167
168     private Access() {
169         // don't allow instantiation
170
}
171 }
172
Popular Tags