Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

[important] 方法精确定位算法优化 #65

Closed
4ra1n opened this issue Jan 22, 2023 · 2 comments
Closed

[important] 方法精确定位算法优化 #65

4ra1n opened this issue Jan 22, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@4ra1n
Copy link
Member

4ra1n commented Jan 22, 2023

某些情况会误报

@4ra1n 4ra1n added the enhancement New feature or request label Jan 22, 2023
@4ra1n 4ra1n self-assigned this Jan 22, 2023
@4ra1n 4ra1n changed the title [improve] 方法定位算法优化 [important] 方法精确定位算法优化 Jan 22, 2023
@4ra1n
Copy link
Member Author

4ra1n commented Jan 22, 2023

    public int find(String total, String methodName, int paramNum) {
        // 以第一处方法名索引开始搜索
        for (int i = total.indexOf(methodName);
            // 循环找直到找不到为止
             i >= 0; i = total.indexOf(methodName, i + 1)) {
            // 如果方法名上一位是空格且下一位是(字符
            // 认为找到的方法的定义
            if (total.charAt(i - 1) == ' ' &&
                    total.charAt(i + methodName.length()) == '(') {
                // 前第二位是空格这是方法调用
                if (i - 2 > 0 && total.charAt(i - 2) == ' ') {
                    continue;
                }
                int curNum = 1;
                List<Character> temp = new ArrayList<>();
                for (int j = i + methodName.length() + 1; ; j++) {
                    temp.add(total.charAt(j));
                    // 遇到结尾
                    if (total.charAt(j) == ')') {
                        // 参数为0个的情况
                        if (total.charAt(j - 1) == '(') {
                            curNum = 0;
                        }
                        // 参数匹配认为找到了
                        if (curNum == paramNum) {
                            return i;
                        } else {
                            if (paramNum > curNum) {
                                int atNum = 0;
                                int rightNum = -1;
                                for (Character character : temp) {
                                    if (character == '@') {
                                        atNum++;
                                    }
                                    if (character == ')') {
                                        rightNum++;
                                    }
                                }
                                if (atNum == 0) {
                                    break;
                                } else {
                                    if (rightNum == atNum) {
                                        return i;
                                    } else if (atNum > rightNum) {
                                        continue;
                                    }
                                }
                            }
                        }
                        break;
                    } else if (total.charAt(j) == ',') {
                        // 已遍历参数数量+1
                        curNum++;
                    }
                }
            }
        }
        return 0;
    }

4ra1n added a commit that referenced this issue Jan 22, 2023
@4ra1n
Copy link
Member Author

4ra1n commented Jan 22, 2023

0.9

@4ra1n 4ra1n closed this as completed Jan 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant