【问题标题】:Finding information related to a class in intellij ide在 intellij ide 中查找与类相关的信息
【发布时间】:2018-04-03 11:43:11
【问题描述】:

如何以编程方式查找有关课程的信息。例如,该类中的代码行数、方法的数量和名称、每个方法中的代码行数、对耦合和内聚方法的引用等。

我正在尝试在 intellij Ide 中开发一个插件。

如何从 actionPreformed 方法中获取这些信息?

public void actionPerformed(AnActionEvent event) {
    Project project = event.getProject();
    Class c = event.getClass();
} 

【问题讨论】:

    标签: java intellij-idea intellij-plugin


    【解决方案1】:

    唯一有用的是使用“Class”类中包含的方法。但是,您无法真正知道行数。已编译的类不包含有关行号的任何信息。您需要分析源文件以获得所需的所有信息。

    Class on javadoc

    【讨论】:

      【解决方案2】:

      这是一些旧代码,我必须在当前编辑器窗口的插入符号位置返回 ASTNode。

      private ASTNode getParentNode(AnActionEvent e) {
          Editor editor = e.getData(PlatformDataKeys.EDITOR);
          PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
          if (psiFile == null || editor == null)
              return null;
      
          int offset = editor.getCaretModel().getOffset();
          PsiElement elementAt = psiFile.findElementAt(offset);
          if (elementAt == null || elementAt.getParent() == null || elementAt.getParent().getNode() == null)
              return null;
      
          return elementAt.getParent().getNode();
      }
      

      可能从编辑器或 psiFile 中您可以获得有关源文件的更多信息...看看。

      【讨论】:

        猜你喜欢
        • 2011-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-20
        • 2013-03-27
        • 1970-01-01
        相关资源
        最近更新 更多