Class ClassHelper

java.lang.Object
com.tangosol.util.ClassHelper

public abstract class ClassHelper extends Object
This abstract class contains dynamic (reflect-based) class, method, and field manipulation methods.

Note: This class is primarily for supporting generated code.

Version:
1.00, 11/22/96
Author:
Cameron Purdy
  • Field Details

    • VOID_PARAMS

      public static final Class[] VOID_PARAMS
      Useful constant for methods with no parameters.
    • VOID

      public static final Object[] VOID
      Useful constant for methods with no arguments.
    • REFLECT_FILTER_SEPARATOR

      public static final String REFLECT_FILTER_SEPARATOR
      Separator for filter patterns.
      See Also:
    • REFLECT_FILTER_PROPERTY

      public static final String REFLECT_FILTER_PROPERTY
      Configure reflection allowed class white list and/or black list by setting the value of this system property to a set of patterns.

      Patterns are separated by ";" (semicolon). Whitespace is significant and is considered part of the pattern.

      • If the pattern starts with "!", the class is rejected if the remaining pattern is matched; otherwise the class is allowed if the pattern matches.
      • If the pattern ends with ".**" it matches any class in the package and all subpackages.
      • If the pattern ends with ".*" it matches any class in the package.
      • If the pattern ends with "*", it matches any class with the pattern as a prefix.
      • If the pattern is equal to the class name, it matches.
      • Otherwise, the pattern is not matched.

      The resulting filter tries to match the class, if any. The first pattern that matches, working from left to right, determines if a reflection is allowed on a class by the filter or rejected. If none of the patterns match the class, reflection will be allowed.

      See Also:
    • REFLECT_ALLOW_ALL

      public static final String REFLECT_ALLOW_ALL
      Value to set system property REFLECT_FILTER_PROPERTY to disable the reflection allowed filter.
      See Also:
    • DEFAULT_REFLECT_ALLOWED_BLACKLIST

      public static final String DEFAULT_REFLECT_ALLOWED_BLACKLIST
      Default reflection filter list which disables reflection against the following types:
      • java.lang.Class
      • java.lang.System
      • java.lang.Runtime
      See Also:
    • DEFAULT_FILTER_LIST

      public static final String DEFAULT_FILTER_LIST
      Composed of DEFAULT_REFLECT_ALLOWED_BLACKLIST followed by allowing reflection against all other classes.
      See Also:
  • Constructor Details

    • ClassHelper

      public ClassHelper()
  • Method Details

    • isInstanceOf

      public static boolean isInstanceOf(Object obj, String sClz)
      Determine if the passed object is an instance of the specified class.
      Parameters:
      obj - the object
      sClz - the class name
      Returns:
      true iff the passed object is an instance of the specified class
    • isInstanceOf

      public static boolean isInstanceOf(Object obj, Class clz)
      Determine if the passed object is an instance of the specified class.
      Parameters:
      obj - the object
      clz - the class
      Returns:
      true iff the passed object is an instance of the specified class
    • getPackageName

      public static String getPackageName(Class clz)
      Determine the package name of a class.
      Parameters:
      clz - the class to determine the package of
      Returns:
      the name of the package, including a trailing dot (which signifies that the string is a package name)
    • getPackageName

      public static String getPackageName(String sName)
      Determine the package name from a class (or package) name.
         in      out
         ------- -------
         [blank] [blank]
         a       [blank]
         .a      .a.
         a.      a.
         .a.     .a.
         a.b     a.
         .a.b    .a.
         a.b.    a.b.
         .a.b.   .a.b.
       
      Parameters:
      sName - the class name to determine the package of
      Returns:
      the name of the package, including a trailing dot (which signifies that the string is a package name)
    • getSimpleName

      public static String getSimpleName(Class clz)
      Determine the simple (unqualified) name of a class.
      Parameters:
      clz - the class to determine the simple name of
      Returns:
      the simple name of the class
    • getSimpleName

      public static String getSimpleName(String sName)
      Determine the simple (unqualified) name of a class.
         in      out
         ------- -------
         [blank] [blank]
         a       a
         .a      [blank]
         a.      [blank]
         .a.     [blank]
         a.b     b
         .a.b    b
         a.b.    [blank]
         .a.b.   [blank]
       
      Parameters:
      sName - the simple or qualified name of the class (or package)
      Returns:
      the simple name of the class
    • getQualifiedName

      public static String getQualifiedName(String pkg, String sName)
      Build the fully qualified name of a class based on a package name and a simple class name.
      Parameters:
      pkg - package name
      sName - simple class name
      Returns:
      fully qualified class name
    • getCompositePackage

      public static String getCompositePackage(String pkg1, String pkg2)
      Build the composite name of a package based on two package names. If there is a conflict, the second package name over-rides the first.
         pkg1        pkg2            composite
         ----------- --------------  ----------------------
         [blank]     [blank]         [blank]
         [blank]     yourpkg.        yourpkg.
         [blank]     .yourpkg.       .yourpkg.
         mypkg.      [blank]         mypkg.
         .mypkg.     [blank]         .mypkg.
         mypkg.      yourpkg.        yourpkg.
         mypkg.      .yourpkg.       mypkg.yourpkg.
         .mypkg.     .yourpkg.       .mypkg.yourpkg.
       
      Parameters:
      pkg1 - the first (base) package
      pkg2 - the second (extending/overriding) package
      Returns:
      the composite package name, including a trailing dot
    • getCompositeName

      public static String getCompositeName(Class clz, String sName)
      Build the composite name of a class based on an existing class and a second class name.
      Parameters:
      clz - the existing class
      sName - the name to use to build the composite class name
      Returns:
      the composite name of the class
    • getCompositeName

      public static String getCompositeName(String sName1, String sName2)
      Build the composite name of a class based on an existing class name and a second class name. For example:
         Class       Name           Derived
         ----------- -------------- ----------------------
         Test        Net            Net
         mypkg.Test  Net            mypkg.Net
         mypkg.Test  yourpkg.       yourpkg.Test
         mypkg.Test  .yourpkg       mypkg.yourpkg.Test
         mypkg.Test  .yourpkg.      mypkg.yourpkg.Test
         mypkg.Test  yourpkg.Net    yourpkg.Net
         mypkg.Test  .yourpkg.Net   mypkg.yourpkg.Net
         mypkg.Test  .yourpkg.Net.  mypkg.yourpkg.Net.Test
       
      Parameters:
      sName1 - the existing class name
      sName2 - the name to use to build the composite class name
      Returns:
      the composite name of the class
    • getDerivedName

      public static String getDerivedName(Class clz, String sPrefix)
      Build the name of a "derived" class based on an existing class and a "derived" class prefix.
      Parameters:
      clz - the existing class
      sPrefix - the prefix to apply to the existing class to determine the name of the "derived" class
      Returns:
      the expected name of the "derived" class
    • getDerivedName

      public static String getDerivedName(String sName, String sPrefix)
      Build the name of a "derived" class based on the name of an existing class and a "derived" class prefix. For example:
         Class       Prefix        Derived
         ----------- ------------- ----------------------
         Test        Net           NetTest
         mypkg.Test  Net           mypkg.NetTest
         mypkg.Test  yourpkg.      yourpkg.Test
         mypkg.Test  .yourpkg      mypkg.yourpkg.Test
         mypkg.Test  .yourpkg.     mypkg.yourpkg.Test
         mypkg.Test  yourpkg.Net   yourpkg.NetTest
         mypkg.Test  .yourpkg.Net  mypkg.yourpkg.NetTest
         mypkg.Test  .yourpkg.Net. mypkg.yourpkg.Net.Test
       
      Parameters:
      sName - the existing class name
      sPrefix - the prefix to apply to the existing class to determine the name of the "derived" class
      Returns:
      the expected name of the "derived" class
    • isPartialNameLegal

      public static boolean isPartialNameLegal(String sName)
      Determine if a partial name (for example, a prefix, class name, package name, etc. as accepted by the above methods) is legal.
      Parameters:
      sName - the partial name
      Returns:
      true if the name is a legal partial name
    • isSimpleNameLegal

      public static boolean isSimpleNameLegal(String sName)
      Determine if the passed string is a legal simple name. This does not check if the name is reserved by the Java language.
      Parameters:
      sName - the string containing the name
      Returns:
      true if a legal name, false otherwise
    • isQualifiedNameLegal

      public static boolean isQualifiedNameLegal(String sName)
      Determine if the passed string is a legal dot-delimited identifier.
      Parameters:
      sName - the string containing the dot-delimited identifier
      Returns:
      true if a legal identifier, false otherwise
    • getClassArray

      public static Class[] getClassArray(Object[] aoParam)
      Calculate the class array based on the parameter array.
      Parameters:
      aoParam - the parameter array
      Returns:
      the class array based on the parameter array
    • unwrap

      public static Class[] unwrap(Class[] aClasses)
      Replace wrapper types with appropriate primitive types.
      Parameters:
      aClasses - an array of classes
      Returns:
      the class array with primitive instead of wrapper types
    • getPackageResources

      public static com.tangosol.util.Resources getPackageResources(String sClass) throws MissingResourceException
      Load the default package resources for the specified class name.
      Parameters:
      sClass - the class name (fully qualified) to get the resources for
      Returns:
      the resource bundle for the class's package
      Throws:
      MissingResourceException
      See Also:
    • getPackageResources

      public static com.tangosol.util.Resources getPackageResources(Class clz) throws MissingResourceException
      Load the default package resources for the specified class.
      Parameters:
      clz - the class to get the resources for
      Returns:
      the resource bundle for the class's package
      Throws:
      MissingResourceException
      See Also:
    • getResources

      public static com.tangosol.util.Resources getResources(Class clz, String sName) throws MissingResourceException
      Load the named resources for the specified class.
      Parameters:
      clz - the class to get the resources for
      sName - the name of the resource class
      Returns:
      the resource bundle for the class's package
      Throws:
      MissingResourceException
    • newInstance

      public static Object newInstance(Class clz, Object[] aoParam) throws InstantiationException, InvocationTargetException
      Instantiate the specified class using the specified parameters.
      Parameters:
      clz - the class to instantiate
      aoParam - the constructor parameters
      Returns:
      a new instance of the specified class
      Throws:
      InstantiationException - if an exception is raised trying to instantiate the object, whether the exception is a security, method access, no such method, or instantiation exception
      InvocationTargetException - if the constructor of the new object instance raises an exception
    • invokeStatic

      public static Object invokeStatic(Class clz, String sName, Object[] aoParam) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
      Invoke the specified static method using the passed arguments.
      Parameters:
      clz - the class to invoke the static method of
      sName - the method name
      aoParam - the method arguments
      Returns:
      the return value (if any) from the method
      Throws:
      NoSuchMethodException - if method with given name cannot be found
      IllegalAccessException - if the current method does not have access
      InvocationTargetException - if an error occurs during invocation
    • invoke

      public static Object invoke(Object obj, String sName, Object[] aoParam) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
      Invoke the specified instance method using the passed arguments.
      Parameters:
      obj - the object to invoke the instance method of
      sName - the method name
      aoParam - the method arguments
      Returns:
      the return value (if any) from the method
      Throws:
      NoSuchMethodException - if method with given name cannot be found
      IllegalAccessException - if the current method does not have access
      InvocationTargetException - if an error occurs during invocation
    • invoke

      public static Object invoke(Class clz, Object obj, String sName, Object[] aoParam) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
      Invoke the specified method using the passed arguments.
      Parameters:
      clz - the class to invoke the method on
      obj - the object to invoke the method on
      sName - the method name
      aoParam - the method arguments
      Returns:
      the return value (if any) from the method invocation
      Throws:
      NoSuchMethodException - if method with given name cannot be found
      IllegalAccessException - if the current method does not have access
      InvocationTargetException - if an error occurs during invocation
    • findMethod

      public static Method findMethod(Class clz, String sName, Class[] aclzParam, boolean fStatic)
      Find a Method that matches the specified name and parameter types. If there are more than one matching methods, the first one will be returned.
      Parameters:
      clz - the class reference
      sName - the method name
      aclzParam - the parameter types (some array elements could be null)
      fStatic - the method scope flag
      Returns:
      the matching Method object or null if no match could be found
    • getComponentType

      public static Class<?> getComponentType(Type type)
      Obtains the component type of a collection or array (taking generic declarations into account).
      Parameters:
      type - the collection or array Type
      Returns:
      the component type of the collection or null if the specified class is not a collection
    • getClass

      public static Class<?> getClass(Type type)
      Obtains the most concrete runtime Class that may be used for the specified Type (taking generic declarations into account).
      Parameters:
      type - the Type
      Returns:
      the runtime Class for the Type.
    • getReifiedTypes

      public static Map<String,Type[]> getReifiedTypes(Class<?> clz, Class<?> clzDefiningType)
      Returns a map of types by type name as defined in clzDefiningType. Each type is reified by traversing the class hierarchy commencing from the first found reference of clzDefiningType in clz. The traversing completes when either a concrete substitution is found or the hierarchy can no longer be traversed, in which case the bounds will be returned.
      
           public class MyCallable
                   implements Callable<Boolean>
               {
               ...
               }
      
           Map<String, Type[]> mapTypes = ClassHelper.getReifiedTypes(MyCallable.class, Callable.class);
           assert mapTypes.get("V")[0] == Boolean.class;
      
       
      The returned map is ordered thus can be traversed assuming each entry being returned is in the same order as the definition of the generic types in clzDefiningType.
      Parameters:
      clz - the concrete class that can be assigned to clzDefiningType
      clzDefiningType - the class (interface or abstract class) that holds the types to be reified
      Returns:
      a map of types by type name
    • getFullyQualifiedClassNameOf

      public static String getFullyQualifiedClassNameOf(String sAbbreviatedClassName)
      Determines the fully qualified class name of the Coherence-style abbreviated class name.
      Parameters:
      sAbbreviatedClassName - the abbreviated class name (or a regular class name)
      Returns:
      the fully qualified class name
    • isReflectionAllowed

      public static boolean isReflectionAllowed(Object oTarget)
      Returns true if oTarget is a valid type for reflection operations.
      Parameters:
      oTarget - the reflection target
      Returns:
      true if oTarget is a valid type for reflection operations