BCEL - 获取类的名称,元素名称和方法名(BCEL - Get Class name,elem

2019-11-03 09:53发布

如何使用BCEL classparaser得到的类名,元素名称和方法名? 香港专业教育学院已经找到一种方式来获得的类名,但元素和方法名给我的东西是错误的。 任何人都可以帮我吗? 这里是我的代码(有一些错误存在,并且未完成):

import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.generic.ConstantPoolGen;

public final class BcelTest
{
    // static int methods;

    public static void main(String[] args)
    {
        ClassParser parser;
        try
        {

            JarFile jar = new JarFile("C:\\Users\\OOO\\Desktop\\Sample.Jar");
            Enumeration<JarEntry> entries = jar.entries();
            ConstantPoolGen cpg = jar.entries();
            while (entries.hasMoreElements())
            {
                JarEntry entry = entries.nextElement();
                if (!entry.getName().endsWith(".class"))
                    continue;

                parser =
                    new ClassParser("C:\\Users\\OOO\\Desktop\\Sample.Jar",
                        entry.getName());
                methods = getMethodName(cpg);

                MyClassVisitor visitor = new MyClassVisitor(parser.parse());
                visitor.start();
            }

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public String getMethodName(ConstantPoolGen cpg)
    {
        return getMethodName(cpg);
    }
}

Answer 1:

您可以拨打parse()JavaParser获得JavaClass类,它提供了所有必要的信息:

import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.Field;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;

public final class BcelTest
{
    public static void main(String[] args)
    {
        JarFile jar = null;
        try
        {
            String jarName = "C:/theFile.jar";
            jar = new JarFile(jarName);
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements())
            {
                JarEntry entry = entries.nextElement();
                if (!entry.getName().endsWith(".class"))
                {
                    continue;
                }

                ClassParser parser = 
                    new ClassParser(jarName, entry.getName());
                JavaClass javaClass = parser.parse();

                System.out.println("Class: "+javaClass.getClassName());
                System.out.println("  Fields:");
                for (Field field : javaClass.getFields())
                {
                    System.out.println("    "+field);
                }
                System.out.println("  Methods:");
                for (Method method : javaClass.getMethods())
                {
                    System.out.println("    "+method);
                }
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (jar != null)
            {
                try
                {
                    jar.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}


文章来源: BCEL - Get Class name,element names ,and method names