- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Private constructor to prevent instantiation.
This is a static helper class. -
Method Summary
Modifier and TypeMethodDescriptionprivate static @NotNull String
convertToClass
(@NotNull String fileName) Converts the given file name to a class name.getClasses
(boolean includeDependencies, @NotNull Predicate<String> condition) Gets all classes from the classpath.
The classes will be filtered by the given condition.getClassesFromDirectory
(@NotNull File directory, @NotNull Predicate<String> condition) Gets all classes from the given directory.
This method will be called recursively if the given directory contains subdirectories.getClassesFromJar
(@NotNull File file, @NotNull Predicate<String> condition) Gets all classes from the given jar file.
Any exceptions which will be thrown while trying to get the classes will be ignored.Gets all files from the classpath.
The files will be extracted from the system property 'java.class.path'.
The system path separator will be used to split the classpath into single files.Gets a condition to filter the classes which should be ignored.
The condition is based on the system property 'unsafe.classes.ignored'.listFiles
(@NotNull File directory, @Nullable FilenameFilter filter, boolean recurse) Gets all files from the given directory.
If the given directory contains subdirectories, this method will be called recursively.
-
Field Details
-
UNSAFE_PACKAGE_DEPTH
Constant for the system property 'unsafe.package.depth'.
The property is used in
getClasses(boolean, Predicate)
to filter the classes
from the classpath by the package depth of the calling class.
The default value is
2
.
- See Also:
-
UNSAFE_CLASSES_IGNORED
Constant for the system property 'unsafe.classes.ignored'.
The property is used in to filter classes which should be fully ignored.
All classes which are listed will not be loaded.
The value of the property is a comma or semicolon separated list of fully qualified class names.
If you want to ignore all classes from a package, you can use a wildcard at the end of the package name.
Wildcard entries are only allowed at the end of the package name, the entry must end with.*
.
The default value is an empty string.
- See Also:
-
-
Constructor Details
-
ClassPathHelper
private ClassPathHelper()Private constructor to prevent instantiation.
This is a static helper class.
-
-
Method Details
-
getClasses
@NotNull static @NotNull List<Class<?>> getClasses(boolean includeDependencies, @NotNull @NotNull Predicate<String> condition) Gets all classes from the classpath.
The classes will be filtered by the given condition.
If dependency classes should not be included, an 'and' clause is added to the condition.
The clause checks uses the package name of the calling class to determine if the class is a dependency or not.
For example, if the caller of this method is in the package "net.luis.utils.util",
all classes in the packages "net.luis.utils" will be returned,
because the depth is 3 which means that the package will be cut after the third dot.
The depth can be changed by using the system property 'unsafe.package.depth'.
Any exceptions which will be thrown while trying to get the classes will be ignored.
- Parameters:
includeDependencies
- Whether to include dependency classes or notcondition
- A condition to filter the classes- Returns:
- A list of all classes
-
getIgnoreCondition
Gets a condition to filter the classes which should be ignored.
The condition is based on the system property 'unsafe.classes.ignored'.
The value of the property is a comma or semicolon separated list of fully qualified class names.
If you want to ignore all classes from a package, you can use a wildcard at the end of the package name.
Wildcard entries are only allowed at the end of the package name, the entry must end with.*
.
If the property is not set, the condition will always return
false
.
This means that no classes will be ignored.
- Returns:
- A condition to filter the classes which should be ignored
-
getClassesFromJar
@NotNull private static @NotNull List<Class<?>> getClassesFromJar(@NotNull @NotNull File file, @NotNull @NotNull Predicate<String> condition) Gets all classes from the given jar file.
Any exceptions which will be thrown while trying to get the classes will be ignored.- Parameters:
file
- The jar filecondition
- A condition to filter the classes- Returns:
- A list of all classes in the given jar file
- Throws:
NullPointerException
- If the given file is null
-
getClassesFromDirectory
@NotNull private static @NotNull List<Class<?>> getClassesFromDirectory(@NotNull @NotNull File directory, @NotNull @NotNull Predicate<String> condition) Gets all classes from the given directory.
This method will be called recursively if the given directory contains subdirectories.
If a jar file is found,
getClassesFromJar(File, Predicate)
will be called.
Any exceptions which will be thrown while trying to get the classes will be ignored.
This method will be mainly used to get all classes in ide environments, from the output directory.
- Parameters:
directory
- The directory to get the classes fromcondition
- A condition to filter the classes- Returns:
- A list of all classes in the given directory
- Throws:
NullPointerException
- If the given directory is null
-
listFiles
@NotNull private static @NotNull List<File> listFiles(@NotNull @NotNull File directory, @Nullable @Nullable FilenameFilter filter, boolean recurse) Gets all files from the given directory.
If the given directory contains subdirectories, this method will be called recursively.- Parameters:
directory
- The directory to get the files fromfilter
- A filter to filter the filesrecurse
- If true, subdirectories will be included- Returns:
- A list of all files in the given directory
- Throws:
NullPointerException
- If the given directory is null
-
getClassPathFiles
Gets all files from the classpath.
The files will be extracted from the system property 'java.class.path'.
The system path separator will be used to split the classpath into single files.- Returns:
- A list of all files from the classpath
-
convertToClass
Converts the given file name to a class name.- Parameters:
fileName
- The file name to convert- Returns:
- The converted class name
- Throws:
NullPointerException
- If the given file name is null
-