Adding Parameters to Annotations Using Rewrite Actions
Adding Parameters to Annotations Using Rewrite Actions
![](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b33bded65a8553cfff2b_5f9c8e03fc2fddb0759f45d0_thumb-video-sensei_0.webp)
![](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79eed0a3c12cb0727ce_5f9f3d01788d350d8b1d4161_Blog3.webp)
In this blog post, we will:
- Demonstrate searching and matching annotations
- Amen annotations using mustache templates
Sensei provides the ability to match problematic code patterns and then amend them to agreed implementations. In this example, I am using @Disabled without a parameter as the problematic code pattern.
Disabled Test Annotation
Disabled tests without a specified reason can prove problematic over the long term because we forget why we disabled it.
@Disabled
void thisTestMethodHasNoDisabledReason(){
Assertions.fail("This test is disabled and should not run");
}
The risk is that, over time, the code base moves on, the disabled test is not updated in step with the purpose of the code and eventually becomes redundant and irrelevant, and potentially never re-enabled.
During code reviews, we will often point out that it is a good idea to add an explanatory description as the annotation parameter.
@Disabled ("Disabled to demonstrate adding a reason")
void thisTestMethodHasDisabledReason(){
Assertions.fail("This test is disabled and should not run");
}
A Sensei Recipe
We can write a recipe to detect when @Disabled is added without explanation and a Quick Fix that reminds us to add the actual reason explaining why we disabled it.
When I think about what I'm going to do, I have to:
- match the Disabled annotation without any parameters
- change the Disabled annotation to have a parameter with marker text "TODO: add a description here"
Create a Warning Recipe
I use Alt+Enter to Create a new Recipe.
![Create A New Recipe Function](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79cddbfc7565fac99c6_5f9d1750e13c7e50f7d4289f_Create_A_Warning_Recipe.png)
Then add the basic descriptive text in the general information.
![General Settings](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79c71bb7f30d7f10c23_5f9d1797612fe22f77edb7e5_GeneralSettings.png)
By making the rule a warning, any matching code is highlighted but not shown as a glaring error.
![Making The Rule A Warning](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79da5ef089db8e9341e_5f9d17b135d89d2a770d6431_MakingaRule.png)
Find the Annotation
In the recipe editor, I change the Search to match an annotation.
This will highlight all annotations in the preview.
![Recipe Editor](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79d29dd8b03943482b3_5f9d17e145546d3c70ead83c_RecipeEditor.png)
Having done that, I want to filter on the type of annotation.
I could just use Disabled but I fully qualify the class with the package so that it only matches the annotation from JUnit 5. Because the source code is shown in the preview, I can easily copy and paste this in from the actual code to avoid any typos.
I then want to match only annotations without Parameters, and I can use the GUI to do that.
![Recipe Settings](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79dde9fe46235b6a516_5f9d17fd477401b47d4900d4_RecipeSettings.png)
i.e. Search:
search:
annotation:
type: "org.junit.jupiter.api.Disabled"
without:
parameters:
- {}
Create a Rewrite Quick Fix Action
For my QuickFix I will use a rewrite action.
I use the Show Variables functionality to show me the Mustache variables and preview the contents.
![QuickFix Settings](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79ded0a3cfa390727cd_5f9d1864be215442f7e2ad6f_Image1.png)
And then I add the extra code needed to create the place marker comment.
![Add Extra code To Create The Place Marker Comment](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79de962d503efa860f8_5f9d187b612fe2053eedb970_Image2.png)
i.e. QuickFix:
availableFixes:
- name: "Add a todo comment parameter"
actions:
- rewrite:
to: "{{{ . }}}(\"TODO: add a description here\")"
target: "self"
Sensei in Action
We have created a short video showing the recipe creation process in action.
Summary
When building a rewrite Quick Fix, it is easier when we can search for the code element that we want to rewrite, because it is then the self entity we can act on.
In this example, I used a rewrite action to amend the Annotation. Rewrite is a general-purpose action that can apply to any code element and is a good default to explore.
Resources to get you started
Trust Agent by Secure Code Warrior
Discover SCW Trust Agent, an innovative solution designed to enhance security by aligning developer secure code knowledge and skills with the work they commit. It provides comprehensive visibility and controls across an organization's entire code repository, analyzing each commit against developers' secure code profiles. With SCW Trust Agent, organizations can strengthen their security posture, optimize development lifecycles, and scale developer-driven security.
Resources to get you started
Women in Security are Winning: How the AWSN is Setting Up a New Generation of Security Superwomen
Secure-by-Design is the latest initiative on everyone’s lips, and the Australian government, collaborating with CISA at the highest levels of global governance, is guiding a higher standard of software quality and security from vendors.
Women in Security are Winning: How the AWSN is Setting Up a New Generation of Security Superwomen
Secure-by-Design is the latest initiative on everyone’s lips, and the Australian government, collaborating with CISA at the highest levels of global governance, is guiding a higher standard of software quality and security from vendors.
SCW Trust Agent - Visibility and Control to Scale Developer Driven Security
SCW Trust Agent, introduced by Secure Code Warrior, offers security leaders the visibility and control needed to scale developer-driven security within organizations. By connecting to code repositories, it assesses code commit metadata, inspects developers, programming languages used, and shipment timestamps to determine developers' security knowledge.
Adding Parameters to Annotations Using Rewrite Actions
![](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b33bded65a8553cfff2b_5f9c8e03fc2fddb0759f45d0_thumb-video-sensei_0.webp)
In this blog post, we will:
- Demonstrate searching and matching annotations
- Amen annotations using mustache templates
Sensei provides the ability to match problematic code patterns and then amend them to agreed implementations. In this example, I am using @Disabled without a parameter as the problematic code pattern.
Disabled Test Annotation
Disabled tests without a specified reason can prove problematic over the long term because we forget why we disabled it.
@Disabled
void thisTestMethodHasNoDisabledReason(){
Assertions.fail("This test is disabled and should not run");
}
The risk is that, over time, the code base moves on, the disabled test is not updated in step with the purpose of the code and eventually becomes redundant and irrelevant, and potentially never re-enabled.
During code reviews, we will often point out that it is a good idea to add an explanatory description as the annotation parameter.
@Disabled ("Disabled to demonstrate adding a reason")
void thisTestMethodHasDisabledReason(){
Assertions.fail("This test is disabled and should not run");
}
A Sensei Recipe
We can write a recipe to detect when @Disabled is added without explanation and a Quick Fix that reminds us to add the actual reason explaining why we disabled it.
When I think about what I'm going to do, I have to:
- match the Disabled annotation without any parameters
- change the Disabled annotation to have a parameter with marker text "TODO: add a description here"
Create a Warning Recipe
I use Alt+Enter to Create a new Recipe.
![Create A New Recipe Function](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79cddbfc7565fac99c6_5f9d1750e13c7e50f7d4289f_Create_A_Warning_Recipe.png)
Then add the basic descriptive text in the general information.
![General Settings](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79c71bb7f30d7f10c23_5f9d1797612fe22f77edb7e5_GeneralSettings.png)
By making the rule a warning, any matching code is highlighted but not shown as a glaring error.
![Making The Rule A Warning](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79da5ef089db8e9341e_5f9d17b135d89d2a770d6431_MakingaRule.png)
Find the Annotation
In the recipe editor, I change the Search to match an annotation.
This will highlight all annotations in the preview.
![Recipe Editor](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79d29dd8b03943482b3_5f9d17e145546d3c70ead83c_RecipeEditor.png)
Having done that, I want to filter on the type of annotation.
I could just use Disabled but I fully qualify the class with the package so that it only matches the annotation from JUnit 5. Because the source code is shown in the preview, I can easily copy and paste this in from the actual code to avoid any typos.
I then want to match only annotations without Parameters, and I can use the GUI to do that.
![Recipe Settings](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79dde9fe46235b6a516_5f9d17fd477401b47d4900d4_RecipeSettings.png)
i.e. Search:
search:
annotation:
type: "org.junit.jupiter.api.Disabled"
without:
parameters:
- {}
Create a Rewrite Quick Fix Action
For my QuickFix I will use a rewrite action.
I use the Show Variables functionality to show me the Mustache variables and preview the contents.
![QuickFix Settings](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79ded0a3cfa390727cd_5f9d1864be215442f7e2ad6f_Image1.png)
And then I add the extra code needed to create the place marker comment.
![Add Extra code To Create The Place Marker Comment](https://cdn.prod.website-files.com/5fec9210c1841a6c20c6ce81/6022b79de962d503efa860f8_5f9d187b612fe2053eedb970_Image2.png)
i.e. QuickFix:
availableFixes:
- name: "Add a todo comment parameter"
actions:
- rewrite:
to: "{{{ . }}}(\"TODO: add a description here\")"
target: "self"
Sensei in Action
We have created a short video showing the recipe creation process in action.
Summary
When building a rewrite Quick Fix, it is easier when we can search for the code element that we want to rewrite, because it is then the self entity we can act on.
In this example, I used a rewrite action to amend the Annotation. Rewrite is a general-purpose action that can apply to any code element and is a good default to explore.
Resources to get you started
Women in Security are Winning: How the AWSN is Setting Up a New Generation of Security Superwomen
Secure-by-Design is the latest initiative on everyone’s lips, and the Australian government, collaborating with CISA at the highest levels of global governance, is guiding a higher standard of software quality and security from vendors.
SCW Trust Agent - Visibility and Control to Scale Developer Driven Security
SCW Trust Agent, introduced by Secure Code Warrior, offers security leaders the visibility and control needed to scale developer-driven security within organizations. By connecting to code repositories, it assesses code commit metadata, inspects developers, programming languages used, and shipment timestamps to determine developers' security knowledge.
Trust Agent by Secure Code Warrior
Discover SCW Trust Agent, an innovative solution designed to enhance security by aligning developer secure code knowledge and skills with the work they commit. It provides comprehensive visibility and controls across an organization's entire code repository, analyzing each commit against developers' secure code profiles. With SCW Trust Agent, organizations can strengthen their security posture, optimize development lifecycles, and scale developer-driven security.