• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 package java.lang.reflect;
19 
20 /**
21  * This class provides static methods to decode class and member modifiers.
22  *
23  * @see Class#getModifiers()
24  * @see Member#getModifiers()
25  */
26 public class Modifier {
27 
28     /**
29      * The {@code int} value representing the {@code public} modifier.
30      */
31     public static final int PUBLIC = 0x1;
32 
33     /**
34      * The {@code int} value representing the {@code private} modifier.
35      */
36     public static final int PRIVATE = 0x2;
37 
38     /**
39      * The {@code int} value representing the {@code protected} modifier.
40      */
41     public static final int PROTECTED = 0x4;
42 
43     /**
44      * The {@code int} value representing the {@code static} modifier.
45      */
46     public static final int STATIC = 0x8;
47 
48     /**
49      * The {@code int} value representing the {@code final} modifier.
50      */
51     public static final int FINAL = 0x10;
52 
53     /**
54      * The {@code int} value representing the {@code synchronized} modifier.
55      */
56     public static final int SYNCHRONIZED = 0x20;
57 
58     /**
59      * The {@code int} value representing the {@code volatile} modifier.
60      */
61     public static final int VOLATILE = 0x40;
62 
63     /**
64      * The {@code int} value representing the {@code transient} modifier.
65      */
66     public static final int TRANSIENT = 0x80;
67 
68     /**
69      * The {@code int} value representing the {@code native} modifier.
70      */
71     public static final int NATIVE = 0x100;
72 
73     /**
74      * The {@code int} value representing the {@code interface} modifier.
75      */
76     public static final int INTERFACE = 0x200;
77 
78     /**
79      * The {@code int} value representing the {@code abstract} modifier.
80      */
81     public static final int ABSTRACT = 0x400;
82 
83     /**
84      * The {@code int} value representing the {@code strictfp} modifier.
85      */
86     public static final int STRICT = 0x800;
87 
88     // Non-public types required by Java 5 update to class file format
89     static final int BRIDGE = 0x40;
90 
91     static final int VARARGS = 0x80;
92 
93     /**
94      * @hide
95      */
96     public static final int SYNTHETIC = 0x1000;
97 
98     static final int ANNOTATION = 0x2000;
99 
100     static final int ENUM = 0x4000;
101 
102     /**
103      * Miranda methods are fabrications to reserve virtual method
104      * table slots in abstract classes that implement interfaces
105      * without declaring the abstract methods that the interface would
106      * require they implement.
107      * @hide
108      */
109     public static final int MIRANDA = 0x200000;
110 
111     /**
112      * Dex addition to mark instance constructors and static class
113      * initializer methods.
114      * @hide
115      */
116     public static final int CONSTRUCTOR = 0x10000;
117 
118     /**
119      * Constructs a new {@code Modifier} instance.
120      */
Modifier()121     public Modifier() {
122     }
123 
124     /**
125      * Returns a mask of all the modifiers that may be applied to classes.
126      * @since 1.7
127      */
classModifiers()128     public static int classModifiers() {
129         return PUBLIC | PROTECTED | PRIVATE | ABSTRACT | STATIC | FINAL | STRICT;
130     }
131 
132     /**
133      * Returns a mask of all the modifiers that may be applied to constructors.
134      * @since 1.7
135      */
constructorModifiers()136     public static int constructorModifiers() {
137         return PUBLIC | PROTECTED | PRIVATE;
138     }
139 
140     /**
141      * Returns a mask of all the modifiers that may be applied to fields.
142      * @since 1.7
143      */
fieldModifiers()144     public static int fieldModifiers() {
145         return PUBLIC | PROTECTED | PRIVATE | STATIC | FINAL | TRANSIENT | VOLATILE;
146     }
147 
148     /**
149      * Returns a mask of all the modifiers that may be applied to interfaces.
150      * @since 1.7
151      */
interfaceModifiers()152     public static int interfaceModifiers() {
153         return PUBLIC | PROTECTED | PRIVATE | ABSTRACT | STATIC | STRICT;
154     }
155 
156     /**
157      * Returns a mask of all the modifiers that may be applied to methods.
158      * @since 1.7
159      */
methodModifiers()160     public static int methodModifiers() {
161         return PUBLIC | PROTECTED | PRIVATE | ABSTRACT | STATIC | FINAL | SYNCHRONIZED | NATIVE | STRICT;
162     }
163 
164     /**
165      * Returns true if the given modifiers contain {@link #ABSTRACT}.
166      */
isAbstract(int modifiers)167     public static boolean isAbstract(int modifiers) {
168         return ((modifiers & ABSTRACT) != 0);
169     }
170 
171     /**
172      * Returns true if the given modifiers contain {@link #FINAL}.
173      */
isFinal(int modifiers)174     public static boolean isFinal(int modifiers) {
175         return ((modifiers & FINAL) != 0);
176     }
177 
178     /**
179      * Returns true if the given modifiers contain {@link #INTERFACE}.
180      */
isInterface(int modifiers)181     public static boolean isInterface(int modifiers) {
182         return ((modifiers & INTERFACE) != 0);
183     }
184 
185     /**
186      * Returns true if the given modifiers contain {@link #NATIVE}.
187      */
isNative(int modifiers)188     public static boolean isNative(int modifiers) {
189         return ((modifiers & NATIVE) != 0);
190     }
191 
192     /**
193      * Returns true if the given modifiers contain {@link #PRIVATE}.
194      */
isPrivate(int modifiers)195     public static boolean isPrivate(int modifiers) {
196         return ((modifiers & PRIVATE) != 0);
197     }
198 
199     /**
200      * Returns true if the given modifiers contain {@link #PROTECTED}.
201      */
isProtected(int modifiers)202     public static boolean isProtected(int modifiers) {
203         return ((modifiers & PROTECTED) != 0);
204     }
205 
206     /**
207      * Returns true if the given modifiers contain {@link #PUBLIC}.
208      */
isPublic(int modifiers)209     public static boolean isPublic(int modifiers) {
210         return ((modifiers & PUBLIC) != 0);
211     }
212 
213     /**
214      * Returns true if the given modifiers contain {@link #STATIC}.
215      */
isStatic(int modifiers)216     public static boolean isStatic(int modifiers) {
217         return ((modifiers & STATIC) != 0);
218     }
219 
220     /**
221      * Returns true if the given modifiers contain {@link #STRICT}.
222      */
isStrict(int modifiers)223     public static boolean isStrict(int modifiers) {
224         return ((modifiers & STRICT) != 0);
225     }
226 
227     /**
228      * Returns true if the given modifiers contain {@link #SYNCHRONIZED}.
229      */
isSynchronized(int modifiers)230     public static boolean isSynchronized(int modifiers) {
231         return ((modifiers & SYNCHRONIZED) != 0);
232     }
233 
234     /**
235      * Returns true if the given modifiers contain {@link #TRANSIENT}.
236      */
isTransient(int modifiers)237     public static boolean isTransient(int modifiers) {
238         return ((modifiers & TRANSIENT) != 0);
239     }
240 
241     /**
242      * Returns true if the given modifiers contain {@link #VOLATILE}.
243      */
isVolatile(int modifiers)244     public static boolean isVolatile(int modifiers) {
245         return ((modifiers & VOLATILE) != 0);
246     }
247 
248     /**
249      * Returns true if the given modifiers contain {@link Modifier#CONSTRUCTOR}.
250      * @hide
251      */
isConstructor(int modifiers)252     public static boolean isConstructor(int modifiers) {
253         return ((modifiers & Modifier.CONSTRUCTOR) != 0);
254     }
255 
256     /**
257      * Returns a string containing the string representation of all modifiers
258      * present in the specified modifiers. Modifiers appear in the order
259      * specified by the Java Language Specification.
260      */
toString(int modifiers)261     public static java.lang.String toString(int modifiers) {
262         StringBuilder buf = new StringBuilder();
263         if (isPublic(modifiers)) {
264             buf.append("public ");
265         }
266         if (isProtected(modifiers)) {
267             buf.append("protected ");
268         }
269         if (isPrivate(modifiers)) {
270             buf.append("private ");
271         }
272         if (isAbstract(modifiers)) {
273             buf.append("abstract ");
274         }
275         if (isStatic(modifiers)) {
276             buf.append("static ");
277         }
278         if (isFinal(modifiers)) {
279             buf.append("final ");
280         }
281         if (isTransient(modifiers)) {
282             buf.append("transient ");
283         }
284         if (isVolatile(modifiers)) {
285             buf.append("volatile ");
286         }
287         if (isSynchronized(modifiers)) {
288             buf.append("synchronized ");
289         }
290         if (isNative(modifiers)) {
291             buf.append("native ");
292         }
293         if (isStrict(modifiers)) {
294             buf.append("strictfp ");
295         }
296         if (isInterface(modifiers)) {
297             buf.append("interface ");
298         }
299         if (buf.length() == 0) {
300             return "";
301         }
302         buf.setLength(buf.length() - 1);
303         return buf.toString();
304     }
305 
306     /**
307      * Returns the modifiers for fields that can be present in a declaration.
308      * @hide
309      */
getDeclarationFieldModifiers(int modifiers)310     static String getDeclarationFieldModifiers(int modifiers) {
311         return Modifier.toString(modifiers & fieldModifiers());
312     }
313 
314     /**
315      * Returns the modifiers for methods that can be present in a declaration.
316      * @hide
317      */
getDeclarationMethodModifiers(int modifiers)318     static String getDeclarationMethodModifiers(int modifiers) {
319         return Modifier.toString(modifiers & (
320                 Modifier.isConstructor(modifiers)
321                         ? Modifier.constructorModifiers()
322                         : Modifier.methodModifiers()));
323     }
324 }
325