< prev index next >

src/jdk.vm.ci/share/classes/jdk.vm.ci.meta/src/jdk/vm/ci/meta/MetaUtil.java

Print this page




   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 java.lang.reflect.Field;
  26 import java.lang.reflect.Modifier;
  27 
  28 /**
  29  * Miscellaneous collection of utility methods used by {@code jdk.vm.ci.meta} and its clients.
  30  */
  31 public class MetaUtil {
  32 
  33     /**
  34      * Extends the functionality of {@link Class#getSimpleName()} to include a non-empty string for
  35      * anonymous and local classes.
  36      *
  37      * @param clazz the class for which the simple name is being requested
  38      * @param withEnclosingClass specifies if the returned name should be qualified with the name(s)
  39      *            of the enclosing class/classes of {@code clazz} (if any). This option is ignored
  40      *            if {@code clazz} denotes an anonymous or local class.
  41      * @return the simple name
  42      */
  43     public static String getSimpleName(Class<?> clazz, boolean withEnclosingClass) {
  44         final String simpleName = clazz.getSimpleName();
  45         if (simpleName.length() != 0) {
  46             if (withEnclosingClass) {
  47                 String prefix = "";


 209                 result.append("V");
 210                 break;
 211             default:
 212                 result.append("L").append(base.replace('.', '/')).append(";");
 213                 break;
 214         }
 215         return result.toString();
 216     }
 217 
 218     /**
 219      * Gets a string representation of an object based soley on its class and its
 220      * {@linkplain System#identityHashCode(Object) identity hash code}. This avoids and calls to
 221      * virtual methods on the object such as {@link Object#hashCode()}.
 222      */
 223     public static String identityHashCodeString(Object obj) {
 224         if (obj == null) {
 225             return "null";
 226         }
 227         return obj.getClass().getName() + "@" + System.identityHashCode(obj);
 228     }
 229 
 230     /**
 231      * Used to lookup constants from {@link Modifier} that are not public (VARARGS, SYNTHETIC etc.).
 232      */
 233     static int getNonPublicModifierStaticField(String name) {
 234         try {
 235             Field field = Modifier.class.getDeclaredField(name);
 236             field.setAccessible(true);
 237             return field.getInt(null);
 238         } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
 239             throw new InternalError(e);
 240         }
 241     }
 242 }


   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 /**
  26  * Miscellaneous collection of utility methods used by {@code jdk.vm.ci.meta} and its clients.
  27  */
  28 public class MetaUtil {
  29 
  30     /**
  31      * Extends the functionality of {@link Class#getSimpleName()} to include a non-empty string for
  32      * anonymous and local classes.
  33      *
  34      * @param clazz the class for which the simple name is being requested
  35      * @param withEnclosingClass specifies if the returned name should be qualified with the name(s)
  36      *            of the enclosing class/classes of {@code clazz} (if any). This option is ignored
  37      *            if {@code clazz} denotes an anonymous or local class.
  38      * @return the simple name
  39      */
  40     public static String getSimpleName(Class<?> clazz, boolean withEnclosingClass) {
  41         final String simpleName = clazz.getSimpleName();
  42         if (simpleName.length() != 0) {
  43             if (withEnclosingClass) {
  44                 String prefix = "";


 206                 result.append("V");
 207                 break;
 208             default:
 209                 result.append("L").append(base.replace('.', '/')).append(";");
 210                 break;
 211         }
 212         return result.toString();
 213     }
 214 
 215     /**
 216      * Gets a string representation of an object based soley on its class and its
 217      * {@linkplain System#identityHashCode(Object) identity hash code}. This avoids and calls to
 218      * virtual methods on the object such as {@link Object#hashCode()}.
 219      */
 220     public static String identityHashCodeString(Object obj) {
 221         if (obj == null) {
 222             return "null";
 223         }
 224         return obj.getClass().getName() + "@" + System.identityHashCode(obj);
 225     }













 226 }
< prev index next >