Move ReflectionScanningHelper -> ClasspathAccess

This commit is contained in:
JeremyStar™ 2024-07-16 14:52:19 +02:00
parent 97dcc5263b
commit 050befbb73
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -17,10 +17,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package de.staropensource.sosengine.base.internal.reflection; package de.staropensource.sosengine.base.reflection;
import de.staropensource.sosengine.base.logging.Logger;
import de.staropensource.sosengine.base.types.CodePart;
import de.staropensource.sosengine.base.types.logging.LogIssuer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.reflections.Reflections;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
@ -30,7 +32,7 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
/** /**
* Helps in scanning the class path. * Allows getting the classpath as {@link URL}s.
* <p> * <p>
* This entire class has been stolen from * This entire class has been stolen from
* <a href="https://github.com/ronmamo/reflections">the Reflections library</a>. * <a href="https://github.com/ronmamo/reflections">the Reflections library</a>.
@ -39,9 +41,9 @@ import java.util.Map;
* @since v1-alpha2 * @since v1-alpha2
*/ */
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
public class ReflectionScanningHelper { public final class ClasspathAccess {
/** /**
* Returns the classpath as URLs. * Returns the classpath as a collection of {@link URL}s.
* *
* @return collection of classpath urls * @return collection of classpath urls
* @since v1-alpha2 * @since v1-alpha2
@ -50,17 +52,15 @@ public class ReflectionScanningHelper {
public static Collection<URL> getClasspathURLs() { public static Collection<URL> getClasspathURLs() {
Collection<URL> urls = new ArrayList<>(); Collection<URL> urls = new ArrayList<>();
String javaClassPath = System.getProperty("java.class.path"); String javaClassPath = System.getProperty("java.class.path");
if (javaClassPath != null) {
for (String path : javaClassPath.split(File.pathSeparator)) { if (javaClassPath != null)
for (String path : javaClassPath.split(File.pathSeparator))
try { try {
urls.add(new File(path).toURI().toURL()); urls.add(new File(path).toURI().toURL());
} catch (Exception e) { } catch (Exception exception) {
if (Reflections.log != null) { Logger.crash(new LogIssuer(ClasspathAccess.class, CodePart.ENGINE), "Failed converting classpath to URL", exception);
Reflections.log.warn("Could not get URL", e);
}
}
}
} }
return fixURLs(urls); return fixURLs(urls);
} }
@ -74,7 +74,7 @@ public class ReflectionScanningHelper {
* @since v1-alpha2 * @since v1-alpha2
*/ */
@NotNull @NotNull
public static Collection<URL> fixURLs(@NotNull Collection<URL> urls) { private static Collection<URL> fixURLs(@NotNull Collection<URL> urls) {
Map<String, URL> distinct = new LinkedHashMap<>(urls.size()); Map<String, URL> distinct = new LinkedHashMap<>(urls.size());
for (URL url : urls) { for (URL url : urls) {
distinct.put(url.toExternalForm(), url); distinct.put(url.toExternalForm(), url);