com.go.teatools
Class TeaToolsUtils

java.lang.Object
  |
  +--com.go.tea.runtime.DefaultContext
        |
        +--com.go.teatools.TeaToolsUtils
All Implemented Interfaces:
Context, OutputReceiver, TeaToolsConstants, UtilityContext
Direct Known Subclasses:
AdminApplication.ContextImpl

public class TeaToolsUtils
extends DefaultContext
implements TeaToolsConstants

A Tea Tool's best friend. This class has several useful methods for writing tools that work with Tea. Many of these methods were taken from Kettle 3.0.x so that they could be reused in future versions and in other applications.

This class was written with the intent that it could be used as a tea context class. It provides a collection of functions to make introspection possible from Tea.

Version:
15 , 12/06/00
Author:
Mark Masse

Fields inherited from interface com.go.teatools.TeaToolsConstants
BEGIN_CODE_TAG, DEFAULT_CONTEXT_CLASS, EMPTY_CLASS_ARRAY, EMPTY_OBJECT_ARRAY, END_CODE_TAG, IMPLICIT_TEA_IMPORTS, TEA_FILE_EXTENSION
 
Constructor Summary
TeaToolsUtils()
          Creates a new TeaToolsUtils
 
Method Summary
 boolean acceptsSubstitution(java.lang.reflect.Method m)
          Returns true if the specified method accepts a Substitution as its last parameter.
 boolean acceptsSubstitution(java.beans.MethodDescriptor md)
          Returns true if the specified method accepts a Substitution as its last parameter.
 boolean compareFileExtension(java.lang.String fileName, java.lang.String extension)
          Returns true if the specified fileName ends with the specified file extension.
 java.lang.String convertArrayClassName(java.lang.String className, int bracketIndex)
          Converts the user-friendly array class name to the VM friendly one.
 java.lang.Class createContextClass(java.lang.ClassLoader loader, java.lang.Class[] classes, java.lang.String[] prefixNames)
          Merges several classes together, producing a new class that has all of the methods of the combined classes.
 java.lang.Class createContextClass(java.lang.ClassLoader loader, ContextClassEntry[] contextClasses)
          Merges several classes together, producing a new class that has all of the methods of the combined classes.
 MethodDescription[] createMethodDescriptions(java.beans.MethodDescriptor[] mds)
          Returns an array of MethodDescriptions to wrap and describe the specified MethodDescriptors.
 ParameterDescription[] createParameterDescriptions(java.beans.MethodDescriptor md)
          Returns an array of ParameterDescriptions to wrap and describe the parameters of the specified MethodDescriptor.
 java.lang.String createPatternString(java.lang.String pattern, int length)
          Creates a String with the specified pattern repeated length times.
 PropertyDescription[] createPropertyDescriptions(java.beans.PropertyDescriptor[] pds)
          Returns an array of PropertyDescriptions to wrap and describe the specified PropertyDescriptors.
 TypeDescription createTypeDescription(java.lang.Class type)
          Returns a TypeDescription object to wrap and describe the specified type.
 java.lang.String createWhitespaceString(int length)
          Creates a String of spaces with the specified length.
 java.lang.String decapitalize(java.lang.String name)
          Converts a string to normal Java variable name capitalization.
 AccessFlags getAccessFlags(int modifier)
          Returns a AccessFlags instance that can be used to check the modifier int returned by the Class.getModifiers or Member.getModifiers method.
 java.lang.String getArrayClassName(java.lang.Class clazz)
          Formats the class name with trailing square brackets.
 int getArrayDimensions(java.lang.Class clazz)
          Returns the array dimensions.
 java.lang.String getArrayDimensionsString(java.lang.Class clazz)
          Returns the array dimensions String (i.e.
 java.lang.Class getArrayType(java.lang.Class clazz)
          Returns the array type.
 java.lang.Object getAttributeValue(java.beans.FeatureDescriptor feature, java.lang.String attributeName)
          Retrieves the value of the named FeatureDescriptor attribute.
 java.beans.BeanInfo getBeanInfo(java.lang.Class beanClass)
          Introspects a Java bean to learn about all its properties, exposed methods, and events.
 java.beans.BeanInfo getBeanInfo(java.lang.Class beanClass, java.lang.Class stopClass)
          Introspects a Java bean to learn all about its properties, exposed methods, below a given "stop" point.
 java.lang.Class getClassForName(java.lang.String className)
          Returns a Class object for a given name.
 java.lang.Class getClassForName(java.lang.String className, java.lang.ClassLoader classLoader)
          Returns a Class object for a given name.
 java.lang.String getClassName(java.lang.Class clazz)
          Returns the class name of the specified class.
 java.lang.String getClassName(java.lang.String fullClassName)
          Returns the class name of the specified class.
 java.lang.String getClassPackage(java.lang.Class clazz)
          Returns the package name of the specified class.
 java.lang.String getClassPackage(java.lang.String fullClassName)
          Returns the package name of the specified class.
 java.lang.String getClassTypeName(java.lang.Class clazz)
          Returns the type of the specified class.
 java.lang.String getDescription(java.beans.FeatureDescriptor feature)
          Returns the FeatureDescriptor's shortDescription or "" if the shortDescription is the same as the displayName.
 java.lang.String getDescriptionFirstSentence(java.beans.FeatureDescriptor feature)
          Returns the first sentence of the FeatureDescriptor's shortDescription.
 java.lang.String getFirstSentence(java.lang.String paragraph)
          Returns the first sentence of the specified paragraph.
 java.lang.String getFullClassName(java.lang.Class clazz)
          Returns the full class name of the specified class.
 java.lang.String getFullClassName(java.lang.String fullClassName)
          Returns the full class name of the specified class.
 java.lang.String getInnerClassName(java.lang.String className)
          Returns the className with '$'s changed to '.'s
 java.lang.String getPackageVersion(java.lang.String packageName)
          Create a version information string based on what the build process provided.
 java.beans.PropertyDescriptor[] getTeaBeanPropertyDescriptors(java.lang.Class beanClass)
          A function that returns an array of all the available properties on a given class.
 java.beans.MethodDescriptor[] getTeaContextMethodDescriptors(java.lang.Class contextClass)
          Gets the MethodDescriptors of the specified context class including all of the MethodDescriptors for methods declared in the class's superclass and interfaces
 java.beans.MethodDescriptor[] getTeaContextMethodDescriptors(java.lang.Class[] contextClasses)
          Gets the complete, combined set of MethodDescriptors for the specified context classes.
 java.beans.MethodDescriptor[] getTeaContextMethodDescriptors(java.lang.Class contextClass, boolean specifiedClassOnly)
          Gets the MethodDescriptors of the specified context class
 java.lang.String getTeaFullClassName(java.lang.Class clazz)
          Returns the full class name of the specified class.
 boolean isForeachCompatible(java.lang.Class clazz)
          Returns true if the specifed class is compatible with Tea's foreach statement.
 boolean isIfCompatible(java.lang.Class clazz)
          Returns true if the specifed class is compatible with Tea's if statement.
 boolean isImplicitTeaImport(java.lang.Class clazz)
          Returns true if the specified class is implicitly imported by Tea.
 boolean isImplicitTeaImport(java.lang.String classOrPackageName)
          Returns true if the specified class or package is implicitly imported by Tea.
 boolean isLikelyContextClass(java.lang.Class clazz)
          Returns true if it is likely that the specified class serves as a Tea runtime context class.
 boolean isTeaFileName(java.lang.String fileName)
          Returns true if the specifed fileName is a Tea file.
 java.lang.Object[] loadContextClasses(java.lang.ClassLoader loader, ContextClassEntry[] contextClasses)
          Loads and returns the runtime context classes specified by the ContextClassEntry array.
static void main(java.lang.String[] args)
          Test program
 java.lang.String[] parseClassName(java.lang.String fullClassName)
          Splits a class name into two strings.
 void print(java.lang.Object obj)
          Method that is the runtime receiver.
 java.lang.String removeFileExtension(java.lang.String fileName)
          Removes the file extension (all text after the last '.' character) from the specified fileName.
 java.beans.FeatureDescriptor[] sortDescriptors(java.beans.FeatureDescriptor[] fds)
          Sorts an array of FeatureDescriptors based on the method name and if these descriptors are MethodDescriptors, by param count as well.
 void sortMethodDescriptors(java.beans.MethodDescriptor[] mds)
          Sorts an array of MethodDescriptors based on the method name and param count.
 void sortPropertyDescriptors(java.beans.PropertyDescriptor[] pds)
          Sorts an array of PropertyDescriptors based on name.
 
Methods inherited from class com.go.tea.runtime.DefaultContext
cardinal, cardinal, currentDate, dateFormat, dateFormat, endsWith, find, find, findFirst, findFirst, findLast, findLast, getAvailableLocales, getAvailableTimeZones, getDateFormat, getDateFormatTimeZone, getLocale, getNullFormat, getNumberFormat, getNumberFormatInfinity, getNumberFormatNaN, nullFormat, numberFormat, numberFormat, ordinal, ordinal, print, print, print, print, print, print, replace, replace, replace, replaceFirst, replaceFirst, replaceLast, replaceLast, setLocale, setLocale, setLocale, shortOrdinal, shortOrdinal, startsWith, substring, substring, toLowerCase, toString, toString, toString, toString, toString, toString, toString, toString, toUpperCase, trim, trimLeading, trimTrailing
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TeaToolsUtils

public TeaToolsUtils()
Creates a new TeaToolsUtils
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Test program

print

public void print(java.lang.Object obj)
           throws java.lang.Exception
Description copied from class: DefaultContext
Method that is the runtime receiver. Implementations should call one of the toString methods when converting this object to a string.

NOTE: This method should not be called directly within a template.

Overrides:
print in class DefaultContext
Following copied from class: com.go.tea.runtime.DefaultContext
See Also:
Compiler.getRuntimeReceiver()

createTypeDescription

public TypeDescription createTypeDescription(java.lang.Class type)
Returns a TypeDescription object to wrap and describe the specified type.

createPropertyDescriptions

public PropertyDescription[] createPropertyDescriptions(java.beans.PropertyDescriptor[] pds)
Returns an array of PropertyDescriptions to wrap and describe the specified PropertyDescriptors.

createMethodDescriptions

public MethodDescription[] createMethodDescriptions(java.beans.MethodDescriptor[] mds)
Returns an array of MethodDescriptions to wrap and describe the specified MethodDescriptors.

createParameterDescriptions

public ParameterDescription[] createParameterDescriptions(java.beans.MethodDescriptor md)
Returns an array of ParameterDescriptions to wrap and describe the parameters of the specified MethodDescriptor.

getAccessFlags

public AccessFlags getAccessFlags(int modifier)
Returns a AccessFlags instance that can be used to check the modifier int returned by the Class.getModifiers or Member.getModifiers method.

getClassForName

public java.lang.Class getClassForName(java.lang.String className)
Returns a Class object for a given name. Primitive classes can be loaded via thier normal names (i.e. "float"). Array classes can be loaded using either the normal Java name (i.e. int[][]) or the VM name (i.e. [[I).

Note this method swallows all exceptions and simply returns null if the class could not be loaded.

Parameters:
className - the name of the Class

getClassForName

public java.lang.Class getClassForName(java.lang.String className,
                                       java.lang.ClassLoader classLoader)
Returns a Class object for a given name. Primitive classes can be loaded via thier normal names (i.e. "float"). Array classes can be loaded using either the normal Java name (i.e. int[][]) or the VM name (i.e. [[I).

Note this method swallows all exceptions and simply returns null if the class could not be loaded.

Parameters:
className - the name of the Class
classLoader - the ClassLoader to use

getFullClassName

public java.lang.String getFullClassName(java.lang.Class clazz)
Returns the full class name of the specified class. This method provides special formatting for array and inner classes.

getFullClassName

public java.lang.String getFullClassName(java.lang.String fullClassName)
Returns the full class name of the specified class. This method provides special formatting for inner classes.

getClassName

public java.lang.String getClassName(java.lang.Class clazz)
Returns the class name of the specified class. The class name returned does not include the package. This method provides special formatting for array and inner classes.

getClassName

public java.lang.String getClassName(java.lang.String fullClassName)
Returns the class name of the specified class. The class name returned does not include the package. This method provides special formatting for inner classes.

getClassPackage

public java.lang.String getClassPackage(java.lang.Class clazz)
Returns the package name of the specified class. Returns "" if the class has no package.

getClassPackage

public java.lang.String getClassPackage(java.lang.String fullClassName)
Returns the package name of the specified class. Returns null if the class has no package.

getClassTypeName

public java.lang.String getClassTypeName(java.lang.Class clazz)
Returns the type of the specified class.


getPackageVersion

public java.lang.String getPackageVersion(java.lang.String packageName)
Create a version information string based on what the build process provided. The string is of the form "M.m.r" or "M.m.r.bbbb" (i.e. 1.1.0.0004) if the build number can be retrieved. Returns null if the version string cannot be retrieved.

parseClassName

public java.lang.String[] parseClassName(java.lang.String fullClassName)
Splits a class name into two strings.
[0] = package name (or null if the class is unpackaged)
[1] = class name

getArrayClassName

public java.lang.String getArrayClassName(java.lang.Class clazz)
Formats the class name with trailing square brackets.

getArrayType

public java.lang.Class getArrayType(java.lang.Class clazz)
Returns the array type. Returns the specified class if it is not an array.

getArrayDimensions

public int getArrayDimensions(java.lang.Class clazz)
Returns the array dimensions. Returns 0 if the specified class is not an array.

getArrayDimensionsString

public java.lang.String getArrayDimensionsString(java.lang.Class clazz)
Returns the array dimensions String (i.e. "[][][]"). Returns "" (empty string) if the specified class is not an array.

convertArrayClassName

public java.lang.String convertArrayClassName(java.lang.String className,
                                              int bracketIndex)
Converts the user-friendly array class name to the VM friendly one. For example:
Parameters:
className - the name of the array class
bracketIndex - the index (withing className) of the first '[' character

getInnerClassName

public java.lang.String getInnerClassName(java.lang.String className)
Returns the className with '$'s changed to '.'s

getBeanInfo

public java.beans.BeanInfo getBeanInfo(java.lang.Class beanClass)
                                throws java.beans.IntrospectionException
Introspects a Java bean to learn about all its properties, exposed methods, and events.
Parameters:
beanClass - the bean class to be analyzed

getBeanInfo

public java.beans.BeanInfo getBeanInfo(java.lang.Class beanClass,
                                       java.lang.Class stopClass)
                                throws java.beans.IntrospectionException
Introspects a Java bean to learn all about its properties, exposed methods, below a given "stop" point.
Parameters:
beanClass - the bean class to be analyzed
stopClass - the base class at which to stop the analysis. Any methods/properties/events in the stopClass or in its baseclasses will be ignored in the analysis

getAttributeValue

public java.lang.Object getAttributeValue(java.beans.FeatureDescriptor feature,
                                          java.lang.String attributeName)
Retrieves the value of the named FeatureDescriptor attribute.
Returns:
The value of the attribute. May be null if the attribute is unknown.

decapitalize

public java.lang.String decapitalize(java.lang.String name)
Converts a string to normal Java variable name capitalization. This normally means converting the first character from upper case to lower case, but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone.

Thus "FooBar" becomes "fooBar" and "X" becomes "x", but "URL" stays as "URL".

Parameters:
name - the string to be decapitalized
Returns:
the decapitalized version of the string

getFirstSentence

public java.lang.String getFirstSentence(java.lang.String paragraph)
Returns the first sentence of the specified paragraph. Uses java.text.BreakIterator.getSentenceInstance()

createPatternString

public java.lang.String createPatternString(java.lang.String pattern,
                                            int length)
Creates a String with the specified pattern repeated length times.

createWhitespaceString

public java.lang.String createWhitespaceString(int length)
Creates a String of spaces with the specified length.

getDescription

public java.lang.String getDescription(java.beans.FeatureDescriptor feature)
Returns the FeatureDescriptor's shortDescription or "" if the shortDescription is the same as the displayName.

getDescriptionFirstSentence

public java.lang.String getDescriptionFirstSentence(java.beans.FeatureDescriptor feature)
Returns the first sentence of the FeatureDescriptor's shortDescription. Returns "" if the shortDescription is the same as the displayName (the default for reflection-generated FeatureDescriptors).

sortDescriptors

public java.beans.FeatureDescriptor[] sortDescriptors(java.beans.FeatureDescriptor[] fds)
Sorts an array of FeatureDescriptors based on the method name and if these descriptors are MethodDescriptors, by param count as well. To prevent damage to the original array, a clone is made, sorted, and returned from this method.

sortMethodDescriptors

public void sortMethodDescriptors(java.beans.MethodDescriptor[] mds)
Sorts an array of MethodDescriptors based on the method name and param count.

sortPropertyDescriptors

public void sortPropertyDescriptors(java.beans.PropertyDescriptor[] pds)
Sorts an array of PropertyDescriptors based on name.

createContextClass

public java.lang.Class createContextClass(java.lang.ClassLoader loader,
                                          ContextClassEntry[] contextClasses)
                                   throws java.lang.Exception
Merges several classes together, producing a new class that has all of the methods of the combined classes. All methods in the combined class delegate to instances of the source classes. If multiple classes implement the same method, the first one provided is used. The merged class implements all of the interfaces provided by the source classes or interfaces.

This method uses com.go.trove.util.MergedClass


createContextClass

public java.lang.Class createContextClass(java.lang.ClassLoader loader,
                                          java.lang.Class[] classes,
                                          java.lang.String[] prefixNames)
                                   throws java.lang.Exception
Merges several classes together, producing a new class that has all of the methods of the combined classes. All methods in the combined class delegate to instances of the source classes. If multiple classes implement the same method, the first one provided is used. The merged class implements all of the interfaces provided by the source classes or interfaces.

This method uses com.go.trove.util.MergedClass


loadContextClasses

public java.lang.Object[] loadContextClasses(java.lang.ClassLoader loader,
                                             ContextClassEntry[] contextClasses)
                                      throws java.lang.Exception
Loads and returns the runtime context classes specified by the ContextClassEntry array.
Returns:
a 2 element Object array:
 
     index [0] is the Class[] containing the context classes.
     index [1] is the String[] containing the context prefix names.

isLikelyContextClass

public boolean isLikelyContextClass(java.lang.Class clazz)
Returns true if it is likely that the specified class serves as a Tea runtime context class.

getTeaContextMethodDescriptors

public java.beans.MethodDescriptor[] getTeaContextMethodDescriptors(java.lang.Class contextClass)
Gets the MethodDescriptors of the specified context class including all of the MethodDescriptors for methods declared in the class's superclass and interfaces
Parameters:
contextClass - the Tea context Class to introspect (any class will work fine)

getTeaContextMethodDescriptors

public java.beans.MethodDescriptor[] getTeaContextMethodDescriptors(java.lang.Class contextClass,
                                                                    boolean specifiedClassOnly)
Gets the MethodDescriptors of the specified context class
Parameters:
contextClass - the Tea context Class to introspect (any class will work fine)
specifiedClassOnly - true indicates that this function should only return MethodDescriptors declared by the specified Class.

getTeaContextMethodDescriptors

public java.beans.MethodDescriptor[] getTeaContextMethodDescriptors(java.lang.Class[] contextClasses)
Gets the complete, combined set of MethodDescriptors for the specified context classes.
Parameters:
contextClasses - the Tea context classes to introspect

getTeaBeanPropertyDescriptors

public java.beans.PropertyDescriptor[] getTeaBeanPropertyDescriptors(java.lang.Class beanClass)
A function that returns an array of all the available properties on a given class.

NOTE: If possible, the results of this method should be cached by the caller.

Parameters:
beanClass - the bean class to introspect
Returns:
an array of all the available properties on the specified class.

getTeaFullClassName

public java.lang.String getTeaFullClassName(java.lang.Class clazz)
Returns the full class name of the specified class. This method provides special formatting for array and inner classes. If the specified class is implicitly imported by Tea, then its package is omitted in the returned name.

isImplicitTeaImport

public boolean isImplicitTeaImport(java.lang.Class clazz)
Returns true if the specified class is implicitly imported by Tea.

Returns true if the specified class represents a primitive type or a class or interface defined in one of the IMPLICIT_TEA_IMPORTS packages. This method also works for array types.


isImplicitTeaImport

public boolean isImplicitTeaImport(java.lang.String classOrPackageName)
Returns true if the specified class or package is implicitly imported by Tea.

acceptsSubstitution

public boolean acceptsSubstitution(java.beans.MethodDescriptor md)
Returns true if the specified method accepts a Substitution as its last parameter.

acceptsSubstitution

public boolean acceptsSubstitution(java.lang.reflect.Method m)
Returns true if the specified method accepts a Substitution as its last parameter.

isForeachCompatible

public boolean isForeachCompatible(java.lang.Class clazz)
Returns true if the specifed class is compatible with Tea's foreach statement. Compatibility implies that the class can be iterated on by the foreach.

isIfCompatible

public boolean isIfCompatible(java.lang.Class clazz)
Returns true if the specifed class is compatible with Tea's if statement. Only Boolean.class and boolean.class qualify.

removeFileExtension

public java.lang.String removeFileExtension(java.lang.String fileName)
Removes the file extension (all text after the last '.' character) from the specified fileName.
Parameters:
fileName - the String with an (optional) extension
Returns:
the String param sans extension

isTeaFileName

public boolean isTeaFileName(java.lang.String fileName)
Returns true if the specifed fileName is a Tea file.

compareFileExtension

public boolean compareFileExtension(java.lang.String fileName,
                                    java.lang.String extension)
Returns true if the specified fileName ends with the specified file extension.