Skip to content

Annotation

Annotations can be generated thanks to Crygen::Types::Annotation class (here abbreviated as CGT::Annotation).

annotation_type = CGT::Annotation.new("MyAnnotation")
puts annotation_type.generate

Output:

@[MyAnnotation]

Passing values as arguments

Using the #add_arg method, you can add values that will be passed as arguments to the annotation.

annotation_type = CGT::Annotation.new("MyAnnotation")
annotation_type.add_arg("1")
annotation_type.add_arg("Hello world".dump)

puts annotation_type.generate

Output:

@[MyAnnotation(1, "Hello world")]

In addition, you can name the argument like:

annotation_type = CGT::Annotation.new("MyAnnotation")
annotation_type.add_arg("number", "1")
annotation_type.add_arg("text", "Hello world".dump)

puts annotation_type.generate

Output:

@[MyAnnotation(number: 1, text: "Hello world")]

Info

If you want to pass the string in the argument, then you have to call the String#dump method.

There's an even better way: if you pass values without necessarily naming arguments, you can use the #add_args method:

annotation_type = CGT::Annotation.new("MyAnnotation")
annotation_type.add_args("1", "true", "Hello world".dump)

puts annotation_type.generate

Output:

@[MyAnnotation(1, true, "Hello world")]