Modifier.java revision 2c87ad3a45cecf9e344487cad1abfdebe79f2c7c
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.  Oracle designates this
9 * particular file as subject to the "Classpath" exception as provided
10 * by Oracle in the LICENSE file that accompanied this code.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 * or visit www.oracle.com if you need additional information or have any
24 * questions.
25 */
26
27package java.lang.reflect;
28
29/**
30 * The Modifier class provides {@code static} methods and
31 * constants to decode class and member access modifiers.  The sets of
32 * modifiers are represented as integers with distinct bit positions
33 * representing different modifiers.  The values for the constants
34 * representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of
35 * <cite>The Java&trade; Virtual Machine Specification</cite>.
36 *
37 * @see Class#getModifiers()
38 * @see Member#getModifiers()
39 *
40 * @author Nakul Saraiya
41 * @author Kenneth Russell
42 */
43public
44class Modifier {
45
46    /**
47     * Return {@code true} if the integer argument includes the
48     * {@code public} modifier, {@code false} otherwise.
49     *
50     * @param   mod a set of modifiers
51     * @return {@code true} if {@code mod} includes the
52     * {@code public} modifier; {@code false} otherwise.
53     */
54    public static boolean isPublic(int mod) {
55        return (mod & PUBLIC) != 0;
56    }
57
58    /**
59     * Return {@code true} if the integer argument includes the
60     * {@code private} modifier, {@code false} otherwise.
61     *
62     * @param   mod a set of modifiers
63     * @return {@code true} if {@code mod} includes the
64     * {@code private} modifier; {@code false} otherwise.
65     */
66    public static boolean isPrivate(int mod) {
67        return (mod & PRIVATE) != 0;
68    }
69
70    /**
71     * Return {@code true} if the integer argument includes the
72     * {@code protected} modifier, {@code false} otherwise.
73     *
74     * @param   mod a set of modifiers
75     * @return {@code true} if {@code mod} includes the
76     * {@code protected} modifier; {@code false} otherwise.
77     */
78    public static boolean isProtected(int mod) {
79        return (mod & PROTECTED) != 0;
80    }
81
82    /**
83     * Return {@code true} if the integer argument includes the
84     * {@code static} modifier, {@code false} otherwise.
85     *
86     * @param   mod a set of modifiers
87     * @return {@code true} if {@code mod} includes the
88     * {@code static} modifier; {@code false} otherwise.
89     */
90    public static boolean isStatic(int mod) {
91        return (mod & STATIC) != 0;
92    }
93
94    /**
95     * Return {@code true} if the integer argument includes the
96     * {@code final} modifier, {@code false} otherwise.
97     *
98     * @param   mod a set of modifiers
99     * @return {@code true} if {@code mod} includes the
100     * {@code final} modifier; {@code false} otherwise.
101     */
102    public static boolean isFinal(int mod) {
103        return (mod & FINAL) != 0;
104    }
105
106    /**
107     * Return {@code true} if the integer argument includes the
108     * {@code synchronized} modifier, {@code false} otherwise.
109     *
110     * @param   mod a set of modifiers
111     * @return {@code true} if {@code mod} includes the
112     * {@code synchronized} modifier; {@code false} otherwise.
113     */
114    public static boolean isSynchronized(int mod) {
115        return (mod & SYNCHRONIZED) != 0;
116    }
117
118    /**
119     * Return {@code true} if the integer argument includes the
120     * {@code volatile} modifier, {@code false} otherwise.
121     *
122     * @param   mod a set of modifiers
123     * @return {@code true} if {@code mod} includes the
124     * {@code volatile} modifier; {@code false} otherwise.
125     */
126    public static boolean isVolatile(int mod) {
127        return (mod & VOLATILE) != 0;
128    }
129
130    /**
131     * Returns true if the given modifiers contain {@link Modifier#CONSTRUCTOR}.
132     * @hide
133     */
134    public static boolean isConstructor(int modifiers) {
135        return ((modifiers & Modifier.CONSTRUCTOR) != 0);
136    }
137
138    /**
139     * Return {@code true} if the integer argument includes the
140     * {@code transient} modifier, {@code false} otherwise.
141     *
142     * @param   mod a set of modifiers
143     * @return {@code true} if {@code mod} includes the
144     * {@code transient} modifier; {@code false} otherwise.
145     */
146    public static boolean isTransient(int mod) {
147        return (mod & TRANSIENT) != 0;
148    }
149
150    /**
151     * Return {@code true} if the integer argument includes the
152     * {@code native} modifier, {@code false} otherwise.
153     *
154     * @param   mod a set of modifiers
155     * @return {@code true} if {@code mod} includes the
156     * {@code native} modifier; {@code false} otherwise.
157     */
158    public static boolean isNative(int mod) {
159        return (mod & NATIVE) != 0;
160    }
161
162    /**
163     * Return {@code true} if the integer argument includes the
164     * {@code interface} modifier, {@code false} otherwise.
165     *
166     * @param   mod a set of modifiers
167     * @return {@code true} if {@code mod} includes the
168     * {@code interface} modifier; {@code false} otherwise.
169     */
170    public static boolean isInterface(int mod) {
171        return (mod & INTERFACE) != 0;
172    }
173
174    /**
175     * Return {@code true} if the integer argument includes the
176     * {@code abstract} modifier, {@code false} otherwise.
177     *
178     * @param   mod a set of modifiers
179     * @return {@code true} if {@code mod} includes the
180     * {@code abstract} modifier; {@code false} otherwise.
181     */
182    public static boolean isAbstract(int mod) {
183        return (mod & ABSTRACT) != 0;
184    }
185
186    /**
187     * Return {@code true} if the integer argument includes the
188     * {@code strictfp} modifier, {@code false} otherwise.
189     *
190     * @param   mod a set of modifiers
191     * @return {@code true} if {@code mod} includes the
192     * {@code strictfp} modifier; {@code false} otherwise.
193     */
194    public static boolean isStrict(int mod) {
195        return (mod & STRICT) != 0;
196    }
197
198    /**
199     * Return a string describing the access modifier flags in
200     * the specified modifier. For example:
201     * <blockquote><pre>
202     *    public final synchronized strictfp
203     * </pre></blockquote>
204     * The modifier names are returned in an order consistent with the
205     * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
206     * <cite>The Java&trade; Language Specification</cite>.
207     * The full modifier ordering used by this method is:
208     * <blockquote> {@code
209     * public protected private abstract static final transient
210     * volatile synchronized native strictfp
211     * interface } </blockquote>
212     * The {@code interface} modifier discussed in this class is
213     * not a true modifier in the Java language and it appears after
214     * all other modifiers listed by this method.  This method may
215     * return a string of modifiers that are not valid modifiers of a
216     * Java entity; in other words, no checking is done on the
217     * possible validity of the combination of modifiers represented
218     * by the input.
219     *
220     * Note that to perform such checking for a known kind of entity,
221     * such as a constructor or method, first AND the argument of
222     * {@code toString} with the appropriate mask from a method like
223     * {@link #constructorModifiers} or {@link #methodModifiers}.
224     *
225     * @param   mod a set of modifiers
226     * @return  a string representation of the set of modifiers
227     * represented by {@code mod}
228     */
229    public static String toString(int mod) {
230        StringBuffer sb = new StringBuffer();
231        int len;
232
233        if ((mod & PUBLIC) != 0)        sb.append("public ");
234        if ((mod & PROTECTED) != 0)     sb.append("protected ");
235        if ((mod & PRIVATE) != 0)       sb.append("private ");
236
237        /* Canonical order */
238        if ((mod & ABSTRACT) != 0)      sb.append("abstract ");
239        if ((mod & STATIC) != 0)        sb.append("static ");
240        if ((mod & FINAL) != 0)         sb.append("final ");
241        if ((mod & TRANSIENT) != 0)     sb.append("transient ");
242        if ((mod & VOLATILE) != 0)      sb.append("volatile ");
243        if ((mod & SYNCHRONIZED) != 0)  sb.append("synchronized ");
244        if ((mod & NATIVE) != 0)        sb.append("native ");
245        if ((mod & STRICT) != 0)        sb.append("strictfp ");
246        if ((mod & INTERFACE) != 0)     sb.append("interface ");
247
248        if ((len = sb.length()) > 0)    /* trim trailing space */
249            return sb.toString().substring(0, len-1);
250        return "";
251    }
252
253    /*
254     * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
255     * <cite>The Java&trade; Virtual Machine Specification</cite>
256     */
257
258    /**
259     * The {@code int} value representing the {@code public}
260     * modifier.
261     */
262    public static final int PUBLIC           = 0x00000001;
263
264    /**
265     * The {@code int} value representing the {@code private}
266     * modifier.
267     */
268    public static final int PRIVATE          = 0x00000002;
269
270    /**
271     * The {@code int} value representing the {@code protected}
272     * modifier.
273     */
274    public static final int PROTECTED        = 0x00000004;
275
276    /**
277     * The {@code int} value representing the {@code static}
278     * modifier.
279     */
280    public static final int STATIC           = 0x00000008;
281
282    /**
283     * The {@code int} value representing the {@code final}
284     * modifier.
285     */
286    public static final int FINAL            = 0x00000010;
287
288    /**
289     * The {@code int} value representing the {@code synchronized}
290     * modifier.
291     */
292    public static final int SYNCHRONIZED     = 0x00000020;
293
294    /**
295     * The {@code int} value representing the {@code volatile}
296     * modifier.
297     */
298    public static final int VOLATILE         = 0x00000040;
299
300    /**
301     * The {@code int} value representing the {@code transient}
302     * modifier.
303     */
304    public static final int TRANSIENT        = 0x00000080;
305
306    /**
307     * The {@code int} value representing the {@code native}
308     * modifier.
309     */
310    public static final int NATIVE           = 0x00000100;
311
312    /**
313     * The {@code int} value representing the {@code interface}
314     * modifier.
315     */
316    public static final int INTERFACE        = 0x00000200;
317
318    /**
319     * The {@code int} value representing the {@code abstract}
320     * modifier.
321     */
322    public static final int ABSTRACT         = 0x00000400;
323
324    /**
325     * The {@code int} value representing the {@code strictfp}
326     * modifier.
327     */
328    public static final int STRICT           = 0x00000800;
329
330    // Bits not (yet) exposed in the public API either because they
331    // have different meanings for fields and methods and there is no
332    // way to distinguish between the two in this class, or because
333    // they are not Java programming language keywords
334    static final int BRIDGE    = 0x00000040;
335    static final int VARARGS   = 0x00000080;
336    /**
337     * @hide
338     */
339    public static final int SYNTHETIC = 0x00001000;
340    static final int ANNOTATION= 0x00002000;
341    static final int ENUM      = 0x00004000;
342    static boolean isSynthetic(int mod) {
343      return (mod & SYNTHETIC) != 0;
344    }
345
346    /**
347     * Miranda methods are fabrications to reserve virtual method
348     * table slots in abstract classes that implement interfaces
349     * without declaring the abstract methods that the interface would
350     * require they implement.
351     * @hide
352     */
353    public static final int MIRANDA = 0x8000;
354    /**
355     * Dex addition to mark instance constructors and static class
356     * initializer methods.
357     * @hide
358     */
359    public static final int CONSTRUCTOR = 0x10000;
360
361    /**
362     * See JLSv3 section 8.1.1.
363     */
364    private static final int CLASS_MODIFIERS =
365        Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
366        Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
367        Modifier.STRICT;
368
369    /**
370     * See JLSv3 section 9.1.1.
371     */
372    private static final int INTERFACE_MODIFIERS =
373        Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
374        Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.STRICT;
375
376
377    /**
378     * See JLSv3 section 8.8.3.
379     */
380    private static final int CONSTRUCTOR_MODIFIERS =
381        Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE;
382
383    /**
384     * See JLSv3 section 8.4.3.
385     */
386    private static final int METHOD_MODIFIERS =
387        Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
388        Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
389        Modifier.SYNCHRONIZED   | Modifier.NATIVE       | Modifier.STRICT;
390
391    /**
392     * See JLSv3 section 8.3.1.
393     */
394    private static final int FIELD_MODIFIERS =
395        Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
396        Modifier.STATIC         | Modifier.FINAL        | Modifier.TRANSIENT |
397        Modifier.VOLATILE;
398
399    /**
400     * Return an {@code int} value OR-ing together the source language
401     * modifiers that can be applied to a class.
402     * @return an {@code int} value OR-ing together the source language
403     * modifiers that can be applied to a class.
404     *
405     * @jls 8.1.1 Class Modifiers
406     * @since 1.7
407     */
408    public static int classModifiers() {
409        return CLASS_MODIFIERS;
410    }
411
412    /**
413     * Return an {@code int} value OR-ing together the source language
414     * modifiers that can be applied to an interface.
415     * @return an {@code int} value OR-ing together the source language
416     * modifiers that can be applied to an inteface.
417     *
418     * @jls 9.1.1 Interface Modifiers
419     * @since 1.7
420     */
421    public static int interfaceModifiers() {
422        return INTERFACE_MODIFIERS;
423    }
424
425    /**
426     * Return an {@code int} value OR-ing together the source language
427     * modifiers that can be applied to a constructor.
428     * @return an {@code int} value OR-ing together the source language
429     * modifiers that can be applied to a constructor.
430     *
431     * @jls 8.8.3 Constructor Modifiers
432     * @since 1.7
433     */
434    public static int constructorModifiers() {
435        return CONSTRUCTOR_MODIFIERS;
436    }
437
438    /**
439     * Return an {@code int} value OR-ing together the source language
440     * modifiers that can be applied to a method.
441     * @return an {@code int} value OR-ing together the source language
442     * modifiers that can be applied to a method.
443     *
444     * @jls 8.4.3 Method Modifiers
445     * @since 1.7
446     */
447    public static int methodModifiers() {
448        return METHOD_MODIFIERS;
449    }
450
451
452    /**
453     * Return an {@code int} value OR-ing together the source language
454     * modifiers that can be applied to a field.
455     * @return an {@code int} value OR-ing together the source language
456     * modifiers that can be applied to a field.
457     *
458     * @jls 8.3.1 Field Modifiers
459     * @since 1.7
460     */
461    public static int fieldModifiers() {
462        return FIELD_MODIFIERS;
463    }
464}
465