Excellent question. In the current version the modifiers are stored as annotations on the declarations. Annotations are not visited by default by the visit statement, so you have to project them out yourself:
void visitModifier(loc file){
Declaration decl = createAstFromFile(file, true);
visit (decl){
case Declaration d:
switch (d@modifiers?[]) { // get out the modifiers or generate empty list
case [*_,\public(),*_] : println("public");
case [*_,\private(),*_] : println("private");
case [*_,\protected(),*_]: println("protected");
default : println("friendly");
}
}
}
Note that d@modifiers
produces a list of modifiers, so you could find \static()
and \final()
in there as well. Without list matching it can also be done of course:
void visitModifier2(loc file){
Declaration decl = createAstFromFile(file, true);
visit (decl){
case Declaration d: {
mods = d@modifiers?[];
if (\public() <- mods) // or if (\public() in mods)
println("public");
else if (\private() <- mods)
println("private");
else if (\protected() <- mods)
println("protected");
else
println("friendly");
}
}
}
or, you could just collect all modifiers in a list like so:
[ *(d@modifiers?[]) | /Declaration d := decl ]