public abstract class ClassHelper extends Object
Note: This class is primarily for supporting generated code.
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_FILTER_LIST
Composed of
DEFAULT_REFLECT_ALLOWED_BLACKLIST followed by allowing reflection against all other classes. |
static 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
|
static String |
REFLECT_ALLOW_ALL
Value to set system property
REFLECT_FILTER_PROPERTY to disable the reflection allowed filter. |
static 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.
|
static String |
REFLECT_FILTER_SEPARATOR
Separator for filter patterns.
|
static Object[] |
VOID
Useful constant for methods with no arguments.
|
static Class[] |
VOID_PARAMS
Useful constant for methods with no parameters.
|
Constructor and Description |
---|
ClassHelper() |
Modifier and Type | Method and Description |
---|---|
static Method |
findMethod(Class clz,
String sName,
Class[] aclzParam,
boolean fStatic)
Find a Method that matches the specified name and parameter types.
|
static Class<?> |
getClass(Type type)
|
static Class[] |
getClassArray(Object[] aoParam)
Calculate the class array based on the parameter array.
|
static Class<?> |
getComponentType(Type type)
Obtains the component type of a collection or array (taking generic declarations into account).
|
static String |
getCompositeName(Class clz,
String sName)
Build the composite name of a class based on an existing class and a
second class name.
|
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.
|
static String |
getCompositePackage(String pkg1,
String pkg2)
Build the composite name of a package based on two package names.
|
static String |
getDerivedName(Class clz,
String sPrefix)
Build the name of a "derived" class based on an existing class and a
"derived" class prefix.
|
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.
|
static String |
getFullyQualifiedClassNameOf(String sAbbreviatedClassName)
Determines the fully qualified class name of the Coherence-style abbreviated class name.
|
static String |
getPackageName(Class clz)
Determine the package name of a class.
|
static String |
getPackageName(String sName)
Determine the package name from a class (or package) name.
|
static com.tangosol.util.Resources |
getPackageResources(Class clz)
Load the default package resources for the specified class.
|
static com.tangosol.util.Resources |
getPackageResources(String sClass)
Load the default package resources for the specified class name.
|
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.
|
static Map<String,Type[]> |
getReifiedTypes(Class<?> clz,
Class<?> clzDefiningType)
Returns a map of types by type name as defined in
clzDefiningType . |
static com.tangosol.util.Resources |
getResources(Class clz,
String sName)
Load the named resources for the specified class.
|
static String |
getSimpleName(Class clz)
Determine the simple (unqualified) name of a class.
|
static String |
getSimpleName(String sName)
Determine the simple (unqualified) name of a class.
|
static Object |
invoke(Class clz,
Object obj,
String sName,
Object[] aoParam)
Invoke the specified method using the passed arguments.
|
static Object |
invoke(Object obj,
String sName,
Object[] aoParam)
Invoke the specified instance method using the passed arguments.
|
static Object |
invokeStatic(Class clz,
String sName,
Object[] aoParam)
Invoke the specified static method using the passed arguments.
|
static boolean |
isInstanceOf(Object obj,
Class clz)
Determine if the passed object is an instance of the specified class.
|
static boolean |
isInstanceOf(Object obj,
String sClz)
Determine if the passed object is an instance of the specified class.
|
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.
|
static boolean |
isQualifiedNameLegal(String sName)
Determine if the passed string is a legal dot-delimited identifier.
|
static boolean |
isReflectionAllowed(Object oTarget)
Returns
true if oTarget is a valid type for reflection operations. |
static boolean |
isSimpleNameLegal(String sName)
Determine if the passed string is a legal simple name.
|
static Object |
newInstance(Class clz,
Object[] aoParam)
Instantiate the specified class using the specified parameters.
|
static Class[] |
unwrap(Class[] aClasses)
Replace wrapper types with appropriate primitive types.
|
public static final Class[] VOID_PARAMS
public static final Object[] VOID
public static final String REFLECT_FILTER_SEPARATOR
public static final String REFLECT_FILTER_PROPERTY
Patterns are separated by ";" (semicolon). Whitespace is significant and is considered part of the pattern.
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.
public static final String REFLECT_ALLOW_ALL
REFLECT_FILTER_PROPERTY
to disable the reflection allowed filter.public static final String DEFAULT_REFLECT_ALLOWED_BLACKLIST
public static final String DEFAULT_FILTER_LIST
DEFAULT_REFLECT_ALLOWED_BLACKLIST
followed by allowing reflection against all other classes.public static boolean isInstanceOf(Object obj, String sClz)
obj
- the objectsClz
- the class namepublic static boolean isInstanceOf(Object obj, Class clz)
obj
- the objectclz
- the classpublic static String getPackageName(Class clz)
clz
- the class to determine the package ofpublic static String getPackageName(String sName)
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.
sName
- the class name to determine the package ofpublic static String getSimpleName(Class clz)
clz
- the class to determine the simple name ofpublic static String getSimpleName(String sName)
in out ------- ------- [blank] [blank] a a .a [blank] a. [blank] .a. [blank] a.b b .a.b b a.b. [blank] .a.b. [blank]
sName
- the simple or qualified name of the class (or package)public static String getQualifiedName(String pkg, String sName)
pkg
- package namesName
- simple class namepublic static String getCompositePackage(String pkg1, String pkg2)
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.
pkg1
- the first (base) packagepkg2
- the second (extending/overriding) packagepublic static String getCompositeName(Class clz, String sName)
clz
- the existing classsName
- the name to use to build the composite class namepublic static String getCompositeName(String sName1, String sName2)
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
sName1
- the existing class namesName2
- the name to use to build the composite class namepublic static String getDerivedName(Class clz, String sPrefix)
clz
- the existing classsPrefix
- the prefix to apply to the existing class to determine
the name of the "derived" classpublic static String getDerivedName(String sName, String sPrefix)
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
sName
- the existing class namesPrefix
- the prefix to apply to the existing class to determine
the name of the "derived" classpublic static boolean isPartialNameLegal(String sName)
sName
- the partial namepublic static boolean isSimpleNameLegal(String sName)
sName
- the string containing the namepublic static boolean isQualifiedNameLegal(String sName)
sName
- the string containing the dot-delimited identifierpublic static Class[] getClassArray(Object[] aoParam)
aoParam
- the parameter arraypublic static Class[] unwrap(Class[] aClasses)
aClasses
- an array of classespublic static com.tangosol.util.Resources getPackageResources(String sClass) throws MissingResourceException
sClass
- the class name (fully qualified) to get the resources forMissingResourceException
getResources(java.lang.Class, java.lang.String)
public static com.tangosol.util.Resources getPackageResources(Class clz) throws MissingResourceException
clz
- the class to get the resources forMissingResourceException
getResources(java.lang.Class, java.lang.String)
public static com.tangosol.util.Resources getResources(Class clz, String sName) throws MissingResourceException
clz
- the class to get the resources forsName
- the name of the resource classMissingResourceException
public static Object newInstance(Class clz, Object[] aoParam) throws InstantiationException, InvocationTargetException
clz
- the class to instantiateaoParam
- the constructor parametersInstantiationException
- if an exception is raised trying
to instantiate the object, whether the exception is a
security, method access, no such method, or instantiation
exceptionInvocationTargetException
- if the constructor of the new
object instance raises an exceptionpublic static Object invokeStatic(Class clz, String sName, Object[] aoParam) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
clz
- the class to invoke the static method ofsName
- the method nameaoParam
- the method argumentsNoSuchMethodException
- if method with given name cannot be foundIllegalAccessException
- if the current method does not have accessInvocationTargetException
- if an error occurs during invocationpublic static Object invoke(Object obj, String sName, Object[] aoParam) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
obj
- the object to invoke the instance method ofsName
- the method nameaoParam
- the method argumentsNoSuchMethodException
- if method with given name cannot be foundIllegalAccessException
- if the current method does not have accessInvocationTargetException
- if an error occurs during invocationpublic static Object invoke(Class clz, Object obj, String sName, Object[] aoParam) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
clz
- the class to invoke the method onobj
- the object to invoke the method onsName
- the method nameaoParam
- the method argumentsNoSuchMethodException
- if method with given name cannot be foundIllegalAccessException
- if the current method does not have accessInvocationTargetException
- if an error occurs during invocationpublic static Method findMethod(Class clz, String sName, Class[] aclzParam, boolean fStatic)
clz
- the class referencesName
- the method nameaclzParam
- the parameter types (some array elements could be null)fStatic
- the method scope flagpublic static Class<?> getComponentType(Type type)
type
- the collection or array Type
null
if the specified class is not a collectionpublic static Class<?> getClass(Type type)
Class
that may be used for
the specified Type
(taking generic declarations into account).type
- the Typepublic static Map<String,Type[]> getReifiedTypes(Class<?> clz, Class<?> clzDefiningType)
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
.clz
- the concrete class that can be assigned to
clzDefiningTypeclzDefiningType
- the class (interface or abstract class) that
holds the types to be reifiedpublic static String getFullyQualifiedClassNameOf(String sAbbreviatedClassName)
sAbbreviatedClassName
- the abbreviated class name (or a regular class name)public static boolean isReflectionAllowed(Object oTarget)
true
if oTarget
is a valid type for reflection operations.oTarget
- the reflection targettrue
if oTarget
is a valid type for reflection operations