CodeNavi自定义规则节点属性(通用属性)

举报
yd_296685231 发表于 2024/06/27 11:00:45 2024/06/27
【摘要】 CodeNavi自定义规则的通用属性

说明:通用属性为所有节点都有的属性

属性

描述

值类型

示例

DSL 规则

annotations

节点的注解

annotation节点集合

@Parameter(names = {"ok", "hello", "yes"})
public void initArrayTest() {}

functionDeclaration fd where
    fd.annotations contain anno where
        anno.name == "Parameter";

startLine

节点的起始行

数值

_

functionDeclaration fd where
    fd.startLine == 21;

endLine

节点的结束行

数值

_

functionDeclaration fd where
    fd.endLine == 23;

enclosingClass

节点所在的类

recordDeclaration节点

public class FunctionCallTest {
    public void setHeader(String header, String option) {
        new MyRandom().intNum();
    }

    public String getString(String ok, String ss) {
        return ss;
    }

    public void getIt() {
        setHeader("Access-Control-Allow-Origin", "*");
    }
}

functionCall fc where and(
    fc.function.name == "setHeader",
    fc.function.enclosingClass.name endWith "FunctionCallTest"
);

enclosingClassName

节点所在的类名

字符串

new MyRandom().intNum();

functionCall fc where and(
    fc.name == "intNum",
    fc.function.enclosingClassName endWith "MyRandom"
);

enclosingFunction

节点所在的方法

functionDeclaration节点

public String getString(String ok, String ss) {
    return ss.replace('1', '2');
}

functionCall fc where and(
    fc.name == "replace",
    fc.enclosingFunction.parameters.size() == 2
);

enclosingFunctionName

节点所在的方法名

字符串

public String getString(String ok, String ss) {
    return ss.replace('1', '2');
}

functionCall fc where and(
    fc.name == "replace",
    fc.enclosingFunctionName == "getString"
);

enclosingIf

节点所在的If块

ifBlock节点

private void testEnclosingIf() {
    boolean flag = true;
    if(!flag) {
        int n = 0;
        System.out.println(n);
        while (n < 5) {
            if (flag) {
                System.out.println("ifBlock2"); // 告警行
                n++;
            }
        }
    }
}

functionCall fc5 where and(
    fc5.enclosingFunction.name == "testEnclosingIf",
    fc5.name == "println",
    fc5.enclosingIf.startLine == 117
);

enclosingFor

节点所在的for循环

forBlock节点

private void testEnclosingFor() {
    int rows = 5;
    int columns = 10;

    for (int i = 0; i < rows; i++) {
    int count = 0;
    count++;
        for (int j = 0; j < columns; j++) {
            if (j % 2 == 0){
                count++; // 告警行
            }
        }
    }
}

unaryOperation uo1 where and(
    uo1.enclosingWhile.size() == 0,
    uo1.enclosingTry.size() == 0,
    uo1.enclosingCatch.size() == 0,
    uo1.enclosingForEach.size() == 0,
    uo1.enclosingFunction.name == "testEnclosingFor",
    uo1.operand.name == "count",
    uo1.enclosingFor.startLine == 30
);

enclosingForEach

节点所在的forEach循环

forEachBlock节点

private void testEnclosingForEach() {
    List<List<Integer>> numbers = new ArrayList<>();
    numbers.add(Arrays.asList(1, 2, 3));
    numbers.add(Arrays.asList(4, 5, 6));
    numbers.add(Arrays.asList(7, 8, 9));

    for (List<Integer> row : numbers) {
        for (int num : row) {
            System.out.println(num + " "); // 告警行
        }
        System.out.println();
    }
}

functionCall fc1 where and(
    fc1.enclosingFunction.name == "testEnclosingForEach",
    fc1.name == "println",
    fc1.enclosingForEach.startLine == 47
);

enclosingWhile

节点所在的while循环

whileBlock节点

private void testEnclosingWhile() {
    int rows = 4;
    int columns = 6;
    int i = 0;

    while (i < rows) {
        int j = 0;
        while (j < columns) {
            System.out.println("* "); // 告警行
            j++;
        }
        System.out.println();
        i++;
    }
}

functionCall fc2 where and(
    fc2.enclosingFunction.name == "testEnclosingWhile",
    fc2.name == "println",
    fc2.enclosingWhile.startLine == 62
);

enclosingTry

节点所在的try块

tryBlock节点

private void testEnclosingTry() {
    try (FileInputStream fileInputStream = new FileInputStream("file.txt")) {
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream))) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.out.print("Error reading file: " + e.getMessage()); // 告警行
        }
    } catch (IOException e) {
        System.out.println("File not found: " + e.getMessage());
    }
}

functionCall fc3 where and(
    fc3.enclosingFunction.name == "testEnclosingTry",
    fc3.name == "print",
    fc3.enclosingTry contain variableDeclaration
);

enclosingCatch

节点所在的catch块

catchBlock节点

private void testEnclosingCatch() {
        try {
            int[] arr = new int[5];
            arr[10] = 20;
            String str = null;
            System.out.println(str.length());
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界异常:" + e.getMessage());
        } catch (NullPointerException e) {
            System.out.println("空指针异常:" + e.getMessage());
        } catch (Exception e) {
            if (e instanceof IOException) {
                System.out.println("IO异常:" + e.getMessage()); // 告警行
            } else {
                System.out.println("其他异常:" + e.getMessage()); // 告警行
            }
        } finally {
            System.out.println("程序执行完毕!");
        }
    }

functionCall fc4 where and(
    fc4.enclosingFunction.name == "testEnclosingCatch",
    fc4.name == "println",
    fc4.enclosingCatch.startLine == 98
);


【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。