1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package jdk.vm.ci.meta;
24
25import static java.lang.reflect.Modifier.PRIVATE;
26import static java.lang.reflect.Modifier.PROTECTED;
27import static java.lang.reflect.Modifier.PUBLIC;
28
29import java.lang.reflect.Modifier;
30
31/**
32 * A Java element (i.e., a class, interface, field or method) that is described by a set of Java
33 * language {@linkplain #getModifiers() modifiers}.
34 */
35public interface ModifiersProvider {
36
37    /**
38     * Returns the modifiers for this element.
39     */
40    int getModifiers();
41
42    /**
43     * @see Modifier#isInterface(int)
44     */
45    default boolean isInterface() {
46        return Modifier.isInterface(getModifiers());
47    }
48
49    /**
50     * @see Modifier#isSynchronized(int)
51     */
52    default boolean isSynchronized() {
53        return Modifier.isSynchronized(getModifiers());
54    }
55
56    /**
57     * @see Modifier#isStatic(int)
58     */
59    default boolean isStatic() {
60        return Modifier.isStatic(getModifiers());
61    }
62
63    /**
64     * The setting of the final modifier bit for types is somewhat confusing, so don't export
65     * isFinal by default. Subclasses like {@link ResolvedJavaField} and {@link ResolvedJavaMethod}
66     * can export it as isFinal, but {@link ResolvedJavaType} can provide a more sensible equivalent
67     * like {@link ResolvedJavaType#isLeaf}.
68     *
69     * @see Modifier#isFinal(int)
70     */
71    default boolean isFinalFlagSet() {
72        return Modifier.isFinal(getModifiers());
73    }
74
75    /**
76     * @see Modifier#isPublic(int)
77     */
78    default boolean isPublic() {
79        return Modifier.isPublic(getModifiers());
80    }
81
82    /**
83     * Determines if this element is neither {@linkplain #isPublic() public},
84     * {@linkplain #isProtected() protected} nor {@linkplain #isPrivate() private}.
85     */
86    default boolean isPackagePrivate() {
87        return ((PUBLIC | PROTECTED | PRIVATE) & getModifiers()) == 0;
88    }
89
90    /**
91     * @see Modifier#isPrivate(int)
92     */
93    default boolean isPrivate() {
94        return Modifier.isPrivate(getModifiers());
95    }
96
97    /**
98     * @see Modifier#isProtected(int)
99     */
100    default boolean isProtected() {
101        return Modifier.isProtected(getModifiers());
102    }
103
104    /**
105     * @see Modifier#isTransient(int)
106     */
107    default boolean isTransient() {
108        return Modifier.isTransient(getModifiers());
109    }
110
111    /**
112     * @see Modifier#isStrict(int)
113     */
114    default boolean isStrict() {
115        return Modifier.isStrict(getModifiers());
116    }
117
118    /**
119     * @see Modifier#isVolatile(int)
120     */
121    default boolean isVolatile() {
122        return Modifier.isVolatile(getModifiers());
123    }
124
125    /**
126     * @see Modifier#isNative(int)
127     */
128    default boolean isNative() {
129        return Modifier.isNative(getModifiers());
130    }
131
132    /**
133     * @see Modifier#isAbstract(int)
134     */
135    default boolean isAbstract() {
136        return Modifier.isAbstract(getModifiers());
137    }
138
139    /**
140     * Checks that the method is concrete and not abstract.
141     *
142     * @return whether the method is a concrete method
143     */
144    default boolean isConcrete() {
145        return !isAbstract();
146    }
147}
148