加载 class 并在不解析 class 的情况下检查方法是否存在
Loading a class and checking for method presence without resolving the class
我正在尝试从文件夹加载 class 以检查特定方法的实现。 class 有一些文件夹或其子文件夹中不存在的导入。使用 Class clazz = Class.forName(className, false, classLoader);
加载 class 工作正常,但是当我调用 clazz.getDeclaredMethod("methodName")
时,我得到一个 NoClassDefFoundError
,因为某些导入无法解析。
是否可以在运行时检查一个class(我不打算调用方法或实例化它)而不加载依赖项?
如果没有,当我以 classes 文件夹作为起点时,我还能如何检查 class 的特定方法实现?
您也许可以使用诸如 Apache Commons BCEL.
之类的库来做到这一点
The Byte Code Engineering Library (Apache Commons BCEL™) is intended to give users a convenient way to analyze, create, and manipulate (binary) Java class files (those ending with .class).
您可以尝试从您的程序中执行“javap”并解析输出。例如:
javap Driver
Compiled from "Driver.java"
public class Driver {
public Driver();
public static void main(java.lang.String[]);
}
有趣的问题。
我认为你不想推出自己的字节码解析器,所以试试 Apache BCEL or Spring ASM。两者都允许您 read/write class 文件而无需加载它们。
我正在尝试从文件夹加载 class 以检查特定方法的实现。 class 有一些文件夹或其子文件夹中不存在的导入。使用 Class clazz = Class.forName(className, false, classLoader);
加载 class 工作正常,但是当我调用 clazz.getDeclaredMethod("methodName")
时,我得到一个 NoClassDefFoundError
,因为某些导入无法解析。
是否可以在运行时检查一个class(我不打算调用方法或实例化它)而不加载依赖项?
如果没有,当我以 classes 文件夹作为起点时,我还能如何检查 class 的特定方法实现?
您也许可以使用诸如 Apache Commons BCEL.
之类的库来做到这一点The Byte Code Engineering Library (Apache Commons BCEL™) is intended to give users a convenient way to analyze, create, and manipulate (binary) Java class files (those ending with .class).
您可以尝试从您的程序中执行“javap”并解析输出。例如:
javap Driver
Compiled from "Driver.java"
public class Driver {
public Driver();
public static void main(java.lang.String[]);
}
有趣的问题。
我认为你不想推出自己的字节码解析器,所以试试 Apache BCEL or Spring ASM。两者都允许您 read/write class 文件而无需加载它们。