栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java:从JAR文件中的类文件中获取方法存根的简便方法?反射?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java:从JAR文件中的类文件中获取方法存根的简便方法?反射?

根据aioobe的回答,您还可以使用ASM的树API(而不是其访问者API)来解析JAR文件中包含的类文件的内容。同样,您可以使用JarFile类读取JAR文件中包含的文件。这是如何完成此操作的示例:

printMethodStubs
方法接受a
JarFile
并继续打印出所有类文件中包含的所有方法的描述。

public void printMethodStubs(JarFile jarFile) throws Exception {    Enumeration<JarEntry> entries = jarFile.entries();    while (entries.hasMoreElements()) {        JarEntry entry = entries.nextElement();        String entryName = entry.getName();        if (entryName.endsWith(".class")) { ClassNode classNode = new ClassNode(); InputStream classFileInputStream = jarFile.getInputStream(entry); try {     ClassReader classReader = new ClassReader(classFileInputStream);     classReader.accept(classNode, 0); } finally {     classFileInputStream.close(); } System.out.println(describeClass(classNode));        }    }}

describeClass
方法接受一个
ClassNode
对象并继续对其进行描述及其相关方法:

public String describeClass(ClassNode classNode) {    StringBuilder classDescription = new StringBuilder();    Type classType = Type.getObjectType(classNode.name);    // The class signature (e.g. - "public class Foo")    if ((classNode.access & Oppres.ACC_PUBLIC) != 0) {        classDescription.append("public ");    }    if ((classNode.access & Oppres.ACC_PRIVATE) != 0) {        classDescription.append("private ");    }    if ((classNode.access & Oppres.ACC_PROTECTED) != 0) {        classDescription.append("protected ");    }    if ((classNode.access & Oppres.ACC_ABSTRACT) != 0) {        classDescription.append("abstract ");    }    if ((classNode.access & Oppres.ACC_INTERFACE) != 0) {        classDescription.append("interface ");    } else {        classDescription.append("class ");    }    classDescription.append(classType.getClassName()).append("n");    classDescription.append("{n");    // The method signatures (e.g. - "public static void main(String[]) throws Exception")    @SuppressWarnings("unchecked")    List<MethodNode> methodNodes = classNode.methods;    for (MethodNode methodNode : methodNodes) {        String methodDescription = describeMethod(methodNode);        classDescription.append("t").append(methodDescription).append("n");    }    classDescription.append("}n");    return classDescription.toString();}

describeMethod
方法接受a
MethodNode
并返回一个描述方法签名的字符串:

public String describeMethod(MethodNode methodNode) {    StringBuilder methodDescription = new StringBuilder();    Type returnType = Type.getReturnType(methodNode.desc);    Type[] argumentTypes = Type.getArgumentTypes(methodNode.desc);    @SuppressWarnings("unchecked")    List<String> thrownInternalClassNames = methodNode.exceptions;    if ((methodNode.access & Oppres.ACC_PUBLIC) != 0) {        methodDescription.append("public ");    }    if ((methodNode.access & Oppres.ACC_PRIVATE) != 0) {        methodDescription.append("private ");    }    if ((methodNode.access & Oppres.ACC_PROTECTED) != 0) {        methodDescription.append("protected ");    }    if ((methodNode.access & Oppres.ACC_STATIC) != 0) {        methodDescription.append("static ");    }    if ((methodNode.access & Oppres.ACC_ABSTRACT) != 0) {        methodDescription.append("abstract ");    }    if ((methodNode.access & Oppres.ACC_SYNCHRONIZED) != 0) {        methodDescription.append("synchronized ");    }    methodDescription.append(returnType.getClassName());    methodDescription.append(" ");    methodDescription.append(methodNode.name);    methodDescription.append("(");    for (int i = 0; i < argumentTypes.length; i++) {        Type argumentType = argumentTypes[i];        if (i > 0) { methodDescription.append(", ");        }        methodDescription.append(argumentType.getClassName());    }    methodDescription.append(")");    if (!thrownInternalClassNames.isEmpty()) {        methodDescription.append(" throws ");        int i = 0;        for (String thrownInternalClassName : thrownInternalClassNames) { if (i > 0) {     methodDescription.append(", "); } methodDescription.append(Type.getObjectType(thrownInternalClassName).getClassName()); i++;        }    }    return methodDescription.toString();}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/441057.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号