Class JavaUtils


  • public class JavaUtils
    extends java.lang.Object
    Utility class to deal with Java language related issues, such as type conversions.
    Author:
    Glen Daniels (gdaniels@apache.org)
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  JavaUtils.ConvertCache
      It the argument to the convert(...) method implements the ConvertCache interface, the convert(...) method will use the set/get methods to store and retrieve converted values.
      static class  JavaUtils.HolderException  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static char CR  
      protected static org.apache.commons.logging.Log log  
      static java.lang.String LS
      The prefered line separator
      static char NL  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object convert​(java.lang.Object arg, java.lang.Class destClass)
      Utility function to convert an Object to some desired Class.
      static java.lang.Object getHolderValue​(java.lang.Object holder)
      Gets the Holder value.
      static java.lang.Class getHolderValueType​(java.lang.Class type)
      Determines if the Class is a Holder class.
      static java.awt.Image getImageFromStream​(java.io.InputStream is)  
      static java.lang.String getLoadableClassName​(java.lang.String text)
      Converts text of the form Foo[] to the proper class name for loading [LFoo
      static java.lang.Class getPrimitiveClass​(java.lang.Class wrapper)  
      static java.lang.Class getPrimitiveClassFromName​(java.lang.String primitive)  
      static java.lang.String getTextClassName​(java.lang.String text)
      Converts text of the form [LFoo to the Foo[]
      static java.lang.String getUniqueValue​(java.util.Collection values, java.lang.String initValue)
      Makes the value passed in initValue unique among the String values contained in values by suffixing it with a decimal digit suffix.
      static java.lang.String getWrapper​(java.lang.String primitive)  
      static java.lang.Class getWrapperClass​(java.lang.Class primitive)  
      static boolean isAttachmentSupported()
      Determine whether attachments are supported by checking if the following classes are available: javax.activation.DataHandler, javax.mail.internet.MimeMultipart.
      static boolean isBasic​(java.lang.Class javaType)  
      static boolean isConvertable​(java.lang.Object obj, java.lang.Class dest)  
      static boolean isConvertable​(java.lang.Object obj, java.lang.Class dest, boolean isEncoded)  
      static boolean isEnumClass​(java.lang.Class cls)
      Determine if the class is a JAX-RPC enum class.
      static boolean isFalse​(java.lang.Object value)  
      static boolean isFalse​(java.lang.Object value, boolean defaultVal)
      Tests the Object 'value': if its null, return default.
      static boolean isFalse​(java.lang.String value)
      Tests the String 'value': return 'true' if its 'false', '0', or 'no' - else 'false' Follow in 'C' tradition of boolean values: false is specific (0), everything else is true;
      static boolean isFalseExplicitly​(java.lang.Object value)  
      static boolean isFalseExplicitly​(java.lang.Object value, boolean defaultVal)
      Tests the Object 'value': if its null, return default.
      static boolean isFalseExplicitly​(java.lang.String value)
      Tests the String 'value': return 'true' if its null, 'false', '0', or 'no' - else 'false'
      static boolean isJavaId​(java.lang.String id)
      isJavaId Returns true if the name is a valid java identifier.
      static boolean isJavaKeyword​(java.lang.String keyword)
      checks if the input string is a valid java keyword.
      static boolean isTrue​(java.lang.Object value)  
      static boolean isTrue​(java.lang.Object value, boolean defaultVal)
      Tests the Object 'value': if its null, return default.
      static boolean isTrue​(java.lang.String value)
      Tests the String 'value': return 'false' if its 'false', '0', or 'no' - else 'true' Follow in 'C' tradition of boolean values: false is specific (0), everything else is true;
      static boolean isTrueExplicitly​(java.lang.Object value)  
      static boolean isTrueExplicitly​(java.lang.Object value, boolean defaultVal)
      Tests the Object 'value': if its null, return default.
      static boolean isTrueExplicitly​(java.lang.String value)
      Tests the String 'value': return 'true' if its 'true', '1', or 'yes' - else 'false'
      static java.lang.String makeNonJavaKeyword​(java.lang.String keyword)
      Turn a java keyword string into a non-Java keyword string.
      static java.lang.String mimeToJava​(java.lang.String mime)
      Given the MIME type string, return the Java mapping.
      static java.lang.String replace​(java.lang.String name, java.lang.String oldT, java.lang.String newT)
      replace: Like String.replace except that the old new items are strings.
      static void setHolderValue​(java.lang.Object holder, java.lang.Object value)
      Sets the Holder value.
      static java.lang.String stackToString​(java.lang.Throwable e)  
      static java.lang.String xmlNameToJava​(java.lang.String name)
      Map an XML name to a Java identifier per the mapping rules of JSR 101 (in version 1.0 this is "Chapter 20: Appendix: Mapping of XML Names"
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • log

        protected static org.apache.commons.logging.Log log
      • LS

        public static final java.lang.String LS
        The prefered line separator
    • Method Detail

      • getWrapperClass

        public static java.lang.Class getWrapperClass​(java.lang.Class primitive)
      • getWrapper

        public static java.lang.String getWrapper​(java.lang.String primitive)
      • getPrimitiveClass

        public static java.lang.Class getPrimitiveClass​(java.lang.Class wrapper)
      • getPrimitiveClassFromName

        public static java.lang.Class getPrimitiveClassFromName​(java.lang.String primitive)
      • isBasic

        public static boolean isBasic​(java.lang.Class javaType)
      • convert

        public static java.lang.Object convert​(java.lang.Object arg,
                                               java.lang.Class destClass)
        Utility function to convert an Object to some desired Class. Right now this works for: arrays <-> Lists, Holders <-> held values
        Parameters:
        arg - the array to convert
        destClass - the actual class we want
      • isConvertable

        public static boolean isConvertable​(java.lang.Object obj,
                                            java.lang.Class dest)
      • isConvertable

        public static boolean isConvertable​(java.lang.Object obj,
                                            java.lang.Class dest,
                                            boolean isEncoded)
      • getImageFromStream

        public static java.awt.Image getImageFromStream​(java.io.InputStream is)
      • isJavaId

        public static boolean isJavaId​(java.lang.String id)
        isJavaId Returns true if the name is a valid java identifier.
        Parameters:
        id - to check
        Returns:
        boolean true/false
      • isJavaKeyword

        public static boolean isJavaKeyword​(java.lang.String keyword)
        checks if the input string is a valid java keyword.
        Returns:
        boolean true/false
      • makeNonJavaKeyword

        public static java.lang.String makeNonJavaKeyword​(java.lang.String keyword)
        Turn a java keyword string into a non-Java keyword string. (Right now this simply means appending an underscore.)
      • getLoadableClassName

        public static java.lang.String getLoadableClassName​(java.lang.String text)
        Converts text of the form Foo[] to the proper class name for loading [LFoo
      • getTextClassName

        public static java.lang.String getTextClassName​(java.lang.String text)
        Converts text of the form [LFoo to the Foo[]
      • xmlNameToJava

        public static java.lang.String xmlNameToJava​(java.lang.String name)
        Map an XML name to a Java identifier per the mapping rules of JSR 101 (in version 1.0 this is "Chapter 20: Appendix: Mapping of XML Names"
        Parameters:
        name - is the xml name
        Returns:
        the java name per JSR 101 specification
      • replace

        public static final java.lang.String replace​(java.lang.String name,
                                                     java.lang.String oldT,
                                                     java.lang.String newT)
        replace: Like String.replace except that the old new items are strings.
        Parameters:
        name - string
        oldT - old text to replace
        newT - new text to use
        Returns:
        replacement string
      • getHolderValueType

        public static java.lang.Class getHolderValueType​(java.lang.Class type)
        Determines if the Class is a Holder class. If so returns Class of held type else returns null
        Parameters:
        type - the suspected Holder Class
        Returns:
        class of held type or null
      • setHolderValue

        public static void setHolderValue​(java.lang.Object holder,
                                          java.lang.Object value)
                                   throws JavaUtils.HolderException
        Sets the Holder value.
        Parameters:
        holder - Holder object
        value - is the object value
        Throws:
        JavaUtils.HolderException
      • isEnumClass

        public static boolean isEnumClass​(java.lang.Class cls)
        Determine if the class is a JAX-RPC enum class. An enumeration class is recognized by a getValue() method, a toString() method, a fromString(String) method a fromValue(type) method and the lack of a setValue(type) method
      • stackToString

        public static java.lang.String stackToString​(java.lang.Throwable e)
      • isTrue

        public static final boolean isTrue​(java.lang.String value)
        Tests the String 'value': return 'false' if its 'false', '0', or 'no' - else 'true' Follow in 'C' tradition of boolean values: false is specific (0), everything else is true;
      • isTrueExplicitly

        public static final boolean isTrueExplicitly​(java.lang.String value)
        Tests the String 'value': return 'true' if its 'true', '1', or 'yes' - else 'false'
      • isTrueExplicitly

        public static final boolean isTrueExplicitly​(java.lang.Object value,
                                                     boolean defaultVal)
        Tests the Object 'value': if its null, return default. if its a Boolean, return booleanValue() if its an Integer, return 'false' if its '0' else 'true' if its a String, return isTrueExplicitly((String)value). All other types return 'true'
      • isTrueExplicitly

        public static final boolean isTrueExplicitly​(java.lang.Object value)
      • isTrue

        public static final boolean isTrue​(java.lang.Object value,
                                           boolean defaultVal)
        Tests the Object 'value': if its null, return default. if its a Boolean, return booleanValue() if its an Integer, return 'false' if its '0' else 'true' if its a String, return 'false' if its 'false', 'no', or '0' - else 'true' All other types return 'true'
      • isTrue

        public static final boolean isTrue​(java.lang.Object value)
      • isFalse

        public static final boolean isFalse​(java.lang.String value)
        Tests the String 'value': return 'true' if its 'false', '0', or 'no' - else 'false' Follow in 'C' tradition of boolean values: false is specific (0), everything else is true;
      • isFalseExplicitly

        public static final boolean isFalseExplicitly​(java.lang.String value)
        Tests the String 'value': return 'true' if its null, 'false', '0', or 'no' - else 'false'
      • isFalseExplicitly

        public static final boolean isFalseExplicitly​(java.lang.Object value,
                                                      boolean defaultVal)
        Tests the Object 'value': if its null, return default. if its a Boolean, return !booleanValue() if its an Integer, return 'true' if its '0' else 'false' if its a String, return isFalseExplicitly((String)value). All other types return 'false'
      • isFalseExplicitly

        public static final boolean isFalseExplicitly​(java.lang.Object value)
      • isFalse

        public static final boolean isFalse​(java.lang.Object value,
                                            boolean defaultVal)
        Tests the Object 'value': if its null, return default. if its a Boolean, return booleanValue() if its an Integer, return 'false' if its '0' else 'true' if its a String, return 'false' if its 'false', 'no', or '0' - else 'true' All other types return 'true'
      • isFalse

        public static final boolean isFalse​(java.lang.Object value)
      • mimeToJava

        public static java.lang.String mimeToJava​(java.lang.String mime)
        Given the MIME type string, return the Java mapping.
      • isAttachmentSupported

        public static boolean isAttachmentSupported()
        Determine whether attachments are supported by checking if the following classes are available: javax.activation.DataHandler, javax.mail.internet.MimeMultipart.
      • getUniqueValue

        public static java.lang.String getUniqueValue​(java.util.Collection values,
                                                      java.lang.String initValue)
        Makes the value passed in initValue unique among the String values contained in values by suffixing it with a decimal digit suffix.