public class ClassUtil extends java.lang.Object
Constructor and Description |
---|
ClassUtil() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
filenameToClassname(java.lang.String fileName)
This method checks and transforms the filename of a potential
Class given by fileName . |
static java.lang.Class<?> |
findClass(java.lang.String className)
Get Class object of specified class name.
|
static void |
findClassNameInFile(java.io.File file,
boolean includeJar,
java.util.Set<java.lang.String> classSet,
java.lang.String qualifiedNamePrefix)
Search for all classes in specified file
|
static void |
findClassNameInFile(java.io.File file,
java.util.Set<java.lang.String> classSet,
java.lang.String qualifiedNamePrefix)
Search for all classes in specified file
|
static java.util.Set<java.lang.String> |
findClassNamesInJAR(java.lang.String fileName)
Search for all classes in JAR file
|
static void |
findClassNamesInJAR(java.lang.String fileName,
java.util.Set<java.lang.String> classSet)
Search for all classes in JAR file
|
static java.util.Set<java.lang.String> |
findClassNamesInPackage(java.lang.String packageName,
boolean includeSubPackages)
This method finds all classes that are located in the package identified by the given
packageName . |
static void |
findClassNamesInPackage(java.lang.String packageName,
boolean includeSubPackages,
java.util.Set<java.lang.String> classes)
This method finds all classes that are located in the package identified by the given
packageName . |
static java.util.HashSet<java.lang.String> |
findClassNamesInPath(java.lang.String path,
boolean includeSubDir)
This method finds all classes that are located in the specified directory.
|
static java.util.HashSet<java.lang.String> |
findClassNamesInPath(java.lang.String path,
boolean includeSubDir,
boolean includeJar)
This method finds all classes that are located in the specified directory.
|
static java.util.HashSet<java.lang.String> |
findClassNamesInPath(java.lang.String path,
java.lang.String packageName,
boolean includeSubDir)
This method finds all classes that are located in the specified directory.
|
static java.util.HashSet<java.lang.String> |
findClassNamesInPath(java.lang.String path,
java.lang.String packageName,
boolean includeSubDir,
boolean includeJar)
This method finds all classes that are located in the specified directory.
|
static void |
findClassNamesInPath(java.lang.String path,
java.lang.String packageName,
boolean includeSubDir,
boolean includeJar,
java.util.Set<java.lang.String> classes)
This method finds all classes that are located in the specified directory.
|
static void |
findClassNamesInPath(java.lang.String path,
java.lang.String packageName,
boolean includeSubDir,
java.util.Set<java.lang.String> classes)
This method finds all classes that are located in the specified directory.
|
static void |
findResourceInFile(java.io.File file,
boolean includeJar,
boolean includeHidden,
java.util.List<java.lang.String> result,
java.lang.String basePath)
Search for all classes in specified file
|
static java.lang.String |
fixClassName(java.lang.String fileName)
This method checks and transforms the filename of a potential
Class given by fileName . |
static java.lang.String |
getBaseClassName(java.lang.String className)
Get the base class name
ex : "plugin.myClass$InternClass$1" --> "plugin.myClass" |
static java.lang.ClassLoader |
getContextClassLoader()
Return the current thread context class loader
|
static java.lang.reflect.Field |
getField(java.lang.Object object,
java.lang.String fieldName,
boolean forceAccess)
Deprecated.
Use
ReflectionUtil.getField(Object, String, boolean) instead |
static java.lang.Object |
getFieldObject(java.lang.Object object,
java.lang.String fieldName,
boolean forceAccess)
Deprecated.
|
static java.io.File |
getFile(java.lang.String fullClassName)
Find the file (.jar or .class usually) that host this class.
|
static java.lang.String |
getFirstPackageName(java.lang.String className)
Get first package name
ex : "plugin.test.myClass" --> "plugin" |
static java.lang.Class<?>[] |
getInterfaces(java.lang.Class<?> c)
Return true if clazz implements the specified interface
|
static java.lang.String |
getJarPath(java.lang.Class<?> c)
Returns the source JAR file (if any) from where the specified class has been loaded from
|
static java.util.List<java.lang.Class<?>> |
getLoadedClasses(java.lang.ClassLoader cl)
Return the list of all loaded classes by the specified
ClassLoader . |
static java.lang.reflect.Method |
getMethod(java.lang.Object object,
java.lang.String methodName,
boolean forceAccess,
java.lang.Class<?>... parameterTypes)
Deprecated.
|
static java.lang.String |
getPackageName(java.lang.String className)
Get package name
ex : "plugin.test.myClass" --> "plugin.test" |
static java.lang.String |
getPathFromQualifiedName(java.lang.String qualifiedName)
Transform the specified qualified name in path.
|
static java.lang.Class<?> |
getPrimitiveType(java.lang.String primitiveName) |
static java.lang.String |
getQualifiedNameFromPath(java.lang.String path)
Transform the specified path in qualified name.
|
static java.util.List<java.lang.String> |
getResourcesInPackage(java.lang.String packageName,
java.lang.String extension,
boolean recursive,
boolean includeFolder,
boolean includeJar,
boolean includeHidden)
This method returns all resources that are located in the package identified by the given
packageName . |
static java.util.List<java.lang.String> |
getResourcesInPath(java.lang.String path,
boolean recursive,
boolean includeFolder,
boolean includeJar,
boolean includeHidden)
This method returns all resources that are located in the specified path.
|
static java.util.List<java.lang.String> |
getResourcesInPath(java.lang.String path,
java.lang.String basePath,
boolean recursive,
boolean includeFolder,
boolean includeJar,
boolean includeHidden)
This method returns all resources that are located in the specified path.
|
static void |
getResourcesInPath(java.lang.String path,
java.lang.String basePath,
boolean recursive,
boolean includeFolder,
boolean includeJar,
boolean includeHidden,
java.util.List<java.lang.String> result)
This method returns all resources that are located in the specified path.
|
static java.lang.String |
getSimpleClassName(java.lang.String className)
Get simple class name
ex : "plugin.test.myClass$InternClass$1" --> "myClass$InternClass$1" |
static java.lang.ClassLoader |
getSystemClassLoader()
Return the system class loader
|
static java.lang.Object |
invokeMethod(java.lang.Object object,
java.lang.String methodName,
boolean forceAccess,
java.lang.Object... args)
Deprecated.
|
static boolean |
isAbstract(java.lang.Class<?> c)
Return true if class is abstract
|
static boolean |
isPrivate(java.lang.Class<?> c)
Return true if class is private
|
static boolean |
isPublic(java.lang.Class<?> c)
Return true if class is public
|
static boolean |
isSubClass(java.lang.Class<?> clazz,
java.lang.Class<?> baseClass)
Return true if clazz is the same class or extends baseClass
|
public ClassUtil()
public static java.lang.ClassLoader getContextClassLoader()
public static java.lang.ClassLoader getSystemClassLoader()
public static java.util.List<java.lang.Class<?>> getLoadedClasses(java.lang.ClassLoader cl)
ClassLoader
.null
if an error occurred.public static java.lang.Class<?> getPrimitiveType(java.lang.String primitiveName)
primitiveName
- public static java.lang.Class<?> findClass(java.lang.String className) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
public static java.lang.String getQualifiedNameFromPath(java.lang.String path)
path
- public static java.lang.String getPathFromQualifiedName(java.lang.String qualifiedName)
public static java.lang.String getPackageName(java.lang.String className)
public static java.lang.String getFirstPackageName(java.lang.String className)
public static java.lang.String getBaseClassName(java.lang.String className)
public static java.lang.String getSimpleClassName(java.lang.String className)
public static java.lang.String getJarPath(java.lang.Class<?> c)
public static java.lang.Class<?>[] getInterfaces(java.lang.Class<?> c)
public static boolean isAbstract(java.lang.Class<?> c)
public static boolean isPublic(java.lang.Class<?> c)
public static boolean isPrivate(java.lang.Class<?> c)
public static boolean isSubClass(java.lang.Class<?> clazz, java.lang.Class<?> baseClass)
public static java.util.List<java.lang.String> getResourcesInPackage(java.lang.String packageName, java.lang.String extension, boolean recursive, boolean includeFolder, boolean includeJar, boolean includeHidden) throws java.io.IOException
packageName
.packageName
- is the name of the Package
to scan (ex: "java.awt.metrics")extension
- resource extension if we want to retrieve only a specific type of resource (ex: ".class")recursive
- if set to true
files from sub packages/folder are also returned.includeFolder
- if true
folder entry are also returnedincludeJar
- if true
all sub JAR files are also scannedincludeHidden
- if true
all hidden files (starting by '.' character) are also scannedjava.io.IOException
public static java.util.List<java.lang.String> getResourcesInPath(java.lang.String path, boolean recursive, boolean includeFolder, boolean includeJar, boolean includeHidden)
path
- path to scan.recursive
- if true
all sub folder are also scanned.includeJar
- if true
all JAR files are also scannedincludeHidden
- if true
all hidden files (starting by '.' character) are also scannedpublic static java.util.List<java.lang.String> getResourcesInPath(java.lang.String path, java.lang.String basePath, boolean recursive, boolean includeFolder, boolean includeJar, boolean includeHidden)
path
- path to scan.basePath
- path prefixrecursive
- if true
all sub folder are also scanned.includeJar
- if true
all JAR files are also scannedincludeHidden
- if true
all hidden files (starting by '.' character) are also scannedpublic static void getResourcesInPath(java.lang.String path, java.lang.String basePath, boolean recursive, boolean includeFolder, boolean includeJar, boolean includeHidden, java.util.List<java.lang.String> result)
path
- path to scan.basePath
- path prefixrecursive
- if true
all sub folder are also scanned.includeJar
- if true
all JAR files are also scannedincludeHidden
- if true
all hidden files (starting by '.' character) are also scannedresult
- result listpublic static void findResourceInFile(java.io.File file, boolean includeJar, boolean includeHidden, java.util.List<java.lang.String> result, java.lang.String basePath)
public static java.util.Set<java.lang.String> findClassNamesInPackage(java.lang.String packageName, boolean includeSubPackages) throws java.io.IOException
packageName
.packageName
- is the name of the Package
to scan.includeSubPackages
- - if true
all sub-packages of the specified Package
will be
included in the search.java.io.IOException
- if the operation failed with an I/O error.public static void findClassNamesInPackage(java.lang.String packageName, boolean includeSubPackages, java.util.Set<java.lang.String> classes) throws java.io.IOException
packageName
.packageName
- is the name of the Package
to scan.includeSubPackages
- - if true
all sub-packages of the specified Package
will be
included in the search.classes
- save found classes herejava.io.IOException
public static java.util.HashSet<java.lang.String> findClassNamesInPath(java.lang.String path, boolean includeSubDir)
path
- path to scan.includeSubDir
- if true
all sub-directory are also scanned.public static java.util.HashSet<java.lang.String> findClassNamesInPath(java.lang.String path, boolean includeSubDir, boolean includeJar)
path
- path to scan.includeSubDir
- if true
all sub-directory are also scanned.includeJar
- if true
all JAR files are also scannedpublic static java.util.HashSet<java.lang.String> findClassNamesInPath(java.lang.String path, java.lang.String packageName, boolean includeSubDir)
path
- path to scan.packageName
- package name prefixincludeSubDir
- if true
all sub-directory are also scanned.public static java.util.HashSet<java.lang.String> findClassNamesInPath(java.lang.String path, java.lang.String packageName, boolean includeSubDir, boolean includeJar)
path
- path to scan.packageName
- package name prefixincludeSubDir
- if true
all sub-directory are also scanned.includeJar
- if true
all JAR files are also scannedpublic static void findClassNamesInPath(java.lang.String path, java.lang.String packageName, boolean includeSubDir, java.util.Set<java.lang.String> classes)
path
- path to scan.packageName
- package name prefixincludeSubDir
- if true
all sub-directory are also scanned.classes
- save found classes herepublic static void findClassNamesInPath(java.lang.String path, java.lang.String packageName, boolean includeSubDir, boolean includeJar, java.util.Set<java.lang.String> classes)
path
- path to scan.packageName
- package name prefixincludeSubDir
- if true
all sub-directory are also scanned.includeJar
- if true
all JAR files are also scannedclasses
- save found classes herepublic static void findClassNameInFile(java.io.File file, boolean includeJar, java.util.Set<java.lang.String> classSet, java.lang.String qualifiedNamePrefix)
public static void findClassNameInFile(java.io.File file, java.util.Set<java.lang.String> classSet, java.lang.String qualifiedNamePrefix)
public static void findClassNamesInJAR(java.lang.String fileName, java.util.Set<java.lang.String> classSet)
public static java.util.Set<java.lang.String> findClassNamesInJAR(java.lang.String fileName)
fileName
- public static java.lang.String filenameToClassname(java.lang.String fileName)
Class
given by fileName
.fileName
- is the filename.class-name
for the given fileName
if it is a
class-file that is no anonymous Class
, else null
.public static java.lang.String fixClassName(java.lang.String fileName)
Class
given by fileName
.fileName
- is the filename.class-name
for the given fileName
if it is a
class-file that is no anonymous Class
, else null
.public static java.io.File getFile(java.lang.String fullClassName)
fullClassName
- The class name to look for.null
if the class was not loaded from a file or for any
other error.@Deprecated public static java.lang.reflect.Method getMethod(java.lang.Object object, java.lang.String methodName, boolean forceAccess, java.lang.Class<?>... parameterTypes) throws java.lang.SecurityException, java.lang.NoSuchMethodException
ReflectionUtil.getMethod(Object, String, boolean, Class...)
insteadjava.lang.SecurityException
java.lang.NoSuchMethodException
@Deprecated public static java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName, boolean forceAccess, java.lang.Object... args) throws java.lang.SecurityException, java.lang.NoSuchMethodException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
ReflectionUtil.invokeMethod(Object, String, boolean, Object...)
insteadjava.lang.SecurityException
java.lang.NoSuchMethodException
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
@Deprecated public static java.lang.reflect.Field getField(java.lang.Object object, java.lang.String fieldName, boolean forceAccess) throws java.lang.SecurityException, java.lang.NoSuchFieldException
ReflectionUtil.getField(Object, String, boolean)
insteadjava.lang.SecurityException
java.lang.NoSuchFieldException
@Deprecated public static java.lang.Object getFieldObject(java.lang.Object object, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.SecurityException, java.lang.NoSuchFieldException
ReflectionUtil.getFieldObject(Object, String, boolean)
insteadjava.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.SecurityException
java.lang.NoSuchFieldException