IDEA 插件开发,工具方法

超、凢脫俗 2022-04-15 03:25 449阅读 0赞
  1. public static PsiClass getPsiClass(AnActionEvent e) {
  2. PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
  3. Editor editor = e.getData(PlatformDataKeys.EDITOR);
  4. if (psiFile == null || editor == null) {
  5. return null;
  6. }
  7. int offset = editor.getCaretModel().getOffset();
  8. PsiElement element = psiFile.findElementAt(offset);
  9. return PsiTreeUtil.getParentOfType(element, PsiClass.class);
  10. }
  11. /**
  12. * 获取类的所有属性
  13. *
  14. * @param e
  15. * @return
  16. */
  17. public static List<PsiField> getFields(AnActionEvent e) {
  18. PsiClass clazz = getPsiClass(e);
  19. return getFields(clazz);
  20. }
  21. public static List<PsiField> getFields(PsiClass clazz) {
  22. List<PsiField> result = new ArrayList<>();
  23. PsiField[] fields = clazz.getFields();
  24. for (PsiField field : fields) {
  25. String fieldName = field.getName();
  26. if (!Constants.EXCLUSION_FIELD.contains(fieldName)) {
  27. result.add(field);
  28. }
  29. }
  30. return result;
  31. }
  32. public static ClassBean getClassBean(AnActionEvent e) {
  33. List<PsiMethod> list = new ArrayList<>();
  34. PsiClass clazz = getPsiClass(e);
  35. String classFullName = clazz.getQualifiedName();
  36. PsiMethod[] methodList = clazz.getMethods();
  37. list.addAll(Arrays.asList(methodList));
  38. return new ClassBean(classFullName, list);
  39. }
  40. public static boolean isInterface(AnActionEvent e) {
  41. PsiClass clazz = getPsiClass(e);
  42. return clazz.isInterface();
  43. }
  44. public static PsiMethod[] getMethod(String getMethodName, AnActionEvent e) {
  45. PsiClass clazz = getPsiClass(e);
  46. PsiMethod[] methods = clazz.findMethodsByName(getMethodName, true);
  47. //boolean isPublic = field.hasModifierProperty(PsiModifier.PUBLIC);
  48. //if (isPublic) { return true; }
  49. return methods;
  50. }
  51. public static PsiClass getClassByName(String className, PsiClass containingClass, Project project) {
  52. PsiClass[] classes = PsiShortNamesCache.getInstance(project).getClassesByName(className,
  53. GlobalSearchScope.projectScope(project));
  54. if (classes.length == 0) {
  55. return null;
  56. }
  57. if (classes.length == 1) {
  58. return classes[0];
  59. }
  60. PsiJavaFile javaFile = (PsiJavaFile)containingClass.getContainingFile();
  61. PsiImportList importList = javaFile.getImportList();
  62. //获取所有引用
  63. Set<String> importedPackageSet = new HashSet();
  64. for (PsiImportStatement statement : importList.getImportStatements()) {
  65. importedPackageSet.add(statement.getQualifiedName());
  66. }
  67. for (PsiClass targetClass : classes) {
  68. PsiJavaFile targetClassContainingFile = (PsiJavaFile)targetClass.getContainingFile();
  69. String packageName = targetClassContainingFile.getPackageName();
  70. if (importedPackageSet.contains(packageName + "." + targetClass.getName())) {
  71. return targetClass;
  72. }
  73. }
  74. return null;
  75. }
  76. public static MethodDoc getDocComments(PsiDocComment docComment) {
  77. MethodDoc methodDoc = new MethodDoc();
  78. if (docComment == null || CommonUtil.isEmpty(docComment.getText())) {
  79. methodDoc.setMethodDoc(METHOD_DOC);
  80. } else {
  81. Map<String, String> result = new HashMap();
  82. try {
  83. String[] textArray = docComment.getText().split("\n");
  84. for (int i = 0; i < textArray.length; i++) {
  85. String line = textArray[i].trim();
  86. line = line.replaceAll("(?:\\*|\\/\\*\\*|@param|@return|\\/)", "");
  87. if (!CommonUtil.isEmpty(line)) {
  88. ArrayList<String> list = new ArrayList<>();
  89. list.addAll(Arrays.asList(line.split(" ")));
  90. Iterator iter = list.iterator();
  91. while (iter.hasNext()) {
  92. if (CommonUtil.isEmpty((String)iter.next())) {
  93. iter.remove();
  94. }
  95. }
  96. if (list.size() == 2) {
  97. result.put(list.get(0), list.get(1));
  98. }
  99. if (i == 1) {
  100. methodDoc.setMethodDoc(line);
  101. }
  102. }
  103. }
  104. } catch (Exception e) {
  105. LOGGER.error("[getDocComments] error,e={}", e);
  106. }
  107. methodDoc.setFieldDoc(result);
  108. }
  109. return methodDoc;
  110. }
  111. public static final NotificationGroup GROUP_DISPLAY_ID_INFO = new NotificationGroup("com.alibaba.scrat.tools",
  112. NotificationDisplayType.STICKY_BALLOON, true);
  113. public static void toClipboard(String str, Project project) {
  114. StringSelection selection = new StringSelection(str);
  115. Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  116. clipboard.setContents(selection, selection);
  117. Notification notification = GROUP_DISPLAY_ID_INFO.createNotification(SUCCESS_TO_CLIPBOARD,
  118. NotificationType.INFORMATION);
  119. Notifications.Bus.notify(notification, project);
  120. }
  121. public static void fail(Project project) {
  122. Notification notification = GROUP_DISPLAY_ID_INFO.createNotification("失败", NotificationType.ERROR);
  123. Notifications.Bus.notify(notification, project);
  124. }
  125. public static SettingData getSettingData() {
  126. if (PropertiesComponent.getInstance().isValueSet(SAVE_KEY)) {
  127. String res = PropertiesComponent.getInstance().getValue(SAVE_KEY);
  128. Gson gson = new Gson();
  129. return gson.fromJson(res, SettingData.class);
  130. }
  131. return new SettingData();
  132. }
  133. public static void setSettingData(SettingData settingData) {
  134. Gson gson = new Gson();
  135. String values = gson.toJson(settingData);
  136. PropertiesComponent.getInstance().setValue(SAVE_KEY, values);
  137. }

发表评论

表情:
评论列表 (有 0 条评论,449人围观)

还没有评论,来说两句吧...

相关阅读