BCEL - Get Class name,element names ,and method names

跟風遠走 提交于 2019-12-12 04:34:38

问题


how to using bcel classparaser to get class names ,element names and method names ? ive already find a way to get class names but element and method names give me something wrong . Anyone can help me with that ? here is my code (with some errors there, and not completed):

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);
    }
}

回答1:


You can call parse() on the JavaParser to obtain a JavaClass class, which offers all the required information:

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();
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/26702103/bcel-get-class-name-element-names-and-method-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!