gpt4 book ai didi

java - java中类中的公共(public)静态字段

转载 作者:行者123 更新时间:2023-12-01 09:04:34 25 4
gpt4 key购买 nike

我正在开发 Burp Suite 扩展。

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

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") {...} 这样的事情时,结果是相同的

最佳答案

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

由于这些是静态字段,因此适当的地方是在声明它们时或在类中的静态 block 内初始化它们。

例如:

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

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

Update

在您的 MyAction 类中声明一个引用来保存 IContextMenuInspiration 调用 对象。像这样的事情:

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

希望这有帮助!

关于java - java中类中的公共(public)静态字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41393343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com