KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > util > ClassUtil


1 package net.sourceforge.pmd.util;
2
3 import java.math.BigDecimal JavaDoc;
4
5
6 /**
7  * Various class-related utility methods
8  *
9  * @author Brian Remedios
10  */

11 public class ClassUtil {
12
13     private ClassUtil() {};
14     
15     private static final TypeMap primitiveTypesByName = new TypeMap( new Class JavaDoc[] {
16             int.class,
17             byte.class,
18             long.class,
19             short.class,
20             float.class,
21             double.class,
22             char.class,
23             boolean.class,
24             });
25     
26     private static final TypeMap typesByNames = new TypeMap( new Class JavaDoc[] {
27             Integer JavaDoc.class,
28             Byte JavaDoc.class,
29             Long JavaDoc.class,
30             Short JavaDoc.class,
31             Float JavaDoc.class,
32             Double JavaDoc.class,
33             Character JavaDoc.class,
34             Boolean JavaDoc.class,
35             BigDecimal JavaDoc.class,
36             String JavaDoc.class,
37             Object JavaDoc.class,
38             });
39     
40     /**
41      * Returns the type(class) for the name specified
42      * or null if not found.
43      *
44      * @param name String
45      * @return Class
46      */

47     public static Class JavaDoc getPrimitiveTypeFor(String JavaDoc name) {
48         return primitiveTypesByName.typeFor(name);
49     }
50     
51     /**
52      * Attempt to determine the actual class given the short name.
53      *
54      * @param shortName String
55      * @return Class
56      */

57     public static Class JavaDoc getTypeFor(String JavaDoc shortName) {
58         
59         Class JavaDoc type = typesByNames.typeFor(shortName);
60         if (type != null) return type;
61         
62         type = primitiveTypesByName.typeFor(shortName);
63         if (type != null) return type;
64         
65         return CollectionUtil.getCollectionTypeFor(shortName);
66     }
67     /**
68      * Returns the abbreviated name of the type,
69      * without the package name
70      *
71      * @param fullTypeName
72      * @return String
73      */

74             
75     public static String JavaDoc withoutPackageName(String JavaDoc fullTypeName) {
76         
77         int dotPos = fullTypeName.lastIndexOf('.');
78         
79         return dotPos > 0 ?
80             fullTypeName.substring(dotPos+1) :
81             fullTypeName;
82     }
83 }
84
Popular Tags