com.ziqni.admin.sdk.util.ClassScanner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ziqni-admin-sdk Show documentation
Show all versions of ziqni-admin-sdk Show documentation
ZIQNI Admin SDK Java Client
/*
* Copyright (c) 2022. ZIQNI LTD registered in England and Wales, company registration number-09693684
*/
package com.ziqni.admin.sdk.util;
import org.apache.commons.lang3.SystemUtils;
import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Type;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
public class ClassScanner {
private final Map classes;
private final String packageToScan;
public ClassScanner(String packageToScan) {
this.classes = new HashMap<>();
this.packageToScan = packageToScan;
scan();
}
public ClassScanner(String packageToScan, List> manuallyAdd) {
this(packageToScan);
manuallyAdd.forEach( clazz -> this.classes.put(clazz.getSimpleName(), clazz));
}
private void scan(){
getClassesInPackage(packageToScan).forEach(clazz ->
classes.put(clazz.getSimpleName(), clazz)
);
}
public Map getClasses() {
return classes;
}
public String getPackageToScan() {
return packageToScan;
}
public Optional get(String simpleName){
return (simpleName==null || simpleName.trim().isEmpty())
? Optional.empty()
: Optional.ofNullable(getClasses().get(simpleName));
}
public static final List> getClassesInPackage(String packageName) {
String path ;
if(SystemUtils.IS_OS_WINDOWS) {
path = packageName.replaceAll("\\.", "\\" + File.separator);
}else {
path = packageName.replaceAll("\\.", File.separator);
}
List> classes = new ArrayList<>();
String[] classPathEntries = System.getProperty("java.class.path").split(
System.getProperty("path.separator")
);
String name;
for (String classpathEntry : classPathEntries) {
if (classpathEntry.endsWith(".jar")) {
File jar = new File(classpathEntry);
try {
JarInputStream is = new JarInputStream(new FileInputStream(jar));
JarEntry entry;
while((entry = is.getNextJarEntry()) != null) {
name = entry.getName();
if (SystemUtils.IS_OS_WINDOWS && name.endsWith(".class")) {
if (name.contains(path.replaceAll("\\\\","/"))) {
String classPath = name.substring(0, entry.getName().length() - 6);
classPath = classPath.replaceAll("[\\|/]", ".");
classes.add(Class.forName(classPath));
}
}
else if (name.contains(path) && name.endsWith(".class")) {
String classPath = name.substring(0, entry.getName().length() - 6);
classPath = classPath.replaceAll("[\\|/]", ".");
classes.add(Class.forName(classPath));
}
}
} catch (Exception ex) {
// Silence is golden, golden, ex.printStackTrace();
}
} else {
try {
File base = new File(classpathEntry + File.separatorChar + path);
for (File file : base.listFiles()) {
name = file.getName();
if (name.endsWith(".class")) {
name = name.substring(0, name.length() - 6);
classes.add(Class.forName(packageName + "." + name));
}
}
} catch (Exception ex) {
// Silence is golden, golden, ex.printStackTrace();
}
}
}
return classes;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy