|
Glassfish example source code file (ClassUtil.java)
The Glassfish ClassUtil.java source code/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html * or packager/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at packager/legal/LICENSE.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * exception as provided by Oracle in the GPL Version 2 section of the License * file that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" * * Contributor(s): * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package org.glassfish.admin.amx.util; import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashSet; import java.util.Set; /* Used internally. */ final class ClassToClassMapping { final Class mSrc; final Class mDest; public ClassToClassMapping(Class src, Class dest) { mSrc = src; mDest = dest; } } /** Provides a variety of useful utilities having to do with classes, types of objects, etc. */ public final class ClassUtil { private ClassUtil() { // disallow instantiation } public static boolean classIsAccessible(String name) { boolean accessible = false; try { accessible = getClassFromName(name) != null; } catch (ClassNotFoundException e) { } return (accessible); } public static boolean sigsEqual( final Class[] sig1, final Class[] sig2) { boolean equal = sig1.length == sig2.length; if (equal) { for (int i = 0; i < sig1.length; ++i) { if (sig1[i] != sig2[i]) { equal = false; break; } } } return (equal); } /** Test whether an Object is an array @param o object to test @return true if the object is an array, false otherwise. */ public static boolean objectIsArray(Object o) { return (classIsArray(o.getClass())); } /** Test whether a Class is an array @param theClass class to test @return true if the class is an array, false otherwise. */ public static boolean classIsArray(Class theClass) { return (classnameIsArray(theClass.getName())); } /** Test whether an Object is an array of primitive types @param o object to test @return true if the object is an array, false otherwise. */ public static boolean objectIsPrimitiveArray(Object o) { return (getPrimitiveArrayTypeCode(o.getClass()) != 0); } /** Test whether a classname is an array @param classname classname string @return true if the object is an array, false otherwise. */ public static boolean classnameIsArray(String classname) { return (classname.startsWith("[")); } /** Strip the package name. @param classname classname string @return the classname, without its package */ public static String stripPackageName(String classname) { final int lastDot = classname.lastIndexOf("."); if (lastDot < 0) { return (classname); } return (classname.substring(lastDot + 1, classname.length())); } /** Test whether a classname is a primitive array @param classname classname string @return true if the object is a primitive array, false otherwise. */ public static boolean classnameIsPrimitiveArray(String classname) { return (getPrimitiveArrayTypeCode(classname) != 0); } /** Return the primitive element type code for an array of primitive types. Same as getPrimitiveArrayTypeCode( theClass.getName() ) @param theClass the Class object @return the element type code; otherwise (char)0 */ public static char getPrimitiveArrayTypeCode(Class theClass) { char typeCode = 0; if (classIsArray(theClass)) { typeCode = getPrimitiveArrayTypeCode(theClass.getName()); } return (typeCode); } /** Return the primitive element type code for an array of primitive types. @param classname classname string @return the element type code; otherwise (char)0 */ public static char getPrimitiveArrayTypeCode(String classname) { char typeCode = 0; final int length = classname.length(); if (classnameIsArray(classname) && classname.charAt(length - 2) == '[') { typeCode = classname.charAt(length - 1); switch (typeCode) { default: typeCode = 0; break; case 'Z': case 'B': case 'C': case 'S': case 'I': case 'J': case 'F': case 'D': break; } } return (typeCode); } /** Get the classname for an array element. @param classname classname string @return the classname for the array element */ public static String getArrayMemberClassName(String classname) { String result = null; if (!classnameIsArray(classname)) { throw new IllegalArgumentException("not an array"); } final int classnameLength = classname.length(); if (classnameIsPrimitiveArray(classname)) { final char lastChar = classname.charAt(classnameLength - 1); switch (lastChar) { default: throw new RuntimeException("illegal primitive"); // a simple type case 'Z': result = "boolean"; break; case 'B': result = "byte"; break; case 'C': result = "char"; break; case 'S': result = "short"; break; case 'I': result = "int"; break; case 'J': result = "long"; break; case 'F': result = "float"; break; case 'D': result = "double"; break; } } else { // strip leading "[L" and trailing ";" result = classname.substring(2, classnameLength - 1); } return (result); } /** Class.forName does not work for primitive types, so we need to do it ourselves here. */ final static class ClassNameToClassMapping { String mName; Class mClass; ClassNameToClassMapping(String name, Class theClass) { mName = name; mClass = theClass; } } private static final ClassNameToClassMapping[] sPrimitiveNameToObjectClass = new ClassNameToClassMapping[] { new ClassNameToClassMapping("int", int.class), new ClassNameToClassMapping("long", long.class), new ClassNameToClassMapping("short", short.class), new ClassNameToClassMapping("byte", byte.class), new ClassNameToClassMapping("boolean", boolean.class), new ClassNameToClassMapping("float", float.class), new ClassNameToClassMapping("double", double.class), new ClassNameToClassMapping("char", char.class), new ClassNameToClassMapping("void", void.class), }; public static Class<?> classForName(String name) throws ClassNotFoundException { Class<?> c = null; try { c = Class.forName(name); } catch (ClassNotFoundException e) { c = Class.forName(name, true, Thread.currentThread().getContextClassLoader()); } catch (NoClassDefFoundError e) { c = Class.forName(name, true, Thread.currentThread().getContextClassLoader()); } return (c); } /** Get a Class from a classname. Class.forName does not work for primitive types; this methods returns the correct Class for any type. @param classname classname string @return the classname for the array element */ public static Class<?> getClassFromName(final String classname) throws ClassNotFoundException { Class<?> theClass = null; if (classname.startsWith("[L")) { // an array theClass = classForName(classname); } else { final int numMappings = Array.getLength(sPrimitiveNameToObjectClass); for (int i = 0; i < numMappings; ++i) { if (sPrimitiveNameToObjectClass[i].mName.equals(classname)) { theClass = sPrimitiveNameToObjectClass[i].mClass; break; } } if (theClass == null) { theClass = classForName(classname); } } return (theClass); } private static final ClassToClassMapping[] sPrimitiveClassToObjectClass = new ClassToClassMapping[] { new ClassToClassMapping(boolean.class, Boolean.class), new ClassToClassMapping(byte.class, Byte.class), new ClassToClassMapping(char.class, Character.class), new ClassToClassMapping(short.class, Short.class), new ClassToClassMapping(int.class, Integer.class), new ClassToClassMapping(long.class, Long.class), new ClassToClassMapping(float.class, Float.class), new ClassToClassMapping(double.class, Double.class), }; /** Map primitive class Classes to Object forms eg int.class to Integer.class @param theClass the class to map @return the corresponding Object class or the original Class if not a primitive. */ public static Class PrimitiveClassToObjectClass(final Class theClass) { Class result = theClass; final int numMappings = Array.getLength(sPrimitiveClassToObjectClass); for (int i = 0; i < numMappings; ++i) { final ClassToClassMapping mapping = sPrimitiveClassToObjectClass[i]; if (mapping.mSrc == theClass) { result = mapping.mDest; break; } } return (result); } /** Map primitive class Classes to Object forms eg int.class to Integer.class @param theClass the class to map @return the corresponding Object class or the original Class if not a primitive. */ public static Class ObjectClassToPrimitiveClass(final Class theClass) { Class result = theClass; final int numMappings = Array.getLength(sPrimitiveClassToObjectClass); for (int i = 0; i < numMappings; ++i) { final ClassToClassMapping mapping = sPrimitiveClassToObjectClass[i]; if (mapping.mDest == theClass) { result = mapping.mSrc; break; } } return (result); } /** Test whether a class is a primitive class. @param theClass the class to test @return true if it's a primitive class, false otherwise. */ public static boolean IsPrimitiveClass(final Class theClass) { boolean isSimple = false; final int numMappings = Array.getLength(sPrimitiveClassToObjectClass); for (int i = 0; i < numMappings; ++i) { final ClassToClassMapping mapping = sPrimitiveClassToObjectClass[i]; if (mapping.mSrc.equals(theClass)) { isSimple = true; break; } } return (isSimple); } /** Convert a primitive class letter to its corresponding class name. @param primitive the primitive character code @return the corresponding classname */ public static String PrimitiveLetterToClassName(final char primitive) { String result = "" + primitive; // see JavaDoc on Class.getName() switch (primitive) { case 'B': result = "byte"; break; case 'C': result = "char"; break; case 'D': result = "double"; break; case 'F': result = "float"; break; case 'I': result = "int"; break; case 'J': result = "long"; break; case 'S': result = "short"; break; case 'Z': result = "boolean"; break; } return (result); } /** Return the corresponding classes for each element in an Object[] If an element is null, then its corresponding Class will also be null. @param args an array of objects. @return an array of classes */ public static String[] getTypes(final Object[] args) { if (args == null) { return (null); } final int numArgs = Array.getLength(args); final String[] types = new String[numArgs]; for (int i = 0; i < numArgs; ++i) { if (args[i] == null) { types[i] = null; } else { types[i] = args[i].getClass().getName(); } } return (types); } /** Return the corresponding classes for each classname. If an element is null, then its corresponding Class will also be null. @param classnames an array of classnames. @return an array of classes */ public static Class[] signatureFromClassnames(String[] classnames) throws ClassNotFoundException { if (classnames == null) { return (null); } final Class[] signature = new Class[classnames.length]; for (int i = 0; i < signature.length; ++i) { signature[i] = getClassFromName(classnames[i]); } return (signature); } /** Return the corresponding classes for each classname. If an element is null, then its corresponding Class will also be null. @param classes an array of classnames. @return an array of classes */ public static String[] classnamesFromSignature(Class[] classes) { final String[] classnames = new String[classes.length]; for (int i = 0; i < classnames.length; ++i) { classnames[i] = classes[i].getName(); } return (classnames); } /** Get a "friendly" classname for a Class. <p> Calls getFriendlyClassname( theClass.getName() ) @param theClass the class for which the name should be gotten @return the "friendly" name */ public static String getFriendlyClassname(Class theClass) { return (getFriendlyClassname(theClass.getName())); } /** Convert a Java class name string into a more user friendly string. Examples: <p> java.lang.String => String java.lang.<type> => Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish ClassUtil.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.