Java 使用方面强制命名约定

Java 使用方面强制命名约定,java,aop,aspectj,pointcuts,Java,Aop,Aspectj,Pointcuts,我刚在大学里开始使用AspectJ,在其中一个实验室里,我们遇到了一个问题,我们需要在所有类中强制执行命名约定,即所有变量不得包含任何数字,即,如果我创建一个名为test1的变量,它应该发出警告。如果你们能给我指出正确的方向,我将不胜感激。我确信这是不可能的 AspectJ根本没有访问局部变量的权限,字段只能通过get()和set()切入点使用,因此只有在访问此类字段时才能声明错误或警告,而不是在没有访问权限的情况下才可以声明错误或警告 我会使用一个专用的源度量工具,比如。这很简单,PMD在构建

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

我确信这是不可能的

AspectJ根本没有访问局部变量的权限,字段只能通过get()和set()切入点使用,因此只有在访问此类字段时才能声明错误或警告,而不是在没有访问权限的情况下才可以声明错误或警告

我会使用一个专用的源度量工具,比如。这很简单,PMD在构建系统和IDE中有广泛的支持

相位实际上是关于行为,而不是命名

更新:好的,如果你能接受这些限制,这里有一个方面可以匹配所有访问名称中带有数字的字段:

public aspect TestAspect {

    pointcut accessBadField() :
        get(* *.*1*) ||
        get(* *.*2*) ||
        get(* *.*3*) ||
        get(* *.*4*) || 
        get(* *.*5*) ||
        get(* *.*6*) ||
        get(* *.*7*) ||
        get(* *.*8*) ||
        get(* *.*9*) ||
        get(* *.*0*)
        ;

    declare warning : accessBadField() : 
        "Please don't use numbers in field names";

}
参考资料:


    • 关于这一点,我能提出的最大建议如下:

      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!";
      }
      

      嗨,对不起,我想我答错问题了。。。。。我又读了一遍。。。。如果任何被访问的变量的名称由整数组成,我应该给出警告。…@John好的,您可以使用get()和set(),但正如我所写,“变量”仅限于字段。不支持局部变量。hmmmm k。。。。但是如果变量名包含整数,如何使用get和set进行检查。。。。。因为它们可能包含字符串名称;国际邮政编码1;我想对第二次申报提出警告……谢谢。。。。。您能解释一下wad get(*0)到底是怎么做的吗。。。。如果我想确保我的变量以数字开头,我将如何更改它…@John查看我现在添加的链接以获得详细解释
      get(**.*1*)
      表示“访问包含1的任何类中任何类型的字段”。这也匹配以1开头或结尾的字段。嗨,对不起,我想我的问题搞错了。。。。。我又读了一遍。。。。如果任何变量名由整数组成,我应该发出警告……天哪,我现在瞎了!:-)+1:)真的很难看,但无论如何……谢谢。。。。。您能解释一下wad get(*0)到底是怎么做的吗。。。。如果我想确保变量以数字开头,我将如何更改它……您应该知道Java标识符不能以数字开头。Java编译器禁止它。您不必为此创建方面。有关特定切入点的更多信息,请参阅