import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public static List<String> discoverLocalApplicationClasses() {
List<String> localClasses = new ArrayList<>();
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (!(classLoader instanceof URLClassLoader)) {
classLoader = ClassLoader.getSystemClassLoader();
}
if (classLoader instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader) classLoader).getURLs();
for (URL url : urls) {
File file = new File(url.toURI());
// 1. LOCAL DEVELOPMENT SCENARIO: Loose build directory
if (file.isDirectory()) {
System.out.println("[PROFILER-AGENT] Scanning local application directory: " + file.getAbsolutePath());
scanDirectoryForClasses(file, "", localClasses);
}
// 2. LINUX DEPLOYMENT SCENARIO: Packaged application code
// Target your core module jar while skipping third-party framework jars (like spring or quantum)
else if (file.isFile() && file.getName().endsWith(".jar") && isApplicationJar(file.getName())) {
System.out.println("[PROFILER-AGENT] Scanning deployed application JAR: " + file.getAbsolutePath());
scanJarForClasses(file, localClasses);
}
}
}
} catch (Exception e) {
System.err.println("[PROFILER-AGENT] Failed to auto-discover classes: " + e.getMessage());
}
System.out.println("[PROFILER-AGENT] Auto-discovered " + localClasses.size() + " application classes.");
return localClasses;
}
/**
* Filter to isolate your core application deployment JARs from standard third-party dependencies.
*/
private static boolean isApplicationJar(String jarName) {
String name = jarName.toLowerCase();
// Add identifiers matching your core application module names (e.g., cpls, comet, qff)
return name.contains("cpls") || name.contains("comet") || name.contains("qff");
}
/**
* Reads a packaged JAR file on Linux and extracts class paths directly from its entry index.
*/
private static void scanJarForClasses(File jarFile, List<String> result) {
try (JarFile jar = new JarFile(jarFile)) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
// Look only for compiled classes, skipping internal nested anonymous classes ($)
if (name.endsWith(".class") && !name.contains("$")) {
// Convert internal path "com/citi/cpls/MyClass.class" into standard "com.citi.cpls.MyClass"
String className = name.substring(0, name.length() - 6)
.replace('/', '.');
result.add(className);
}
}
} catch (Exception e) {
System.err.println("[PROFILER-AGENT] Error reading JAR entries from " + jarFile.getName() + ": " + e.getMessage());
}
}
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)