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

[important] [bug] 方法定位算法遇到泛型时的问题 #70

Closed
4ra1n opened this issue Feb 1, 2023 · 3 comments
Closed

[important] [bug] 方法定位算法遇到泛型时的问题 #70

4ra1n opened this issue Feb 1, 2023 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@4ra1n
Copy link
Member

4ra1n commented Feb 1, 2023

No description provided.

@4ra1n 4ra1n added the bug Something isn't working label Feb 1, 2023
@4ra1n 4ra1n self-assigned this Feb 1, 2023
@4ra1n
Copy link
Member Author

4ra1n commented Feb 1, 2023

这个问题暂时关闭,因为实现起来有很大困难,且场景不多

@4ra1n 4ra1n closed this as completed Feb 1, 2023
@4ra1n 4ra1n changed the title [bug] 方法定位算法遇到范型时的问题 [important] [bug] 方法定位算法遇到范型时的问题 Feb 2, 2023
@4ra1n
Copy link
Member Author

4ra1n commented Feb 2, 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;
                                // 右括号默认是-1
                                // 因为参数最终一定以右括号结尾
                                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++;
                    } else if (total.charAt(j) == '<') {
                        int dNum = -1;
                        while (true) {
                            j++;
                            if (total.charAt(j) == '>') {
                                if (dNum != -1) {
                                    curNum -= dNum;
                                }
                                break;
                            }
                            if (total.charAt(j) == ',') {
                                dNum++;
                            }
                        }
                    }
                }
            }
        }
        return 0;
    }

@4ra1n 4ra1n reopened this Feb 2, 2023
4ra1n pushed a commit that referenced this issue Feb 2, 2023
@4ra1n 4ra1n changed the title [important] [bug] 方法定位算法遇到范型时的问题 [important] [bug] 方法定位算法遇到泛型时的问题 Feb 2, 2023
@4ra1n
Copy link
Member Author

4ra1n commented Feb 6, 2023

1.0

@4ra1n 4ra1n closed this as completed Feb 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant