Public static fields in classes in java

132 views Asked by At

I'm developing a Burp Suite extension.

I have a class BurpExtender, it has public static field.

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

and in this file i have another class 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.
    }
}

I had another solution, when i tryed to do smth like JMenuItem item = new JMenuItem(new AbstractAction("123") {...} result it the same

1

There are 1 answers

3
anacron On

You will need need to initialize the helper and stdout objects in your BurpExtender class.

Since these are static fields, an appropriate place would be to initialize them when declaring them or inside a static block in your class.

For Example:

  1. While declaring them:
public static PrintWriter stdout = System.out;
public static IExtensionHelpers helpers = new ExtensionHelperImpl();// something like this.
  1. Or inside a static block
public static PrintWriter stdout;
public static IExtensionHelpers helpers;

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

Without this initialization, the stdout and helpers references will be pointing to null. This causes a NullPointerException when you try to use BurpExtender.stdout or BurpExtender.helpers in your other classes.

Update

In your MyAction class declare a reference to hold the IContextMenuInvocation invocation object. Some thing like this:

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..
    }
}

Then inside your outer class, change the createMenuItems method like this:

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

Hope this helps!