gpt4 book ai didi

java - 如何修改 eclipse 插件中的函数体?

转载 作者:行者123 更新时间:2023-11-30 09:01:08 25 4
gpt4 key购买 nike

我已经设法创建了一个弹出菜单并获得了一个IMethod,但我不知道如何修改该方法。对于此示例,假设我想在单击按钮时将文本 system.out.println("Hello, world!"); 添加到现有方法的底部。

我目前拥有的如下:

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.jdt.core.IMethod;

public class HelloWorldAction implements IObjectActionDelegate {

private Shell shell;

private IMethod currentMethod;

/**
* Constructor for Action1.
*/
public HelloWorldAction() {
super();
}

/**
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
shell = targetPart.getSite().getShell();
}

/**
* @see IActionDelegate#run(IAction)
*/
public void run(IAction action) {
//TODO: preform the actions.
}

/**
* @see IActionDelegate#selectionChanged(IAction, ISelection)
*/
public void selectionChanged(IAction action, ISelection selection) {
if (!(selection instanceof IStructuredSelection)) {
action.setEnabled(false);
return;
}
IStructuredSelection sel = (IStructuredSelection) selection;

if (!(sel.getFirstElement() instanceof IMethod)) {
//Only handles IMethods.
action.setEnabled(false);
return;
}

action.setEnabled(true);
this.currentMethod = (IMethod) sel.getFirstElement();
}
}

我坚持修改 currentMethod。我看过this help page on modifying code ,但我不知道如何获取 DocumentASTIMethod 所需的任何东西。正确的做法是什么?

最佳答案

根据 Eclipse API :

Modifying a compilation unit Most simple modifications of Java source can be done using the Java element API.

For example, you can query a type from a compilation unit. Once you have the IType, you can use protocols such as createField, createInitializer, createMethod, or createType to add source code members to the type. The source code and information about the location of the member is supplied in these methods.

我会尝试使用:

currentMethod.getCompilationUnit().getTypes()[0].createMethod(" hello world code goes here ",null,true,null); 
//not sure if progress monitor can be null, please check

根据 Eclipse API :

IMethod createMethod(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException

Creates and returns a method or constructor in this type with the given contents. Optionally, the new element can be positioned before the specified sibling. If no sibling is specified, the element will be appended to this type.

It is possible that a method with the same signature already exists in this type. The value of the force parameter affects the resolution of such a conflict:

true - in this case the method is created with the new contents

false - in this case a JavaModelException is thrown

如果这就是您要找的,请告诉我。

关于java - 如何修改 eclipse 插件中的函数体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26492523/

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