IDEA 插件开发,工具方法


    public static PsiClass getPsiClass(AnActionEvent e) {
        PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
        Editor editor = e.getData(PlatformDataKeys.EDITOR);
        if (psiFile == null || editor == null) {
            return null;
        }
        int offset = editor.getCaretModel().getOffset();
        PsiElement element = psiFile.findElementAt(offset);
        return PsiTreeUtil.getParentOfType(element, PsiClass.class);
    }

    /**
     * 获取类的所有属性
     *
     * @param e
     * @return
     */
    public static List<PsiField> getFields(AnActionEvent e) {
        PsiClass clazz = getPsiClass(e);
        return getFields(clazz);
    }

    public static List<PsiField> getFields(PsiClass clazz) {
        List<PsiField> result = new ArrayList<>();
        PsiField[] fields = clazz.getFields();
        for (PsiField field : fields) {
            String fieldName = field.getName();
            if (!Constants.EXCLUSION_FIELD.contains(fieldName)) {
                result.add(field);
            }
        }
        return result;
    }

    public static ClassBean getClassBean(AnActionEvent e) {
        List<PsiMethod> list = new ArrayList<>();
        PsiClass clazz = getPsiClass(e);
        String classFullName = clazz.getQualifiedName();
        PsiMethod[] methodList = clazz.getMethods();
        list.addAll(Arrays.asList(methodList));
        return new ClassBean(classFullName, list);
    }

    public static boolean isInterface(AnActionEvent e) {
        PsiClass clazz = getPsiClass(e);
        return clazz.isInterface();
    }

    public static PsiMethod[] getMethod(String getMethodName, AnActionEvent e) {
        PsiClass clazz = getPsiClass(e);
        PsiMethod[] methods = clazz.findMethodsByName(getMethodName, true);
        //boolean isPublic = field.hasModifierProperty(PsiModifier.PUBLIC);
        //if (isPublic) { return true; }
        return methods;
    }

    public static PsiClass getClassByName(String className, PsiClass containingClass, Project project) {
        PsiClass[] classes = PsiShortNamesCache.getInstance(project).getClassesByName(className,
            GlobalSearchScope.projectScope(project));
        if (classes.length == 0) {
            return null;
        }
        if (classes.length == 1) {
            return classes[0];
        }
        PsiJavaFile javaFile = (PsiJavaFile)containingClass.getContainingFile();
        PsiImportList importList = javaFile.getImportList();

        //获取所有引用
        Set<String> importedPackageSet = new HashSet();
        for (PsiImportStatement statement : importList.getImportStatements()) {
            importedPackageSet.add(statement.getQualifiedName());
        }
        for (PsiClass targetClass : classes) {
            PsiJavaFile targetClassContainingFile = (PsiJavaFile)targetClass.getContainingFile();
            String packageName = targetClassContainingFile.getPackageName();
            if (importedPackageSet.contains(packageName + "." + targetClass.getName())) {
                return targetClass;
            }
        }
        return null;
    }



    public static MethodDoc getDocComments(PsiDocComment docComment) {
        MethodDoc methodDoc = new MethodDoc();

        if (docComment == null || CommonUtil.isEmpty(docComment.getText())) {
            methodDoc.setMethodDoc(METHOD_DOC);
        } else {
            Map<String, String> result = new HashMap();
            try {
                String[] textArray = docComment.getText().split("\n");
                for (int i = 0; i < textArray.length; i++) {
                    String line = textArray[i].trim();
                    line = line.replaceAll("(?:\\*|\\/\\*\\*|@param|@return|\\/)", "");
                    if (!CommonUtil.isEmpty(line)) {
                        ArrayList<String> list = new ArrayList<>();
                        list.addAll(Arrays.asList(line.split(" ")));
                        Iterator iter = list.iterator();
                        while (iter.hasNext()) {
                            if (CommonUtil.isEmpty((String)iter.next())) {
                                iter.remove();
                            }
                        }
                        if (list.size() == 2) {
                            result.put(list.get(0), list.get(1));
                        }
                        if (i == 1) {
                            methodDoc.setMethodDoc(line);
                        }
                    }
                }
            } catch (Exception e) {
                LOGGER.error("[getDocComments] error,e={}", e);
            }
            methodDoc.setFieldDoc(result);
        }
        return methodDoc;
    }

 public static final NotificationGroup GROUP_DISPLAY_ID_INFO = new NotificationGroup("com.alibaba.scrat.tools",
        NotificationDisplayType.STICKY_BALLOON, true);

    public static void toClipboard(String str, Project project) {
        StringSelection selection = new StringSelection(str);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, selection);
        Notification notification = GROUP_DISPLAY_ID_INFO.createNotification(SUCCESS_TO_CLIPBOARD,
            NotificationType.INFORMATION);
        Notifications.Bus.notify(notification, project);
    }

    public static void fail(Project project) {
        Notification notification = GROUP_DISPLAY_ID_INFO.createNotification("失败", NotificationType.ERROR);
        Notifications.Bus.notify(notification, project);
    }

 public static SettingData getSettingData() {
        if (PropertiesComponent.getInstance().isValueSet(SAVE_KEY)) {
            String res = PropertiesComponent.getInstance().getValue(SAVE_KEY);
            Gson gson = new Gson();
            return gson.fromJson(res, SettingData.class);
        }
        return new SettingData();
    }

    public static void setSettingData(SettingData settingData) {
        Gson gson = new Gson();
        String values = gson.toJson(settingData);
        PropertiesComponent.getInstance().setValue(SAVE_KEY, values);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值