Adding Parameters to Annotations Using Rewrite Actions
Tags: junit, java, securecode, securecoding
Published: 2020-11-04 14:04:00

---
title: Adding Parameters to Annotations Using Rewrite Actions
published: true
description: Sensei provides the ability to match problematic code patterns and then amend them to agreed implementations.
tags: JUnit, Java, SecureCode, SecureCoding
cover_image: https://assets-global.website-files.com/5f65e976b90b482ef088d1aa/5f9c8e03fc2fddb0759f45d0_thumb-video-sensei_0.jpg
canonical_url: https://www.securecodewarrior.com/blog/adding-parameters-to-annotations-using-rewrite-actions?pk_source=contentsyndication&pk_medium=devto&pk_campaign=sensei_devrel&pk_content=adding-parameters-to-an-annotation
---

In this example, we will:

- Demonstrate searching and matching annotations.
- Amend 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.

![Sensei Demo Adding Annotation Parameter](https://dev-to-uploads.s3.amazonaws.com/i/f6nqeb4fpilzandihznx.gif)

## 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");
}
```

## Demo Video of Sensei in Action

{% youtube mjXGliXJ7M8 %}


## 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.

### Task

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"


### Solution

I use `Alt+Enter` to Create a new Recipe.

![](https://assets-global.website-files.com/5f65e976b90b482ef088d1aa/5f9d1750e13c7e50f7d4289f_Create_A_Warning_Recipe.png)

Then add the basic descriptive text in the general information.

![](https://assets-global.website-files.com/5f65e976b90b482ef088d1aa/5f9d1797612fe22f77edb7e5_GeneralSettings.png)

By making the rule a warning, any matching code is highlighted but not shown as a glaring error.

![](https://assets-global.website-files.com/5f65e976b90b482ef088d1aa/5f9d17b135d89d2a770d6431_MakingaRule.png)

General:

~~~~~~~~
name: remember to add disabled description
short description: @Disabled should really have a description explaining why
Level: Error
~~~~~~~~

In the recipe editor, I change the Search to match an annotation.

This will highlight all annotations in the preview.

![](https://assets-global.website-files.com/5f65e976b90b482ef088d1aa/5f9d17e145546d3c70ead83c_RecipeEditor.png)

Having done that, I want to filter on the `type`. 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 preview is displayed, I can easily copy and paste this from the code.

I then want to match only annotations without Parameters, and I can use the GUI to do that.

![](https://assets-global.website-files.com/5f65e976b90b482ef088d1aa/5f9d17fd477401b47d4900d4_RecipeSettings.png)

i.e. Search:

~~~~~~~~
search:
  annotation:
    type: "org.junit.jupiter.api.Disabled"
    without:
      parameters:
      - {}
~~~~~~~~

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.

![](https://assets-global.website-files.com/5f65e976b90b482ef088d1aa/5f9d1864be215442f7e2ad6f_Image1.png)

And then I add the extra code needed to create the place marker comment.

![](https://assets-global.website-files.com/5f65e976b90b482ef088d1aa/5f9d187b612fe2053eedb970_Image2.png)


i.e. QuickFix:

```
availableFixes:
- name: "Add a todo comment parameter"
  actions:
  - rewrite:
      to: "{{{ . }}}(\"TODO: add a description here\")"
      target: "self"
```

## 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.

---

_All the code for this blog post can be found on GitHub in the [`junitexamples` module](https://github.com/SecureCodeWarrior/sensei-blog-examples/tree/master/junitexamples) of our blog examples repository [github.com/SecureCodeWarrior/sensei-blog-examples](https://github.com/SecureCodeWarrior/sensei-blog-examples)_

_The Sensei IntelliJ plugin can be downloaded for free on the [IntelliJ Marketplace](https://plugins.jetbrains.com/plugin/14015-sensei-by-secure-code-warrior-/getstarted)._