• 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}
30      * modifier.
31      */
32     public static final int PUBLIC = 0x1;
33 
34     /**
35      * The {@code int} value representing the {@code private}
36      * modifier.
37      */
38     public static final int PRIVATE = 0x2;
39 
40     /**
41      * The {@code int} value representing the {@code protected}
42      * modifier.
43      */
44     public static final int PROTECTED = 0x4;
45 
46     /**
47      * The {@code int} value representing the {@code static} modifier.
48      */
49     public static final int STATIC = 0x8;
50 
51     /**
52      * The {@code int} value representing the {@code final} modifier.
53      */
54     public static final int FINAL = 0x10;
55 
56     /**
57      * The {@code int} value representing the {@code synchronized}
58      * modifier.
59      */
60     public static final int SYNCHRONIZED = 0x20;
61 
62     /**
63      * The {@code int} value representing the {@code volatile}
64      * modifier.
65      */
66     public static final int VOLATILE = 0x40;
67 
68     /**
69      * The {@code int} value representing the {@code transient}
70      * modifier.
71      */
72     public static final int TRANSIENT = 0x80;
73 
74     /**
75      * The {@code int} value representing the {@code native} modifier.
76      */
77     public static final int NATIVE = 0x100;
78 
79     /**
80      * The {@code int} value representing the {@code interface}
81      * modifier.
82      */
83     public static final int INTERFACE = 0x200;
84 
85     /**
86      * The {@code int} value representing the {@code abstract}
87      * modifier.
88      */
89     public static final int ABSTRACT = 0x400;
90 
91     /**
92      * The {@code int} value representing the {@code strict} modifier.
93      */
94     public static final int STRICT = 0x800;
95 
96     // Non-public types required by Java 5 update to class file format
97     static final int BRIDGE = 0x40;
98 
99     static final int VARARGS = 0x80;
100 
101     static final int SYNTHETIC = 0x1000;
102 
103     static final int ANNOTATION = 0x2000;
104 
105     static final int ENUM = 0x4000;
106 
107     /**
108      * Constructs a new {@code Modifier} instance.
109      */
Modifier()110     public Modifier() {
111     }
112 
113     /**
114      * Returns a mask of all the modifiers that may be applied to classes.
115      * @since 1.7
116      * @hide 1.7
117      */
classModifiers()118     public static int classModifiers() {
119         return PUBLIC | PROTECTED | PRIVATE | ABSTRACT | STATIC | FINAL | STRICT;
120     }
121 
122     /**
123      * Returns a mask of all the modifiers that may be applied to constructors.
124      * @since 1.7
125      * @hide 1.7
126      */
constructorModifiers()127     public static int constructorModifiers() {
128         return PUBLIC | PROTECTED | PRIVATE;
129     }
130 
131     /**
132      * Returns a mask of all the modifiers that may be applied to fields.
133      * @since 1.7
134      * @hide 1.7
135      */
fieldModifiers()136     public static int fieldModifiers() {
137         return PUBLIC | PROTECTED | PRIVATE | STATIC | FINAL | TRANSIENT | VOLATILE;
138     }
139 
140     /**
141      * Returns a mask of all the modifiers that may be applied to interfaces.
142      * @since 1.7
143      * @hide 1.7
144      */
interfaceModifiers()145     public static int interfaceModifiers() {
146         return PUBLIC | PROTECTED | PRIVATE | ABSTRACT | STATIC | STRICT;
147     }
148 
149     /**
150      * Returns a mask of all the modifiers that may be applied to methods.
151      * @since 1.7
152      * @hide 1.7
153      */
methodModifiers()154     public static int methodModifiers() {
155         return PUBLIC | PROTECTED | PRIVATE | ABSTRACT | STATIC | FINAL | SYNCHRONIZED | NATIVE | STRICT;
156     }
157 
158     /**
159      * Indicates whether or not the specified modifiers contain the {@code
160      * abstract} modifier.
161      *
162      * @param modifiers
163      *            the modifiers to test
164      * @return {@code true} if the specified modifiers contain the {@code
165      *         abstract} modifier, {@code false} otherwise
166      */
isAbstract(int modifiers)167     public static boolean isAbstract(int modifiers) {
168         return ((modifiers & ABSTRACT) != 0);
169     }
170 
171     /**
172      * Indicates whether or not the specified modifiers contain the {@code
173      * final} modifier.
174      *
175      * @param modifiers
176      *            the modifiers to test
177      * @return {@code true} if the specified modifiers contain the {@code
178      *         final} modifier, {@code false} otherwise
179      */
isFinal(int modifiers)180     public static boolean isFinal(int modifiers) {
181         return ((modifiers & FINAL) != 0);
182     }
183 
184     /**
185      * Indicates whether or not the specified modifiers contain the {@code
186      * interface} modifier.
187      *
188      * @param modifiers
189      *            the modifiers to test
190      * @return {@code true} if the specified modifiers contain the {@code
191      *         interface} modifier, {@code false} otherwise
192      */
isInterface(int modifiers)193     public static boolean isInterface(int modifiers) {
194         return ((modifiers & INTERFACE) != 0);
195     }
196 
197     /**
198      * Indicates whether or not the specified modifiers contain the {@code
199      * native} modifier.
200      *
201      * @param modifiers
202      *            the modifiers to test
203      * @return {@code true} if the specified modifiers contain the {@code
204      *         native} modifier, {@code false} otherwise
205      */
isNative(int modifiers)206     public static boolean isNative(int modifiers) {
207         return ((modifiers & NATIVE) != 0);
208     }
209 
210     /**
211      * Indicates whether or not the specified modifiers contain the {@code
212      * private} modifier.
213      *
214      * @param modifiers
215      *            the modifiers to test
216      * @return {@code true} if the specified modifiers contain the {@code
217      *         private} modifier, {@code false} otherwise
218      */
isPrivate(int modifiers)219     public static boolean isPrivate(int modifiers) {
220         return ((modifiers & PRIVATE) != 0);
221     }
222 
223     /**
224      * Indicates whether or not the specified modifiers contain the {@code
225      * protected} modifier.
226      *
227      * @param modifiers
228      *            the modifiers to test
229      * @return {@code true} if the specified modifiers contain the {@code
230      *         protected} modifier, {@code false} otherwise
231      */
isProtected(int modifiers)232     public static boolean isProtected(int modifiers) {
233         return ((modifiers & PROTECTED) != 0);
234     }
235 
236     /**
237      * Indicates whether or not the specified modifiers contain the {@code
238      * public} modifier.
239      *
240      * @param modifiers
241      *            the modifiers to test
242      * @return {@code true} if the specified modifiers contain the {@code
243      *         public} modifier, {@code false} otherwise
244      */
isPublic(int modifiers)245     public static boolean isPublic(int modifiers) {
246         return ((modifiers & PUBLIC) != 0);
247     }
248 
249     /**
250      * Indicates whether or not the specified modifiers contain the {@code
251      * static} modifier.
252      *
253      * @param modifiers
254      *            the modifiers to test
255      * @return {@code true} if the specified modifiers contain the {@code
256      *         static} modifier, {@code false} otherwise
257      */
isStatic(int modifiers)258     public static boolean isStatic(int modifiers) {
259         return ((modifiers & STATIC) != 0);
260     }
261 
262     /**
263      * Indicates whether or not the specified modifiers contain the {@code
264      * strict} modifier.
265      *
266      * @param modifiers
267      *            the modifiers to test
268      * @return {@code true} if the specified modifiers contain the {@code
269      *         strict} modifier, {@code false} otherwise
270      */
isStrict(int modifiers)271     public static boolean isStrict(int modifiers) {
272         return ((modifiers & STRICT) != 0);
273     }
274 
275     /**
276      * Indicates whether or not the specified modifiers contain the {@code
277      * synchronized} modifier.
278      *
279      * @param modifiers
280      *            the modifiers to test
281      * @return {@code true} if the specified modifiers contain the {@code
282      *         synchronized} modifier, {@code false} otherwise
283      */
isSynchronized(int modifiers)284     public static boolean isSynchronized(int modifiers) {
285         return ((modifiers & SYNCHRONIZED) != 0);
286     }
287 
288     /**
289      * Indicates whether or not the specified modifiers contain the {@code
290      * transient} modifier.
291      *
292      * @param modifiers
293      *            the modifiers to test
294      * @return {@code true} if the specified modifiers contain the {@code
295      *         transient} modifier, {@code false} otherwise
296      */
isTransient(int modifiers)297     public static boolean isTransient(int modifiers) {
298         return ((modifiers & TRANSIENT) != 0);
299     }
300 
301     /**
302      * Indicates whether or not the specified modifiers contain the {@code
303      * volatile} modifier.
304      *
305      * @param modifiers
306      *            the modifiers to test
307      * @return {@code true} if the specified modifiers contain the {@code
308      *         volatile} modifier, {@code false} otherwise
309      */
isVolatile(int modifiers)310     public static boolean isVolatile(int modifiers) {
311         return ((modifiers & VOLATILE) != 0);
312     }
313 
314     /**
315      * Returns a string containing the string representation of all modifiers
316      * present in the specified modifiers. Modifiers appear in the order
317      * specified by the Java Language Specification:
318      *
319      * {@code public private protected abstract static final transient volatile native synchronized interface strict}
320      *
321      * @param modifiers
322      *            the modifiers to print
323      * @return a printable representation of the modifiers
324      */
toString(int modifiers)325     public static java.lang.String toString(int modifiers) {
326         StringBuilder buf = new StringBuilder();
327 
328         if (isPublic(modifiers)) {
329             buf.append("public ");
330         }
331         if (isProtected(modifiers)) {
332             buf.append("protected ");
333         }
334         if (isPrivate(modifiers)) {
335             buf.append("private ");
336         }
337         if (isAbstract(modifiers)) {
338             buf.append("abstract ");
339         }
340         if (isStatic(modifiers)) {
341             buf.append("static ");
342         }
343         if (isFinal(modifiers)) {
344             buf.append("final ");
345         }
346         if (isTransient(modifiers)) {
347             buf.append("transient ");
348         }
349         if (isVolatile(modifiers)) {
350             buf.append("volatile ");
351         }
352         if (isSynchronized(modifiers)) {
353             buf.append("synchronized ");
354         }
355         if (isNative(modifiers)) {
356             buf.append("native ");
357         }
358         if (isStrict(modifiers)) {
359             buf.append("strictfp ");
360         }
361         if (isInterface(modifiers)) {
362             buf.append("interface ");
363         }
364         if (buf.length() == 0) {
365             return "";
366         }
367         buf.setLength(buf.length() - 1);
368         return buf.toString();
369     }
370 }
371