2016-12-30 34 views
0

我正在开发Burp套件扩展。java中的类中的公共静态字段

我有一个类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. 
    } 
} 

我有另一种解决方案,当我tryed做水木清华像JMenuItem的项目=新的JMenuItem(新AbstractAction( “123”){ ...}导致它同

+0

U需要初始化静态变量 – Athul

+0

我的意思是定义它 – Athul

+0

你不知道,但你有没有搜索是什么NullPointerException?这是你会遇到最多的例外。 – AxelH

回答

1

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

由于这些是静态字段,因此在声明它们时或者在类中的静态块内部时,可以将它们初始化。

例如:

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

    如果没有这个初始化,stdouthelpers引用将指向null。当您尝试在其他类中使用 BurpExtender.stdoutBurpExtender.helpers时,会导致NullPointerException。

    更新

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

    希望这有助于!

    +0

    如果一个不会使它静态,有没有办法在另一个使用BurpExtension类的私人/公共领域?如果我让MyAction内在? –

    +0

    是的,_non-static_内部类将* *有权访问外部类中声明的_non-static_私有和公共字段。 – anacron

    +0

    In(actionPerformed)我需要使用(IContextMenuInvocation调用)。是否有可能使用它和BurpExtender.helpers? –

    相关问题