KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > util > ClassUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/util/ClassUtil.java,v 1.3 2005/12/25 03:45:57 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:57 $
28  */

29
30 package com.sun.cli.util;
31
32 import javax.management.*;
33 import java.lang.reflect.Array JavaDoc;
34 import java.lang.reflect.Constructor JavaDoc;
35
36
37 /*
38     Used internally.
39  */

40 final class ClassToClassMapping
41 {
42     final Class JavaDoc mSrc;
43     final Class JavaDoc mDest;
44
45         public
46     ClassToClassMapping( Class JavaDoc src, Class JavaDoc dest )
47     {
48         mSrc = src;
49         mDest = dest;
50     }
51 }
52
53
54 /*
55     Various utilities used for classes.
56  */

57 public final class ClassUtil
58 {
59         private
60     ClassUtil( )
61     {
62         // disallow instantiation
63
}
64     
65         static private void
66     p( Object JavaDoc o )
67     {
68         System.out.println( o.toString() );
69     }
70     
71     /*
72         Test whether an Object is an array
73         
74         @param o object to test
75         @returns true if the object is an array, false otherwise.
76      */

77         public static boolean
78     objectIsArray( Object JavaDoc o )
79     {
80         return( classIsArray( o.getClass() ) );
81     }
82     
83     /*
84         Test whether a Class is an array
85         
86         @param theClass class to test
87         @returns true if the class is an array, false otherwise.
88      */

89         public static boolean
90     classIsArray( Class JavaDoc theClass )
91     {
92         return( classnameIsArray( theClass.getName() ) );
93     }
94     
95     /*
96         Test whether an Object is an array of primitive types
97         
98         @param o object to test
99         @returns true if the object is an array, false otherwise.
100      */

101         public static boolean
102     objectIsPrimitiveArray( Object JavaDoc o )
103     {
104         return( getPrimitiveArrayTypeCode( o.getClass() ) != 0 );
105     }
106     
107     /*
108         Test whether a classname is an array
109         
110         @param classname classname string
111         @returns true if the object is an array, false otherwise.
112      */

113         public static boolean
114     classnameIsArray( String JavaDoc classname )
115     {
116         return( classname.startsWith( "[" ) );
117     }
118     
119     
120     /*
121         Test whether a classname is a primitive array
122         
123         @param classname classname string
124         @returns true if the object is a primitive array, false otherwise.
125      */

126         public static boolean
127     classnameIsPrimitiveArray( String JavaDoc classname )
128     {
129         return( getPrimitiveArrayTypeCode( classname ) != 0 );
130     }
131     
132     /*
133         Return the primitive element type code for an array of primitive types.
134         Same as getPrimitiveArrayTypeCode( theClass.getName() )
135         
136         @param classname the Class object
137         @returns the element type code; otherwise (char)0
138      */

139         public static char
140     getPrimitiveArrayTypeCode( Class JavaDoc theClass )
141     {
142         char typeCode = 0;
143         
144         if ( classIsArray( theClass ) )
145         {
146             typeCode = getPrimitiveArrayTypeCode( theClass.getName() );
147         }
148         
149         return( typeCode );
150     }
151     
152     /*
153         Return the primitive element type code for an array of primitive types.
154         
155         @param classname classname string
156         @returns the element type code; otherwise (char)0
157      */

158         public static char
159     getPrimitiveArrayTypeCode( String JavaDoc classname )
160     {
161         char typeCode = 0;
162         
163         final int length = classname.length();
164         
165         if ( classnameIsArray( classname ) &&
166                 classname.charAt( length - 2 ) == '[' )
167         {
168             typeCode = classname.charAt( length - 1 );
169             
170             switch( typeCode )
171             {
172                 default: typeCode = 0; break;
173                 
174                 case 'Z':
175                 case 'B':
176                 case 'C':
177                 case 'S':
178                 case 'I':
179                 case 'J':
180                 case 'F':
181                 case 'D':
182                     break;
183             }
184         }
185         
186         return( typeCode );
187     }
188     
189  
190     /*
191         Get the classname for an array element.
192         
193         @param classname classname string
194         @returns the classname for the array element
195      */

196         public static String JavaDoc
197     getArrayMemberClassName( String JavaDoc classname )
198     {
199         String JavaDoc result = null;
200         
201         if ( ! classnameIsArray( classname ) )
202         {
203             throw new IllegalArgumentException JavaDoc( "not an array" );
204         }
205         
206         final int classnameLength = classname.length();
207         
208         
209         if ( classnameIsPrimitiveArray( classname ) )
210         {
211             final char lastChar = classname.charAt(classnameLength -1 );
212             
213             switch( lastChar )
214             {
215                 default: assert( false );
216                 
217                 // a simple type
218
case 'Z': result = "boolean"; break;
219                 case 'B': result = "byte"; break;
220                 case 'C': result = "char"; break;
221                 case 'S': result = "short"; break;
222                 case 'I': result = "int"; break;
223                 case 'J': result = "long"; break;
224                 case 'F': result = "float"; break;
225                 case 'D': result = "double"; break;
226             }
227         }
228         else
229         {
230             // strip leading "[L" and trailing ";"
231
result = classname.substring( 2, classnameLength - 1 );
232         }
233         
234         return( result );
235     }
236     
237
238     
239          
240     /*
241         Class.forName does not work for primitive types, so we need to do it ourselves here.
242      */

243     final static class ClassNameToClassMapping
244     {
245         String JavaDoc mName;
246         Class JavaDoc mClass;
247
248         ClassNameToClassMapping( String JavaDoc name, Class JavaDoc theClass )
249         {
250             mName = name;
251             mClass = theClass;
252         }
253     }
254     
255     private static final ClassNameToClassMapping [] sPrimitiveNameToObjectClass =
256         new ClassNameToClassMapping []
257         {
258             new ClassNameToClassMapping( "int", int.class ),
259             new ClassNameToClassMapping( "long", long.class ),
260             new ClassNameToClassMapping( "short", short.class ),
261             new ClassNameToClassMapping( "byte", byte.class ),
262             new ClassNameToClassMapping( "boolean", boolean.class ),
263             new ClassNameToClassMapping( "float", float.class ),
264             new ClassNameToClassMapping( "double", double.class ),
265             new ClassNameToClassMapping( "char", char.class ),
266             new ClassNameToClassMapping( "void", void.class ),
267         };
268      
269     /*
270         Get a Class from a classname. Class.forName does not work for primitive types;
271         this methods returns the correct Class for any type.
272         
273         @param classname classname string
274         @returns the classname for the array element
275      */

276         public static Class JavaDoc
277     getClassFromName( final String JavaDoc classname )
278         throws ClassNotFoundException JavaDoc
279     {
280         Class JavaDoc theClass = null;
281         
282         if ( classname.startsWith( "[L" ))
283         {
284             // an array
285
theClass = Class.forName( classname );
286         }
287         else
288         {
289             final int numMappings = Array.getLength( sPrimitiveNameToObjectClass );
290             for( int i = 0; i < numMappings; ++i )
291             {
292                 if ( sPrimitiveNameToObjectClass[ i ].mName.equals( classname ) )
293                 {
294                     theClass = sPrimitiveNameToObjectClass[ i ].mClass;
295                     break;
296                 }
297             }
298             
299             if ( theClass == null )
300             {
301                 theClass = theClass.forName( classname );
302             }
303         }
304         return( theClass );
305     }
306     
307     
308     private static final ClassToClassMapping [] sPrimitiveClassToObjectClass =
309         new ClassToClassMapping []
310          {
311             new ClassToClassMapping( int.class, Integer JavaDoc.class ),
312             new ClassToClassMapping( long.class, Long JavaDoc.class ),
313             new ClassToClassMapping( short.class, Short JavaDoc.class ),
314             new ClassToClassMapping( byte.class, Byte JavaDoc.class ),
315             new ClassToClassMapping( boolean.class, Boolean JavaDoc.class),
316             new ClassToClassMapping( float.class, Float JavaDoc.class ),
317             new ClassToClassMapping( double.class, Double JavaDoc.class ),
318             new ClassToClassMapping( char.class, Character JavaDoc.class ),
319          };
320     /*
321         Map primitive class Classes to Object forms eg int.class to Integer.class
322         
323         @param theClass the class to map
324         @returns the corresponding Object class or the original Class if not a primitive.
325      */

326         public static Class JavaDoc
327     PrimitiveClassToObjectClass( final Class JavaDoc theClass )
328     {
329         Class JavaDoc result = theClass;
330         
331         final int numMappings = Array.getLength( sPrimitiveClassToObjectClass );
332         for( int i = 0; i < numMappings; ++i )
333         {
334             final ClassToClassMapping mapping = sPrimitiveClassToObjectClass[ i ];
335             
336             if ( mapping.mSrc.equals( theClass ) )
337             {
338                 result = mapping.mDest;
339                 break;
340             }
341         }
342             
343         return( result );
344     }
345     
346     /*
347         Test whether a class is a primitive class.
348         
349         @param theClass the class to test
350         @returns true if it's a primitive class, false otherwise.
351      */

352         public static boolean
353     IsPrimitiveClass( final Class JavaDoc theClass )
354     {
355         boolean isSimple = false;
356         
357         final int numMappings = Array.getLength( sPrimitiveClassToObjectClass );
358         for( int i = 0; i < numMappings; ++i )
359         {
360             final ClassToClassMapping mapping = sPrimitiveClassToObjectClass[ i ];
361             
362             if ( mapping.mSrc.equals( theClass ) )
363             {
364                 isSimple = true;
365                 break;
366             }
367         }
368             
369         return( isSimple );
370     }
371     
372     
373         public static String JavaDoc
374     PrimitiveLetterToClassName( final char primitive)
375     {
376         String JavaDoc result = "" + primitive;
377         
378         // see JavaDoc on Class.getName()
379
switch( primitive )
380         {
381             case 'B': result = "byte"; break;
382             case 'C': result = "char"; break;
383             case 'D': result = "double"; break;
384             case 'F': result = "float"; break;
385             case 'I': result = "int"; break;
386             case 'J': result = "long"; break;
387             case 'S': result = "short"; break;
388             case 'Z': result = "boolean";break;
389         }
390         
391         return( result );
392     }
393     
394
395     
396     
397         public static String JavaDoc []
398     getTypes( final Object JavaDoc [] args )
399     {
400         if ( args == null )
401             return( null );
402             
403         final int numArgs = Array.getLength( args );
404         
405         final String JavaDoc [] types = new String JavaDoc [ numArgs ];
406         
407         for( int i = 0; i < numArgs; ++i )
408         {
409             types[ i ] = args[ i ].getClass().getName();
410         }
411         
412         return( types );
413     }
414     
415     
416         public static String JavaDoc
417     getFriendlyClassname( Class JavaDoc theClass )
418     {
419         return( getFriendlyClassname( theClass.getName() ) );
420     }
421     
422     /*
423         Convert a Java class name string into a more user friendly string. Examples
424         java.lang.String => String
425         java.lang.<type> => <type>;
426         [i => int[]
427         [Lfoo.bar.ClassName; => foo.bar.ClassName[]
428         
429         The types thus correspond exactly to what a Java programmer would write, rather
430         than the internal JVM representation.
431         
432         @param type
433         @returns a friendlier string representing the type
434      */

435     final static String JavaDoc javaLang = "java.lang.";
436         public static String JavaDoc
437     getFriendlyClassname( String JavaDoc type )
438     {
439         String JavaDoc result = type;
440         
441         if ( type.startsWith( "[" ) )
442         {
443             // count how deep the array is
444
int depth = 0;
445             while ( type.charAt( depth ) == (int)'[' )
446             {
447                 ++depth;
448             }
449             
450             // strip all the '[' characters
451
result = type.substring( depth, type.length() );
452             
453             if ( result.startsWith( "L" ) && result.endsWith( ";" ) )
454             {
455                 result = result.substring( 1, result.length() - 1 );
456             }
457             else if ( result.length() == 1 )
458             {
459                 // a simple type
460
switch( result.charAt( 0 ) )
461                 {
462                     case 'Z': result = "boolean"; break;
463                     case 'B': result = "byte"; break;
464                     case 'C': result = "char"; break;
465                     case 'S': result = "short"; break;
466                     case 'I': result = "int"; break;
467                     case 'J': result = "long"; break;
468                     case 'F': result = "float"; break;
469                     case 'D': result = "double"; break;
470                 }
471             }
472             
473             for( int i = 0; i < depth; ++i )
474             {
475                 result = result + "[]";
476             }
477         }
478         
479         if ( result.startsWith( javaLang ) )
480         {
481             result = result.substring( javaLang.length(), result.length() );
482         }
483         
484         return( result );
485     }
486     
487         
488     /*
489      */

490         public static Class JavaDoc
491     getArrayElementClass( final Class JavaDoc arrayClass )
492     {
493         final String JavaDoc arrayClassName = arrayClass.getName();
494             
495         if ( ! classnameIsArray( arrayClassName ) )
496         {
497             throw new IllegalArgumentException JavaDoc( "not an array" );
498         }
499         
500         String JavaDoc name = arrayClassName;
501         
502         // strip leading "["
503
name = name.substring( 1, name.length() );
504         
505         if ( ! name.startsWith( "[" ) )
506         {
507             // element is not an array
508

509             if ( name.startsWith( "L" ) )
510             {
511                 // an Object class; strip leading "L" and trailing ";"
512
name = name.substring( 1, name.length() - 1);
513             }
514             else if ( name.length() == 1 )
515             {
516                 // may be a primitive type
517
name = PrimitiveLetterToClassName( name.charAt( 0 ) );
518             }
519         }
520         else
521         {
522             // element is an array; return it
523
}
524         
525         Class JavaDoc theClass = null;
526         try
527         {
528             theClass = getClassFromName( name );
529         }
530         catch( ClassNotFoundException JavaDoc e )
531         {
532             assert( false );
533         }
534         
535         return( theClass );
536     }
537     
538         public static Class JavaDoc
539     getInnerArrayElementClass( final Class JavaDoc arrayClass )
540         throws ClassNotFoundException JavaDoc
541     {
542         Class JavaDoc elementClass = arrayClass;
543         
544         do
545         {
546             elementClass = getArrayElementClass( elementClass );
547         }
548         while ( classIsArray( elementClass ) );
549         
550         return( elementClass );
551     }
552     
553     
554
555
556         private static Object JavaDoc
557     InstantiateObject( final String JavaDoc theString )
558         throws Exception JavaDoc
559     {
560         Object JavaDoc result = null;
561         
562         try
563         {
564             result = InstantiateNumber( theString );
565         }
566         catch( NumberFormatException JavaDoc e )
567         {
568             result = theString;
569         }
570         
571         return( result );
572     }
573     
574     /*
575         Return true if caller signature is compatible with callee.
576         
577         @param callee the signature of the method to be called
578         @param caller the signature of the argument list
579      */

580         public static boolean
581     signaturesAreCompatible( Class JavaDoc [] callee, Class JavaDoc [] argsSignature )
582     {
583         boolean compatible = false;
584         
585         if ( callee.length == argsSignature.length )
586         {
587             compatible = true;
588             
589             for( int i = 0; i < callee.length; ++i )
590             {
591                 if ( ! callee[ i ].isAssignableFrom( argsSignature[ i ] ) )
592                 {
593                     compatible = false;
594                     break;
595                 }
596             }
597         }
598         
599         return( compatible );
600     }
601     
602         public static Object JavaDoc
603     InstantiateObject( final Class JavaDoc theClass, final Object JavaDoc [] args )
604         throws Exception JavaDoc
605     {
606         final Class JavaDoc [] signature = new Class JavaDoc [ args.length ];
607         
608         for( int i = 0; i < signature.length; ++i )
609         {
610             signature[ i ] = args[ i ].getClass();
611         }
612         
613         Constructor JavaDoc constructor = null;
614         try
615         {
616             // this will fail if a constructor takes an interface;
617
// the code below will then find a compatible constructor
618
constructor = theClass.getConstructor( signature );
619         }
620         catch( NoSuchMethodException JavaDoc e )
621         {
622             final Constructor JavaDoc [] constructors = theClass.getConstructors();
623             
624             int numMatches = 0;
625             for( int i = 0; i < constructors.length; ++i )
626             {
627                 final Constructor JavaDoc tempConstructor = constructors[ i ];
628                 
629                 final Class JavaDoc [] tempSignature = tempConstructor.getParameterTypes();
630                 
631                 if ( signaturesAreCompatible( tempSignature, signature ) )
632                 {
633                     ++numMatches;
634                     constructor = tempConstructor;
635                     // keep going; there could be more than one valid match
636
}
637             }
638             
639             // to succeed, there must be exactly one match
640
if ( numMatches != 1 )
641             {
642                 throw e;
643             }
644         }
645         
646         Object JavaDoc result = null;
647         try
648         {
649             result = constructor.newInstance( args );
650         }
651         catch( java.lang.reflect.InvocationTargetException JavaDoc e )
652         {
653             // InvocationTargetException wraps the real cause
654
final Throwable JavaDoc cause = e.getCause();
655             
656             if ( cause instanceof Exception JavaDoc )
657             {
658                 throw (Exception JavaDoc)cause;
659             }
660             else
661             {
662                 // shouldn't happen, so we'll just rethrow it
663
throw e;
664             }
665         }
666         
667         return( result );
668     }
669     
670     
671         public static Object JavaDoc
672     InstantiateObject( final Class JavaDoc theClass, final String JavaDoc theString )
673         throws Exception JavaDoc
674     {
675         final Class JavaDoc [] signature = new Class JavaDoc [] { String JavaDoc.class };
676         final Constructor JavaDoc constructor = theClass.getConstructor( signature );
677         
678         Object JavaDoc result = null;
679         try
680         {
681             result = constructor.newInstance( new Object JavaDoc[] { theString } );
682         }
683         catch( java.lang.reflect.InvocationTargetException JavaDoc e )
684         {
685             // InvocationTargetException wraps the real cause
686
Throwable JavaDoc cause = e.getCause();
687             
688             if ( cause instanceof Exception JavaDoc )
689             {
690                 throw (Exception JavaDoc)cause;
691             }
692             else
693             {
694                 // shouldn't happen, so we'll just rethrow it
695
throw e;
696             }
697         }
698         
699         return( result );
700     }
701     
702     /*
703         Don't get fancy here, simple precedence:
704             Integer, Long if no decimal point, use Long if won't fit in an Integer
705             Double if decimal point (for maximum precision)
706      */

707         private static Object JavaDoc
708     InstantiateNumber( final String JavaDoc theString )
709         throws Exception JavaDoc
710     {
711         Object JavaDoc result = null;
712         
713         if ( theString.indexOf( '.' ) >= 0 )
714         {
715             result = InstantiateObject( Double JavaDoc.class, theString );
716         }
717         else
718         {
719             try
720             {
721                 result = InstantiateObject( Integer JavaDoc.class, theString );
722             }
723             catch( NumberFormatException JavaDoc e )
724             {
725                 // perhaps it wouldn't fit; try it as a long
726
result = InstantiateObject( Long JavaDoc.class, theString );
727             }
728         }
729         return( result );
730     }
731
732     
733     /*
734         Given a Class and a String, create a new instance with a constructor that accept
735         a String. Primitive types are instantiated as their equivalent Object forms.
736         
737         @param theClass the class from which an instance should be instantiated
738         @param theString the string to be supplied to the constructor
739      */

740         public static Object JavaDoc
741     InstantiateFromString( final Class JavaDoc theClass, final String JavaDoc theString )
742         throws Exception JavaDoc
743     {
744         Object JavaDoc result = null;
745         
746         // char and Character do not have a String constructor, so we must special-case it
747
if ( theClass == Object JavaDoc.class )
748         {
749             // special case, apply rules to create an object
750
result = InstantiateObject( theString );
751         }
752         else if ( theClass == Number JavaDoc.class )
753         {
754             // special case, apply rules to create a number
755
result = InstantiateNumber( theString );
756         }
757         else if ( theClass == Character JavaDoc.class || theClass == char.class)
758         {
759             if ( theString.length() != 1 )
760             {
761                 throw new IllegalArgumentException JavaDoc( "not a character: " + theString );
762             }
763             
764             result = new Character JavaDoc( theString.charAt( 0 ) );
765         }
766         else
767         {
768             
769             final Class JavaDoc objectClass = PrimitiveClassToObjectClass( theClass );
770             
771             result = InstantiateObject( objectClass, theString );
772         }
773         
774         return( result );
775     }
776     
777     
778     /*
779         Given a Class, create a new instance with an empty constructor.
780         Primitive types are instantiated as their equivalent Object forms.
781         Any value is acceptable in the newly created object.
782         
783         @param theClass the class from which an instance should be instantiated
784      */

785         public static Object JavaDoc
786     InstantiateDefault( final Class JavaDoc inClass )
787         throws Exception JavaDoc
788     {
789         Object JavaDoc result = null;
790         
791         final Class JavaDoc objectClass = PrimitiveClassToObjectClass( inClass );
792         
793         if ( Number JavaDoc.class.isAssignableFrom( objectClass ) )
794         {
795             result = InstantiateFromString( objectClass, "0" );
796         }
797         else if ( objectClass == Boolean JavaDoc.class)
798         {
799             result = new Boolean JavaDoc( "true" );
800         }
801         else if ( objectClass == Character JavaDoc.class)
802         {
803             result = new Character JavaDoc( 'X' );
804         }
805         else if ( classIsArray( objectClass ) )
806         {
807             result = Array.newInstance( objectClass, 0 );
808         }
809         else if ( objectClass == Object JavaDoc.class )
810         {
811             result = new String JavaDoc( "anyObject" );
812         }
813         else if ( objectClass == String JavaDoc.class )
814         {
815             result = new String JavaDoc( "" );
816         }
817         else if ( objectClass == java.net.URL JavaDoc.class )
818         {
819             result = new java.net.URL JavaDoc( "http://www.sun.com" );
820         }
821         else if ( objectClass == java.net.URI JavaDoc.class )
822         {
823             result = new java.net.URI JavaDoc( "http://www.sun.com" );
824         }
825         else if ( classIsArray( inClass ) )
826         {
827             final int dimensions = 3;
828             result = Array.newInstance( getInnerArrayElementClass( inClass ), dimensions );
829         }
830         else
831         {
832             result = objectClass.newInstance();
833             //result = InstantiateFromString( objectClass, "0" );
834
}
835         return( result );
836     }
837     
838     
839     /*
840         We allow abbrevations of certain standard java types
841         
842         Turn "Integer" into "java.lang.Integer", etc.
843      */

844      final static String JavaDoc [] sJavaLangTypes =
845         { "Character", "Boolean", "Byte", "Short", "Integer", "Long", "Float", "Double", "String", "Object"};
846      final static int sNumBaseTypes = Array.getLength( sJavaLangTypes );
847      
848         public static String JavaDoc
849     ExpandClassName( final String JavaDoc name )
850     {
851         String JavaDoc fullName = name;
852         
853         final int numTypes = sNumBaseTypes;
854         for( int i = 0; i < numTypes; ++i )
855         {
856             if ( name.equals( sJavaLangTypes[ i ] ) )
857             {
858                 fullName = "java.lang." + name;
859                 break;
860             }
861         }
862         
863         if ( fullName == name ) // no match so far
864
{
865             if ( name.equals( "Number" ) )
866             {
867                 fullName = "java.lang." + name;
868             }
869             else if ( name.equals( "BigDecimal" ) || name.equals( "BigInteger" ) )
870             {
871                 fullName = "java.math." + name;
872             }
873             else if ( name.equals( "URL" ) || name.equals( "URI" ) )
874             {
875                 fullName = "java.net." + name;
876             }
877             else if ( name.equals( "Date" ) )
878             {
879                 fullName = "java.util." + name;
880             }
881             else if ( name.equals( "ObjectName" ) )
882             {
883                 fullName = "javax.management." + name;
884             }
885             
886         }
887         
888         return( fullName );
889     }
890     
891     
892
893     /*
894         Convert inner element. Only works for arrays of Objects. Example:
895         
896         mapActualElementClass( "[[[LObject;", "Long" ) =>[[[LLong;
897      */

898         public static Class JavaDoc
899     convertArrayClass( final Class JavaDoc arrayClass, final Class JavaDoc newInnerType )
900         throws ClassNotFoundException JavaDoc
901     {
902         final String JavaDoc arrayClassname = arrayClass.getName();
903         if ( ! arrayClassname.endsWith( ";" ) )
904         {
905             throw new IllegalArgumentException JavaDoc( "not an array of Object" );
906         }
907         
908         final int innerNameBegin = 1 + arrayClassname.indexOf( "L" );
909         
910         final String JavaDoc newClassName = arrayClassname.substring( 0, innerNameBegin ) + newInnerType.getName() + ";";
911         
912         final Class JavaDoc newClass = getClassFromName( newClassName );
913         
914         return( newClass );
915     }
916     
917     
918 }
919
920
Popular Tags