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  */
  23 package jdk.vm.ci.meta;
  24 
  25 import static java.lang.reflect.Modifier.*;
  26 
  27 import java.lang.reflect.*;
  28 
  29 /**
  30  * A Java element (i.e., a class, interface, field or method) that is described by a set of Java
  31  * language {@linkplain #getModifiers() modifiers}.
  32  */
  33 public interface ModifiersProvider {
  34     int BRIDGE = MetaUtil.getNonPublicModifierStaticField("BRIDGE");
  35     int VARARGS = MetaUtil.getNonPublicModifierStaticField("VARARGS");
  36     int SYNTHETIC = MetaUtil.getNonPublicModifierStaticField("SYNTHETIC");
  37     int ANNOTATION = MetaUtil.getNonPublicModifierStaticField("ANNOTATION");
  38     int ENUM = MetaUtil.getNonPublicModifierStaticField("ENUM");
  39     int MANDATED = MetaUtil.getNonPublicModifierStaticField("MANDATED");
  40 
  41     /**
  42      * Returns the Java Virtual Machine modifiers for this element. Note that this can differ from
  43      * standard Java Reflection modifiers. For example at the JVM level, classes (
  44      * {@link ResolvedJavaType}) can not be private or protected.
  45      */
  46     int getModifiers();
  47 
  48     /**
  49      * @see Modifier#isInterface(int)
  50      */
  51     default boolean isInterface() {
  52         return Modifier.isInterface(getModifiers());
  53     }
  54 
  55     /**
  56      * @see Modifier#isSynchronized(int)
  57      */
  58     default boolean isSynchronized() {
  59         return Modifier.isSynchronized(getModifiers());
  60     }
  61 
  62     /**
  63      * @see Modifier#isStatic(int)
  64      */
  65     default boolean isStatic() {
  66         return Modifier.isStatic(getModifiers());
  67     }
  68 
  69     /**
  70      * The setting of the final modifier bit for types is somewhat confusing, so don't export
  71      * isFinal by default. Subclasses like {@link ResolvedJavaField} and {@link ResolvedJavaMethod}
  72      * can export it as isFinal, but {@link ResolvedJavaType} can provide a more sensible equivalent
  73      * like {@link ResolvedJavaType#isLeaf}.
  74      *
  75      * @see Modifier#isFinal(int)
  76      */
  77     default boolean isFinalFlagSet() {
  78         return Modifier.isFinal(getModifiers());
  79     }
  80 
  81     /**
  82      * @see Modifier#isPublic(int)
  83      */
  84     default boolean isPublic() {
  85         return Modifier.isPublic(getModifiers());
  86     }
  87 
  88     /**
  89      * Determines if this element is neither {@linkplain #isPublic() public},
  90      * {@linkplain #isProtected() protected} nor {@linkplain #isPrivate() private}.
  91      */
  92     default boolean isPackagePrivate() {
  93         return ((PUBLIC | PROTECTED | PRIVATE) & getModifiers()) == 0;
  94     }
  95 
  96     /**
  97      * @see Modifier#isPrivate(int)
  98      */
  99     default boolean isPrivate() {
 100         return Modifier.isPrivate(getModifiers());
 101     }
 102 
 103     /**
 104      * @see Modifier#isProtected(int)
 105      */
 106     default boolean isProtected() {
 107         return Modifier.isProtected(getModifiers());
 108     }
 109 
 110     /**
 111      * @see Modifier#isTransient(int)
 112      */
 113     default boolean isTransient() {
 114         return Modifier.isTransient(getModifiers());
 115     }
 116 
 117     /**
 118      * @see Modifier#isStrict(int)
 119      */
 120     default boolean isStrict() {
 121         return Modifier.isStrict(getModifiers());
 122     }
 123 
 124     /**
 125      * @see Modifier#isVolatile(int)
 126      */
 127     default boolean isVolatile() {
 128         return Modifier.isVolatile(getModifiers());
 129     }
 130 
 131     /**
 132      * @see Modifier#isNative(int)
 133      */
 134     default boolean isNative() {
 135         return Modifier.isNative(getModifiers());
 136     }
 137 
 138     /**
 139      * @see Modifier#isAbstract(int)
 140      */
 141     default boolean isAbstract() {
 142         return Modifier.isAbstract(getModifiers());
 143     }
 144 
 145     /**
 146      * Checks that the method is concrete and not abstract.
 147      *
 148      * @return whether the method is a concrete method
 149      */
 150     default boolean isConcrete() {
 151         return !isAbstract();
 152     }
 153 
 154     static int jvmClassModifiers() {
 155         // no SUPER
 156         return PUBLIC | FINAL | INTERFACE | ABSTRACT | ANNOTATION | ENUM | SYNTHETIC;
 157     }
 158 
 159     static int jvmMethodModifiers() {
 160         return PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL | SYNCHRONIZED | BRIDGE | VARARGS | NATIVE | ABSTRACT | STRICT | SYNTHETIC;
 161     }
 162 
 163     static int jvmFieldModifiers() {
 164         return PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL | VOLATILE | TRANSIENT | ENUM | SYNTHETIC;
 165     }
 166 }