|
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 com.sun.enterprise.admin.util; import java.lang.reflect.Array; import java.lang.reflect.Constructor; /* Used internally. */ final class ClassToClassMapping { final Class mSrc; final Class mDest; public ClassToClassMapping( Class src, Class dest ) { mSrc = src; mDest = dest; } } /* Various utilities used for classes. */ public final class ClassUtil { private ClassUtil( ) { // disallow instantiation } static private void p( Object o ) { System.out.println( o.toString() ); } /* Test whether an Object is an array @param o object to test @returns 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 @returns 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 @returns 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 @returns true if the object is an array, false otherwise. */ public static boolean classnameIsArray( String classname ) { return( classname.startsWith( "[" ) ); } /* Test whether a classname is a primitive array @param classname classname string @returns 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 classname the Class object @returns 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 @returns 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 @returns 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: assert( false ); // 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 ), }; /* 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 @returns 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 = Class.forName( 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 = theClass.forName( classname ); } } return( theClass ); } private static final ClassToClassMapping [] sPrimitiveClassToObjectClass = new ClassToClassMapping [] { new ClassToClassMapping( int.class, Integer.class ), new ClassToClassMapping( long.class, Long.class ), new ClassToClassMapping( short.class, Short.class ), new ClassToClassMapping( byte.class, Byte.class ), new ClassToClassMapping( boolean.class, Boolean.class), new ClassToClassMapping( float.class, Float.class ), new ClassToClassMapping( double.class, Double.class ), new ClassToClassMapping( char.class, Character.class ), }; /* Map primitive class Classes to Object forms eg int.class to Integer.class @param theClass the class to map @returns 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.equals( theClass ) ) { result = mapping.mDest; break; } } return( result ); } /* Test whether a class is a primitive class. @param theClass the class to test @returns 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 ); } 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 ); } 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 ) { types[ i ] = args[ i ].getClass().getName(); } return( types ); } public static String getFriendlyClassname( Class theClass ) { return( getFriendlyClassname( theClass.getName() ) ); } /* Convert a Java class name string into a more user friendly string. Examples 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.