FileDocCategorySizeDatePackage
TypeUtil.javaAPI DocGlassfish v2 API17308Fri May 04 22:32:08 BST 2007com.sun.enterprise.util

TypeUtil

public class TypeUtil extends Object
Datatype management utility methods

Fields Summary
private static final char[]
digits
private static Hashtable
primitiveClasses_
private static final byte[]
charval
Constructors Summary
Methods Summary
public static java.lang.StringaddCommas(float f)
Add commas to a number for "123,456.7" style formatting.

deprecated
Use standard java.* APIs which create the correct localized number format.

	String floatStr = truncateFloat(f, 0);
	return addCommas(floatStr);
    
public static java.lang.StringaddCommas(java.lang.String numStr)
Add commas to a number for "123,456.7" style formatting.

deprecated
Use standard java.* APIs which create the correct localized number format.

	int dotIndex = numStr.lastIndexOf('.");
	String n;

	String fraction = "";
	if (dotIndex >= 0) {
	    fraction = numStr.substring(dotIndex);
	    n = numStr.substring(0, dotIndex);
	} else {
	    n = numStr;
	}

	String val = "";
	int lastIndex = 0;
	for (int i = n.length(); i > 0; i -= 3) {
	    String comma;
	    if (i > 3) {
		comma = ",";
	    } else {
		comma = "";
	    }
	    int start = Math.max(i - 3, 0);
	    val = comma + n.substring(start, i) + val;
	    lastIndex = start;
	}
	val = n.substring(0, lastIndex) + val + fraction;
	return val;
    
public static java.lang.StringarrayToString(java.lang.String[] from, java.lang.String separator)
Convert an array of strings to a single line with elements separated by the given separator. Similar to Tcl's join.

param
from the array of strings to convert
param
separator the string to insert between each element

	StringBuffer sb = new StringBuffer(100);
	String sep = "";
	for (int i = 0; i < from.length; i++) {
	   sb.append(sep);
	   sb.append(from[i]);
	   sep = separator;
	}
	return sb.toString();
    
public static java.lang.reflect.MethodgetDeclaredMethod(java.lang.Class declaringClass, java.lang.ClassLoader loader, java.lang.String name, java.lang.String[] paramClassNames)


        Class[] parameterTypes=null;
        if (paramClassNames!=null) {       
            parameterTypes = new Class[paramClassNames.length];
            for(int pIndex = 0; pIndex < parameterTypes.length; pIndex++) {
                String next = paramClassNames[pIndex];
                if( primitiveClasses_.containsKey(next) ) {
                    parameterTypes[pIndex] = 
                        (Class) primitiveClasses_.get(next);
                } else {
                    parameterTypes[pIndex] = Class.forName(next, true, loader);
                }
            }
        }
        return declaringClass.getDeclaredMethod(name, parameterTypes);
    
public static java.lang.reflect.MethodgetMethod(java.lang.Class declaringClass, java.lang.ClassLoader loader, java.lang.String name, java.lang.String[] paramClassNames)


        Class[] parameterTypes=null;
        if (paramClassNames!=null) {       
            parameterTypes = new Class[paramClassNames.length];
            for(int pIndex = 0; pIndex < parameterTypes.length; pIndex++) {
                String next = paramClassNames[pIndex];
                if( primitiveClasses_.containsKey(next) ) {
                    parameterTypes[pIndex] = 
                        (Class) primitiveClasses_.get(next);
                } else {
                    parameterTypes[pIndex] = Class.forName(next, true, loader);
                }
            }
        }
        return declaringClass.getMethod(name, parameterTypes);
    
public static java.util.VectorgetPossibleCmpCmrFields(java.lang.ClassLoader cl, java.lang.String className)


        Vector fieldDescriptors = new Vector();
        Class theClass = cl.loadClass(className);

        // Start with all *public* methods
        Method[] methods = theClass.getMethods();

        // Find all accessors that could be cmp fields. This list 
        // will contain all cmr field accessors as well, since there
        // is no good way to distinguish between the two purely based
        // on method signature.
        for(int mIndex = 0; mIndex < methods.length; mIndex++) {
            Method next = methods[mIndex];
            String nextName = next.getName();
            int nextModifiers = next.getModifiers();
            if( Modifier.isAbstract(nextModifiers) ) {
                if( nextName.startsWith("get") &&
                    nextName.length() > 3 ) {
                    String field = 
                        nextName.substring(3,4).toLowerCase() + 
                        nextName.substring(4);
                    fieldDescriptors.add(new FieldDescriptor(field));
                }
            }
        }
        return fieldDescriptors;
    
public static java.util.SetgetSuperInterfaces(java.lang.ClassLoader cl, java.lang.String className, java.lang.String baseClassName)
Get all super-interfaces of a class, excluding the given base interface. Returns a set of strings containing class names.

        Set allSuper = new HashSet();
        if( !className.equals(baseClassName) ) {
            Class theClass          = cl.loadClass(className);
            Class[] superInterfaces = theClass.getInterfaces();

            for(int superIndex = 0; superIndex < superInterfaces.length; superIndex++) {
                Class currentClass      = superInterfaces[superIndex];
                String currentClassName = currentClass.getName();
                if( !currentClassName.equals(baseClassName) ) {
                    allSuper.add(currentClassName);
                    allSuper.addAll(getSuperInterfaces(cl, currentClassName, baseClassName));
                }
            } // End for -- each super interface
        }
        return allSuper;
    
public static inthashCode(java.lang.String s)
Work around a performance bug in String.hashCode() for strings longer than sixteen characters, by calculating a (slower) hash on all the characters in the string. Not needed starting in the JDK 1.2 release.

	int length = s.length();
	int h = 1;

	for (int i = 0; i < length; i++)
	    h = (h * 37) + (int) s.charAt(i);
	return h;
    
public static intintGetBytes(int src, byte[] buf, int offset)
Place a byte representation of src into the byte array buf. No commas or any other formatting is done to the integer.

param
src - the integer to convert. Must not be Integer.MIN_VALUE.
param
buf - the buf to put the result in
param
offset - the offset in buf to place the first digit
return
the number of bytes added to buf
exception
IllegalArgumentException if src is Integer.MIN_VALUE.


                                                                                
       
	 
	 
	 
     
	int power = 1000000000;  // magnitude of highest digit this can handle
	int this_digit;
	boolean have_emitted = false;
	int init_offset = offset;

	// special case src is zero
	if (src == 0) {
	    buf[offset] = charval[0];
	    return 1;
	}
	else if (src < 0) {
	    if (src == Integer.MIN_VALUE)
		throw new IllegalArgumentException();
	    
	    // emit the negation sign and continue as if positive
	    buf[offset++] = (byte) '-";
	    src = Math.abs(src);
	}

	// iterate until there are no more digits to emit
	while (power > 0) {
	    this_digit = src / power;
	    if (this_digit != 0 || have_emitted) {
		// emit this digit
		have_emitted = true;
		buf[offset++] = charval[this_digit];
	    }
	    src = src % power;
	    power = power / 10;
	}
	return offset - init_offset;
    
public static intintGetChars(int src, char[] buf, int offset)
Place a character representation of src into the buffer. No formatting (e.g. localization) is done.

param
src - the integer to convert. Must not be Integer.MIN_VALUE.
param
buf - the buf to put the result in
param
offset - the offset in buf to place the first digit
return
the number of bytes added to buf
exception
IllegalArgumentException if src is Integer.MIN_VALUE.


     
        primitiveClasses_ = new Hashtable();
        primitiveClasses_.put(Character.TYPE.getName(), Character.TYPE);
        primitiveClasses_.put(Boolean.TYPE.getName(), Boolean.TYPE);
        primitiveClasses_.put(Byte.TYPE.getName(), Byte.TYPE);
        primitiveClasses_.put(Integer.TYPE.getName(), Integer.TYPE);
        primitiveClasses_.put(Long.TYPE.getName(), Long.TYPE);
        primitiveClasses_.put(Short.TYPE.getName(), Short.TYPE);
        primitiveClasses_.put(Float.TYPE.getName(), Float.TYPE);
        primitiveClasses_.put(Double.TYPE.getName(), Double.TYPE);
    
	int power = 1000000000;  // magnitude of highest digit this can handle
	int this_digit;
	boolean have_emitted = false;
	int init_offset = offset;

	// special case src is zero
	if (src == 0) {
	    buf[offset] = digits[0];
	    return 1;
	}
	else if (src < 0) {
	    if (src == Integer.MIN_VALUE)
		throw new IllegalArgumentException();
	    
	    // emit the negation sign and continue as if positive
	    buf[offset++] = '-";
	    src = Math.abs(src);
	}

	// iterate until there are no more digits to emit
	while (power > 0) {
	    this_digit = src / power;
	    if (this_digit != 0 || have_emitted) {
		// emit this digit
		have_emitted = true;
		buf[offset++] = digits[this_digit];
	    }
	    src = src % power;
	    power = power / 10;
	}
	return offset - init_offset;
    
public static booleanisSubclassOf(java.lang.Class sub, java.lang.Class sup)
Test if a class is a subclass of another.

deprecated
Use sup.isAssignableFrom(sub)

	if (sub == sup) {
	    return true;
	}
	Class superclass = sub.getSuperclass();
	while (superclass != null && superclass != sup) {
	    superclass = superclass.getSuperclass();
	}
	return (superclass != null);
    
public static java.lang.StringpropertyNameToSetterMethod(java.lang.String propertyName)
Convert a java beans property name to a setter method name


        if( (propertyName == null) ||
            (propertyName.length() == 0) ) {
            throw new IllegalArgumentException("Invalid property name " +
                                               propertyName);
        } 

        return ( "set" + propertyName.substring(0, 1).toUpperCase() +
                 propertyName.substring(1) );

    
public static booleansameMethodSignature(java.lang.reflect.Method m1, java.lang.reflect.Method m2)
Compares the signatures of two methods to see if they have the same method name, parameters, and return type. Note that this equality check does NOT cover : 1) declaring class 2) exceptions


        boolean same = false;

        Class[] pm1 = m1.getParameterTypes();
        Class[] pm2 = m2.getParameterTypes();
        
        if( (m1.getName().equals(m2.getName())) &&
            (m1.getReturnType() == m2.getReturnType()) ) {
            same = sameParamTypes(m1, m2);
        }

        return same;
    
public static booleansameParamTypes(java.lang.reflect.Method m1, java.lang.reflect.Method m2)
Compares the signatures of two methods to see if they have the same numer of parameters and same parameter types. Note that this equality check does NOT cover : 1) declaring class 2) exceptions 3) method name 4) return type


        boolean same = false;

        Class[] pm1 = m1.getParameterTypes();
        Class[] pm2 = m2.getParameterTypes();
        
        if( (pm1.length == pm2.length) ) {
            same = true;
            for(int i = 0; i < pm1.length; i++) {
                if( pm1[i] != pm2[i] ) {
                    same = false;
                    break;
                }
            }
        }

        return same;
    
public static java.lang.StringsetterMethodToPropertyName(java.lang.String setterMethodName)
Convert a java beans setter method to its property name.


        if( (setterMethodName == null) ||
            (setterMethodName.length() <= 3) ||
            !setterMethodName.startsWith("set") ) {
            throw new IllegalArgumentException("Invalid setter method name " +
                                               setterMethodName);
        } 

        return ( setterMethodName.substring(3, 4).toLowerCase() +
                 setterMethodName.substring(4) );

    
public static java.lang.String[]stringToArray(java.lang.String from, java.lang.String separator)
Convert a string of delimited strings to an array of strings. Similar to AWK's and Tcl's split.

param
from the string to convert
param
separator the delimiter

	if (from == null) {
	    return null;
	}
	if (separator == null) {
	    separator = " ";
	}
	StringTokenizer toks = new StringTokenizer(from, separator);
	String[] result = new String[toks.countTokens()];
	int i = 0;
	while (toks.hasMoreTokens()) {
	    result[i++] = toks.nextToken().trim();
	}
	return result;
    
public static java.lang.StringtruncateFloat(float f, int digits)
Truncate a float to the required number of significant digits.

	double factor = Math.pow(10, digits);
	f = (float)(Math.round(f * factor) / factor);
	return Float.toString(f);
    
public static java.lang.String[]wordWrap(java.lang.String msg, int widthInChars)
Word-wrap a string into an array of strings. Space is the only separator character recognized.

	int width = widthInChars;
	int nextBreak =0;
	int lastBreak = 0;
	int length = msg.length();
	int lengthLeft = length;
	boolean breakFound = true;
	Vector v = new Vector();
	int nextNewline = msg.indexOf("\n");
	    
	while (lengthLeft > width || nextNewline != -1) {
	    // Find a convenient word break, always respecting explicit line
	    // breaks.
	    nextBreak = nextNewline;

	    // If no newline, look for a space.
	    if (nextBreak == -1 || 
		nextBreak <= lastBreak ||
		nextBreak > lastBreak + width) {
		nextBreak = msg.lastIndexOf(" ", lastBreak + width);
	    }

	    // No space, break it at the wrap width.
	    if (nextBreak == -1 || nextBreak <= lastBreak) {
		nextBreak = lastBreak + width - 1;
		breakFound = false;
		if (nextBreak > length) {
		    break;
		}
	    }

	    // Save the substring and adjust indexes.
	    String substr = msg.substring(lastBreak, nextBreak);
	    v.addElement(substr);
	    lengthLeft -= substr.length();

	    lastBreak = nextBreak;
	    if (breakFound) {
		++lastBreak;
	    }
	    breakFound = true;
	    nextNewline = msg.indexOf("\n", lastBreak);
	}

	v.addElement(msg.substring(lastBreak));
	String[] lines = new String[v.size()];
	v.copyInto(lines);
	return lines;