java - 通过方面执行命名约定

标签 java aop aspectj pointcuts

我刚刚在大学开始使用 AspectJ,在一个实验室中我们有一个问题,我们需要在所有类中强制执行命名约定,该约定规定所有变量不得包含任何数字,即如果我创建一个名为 test1 的变量应发出警告。如果你们能给我指明正确的方向,我将不胜感激。

最佳答案

对此我能提出的最多建议如下:

public aspect NamingConventionsAspect {
    pointcut methodWith0() : execution(* *.*0*(..));
    pointcut methodWith1() : execution(* *.*1*(..));
    pointcut methodWith2() : execution(* *.*2*(..));
    pointcut methodWith3() : execution(* *.*3*(..));
    pointcut methodWith4() : execution(* *.*4*(..));
    pointcut methodWith5() : execution(* *.*5*(..));
    pointcut methodWith6() : execution(* *.*6*(..));
    pointcut methodWith7() : execution(* *.*7*(..));
    pointcut methodWith8() : execution(* *.*8*(..));
    pointcut methodWith9() : execution(* *.*9*(..));
    pointcut readFieldWith0() : get(* *.*0*);
    pointcut readFieldWith1() : get(* *.*1*);
    pointcut readFieldWith2() : get(* *.*2*);
    pointcut readFieldWith3() : get(* *.*3*);
    pointcut readFieldWith4() : get(* *.*4*);
    pointcut readFieldWith5() : get(* *.*5*);
    pointcut readFieldWith6() : get(* *.*6*);
    pointcut readFieldWith7() : get(* *.*7*);
    pointcut readFieldWith8() : get(* *.*8*);
    pointcut readFieldWith9() : get(* *.*9*);
    pointcut setFieldWith0() : set(* *.*0*);
    pointcut setFieldWith1() : set(* *.*1*);
    pointcut setFieldWith2() : set(* *.*2*);
    pointcut setFieldWith3() : set(* *.*3*);
    pointcut setFieldWith4() : set(* *.*4*);
    pointcut setFieldWith5() : set(* *.*5*);
    pointcut setFieldWith6() : set(* *.*6*);
    pointcut setFieldWith7() : set(* *.*7*);
    pointcut setFieldWith8() : set(* *.*8*);
    pointcut setFieldWith9() : set(* *.*9*);
    pointcut classWith0() : within(*0*);
    pointcut classWith1() : within(*1*);
    pointcut classWith2() : within(*2*);
    pointcut classWith3() : within(*3*);
    pointcut classWith4() : within(*4*);
    pointcut classWith5() : within(*5*);
    pointcut classWith6() : within(*6*);
    pointcut classWith7() : within(*7*);
    pointcut classWith8() : within(*8*);
    pointcut classWith9() : within(*9*);

    declare error : methodWith0() || methodWith1() || methodWith2() || methodWith3() || methodWith4() || 
    methodWith5() || methodWith6() || methodWith7() || methodWith8() || methodWith9() || readFieldWith0() || 
    readFieldWith1() || readFieldWith2() || readFieldWith3() || readFieldWith4() || readFieldWith5() || 
    readFieldWith6() || readFieldWith7() || readFieldWith8() || readFieldWith9() || setFieldWith0() || 
    setFieldWith1() || setFieldWith2() || setFieldWith3() || setFieldWith4() || setFieldWith5() || setFieldWith6() || 
    setFieldWith7() || setFieldWith8() || setFieldWith9() || classWith0() || classWith1() || classWith2() || 
    classWith3() || classWith4() || classWith5() || classWith6() || classWith7() || classWith8() || classWith9() : 
        "Identifiers shouldn't contain numbers!";
}

关于java - 通过方面执行命名约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5978101/

相关文章:

Java Aspect 返回要在方法中使用的值

java - 无法将方面应用于 String 类

java - JProgressBar 中的颜色

java - 使用 JTextField 时无法在内部类错误中引用非最终变量

java - 在Java中执行linux终端命令,涉及写入日志文件,但没有权限?

java - Spring AOP : a proxy per advice?

spring - 有人有 Spring AOP @DeclareParents 示例吗?

aop - 如何从aspectj中排除方法

java - AspectJ 的 CGLIB 字节代码生成

java - 我们可以使用 Dukescript 作为小程序的替代品吗?