【问题标题】:Public static fields in classes in java [duplicate]java中类中的公共静态字段[重复]
【发布时间】:2016-12-30 08:54:56
【问题描述】:

我正在开发 Burp Suite 扩展。

我有一个 BurpExtender 类,它有公共静态字段。

public class BurpExtender implements IBurpExtender, IContextMenuFactory{

    private IBurpExtenderCallbacks callbacks;
    public static PrintWriter stdout;
    public static IExtensionHelpers helpers;
    ...
    @Override
        public void registerExtenderCallbacks(IBurpExtenderCallbacks callbacks) {

            this.callbacks = callbacks;
            this.helpers = callbacks.getHelpers();
            PrintWriter stdout = new PrintWriter(callbacks.getStdout(), true);

            callbacks.setExtensionName("REQUESTSENDER");
            callbacks.registerContextMenuFactory(BurpExtender.this);
            stdout.println("Registered");

        }

    public List<JMenuItem> createMenuItems(final IContextMenuInvocation invocation) {
        List<JMenuItem> menuItemList = new ArrayList<JMenuItem>();
        JMenuItem item = new JMenuItem(new MyAction());
        menuItemList.add(item);
        return menuItemList;
    }

在这个文件中我有另一个类 MyAction:

private class MyAction extends AbstractAction{
    public MyAction(){
        super("Name");
    }


    public void actionPerformed(ActionEvent e) {
        //Here i want to use BurpExtender.helpers, but i cant figure out, how to.
        //BurpExtender.stdout doesnt work here. Dunno why, NullPoinerException.
    }
}

我有另一个解决方案,当我尝试像 JMenuItem item = new JMenuItem(new AbstractAction("123") {...} 那样做时,结果是一样的

【问题讨论】:

  • 你需要初始化静态变量
  • 您不知道,但您是否搜索过 NullPointerException 是什么?这是您遇到最多的例外情况。
  • 当然我在 BurpExtender 中初始化助手和标准输出
  • 1 我没有看到任何初始化,因此如果您在构造函数中执行此操作,则需要确保在使用该操作之前构建一个实例。 2 一个 NPE 告诉你,你在标准输出中有一个 null
  • 简单,你的输出中有"Registered"吗?如果没有,那么你永远不会初始化它。由于这是在 registerXXX 方法中完成的,因此可能不会这样做。如果您使用实例方法进行初始化,请始终检查静态实例的值(因为这可能永远不会发生)

标签: java burp


【解决方案1】:

您需要在BurpExtender 类中初始化helperstdout 对象。

由于这些是静态字段,适当的位置是在声明它们时或在类中的静态块内初始化它们。

例如:

  1. 在声明它们时:
public static PrintWriter stdout = System.out;
public static IExtensionHelpers helpers = new ExtensionHelperImpl();// something like this.
  1. 或在静态块内
public static PrintWriter stdout;
public static IExtensionHelpers helpers;

static {
    stdout = System.out;
    helpers = new ExtensionHelperImpl();// something like this.
}

如果不进行此初始化,stdouthelpers 引用将指向 null。当您尝试使用时,这会导致 NullPointerException BurpExtender.stdoutBurpExtender.helpers 在您的其他课程中。

更新

在您的MyAction 类中声明一个引用来保存IContextMenuInvocation invocation 对象。像这样的东西:

private class MyAction extends AbstractAction{
    private IContextMenuInvocation invocation;

    public MyAction(IContextMenuInvocation invocation){
        super("Name");
        this.invocation = invocation;
    }


    public void actionPerformed(ActionEvent e) {
        //Here you can use BurpExtender.helpers and IContextMenuInvocation invocation also.
        BurpExtender.helpers.doSomething();
        invocation.invoke();// for example..
    }
}

然后在你的外部类中,像这样更改createMenuItems 方法:

public List<JMenuItem> createMenuItems(final IContextMenuInvocation invocation) {
    List<JMenuItem> menuItemList = new ArrayList<JMenuItem>();
    JMenuItem item = new JMenuItem(new MyAction(invocation));// this is the change
    menuItemList.add(item);
    return menuItemList;
}

希望这会有所帮助!

【讨论】:

  • 如果不让它成为静态的,有没有办法在另一个中使用 BurpExtension 类的私有/公共字段?如果我将 MyAction 设为内部?
  • 是的,non-static 内部类 WILL 可以访问 non-static 中声明的私有和公共字段外部类。
  • 在(actionPerformed)中我需要使用(IContextMenuInvocation 调用)。是否可以使用它和 BurpExtender.helpers?
猜你喜欢
  • 2019-03-09
  • 1970-01-01
  • 2015-05-19
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 1970-01-01
  • 2015-07-31
相关资源
最近更新 更多