java中类的公共静态字段

时间:2016-12-30 08:54:56

标签: java burp

我正在开发一个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项目=新的JMenuItem(新的AbstractAction(“123”){...}那样结果时,它是相同的

1 个答案:

答案 0 :(得分:1)

您需要初始化helper课程中的stdoutBurpExtender个对象。

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

例如:

  
      
  1. 在声明时:
  2.   
public static PrintWriter stdout = System.out;
public static IExtensionHelpers helpers = new ExtensionHelperImpl();// something like this.
  
      
  1. 或在静态区块内
  2.   
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;
}

希望这有帮助!

相关问题