gpt4 book ai didi

java - 为什么使用 getInterfaces() 搜索时不出现 java.sql.PreparedStatement 接口(interface)

转载 作者:行者123 更新时间:2023-12-01 22:04:53 24 4
gpt4 key购买 nike

我正在尝试检测一个实现java.sql.PreparedStatement接口(interface)的类。我已经在转换方法中使用以下代码进行了尝试,但它甚至没有经过这样的类。但我正在使用包含 executeQuery 方法的主类运行我的代理。

public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer)
throws IllegalClassFormatException {

byte[] byteCode = classfileBuffer;

ClassPool classPool = ClassPool.getDefault();

try {
CtClass ctClass = classPool.makeClass(new ByteArrayInputStream(classfileBuffer));

if (!ctClass.isInterface()) {
CtMethod[] methods = ctClass.getDeclaredMethods();
Class[] interfaces =className.getClass().getInterfaces();
for (Class c : interfaces) {
System.out.println(c.getName());
if (c.getName().equals("java.sql.PreparedStatement")) {
System.out.println(c.getName());
for (CtMethod method : methods) {
if (method.getName().equals("executeQuery")) {
this.testSignature(method);
}
}
}
}
}
byteCode = ctClass.toBytecode();
ctClass.detach();
} catch (Throwable e) {
e.printStackTrace();
}
return byteCode;
}

我也尝试使用classBeingRedefine.getInterfaces(),然后它在某些类上返回 nullPointerException,因为 classBeingRedefine 变为 null。

编辑 1c.getName() 的输出是一长串以下类名。

    java.lang.CharSequence
java.io.Serializable
java.lang.Comparable

编辑2

String fullyQualifiedClassName = className.replace('/','.');
Class currentClass = Class.forName(fullyQualifiedClassName);
Class[] interfaces = currentClass.getInterfaces();

最佳答案

原因是您正在寻找 String 类的接口(interface),因为变量 className 实际上是一个字符串。

您可以在方法中传递Class className或使用

Class c = Class.forName("Fully qualified class Name");
c.getInterfaces();

关于java - 为什么使用 getInterfaces() 搜索时不出现 java.sql.PreparedStatement 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32985233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com