Evgeny
Evgeny

Reputation: 31

Java Dynamic Resource Discovery

The following code creates a list of classes and interfaces in the specified package.

public static List<Class> getClasses(ClassLoader loader, String pack) throws IOException, ClassNotFoundException {
    BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) loader.getResource(pack.replace('.', '/')).getContent()));
    List<Class> classes = new ArrayList<>();
    for (String line; (line = reader.readLine()) != null; ) {
        if (line.endsWith(".class")) {
            classes.add(Class.forName(pack + "." + line.substring(0, line.lastIndexOf('.'))));
        }
    }
    reader.close();
    return classes;
}

This code works, however please advise when it may NOT work, such as when the code is obfuscated or when a Java binary is converted to a native binary using GraalVM or what else?

Upvotes: 1

Views: 35

Answers (0)

Related Questions