首页 > 解决方案 > Intellij IDEA Plugin Gradle 开发 - 为现有类生成代码

问题描述

我正在使用 gradle 开发我的 IDEA IntelliJ 插件,我正在尝试创建一个按钮,将函数添加到当前类或给定类。我在 Eclipse 中看到了一个示例,但找不到等效的库。

我试图在 Eclipse 中寻找与 ICompilationUnit 等效的库,但找不到任何东西

ICompilationUnit 单元 = 编译单元(activeEditor());

    final String messageName = inputBox("Create New Message",
            "Message Name");

    if (messageName == null)
        return null;
    String camel = camelCase(messageName);
    final String methodName = "handle" + camel;

    IType type = null;
    try {
        unit.createImport(Global.WHEN_RECEIVED_CLASS_NAME.<String>data(), null, null);
        type = unit.getType(drop(unit.getElementName(), 4));

        if (!type.getSuperclassName().equals("SimpleAgent")) {
            msgbox("cannot create message - unrecognized agent type");
            return null;
        }

        IMethod mtd = method(type, methodName);

        if (mtd == null) {
            mtd = type
                    .createMethod(
                            "@WhenReceived(\""
                                    + messageName
                                    + "\")\npublic void "
                                    + methodName
                                    + "(){\n\t//TODO: Add Message Handling Code Here.\n\t"
                                    + "//you can add any parameters to the method in order to receive them within the message.\n}\n",
                            null, false, null);
        }

        CompilationUnitEditor editor = activeCompilationUnitEditor();
        editor.setSelection(mtd);

我的主要目标是添加一个按钮,一旦单击它就会打开一个文本框,而不是将给定文本的函数添加到当前类。如果没有输入任何内容,则会出错。

非常感谢您。

标签: gradleintellij-ideaplugins

解决方案


推荐阅读