Author: bayard
Date: Thu Jan 17 00:05:23 2008
New Revision: 612749
URL: http://svn.apache.org/viewvc?rev=612749&view=rev
Log:
Applying my patch from LANG-298, based on Tomasz Blachowicz's original patch,
and adds getPackageCanonicalName and getShortCanonicalName sets of methods
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java
commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java?rev=612749&r1=612748&r2=612749&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java
(original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java
Thu Jan 17 00:05:23 2008
@@ -35,6 +35,7 @@
* @author Gary Gregory
* @author Norm Deane
* @author Alban Peignier
+ * @author Tomasz Blachowicz
* @since 2.0
* @version $Id$
*/
@@ -94,15 +95,35 @@
* Maps a primitive class name to its corresponding abbreviation used in
array class names.
*/
private static Map abbreviationMap = new HashMap();
+
+ /**
+ * Maps an abbreviation used in array class names to corresponding
primitive class name.
+ */
+ private static Map reverseAbbreviationMap = new HashMap();
+
+ /**
+ * Add primitive type abbreviation to maps of abbreviations.
+ *
+ * @param primitive Canonical name of primitive type
+ * @param abbreviation Corresponding abbreviation of primitive type
+ */
+ private static void addAbbreviation(String primitive, String abbreviation)
{
+ abbreviationMap.put(primitive, abbreviation);
+ reverseAbbreviationMap.put(abbreviation, primitive);
+ }
+
+ /**
+ * Feed abbreviation maps
+ */
static {
- abbreviationMap.put( "int", "I" );
- abbreviationMap.put( "boolean", "Z" );
- abbreviationMap.put( "float", "F" );
- abbreviationMap.put( "long", "J" );
- abbreviationMap.put( "short", "S" );
- abbreviationMap.put( "byte", "B" );
- abbreviationMap.put( "double", "D" );
- abbreviationMap.put( "char", "C" );
+ addAbbreviation("int", "I");
+ addAbbreviation("boolean", "Z");
+ addAbbreviation("float", "F");
+ addAbbreviation("long", "J");
+ addAbbreviation("short", "S");
+ addAbbreviation("byte", "B");
+ addAbbreviation("double", "D");
+ addAbbreviation("char", "C");
}
/**
@@ -605,7 +626,7 @@
String clsName = "[" + abbreviationMap.get(className);
clazz = Class.forName(clsName, initialize,
classLoader).getComponentType();
} else {
- clazz = Class.forName(toProperClassName(className), initialize,
classLoader);
+ clazz = Class.forName(toCanonicalName(className), initialize,
classLoader);
}
return clazz;
}
@@ -718,7 +739,7 @@
* @param className the class name
* @return the converted name
*/
- private static String toProperClassName(String className) {
+ private static String toCanonicalName(String className) {
className = StringUtils.deleteWhitespace(className);
if (className == null) {
throw new NullArgumentException("className");
@@ -760,5 +781,144 @@
classes[i] = array[i].getClass();
}
return classes;
+ }
+
+ // Short canonical name
+ // ----------------------------------------------------------------------
+ /**
+ * <p>Gets the canonical name minus the package name for an
<code>Object</code>.</p>
+ *
+ * @param object the class to get the short name for, may be null
+ * @param valueIfNull the value to return if null
+ * @return the canonical name of the object without the package name, or
the null value
+ * @since 2.4
+ */
+ public static String getShortCanonicalName(Object object, String
valueIfNull) {
+ if (object == null) {
+ return valueIfNull;
+ }
+ return getShortCanonicalName(object.getClass().getName());
+ }
+
+ /**
+ * <p>Gets the canonical name minus the package name from a
<code>Class</code>.</p>
+ *
+ * @param cls the class to get the short name for.
+ * @return the canonical name without the package name or an empty string
+ * @since 2.4
+ */
+ public static String getShortCanonicalName(Class cls) {
+ if (cls == null) {
+ return StringUtils.EMPTY;
+ }
+ return getShortCanonicalName(cls.getName());
+ }
+
+ /**
+ * <p>Gets the canonical name minus the package name from a String.</p>
+ *
+ * <p>The string passed in is assumed to be a canonical name - it is not
checked.</p>
+ *
+ * @param className the className to get the short name for
+ * @return the canonical name of the class without the package name or an
empty string
+ * @since 2.4
+ */
+ public static String getShortCanonicalName(String canonicalName) {
+ return ClassUtils.getShortClassName(getCanonicalName(canonicalName));
+ }
+
+ // Package name
+ // ----------------------------------------------------------------------
+ /**
+ * <p>Gets the package name from the canonical name of an
<code>Object</code>.</p>
+ *
+ * @param object the class to get the package name for, may be null
+ * @param valueIfNull the value to return if null
+ * @return the package name of the object, or the null value
+ * @since 2.4
+ */
+ public static String getPackageCanonicalName(Object object, String
valueIfNull) {
+ if (object == null) {
+ return valueIfNull;
+ }
+ return getPackageCanonicalName(object.getClass().getName());
+ }
+
+ /**
+ * <p>Gets the package name from the canonical name of a
<code>Class</code>.</p>
+ *
+ * @param cls the class to get the package name for, may be
<code>null</code>.
+ * @return the package name or an empty string
+ * @since 2.4
+ */
+ public static String getPackageCanonicalName(Class cls) {
+ if (cls == null) {
+ return StringUtils.EMPTY;
+ }
+ return getPackageCanonicalName(cls.getName());
+ }
+
+ /**
+ * <p>Gets the package name from the canonical name. </p>
+ *
+ * <p>The string passed in is assumed to be a canonical name - it is not
checked.</p>
+ * <p>If the class is unpackaged, return an empty string.</p>
+ *
+ * @param canonicalName the canonical name to get the package name for,
may be <code>null</code>
+ * @return the package name or an empty string
+ * @since 2.4
+ */
+ public static String getPackageCanonicalName(String canonicalName) {
+ return ClassUtils.getPackageName(getCanonicalName(canonicalName));
+ }
+
+ /**
+ * <p>Converts a given name of class into canonical format.
+ * If name of class is not a name of array class it returns
+ * unchanged name.</p>
+ * <p>Example:
+ * <ul>
+ * <li><code>getCanonicalName("[I") = "int[]"</code></li>
+ * <li><code>getCanonicalName("[Ljava.lang.String;") =
"java.lang.String[]"</code></li>
+ * <li><code>getCanonicalName("java.lang.String") =
"java.lang.String"</code></li>
+ * </ul>
+ * </p>
+ *
+ * @param className the name of class
+ * @return canonical form of class name
+ * @since 2.4
+ */
+ private static String getCanonicalName(String className) {
+ className = StringUtils.deleteWhitespace(className);
+ if (className == null) {
+ return null;
+ } else {
+ int dim = 0;
+ while(className.startsWith("[")) {
+ dim++;
+ className = className.substring(1);
+ }
+ if(dim < 1) {
+ return className;
+ } else {
+ if(className.startsWith("L")) {
+ className = className.substring(
+ 1,
+ className.endsWith(";")
+ ? className.length() - 1
+ : className.length());
+ } else {
+ if(className.length() > 0) {
+ className = (String) reverseAbbreviationMap.get(
+ className.substring(0, 1));
+ }
+ }
+ StringBuffer canonicalClassNameBuffer = new
StringBuffer(className);
+ for(int i = 0; i < dim; i++) {
+ canonicalClassNameBuffer.append("[]");
+ }
+ return canonicalClassNameBuffer.toString();
+ }
+ }
}
}
Modified:
commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java?rev=612749&r1=612748&r2=612749&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java
(original)
+++
commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java
Thu Jan 17 00:05:23 2008
@@ -38,6 +38,7 @@
*
* @author Stephen Colebourne
* @author Gary D. Gregory
+ * @author Tomasz Blachowicz
* @version $Id$
*/
public class ClassUtilsTest extends TestCase {
@@ -597,6 +598,69 @@
assertEquals("String", ClassUtils.getShortClassName(results[0]));
assertEquals("Integer", ClassUtils.getShortClassName(results[1]));
assertEquals("Double", ClassUtils.getShortClassName(results[2]));
+ }
+
+ public void test_getShortCanonicalName_Object() {
+ assertEquals("<null>", ClassUtils.getShortCanonicalName(null,
"<null>"));
+ assertEquals("ClassUtils", ClassUtils.getShortCanonicalName(new
ClassUtils(), "<null>"));
+ assertEquals("ClassUtils[]", ClassUtils.getShortCanonicalName(new
ClassUtils[0], "<null>"));
+ assertEquals("ClassUtils[][]", ClassUtils.getShortCanonicalName(new
ClassUtils[0][0], "<null>"));
+ assertEquals("int[]", ClassUtils.getShortCanonicalName(new int[0],
"<null>"));
+ assertEquals("int[][]", ClassUtils.getShortCanonicalName(new
int[0][0], "<null>"));
+ }
+
+ public void test_getShortCanonicalName_Class() {
+ assertEquals("ClassUtils",
ClassUtils.getShortCanonicalName(ClassUtils.class));
+ assertEquals("ClassUtils[]",
ClassUtils.getShortCanonicalName(ClassUtils[].class));
+ assertEquals("ClassUtils[][]",
ClassUtils.getShortCanonicalName(ClassUtils[][].class));
+ assertEquals("int[]", ClassUtils.getShortCanonicalName(int[].class));
+ assertEquals("int[][]",
ClassUtils.getShortCanonicalName(int[][].class));
+ }
+
+ public void test_getShortCanonicalName_String() {
+ assertEquals("ClassUtils",
ClassUtils.getShortCanonicalName("org.apache.commons.lang.ClassUtils"));
+ assertEquals("ClassUtils[]",
ClassUtils.getShortCanonicalName("[Lorg.apache.commons.lang.ClassUtils;"));
+ assertEquals("ClassUtils[][]",
ClassUtils.getShortCanonicalName("[[Lorg.apache.commons.lang.ClassUtils;"));
+ assertEquals("ClassUtils[]",
ClassUtils.getShortCanonicalName("org.apache.commons.lang.ClassUtils[]"));
+ assertEquals("ClassUtils[][]",
ClassUtils.getShortCanonicalName("org.apache.commons.lang.ClassUtils[][]"));
+ assertEquals("int[]", ClassUtils.getShortCanonicalName("[I"));
+ assertEquals("int[][]", ClassUtils.getShortCanonicalName("[[I"));
+ assertEquals("int[]", ClassUtils.getShortCanonicalName("int[]"));
+ assertEquals("int[][]", ClassUtils.getShortCanonicalName("int[][]"));
+ }
+
+ public void test_getPackageCanonicalName_Object() {
+ assertEquals("<null>", ClassUtils.getPackageCanonicalName(null,
"<null>"));
+ assertEquals("org.apache.commons.lang",
ClassUtils.getPackageCanonicalName(new ClassUtils(), "<null>"));
+ assertEquals("org.apache.commons.lang",
ClassUtils.getPackageCanonicalName(new ClassUtils[0], "<null>"));
+ assertEquals("org.apache.commons.lang",
ClassUtils.getPackageCanonicalName(new ClassUtils[0][0], "<null>"));
+ assertEquals("", ClassUtils.getPackageCanonicalName(new int[0],
"<null>"));
+ assertEquals("", ClassUtils.getPackageCanonicalName(new int[0][0],
"<null>"));
+ }
+
+ public void test_getPackageCanonicalName_Class() {
+ assertEquals("org.apache.commons.lang",
ClassUtils.getPackageCanonicalName(ClassUtils.class));
+ assertEquals("org.apache.commons.lang",
ClassUtils.getPackageCanonicalName(ClassUtils[].class));
+ assertEquals("org.apache.commons.lang",
ClassUtils.getPackageCanonicalName(ClassUtils[][].class));
+ assertEquals("", ClassUtils.getPackageCanonicalName(int[].class));
+ assertEquals("", ClassUtils.getPackageCanonicalName(int[][].class));
+ }
+
+ public void test_getPackageCanonicalName_String() {
+ assertEquals("org.apache.commons.lang",
+
ClassUtils.getPackageCanonicalName("org.apache.commons.lang.ClassUtils"));
+ assertEquals("org.apache.commons.lang",
+
ClassUtils.getPackageCanonicalName("[Lorg.apache.commons.lang.ClassUtils;"));
+ assertEquals("org.apache.commons.lang",
+
ClassUtils.getPackageCanonicalName("[[Lorg.apache.commons.lang.ClassUtils;"));
+ assertEquals("org.apache.commons.lang",
+
ClassUtils.getPackageCanonicalName("org.apache.commons.lang.ClassUtils[]"));
+ assertEquals("org.apache.commons.lang",
+
ClassUtils.getPackageCanonicalName("org.apache.commons.lang.ClassUtils[][]"));
+ assertEquals("", ClassUtils.getPackageCanonicalName("[I"));
+ assertEquals("", ClassUtils.getPackageCanonicalName("[[I"));
+ assertEquals("", ClassUtils.getPackageCanonicalName("int[]"));
+ assertEquals("", ClassUtils.getPackageCanonicalName("int[][]"));
}
}