View Javadoc

    1   package net.sourceforge.pmd.util;
    2
    3   import java.math.BigDecimal;
    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[] {
    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[] {
    27  			Integer.class,
    28  			Byte.class,
    29  			Long.class,
    30  			Short.class,
    31  			Float.class,
    32  			Double.class,
    33  			Character.class,
    34  			Boolean.class,
    35  			BigDecimal.class,
    36  			String.class,
    37  			Object.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 getPrimitiveTypeFor(String 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 getTypeFor(String shortName) {
    58
    59  		Class 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 withoutPackageName(String fullTypeName) {
    76
    77  		int dotPos = fullTypeName.lastIndexOf('.');
    78
    79  		return dotPos > 0 ?
    80  			fullTypeName.substring(dotPos+1) :
    81  			fullTypeName;
    82  	}
    83  }