⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 browsertreepane.java

📁 Java Bytecode Editor 是一个 JAVA 的字节码反汇编和修改器。它可以很方便的修改已经编译成 Class 文件的 JAVA 文件。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 2 of the license, or (at your option) any later version.
*/

package ee.ioc.cs.jbe.browser;

import org.gjt.jclasslib.structures.*;
import org.gjt.jclasslib.structures.attributes.*;
import org.gjt.jclasslib.structures.constants.ConstantLargeNumeric;
import org.gjt.jclasslib.structures.elementvalues.*;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * The pane containing the tree structure for the class file shown in the
 * child window.
 *
 * @author <a href="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>, <a href="mailto:vitor.carreira@gmail.com">Vitor Carreira</a>
 * @version $Revision: 1.1 $ $Date: 2006/09/25 16:00:58 $
 */
public class BrowserTreePane extends JPanel {

    private static final Dimension treeMinimumSize = new Dimension(100, 150);
    private static final Dimension treePreferredSize = new Dimension(250, 150);

    private BrowserServices services;
    private JTree tree;
    private Map<String, TreePath> categoryToPath = new HashMap<String, TreePath>();

    /**
     * Constructor.
     *
     * @param services the associated browser services.
     */
    public BrowserTreePane(BrowserServices services) {
        this.services = services;
        setLayout(new BorderLayout());
        setupComponent();
    }

    /**
     * Get the tree view.
     *
     * @return the tree view
     */
    public JTree getTree() {
        return tree;
    }

    /**
     * Get the tree path for a given category.
     *
     * @param category the category. One the <tt>BrowserTree.NODE_</tt> constants.
     * @return the tree path.
     */
    public TreePath getPathForCategory(String category) {
        return (TreePath)categoryToPath.get(category);
    }

    /**
     * Show the specified method if it exists.
     *
     * @param methodName      the name of the method
     * @param methodSignature the signature of the method (in class file format)
     */
    public void showMethod(String methodName, String methodSignature) {

        TreePath methodsPath = (TreePath)categoryToPath.get(BrowserTreeNode.NODE_METHOD);
        if (methodsPath == null) {
            return;
        }
        MethodInfo[] methods = services.getClassFile().getMethods();
        TreeNode methodsNode = (TreeNode)methodsPath.getLastPathComponent();
        for (int i = 0; i < methodsNode.getChildCount(); i++) {
            BrowserTreeNode treeNode = (BrowserTreeNode)methodsNode.getChildAt(i);
            MethodInfo testMethod = methods[treeNode.getIndex()];
            try {
                if (testMethod.getName().equals(methodName) && testMethod.getDescriptor().equals(methodSignature)) {
                    TreePath path = methodsPath.pathByAddingChild(treeNode);
                    BrowserTreeNode codeNode = findCodeNode(treeNode, testMethod);
                    if (codeNode != null) {
                        path = path.pathByAddingChild(codeNode);
                    }

                    tree.makeVisible(path);
                    tree.scrollPathToVisible(path);
                    tree.setSelectionPath(path);
                    return;
                }
            } catch (InvalidByteCodeException ex) {
            }
        }
    }

    /**
     * Rebuild the tree from the <tt>ClassFile</tt> object.
     */
    public void rebuild() {
        categoryToPath.clear();
        tree.clearSelection();
        tree.setModel(buildTreeModel());
    }

    private void setupComponent() {

        JScrollPane treeScrollPane = new JScrollPane(buildTree());
        treeScrollPane.setMinimumSize(treeMinimumSize);
        treeScrollPane.setPreferredSize(treePreferredSize);

        add(treeScrollPane, BorderLayout.CENTER);
    }

    private JTree buildTree() {

        tree = new JTree(buildTreeModel());

        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.setRootVisible(false);
        tree.setShowsRootHandles(true);

        return tree;
    }

    private TreeModel buildTreeModel() {
        BrowserTreeNode rootNode = buildRootNode();
        DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);

        return treeModel;
    }

    private BrowserTreeNode buildRootNode() {

        BrowserTreeNode rootNode = new BrowserTreeNode("Class file");
        ClassFile classFile = services.getClassFile();
        if (classFile != null) {

            BrowserTreeNode generalNode = new BrowserTreeNode("General Information", BrowserTreeNode.NODE_GENERAL);
            BrowserTreeNode constantPoolNode = buildConstantPoolNode();
            BrowserTreeNode interfacesNode = buildInterfacesNode();
            BrowserTreeNode fieldsNode = buildFieldsNode();
            BrowserTreeNode methodsNode = buildMethodsNode();
            BrowserTreeNode attributesNode = buildAttributesNode();

            rootNode.add(generalNode);
            rootNode.add(constantPoolNode);
            rootNode.add(interfacesNode);
            rootNode.add(fieldsNode);
            rootNode.add(methodsNode);
            rootNode.add(attributesNode);

            categoryToPath.put(BrowserTreeNode.NODE_GENERAL, new TreePath(new Object[]{rootNode, generalNode}));
            categoryToPath.put(BrowserTreeNode.NODE_CONSTANT_POOL, new TreePath(new Object[]{rootNode, constantPoolNode}));
            categoryToPath.put(BrowserTreeNode.NODE_INTERFACE, new TreePath(new Object[]{rootNode, interfacesNode}));
            categoryToPath.put(BrowserTreeNode.NODE_FIELD, new TreePath(new Object[]{rootNode, fieldsNode}));
            categoryToPath.put(BrowserTreeNode.NODE_METHOD, new TreePath(new Object[]{rootNode, methodsNode}));
            categoryToPath.put(BrowserTreeNode.NODE_ATTRIBUTE, new TreePath(new Object[]{rootNode, attributesNode}));
        }

        return rootNode;
    }

    private BrowserTreeNode buildConstantPoolNode() {

        BrowserTreeNode constantPoolNode = new BrowserTreeNode("Constant Pool", BrowserTreeNode.NODE_CONSTANT_POOL_GENERAL);

        CPInfo[] constantPool = services.getClassFile().getConstantPool();
        int constantPoolCount = constantPool.length;

        for (int i = 1; i < constantPoolCount; i++) {
            i += addConstantPoolEntry(constantPool[i], i, constantPoolCount, constantPoolNode);
        }

        return constantPoolNode;
    }

    private int addConstantPoolEntry(CPInfo constantPoolEntry,
                                     int index,
                                     int constantPoolCount,
                                     BrowserTreeNode constantPoolNode) {


        if (constantPoolEntry == null) {
            constantPoolNode.add(buildNullNode());
        } else {
            String constDescription ="";
            try {
                constDescription = constantPoolEntry.getTagVerbose().substring(9) + ": "+constantPoolEntry.getVerbose();
            } catch (InvalidByteCodeException e) {
                e.printStackTrace();
            }
            BrowserTreeNode entryNode =
                    new BrowserTreeNode(getFormattedIndex(index, constantPoolCount) +
                    constDescription,
                            BrowserTreeNode.NODE_CONSTANT_POOL,
                            index);

            constantPoolNode.add(entryNode);
            if (constantPoolEntry instanceof ConstantLargeNumeric) {
                addConstantPoolContinuedEntry(index + 1,
                        constantPoolCount,
                        constantPoolNode);
                return 1;
            }
        }
        return 0;
    }

    private void addConstantPoolContinuedEntry(int index,
                                               int constantPoolCount,
                                               BrowserTreeNode constantPoolNode) {

        BrowserTreeNode entryNode =
                new BrowserTreeNode(getFormattedIndex(index, constantPoolCount) +
                "(large numeric continued)",
                        BrowserTreeNode.NODE_NO_CONTENT);
        constantPoolNode.add(entryNode);
    }

    private BrowserTreeNode buildInterfacesNode() {

        BrowserTreeNode interfacesNode = new BrowserTreeNode("Interfaces");
        interfacesNode.setType(BrowserTreeNode.NODE_INTERFACES_GENERAL);
        int[] interfaces = services.getClassFile().getInterfaces();
        int interfacesCount = interfaces.length;
        BrowserTreeNode entryNode;
        for (int i = 0; i < interfacesCount; i++) {
            entryNode = new BrowserTreeNode("Interface " + i,
                    BrowserTreeNode.NODE_INTERFACE,
                    i);
            interfacesNode.add(entryNode);
        }

        return interfacesNode;
    }

    private BrowserTreeNode buildFieldsNode() {

        return buildClassMembersNode("Fields",
                BrowserTreeNode.NODE_FIELD,
                services.getClassFile().getFields());
    }

    private BrowserTreeNode buildMethodsNode() {

        return buildClassMembersNode("Methods",
                BrowserTreeNode.NODE_METHOD,
                services.getClassFile().getMethods());
    }

    private BrowserTreeNode buildClassMembersNode(String text,
                                                  String type,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -