Package net.sf.okapi.common
Class ClassUtil
- java.lang.Object
-
- net.sf.okapi.common.ClassUtil
-
public class ClassUtil extends Object
Collection of helper functions for working with classes.
-
-
Constructor Summary
Constructors Constructor Description ClassUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringbuildClassPath(Class<?>... classes)static StringbuildClassPath(String... paths)static StringextractPackageName(String className)Extracts the package name part of a qualified class name.static StringextractShortClassName(String className)Extracts the class name part of a qualified class name.static Class<?>getClass(Object obj)Gets the runtime class of a given object.static Class<?>getClass(String className)Gets the class reference for a given qualified class name.static StringgetClassFilePath(Class<?> cls)static StringgetClassName(Class<?> classRef)Gets the non-qualified (w/o package name prefix) class name of a given class reference.static StringgetClassName(Object obj)Gets the non-qualified (without package name prefix) class name for a given object.static StringgetPackageName(Class<?> classRef)Gets the package name of a given class reference.static StringgetPackageName(Object obj)Gets the name of the package containing a given object's class.static StringgetPath(Class<?> cls)static StringgetQualifiedClassName(Class<?> classRef)Gets the qualified class name of a given class reference.static StringgetQualifiedClassName(Object obj)Gets the qualified class name of a given object.static StringgetShortClassName(Class<?> classRef)Gets the non-qualified class name (w/o package name prefix) of a given class reference.static StringgetShortClassName(Object obj)Gets the non-qualified class name (w/o package name prefix) of a given object.static <T> TinstantiateClass(Class<T> classRef)Creates a new instance of a given class.static <T> TinstantiateClass(Class<T> classRef, Object... constructorParameters)Creates a new instance of the class using a given class loader and initialization parameters.static ObjectinstantiateClass(String className)Creates a new instance of the class with a given class name.static ObjectinstantiateClass(String className, ClassLoader classLoader)Creates a new instance of the class with a given class name using a given class loader.static ObjectinstantiateClass(String className, ClassLoader classLoader, Object... constructorParameters)Creates a new instance of the class with a given class name and initialization parameters using a given class loader.static ObjectinstantiateClass(String className, Object... constructorParameters)Creates a new instance of the class with a given class name and initialization parameters.static booleanisInJar(Class<?> cls, String resPath)Detect if a given class is located in a JAR.static StringqualifyName(Class<?> siblingClassRef, String shortClassName)Builds a qualified class name for the given class name.static StringqualifyName(Object sibling, String shortClassName)Builds a qualified class name for the given class name.static StringqualifyName(String packageName, String shortClassName)Builds a qualified class name from given parts.
-
-
-
Method Detail
-
getClass
public static Class<?> getClass(Object obj)
Gets the runtime class of a given object.- Parameters:
obj- The given object- Returns:
- The object's runtime class
-
getClass
public static Class<?> getClass(String className)
Gets the class reference for a given qualified class name.- Parameters:
className- The given class name- Returns:
- Class reference
-
getClassName
public static String getClassName(Object obj)
Gets the non-qualified (without package name prefix) class name for a given object.- Parameters:
obj- The given object- Returns:
- The object's class name (w/o package name prefix)
-
getClassName
public static String getClassName(Class<?> classRef)
Gets the non-qualified (w/o package name prefix) class name of a given class reference.- Parameters:
classRef- The given class reference- Returns:
- The name of the class (w/o package name prefix)
-
getQualifiedClassName
public static String getQualifiedClassName(Object obj)
Gets the qualified class name of a given object.- Parameters:
obj- The given object- Returns:
- Qualified class name
-
getQualifiedClassName
public static String getQualifiedClassName(Class<?> classRef)
Gets the qualified class name of a given class reference.- Parameters:
classRef- The given class reference- Returns:
- Qualified class name
-
getShortClassName
public static String getShortClassName(Class<?> classRef)
Gets the non-qualified class name (w/o package name prefix) of a given class reference.- Parameters:
classRef- The given class reference- Returns:
- Non-qualified class name
-
getShortClassName
public static String getShortClassName(Object obj)
Gets the non-qualified class name (w/o package name prefix) of a given object.- Parameters:
obj- The given object- Returns:
- Non-qualified class name
-
getPackageName
public static String getPackageName(Object obj)
Gets the name of the package containing a given object's class.- Parameters:
obj- The given object- Returns:
- Package name of the given object's class (w/o the trailing dot), or an empty string
-
getPackageName
public static String getPackageName(Class<?> classRef)
Gets the package name of a given class reference.- Parameters:
classRef- The given class reference- Returns:
- Package name of the given class reference (w/o the trailing dot), or an empty string
-
extractPackageName
public static String extractPackageName(String className)
Extracts the package name part of a qualified class name.- Parameters:
className- Qualified class name- Returns:
- Package name (w/o the trailing dot)
-
extractShortClassName
public static String extractShortClassName(String className)
Extracts the class name part of a qualified class name.- Parameters:
className- Qualified class name- Returns:
- Class name
-
qualifyName
public static String qualifyName(String packageName, String shortClassName)
Builds a qualified class name from given parts.- Parameters:
packageName- Package nameshortClassName- Class name- Returns:
- Qualified class name
-
qualifyName
public static String qualifyName(Class<?> siblingClassRef, String shortClassName)
Builds a qualified class name for the given class name. Package name is determined from a reference to another class in the same package.- Parameters:
siblingClassRef- Reference to another class in the same packageshortClassName- Non-qualified name of the class to get a qualified name for- Returns:
- Qualified class name
-
qualifyName
public static String qualifyName(Object sibling, String shortClassName)
Builds a qualified class name for the given class name. Package name is determined from an instance of another class in the same package.- Parameters:
sibling- Existing object, an instance of another class in the same packageshortClassName- Non-qualified name of the class to get a qualified name for- Returns:
- Qualified class name
-
instantiateClass
public static <T> T instantiateClass(Class<T> classRef) throws InstantiationException, IllegalAccessException
Creates a new instance of a given class.- Type Parameters:
T- the type of the given class- Parameters:
classRef- The given class- Returns:
- a newly created instance of the given class
- Throws:
InstantiationException- when we fail to create an instance.IllegalAccessException- when trying to create an instance but don't have the right.
-
instantiateClass
public static Object instantiateClass(String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException
Creates a new instance of the class with a given class name.- Parameters:
className- The given class name- Returns:
- a newly created instance of the class with the given class name
- Throws:
InstantiationException- when we fail to create an instance.IllegalAccessException- when trying to create an instance but don't have the right.ClassNotFoundException- when no definition for the class was found
-
instantiateClass
public static Object instantiateClass(String className, ClassLoader classLoader) throws InstantiationException, IllegalAccessException, ClassNotFoundException
Creates a new instance of the class with a given class name using a given class loader.- Parameters:
className- The given class nameclassLoader- The class loader from which the class must be loaded- Returns:
- A newly created instance of the desired class.
- Throws:
InstantiationException- when we fail to create an instance.IllegalAccessException- when trying to create an instance but don't have the right.ClassNotFoundException- when no definition for the class was found
-
instantiateClass
public static <T> T instantiateClass(Class<T> classRef, Object... constructorParameters) throws SecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException
Creates a new instance of the class using a given class loader and initialization parameters.- Type Parameters:
T- the type of the class to create- Parameters:
classRef- The given classconstructorParameters- The initialization parameters for the class constructor- Returns:
- A newly created instance of the desired class
- Throws:
SecurityException- if we don't have the right to create the classNoSuchMethodException- when a constructor cannot be foundIllegalArgumentException- if the classRef was null.InstantiationException- when we fail to create an instance.IllegalAccessException- when trying to create an instance but don't have the right.InvocationTargetException- when the constructor throws an exception.
-
instantiateClass
public static Object instantiateClass(String className, Object... constructorParameters) throws SecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
Creates a new instance of the class with a given class name and initialization parameters.- Parameters:
className- The given class nameconstructorParameters- The initialization parameters for the class constructor- Returns:
- A newly created instance of the desired class
- Throws:
SecurityException- if we don't have the right to create the classNoSuchMethodException- when a constructor cannot be foundIllegalArgumentException- if the class name is null or emptyInstantiationException- when we fail to create an instance.IllegalAccessException- when trying to create an instance but don't have the right.InvocationTargetException- when the constructor throws an exception.ClassNotFoundException- when no definition for the class was found
-
instantiateClass
public static Object instantiateClass(String className, ClassLoader classLoader, Object... constructorParameters) throws SecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
Creates a new instance of the class with a given class name and initialization parameters using a given class loader.- Parameters:
className- The given class nameclassLoader- The given class loaderconstructorParameters- The initialization parameters for the class constructor- Returns:
- A newly created instance of the desired class
- Throws:
SecurityException- if we don't have the right to create the classNoSuchMethodException- when a constructor cannot be foundIllegalArgumentException- if the class name is null or empty, or if the classLoader is null.InstantiationException- when we fail to create an instance.IllegalAccessException- when trying to create an instance but don't have the right.InvocationTargetException- when the constructor throws an exception.ClassNotFoundException- when no definition for the class was found
-
isInJar
public static boolean isInJar(Class<?> cls, String resPath)
Detect if a given class is located in a JAR.- Parameters:
cls- the given class.resPath- resource path inside the jar- Returns:
- true if the class is located in a JAR.
-
-