2

I just started AspectJ in university and in one of the labs we have a question where we need to enforce a naming convention across all classes which states that all variables must not include any numbers, i.e. if I create a variable called test1 it should give out a warning. I would appreciate if you guys could point me in the right direction.

insumity
  • 5,311
  • 8
  • 36
  • 64
John
  • 4,413
  • 6
  • 25
  • 28
  • Hi sorry i think i got the question wrong..... i read it again.... i'm supposed to give a warning if any variable name consist of integers..... – John May 12 '11 at 13:16

2 Answers2

1

I'm pretty sure that's impossible.

AspectJ does not have access to local variables at all, and fields are only available through the get() and set() pointcuts, so you can only declare an error or warning if such a field is accessed, not if it is just defined without access.

I'd go with a dedicated Source metric tool like PMD instead. It's easy to write a custom rule, and PMD has wide support in build systems and IDEs.

Aspects are really about behaviour, not about naming.

Update: OK, if you can live with the restrictions, here's an aspect that matches all access to fields with a number in their name:

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

}

Reference:

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Hi sorry i think i got the question wrong..... i read it again.... i'm supposed to give a warning if any variable accessed's name consist of integers..... – John May 12 '11 at 13:16
  • @John ok, you can use get() and set() for that, but as I wrote, "variables" are limited to fields. local variables are not supported. – Sean Patrick Floyd May 12 '11 at 13:21
  • hmmmm k.... but how do i check with get and set if the variable name contains integers..... Casuse they might contain String name; int postCode1; I wanna give a warning for the second declaration..... – John May 12 '11 at 13:22
  • Thanks..... would it be possible for you to explain wad get(* *.*0*) exactly does.... and how would i change it if i wanna make sure my variable starts with a digit..... – John May 12 '11 at 13:57
  • @John see the link I have added now for detailed explanation. `get(* *.*1*)` means "access a field of any type in any class that contains a 1". This also matches fields starting or ending with a 1. – Sean Patrick Floyd May 12 '11 at 14:07
  • Thanks.... I've been reading up on the information you provided.... but still am stuck with a couple of things.... I need to inforce a rule which says that the variable name must contain a special character lets say _ String test_try should not give a warning and String testTry should give a warning as it doesnt contain a _ – John May 12 '11 at 14:50
1

The maximum what I can suggest about this is the following:

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!";
}
Constantiner
  • 14,231
  • 4
  • 27
  • 34
  • Good lord, I'm blind now! :-) +1 – Sean Patrick Floyd May 12 '11 at 13:48
  • :) Really ugly way but anyway… – Constantiner May 12 '11 at 13:51
  • Thanks..... would it be possible for you to explain wad get(* .*0) exactly does.... and how would i change it if i wanna make sure my variable starts with a digit.... – John May 12 '11 at 14:07
  • You should know Java identifiers can't start with digits. It is prohibited by Java compiler. You haven't to create an aspect for that. – Constantiner May 12 '11 at 14:10
  • About particular pointcuts more information is [here](http://www.eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html) – Constantiner May 12 '11 at 14:11
  • Thanks.... I've been reading up on the information you provided.... but still am stuck with a couple of things.... I need to inforce a rule which says that the variable name must contain a special character lets say _ String test_try should not give a warning and String testTry should give a warning as it doesnt contain a _ – John May 12 '11 at 14:47
  • The short answer is: as far as AspectJ currently doesn't support any kind of regular expressions or so the complex naming conventions enforcers are impossible. My sample covers just numbers but it has a lot of limitations. To solve your problem you should use some special tools as PMD as pointed by @Sean Patrick Floyd – Constantiner May 12 '11 at 14:56
  • thanks.... i was wondering if i could use something like pointcut namingConvention() :get(* *.*_*); declare warning : !namingConvention() : "Field names must contain a _"; – John May 12 '11 at 15:19