Java's native support for class path inspection is somewhat limited. It doesn't provide convenient means to inspect the class path entries, requiring the programmer to write large chunks of code. Moreover, if the programmer needs to inspect the class path on a different class the same code has to be rewritten again, turning the source code in a boilerplate code nightmare.
To overcome this limitations, the Class path utilities provides methods to inspect the class path entries in a simple way.
Please refer to the Class path utilities Javadoc for the complete API specification.
To find the names of the classes on the class path the getClasspathClassNames() method should be used. The method returns a list with the FQNs of the class path classes.
The following example finds the names of all classes on the class path and prints them to the standard output stream:
... List<String> classNames = null; try { classNames = ClasspathUtils.getClasspathClassNames(); } catch (ResourceNotFoundException resourceNotFoundException) { // Exception-handling code here... } System.out.println("Classpath classes names: "); for (String className : classNames) System.out.println(className); ...
This snippet produces an output similar to this:
Classpath classes names: pt.digitalis.utils.inspection.ClasspathUtils$1 pt.digitalis.utils.inspection.ClasspathUtils pt.digitalis.utils.inspection.ClasspathUtilsTest$1ClassFileFilter pt.digitalis.utils.inspection.ClasspathUtilsTest pt.digitalis.utils.inspection.MockUpClass pt.digitalis.utils.inspection.ReflectionUtils pt.digitalis.utils.inspection.ReflectionUtilsTest pt.digitalis.utils.inspection.ResourceUtils$FolderFilter pt.digitalis.utils.inspection.ResourceUtils pt.digitalis.utils.inspection.ResourceUtilsTest
To find the names of the files on the class path the getClasspathFileNames() method should be used. The method returns a list with the relative names of the class path files.
The following example finds the names of all files on the class path and prints them to the standard output stream:
... List<String> fileNames = null; try { fileNames = ClasspathUtils.getClasspathFileNames(); } catch (ResourceNotFoundException e) { // Exception-handling code here... } System.out.println("Class path file names:"); for (String fileName : fileNames) System.out.println(fileName); ...
This snippet produces an output similar to this:
Class path file names: META-INF/MANIFEST.MF LICENSE.txt modules.properties pt/digitalis/utils/inspection/ClasspathUtils$1.class pt/digitalis/utils/inspection/ClasspathUtils.class pt/digitalis/utils/inspection/ClasspathUtilsTest$1ClassFileFilter.class pt/digitalis/utils/inspection/ClasspathUtilsTest.class pt/digitalis/utils/inspection/MockUpClass.class pt/digitalis/utils/inspection/ReflectionUtils.class pt/digitalis/utils/inspection/ReflectionUtilsTest.class pt/digitalis/utils/inspection/ResourceUtils$FolderFilter.class pt/digitalis/utils/inspection/ResourceUtils.class pt/digitalis/utils/inspection/ResourceUtilsTest.class pt/digitalis/utils/inspection/AttributeAnnotation.class pt/digitalis/utils/inspection/ClasspathUtils$FilenameFilter.class pt/digitalis/utils/inspection/MethodAnnotation.class pt/digitalis/utils/inspection/TypeAnnotation.class pt/digitalis/utils/inspection/exception/AuxiliaryOperationException.class pt/digitalis/utils/inspection/exception/InvalidResourceException.class pt/digitalis/utils/inspection/exception/ResourceNotFoundException.class pt/digitalis/utils/inspection/exception/TestExceptions.class
Note that several file extensions are captured by the mehtod.
Sometimes is useful to find the names of files with a given extension. The Class path utilities provides a method (getClasspathFileNamesWithExtension(String)) to do this task. The method's argument is a String with the file extension that should be used for selection and the return is list with the files relative paths.
Here's an example of how to capture the names of the .class files:
... // The ".class" extension final static String CLASS_FILE_EXTENSION = ".class"; List<String> fileNames = null; try { fileNames = ClasspathUtils.getClasspathFileNamesWithExtension(CLASS_FILE_EXTENSION); } catch (ResourceNotFoundException resourceNotFoundException) { // Exception-handling code here... } System.out.println("Class path files with " + CLASS_FILE_EXTENSION + " extension:"); for (String fileName : fileNames) System.out.println(fileName); ...
The produced output should be something like this:
Class path files with .class extension: pt/digitalis/utils/inspection/ClasspathUtils$1.class pt/digitalis/utils/inspection/ClasspathUtils.class pt/digitalis/utils/inspection/ClasspathUtilsTest$1ClassFileFilter.class pt/digitalis/utils/inspection/ClasspathUtilsTest.class pt/digitalis/utils/inspection/MockUpClass.class pt/digitalis/utils/inspection/ReflectionUtils.class pt/digitalis/utils/inspection/ReflectionUtilsTest.class pt/digitalis/utils/inspection/ResourceUtils$FolderFilter.class pt/digitalis/utils/inspection/ResourceUtils.class pt/digitalis/utils/inspection/ResourceUtilsTest.class pt/digitalis/utils/inspection/AttributeAnnotation.class pt/digitalis/utils/inspection/ClasspathUtils$FilenameFilter.class pt/digitalis/utils/inspection/MethodAnnotation.class pt/digitalis/utils/inspection/TypeAnnotation.class pt/digitalis/utils/inspection/exception/AuxiliaryOperationException.class pt/digitalis/utils/inspection/exception/InvalidResourceException.class pt/digitalis/utils/inspection/exception/ResourceNotFoundException.class pt/digitalis/utils/inspection/exception/TestExceptions.class
Note that only .class files appear on the list.
To achieve a greater API flexibilty a method that filters files based on a given criteria is provided. The method's name is getClasspathFileNames(FilenameFilter). It's argument is a user-defined FilenameFilter and it returns a list with the relative paths of the class path files. The FilenameFilter API works pretty much like Java's own FileFilter and can be found here.
The followin example shows how to use this method. The snippet starts with the definition of the file name filter to be used. The remaining code is in the vein of the other examples.
... // Define the file name filter final class InspectionNamedFileFilter implements FilenameFilter { public boolean accept(String filename) { return filename.contains("inspection"); } } List<String> fileNames = null; try { fileNames = ClasspathUtils.getClasspathFileNames(new InspectionNamedFileFilter()); } catch (ResourceNotFoundException e) { // Exception-handling code here... } System.out.println("Class path files with 'inspection' on the name:"); for (String fileName : fileNames) System.out.println(fileName); ...
The output should be something like this:
Class path files with 'inspection' on the name: inspection-utils.jar pt/digitalis/utils/inspection/ClasspathUtils$1.class pt/digitalis/utils/inspection/ClasspathUtils.class pt/digitalis/utils/inspection/ClasspathUtilsTest$1ClassFileFilter.class pt/digitalis/utils/inspection/ClasspathUtilsTest.class pt/digitalis/utils/inspection/MockUpClass.class pt/digitalis/utils/inspection/ReflectionUtils.class pt/digitalis/utils/inspection/ReflectionUtilsTest.class pt/digitalis/utils/inspection/ResourceUtils$FolderFilter.class pt/digitalis/utils/inspection/ResourceUtils.class pt/digitalis/utils/inspection/ResourceUtilsTest.class pt/digitalis/utils/inspection/exception/AuxiliaryOperationException.class pt/digitalis/utils/inspection/exception/InvalidResourceException.class pt/digitalis/utils/inspection/exception/ResourceNotFoundException.class pt/digitalis/utils/inspection/exception/TestExceptions.class pt/digitalis/utils/inspection/AttributeAnnotation.class pt/digitalis/utils/inspection/ClasspathUtils$FilenameFilter.class pt/digitalis/utils/inspection/MethodAnnotation.class pt/digitalis/utils/inspection/TypeAnnotation.class