May 14, 2021 Groovy
Comments are forms of metadata that provides data about programs that are not part of the program itself. Comments have no direct effect on the operation of the code they comment on.
Comments are mainly used for the following reasons -
Compiler Information - The compiler can use comments to detect errors or suppress warnings.
Compile-time and deployment-time processing - Software tools can process comment information to generate code, XML files, and so on.
Run-time processing - Some comments can be checked at runtime.
In Groovy, the basic comments are as follows:
@interface - the at symbol character ( s) indicates to the compiler that the following is a comment.
Comments can define members in the form of methods without principals and optional default values.
Comments can be applied to the following types -
A single-line comment starts with // and can be anywhere on the line. From //start to the end of a line, it is considered part of the comment.
// a standalone single line comment
println "hello" // a comment till the end of the line
Multi-line comments begin with /? and can be anywhere on the line. S tarts with /, including new lines, until the first / end is considered part of the comment. Multiple lines of comments can be put at the beginning of a declaration or in the middle of a declaration.
/* a standalone multiline comment
spanning two lines */
println "hello" /* a multiline comment starting
at the end of a statement */
println 1 /* one */ + 2 /* two */
Here's an example of a string comment -
@interface Simple { String str1() default "HelloWorld"; }
enum DayOfWeek { mon, tue, wed, thu, fri, sat, sun } @interface Scheduled { DayOfWeek dayOfWeek() }
@interface Simple {} @Simple class User { String username int age } def user = new User(username: "Joe",age:1); println(user.age); println(user.username);
When you use comments, you need to set at least all members that do not have default values. H ere's an example. When you use the comment example after definition, you need to assign a value to it.
@interface Example { int status() } @Example(status = 1)
A good feature of comments in Groovy is that you can also use closures as comment values. Therefore, comments can be used with a variety of expressions.
Here's an example. T he comment Onlyif is created based on class values. Comments are then applied to two methods that publish different messages to the resulting variable based on the value of the numeric variable.
@interface OnlyIf { Class value() } @OnlyIf({ number<=6 }) void Version6() { result << 'Number greater than 6' } @OnlyIf({ number>=6 }) void Version7() { result << 'Number greater than 6' }
This is a very useful feature of comments in groovy. S ometimes there may be more than one comment on a method, as shown below. Sometimes this can become cumbersome with multiple comments.
@Procedure @Master class MyMasterProcedure {}
In this case, you can define a metacomment that brings together multiple comments and applies them to the method. So for the example above, you can use Annotation College to define a collection of comments.
import groovy.transform.AnnotationCollector @Procedure @Master @AnnotationCollector
Once done, you can apply the following meta-commenter to the method -
import groovy.transform.AnnotationCollector @Procedure @Master @AnnotationCollector @MasterProcedure class MyMasterProcedure {}