Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions rules/S8322/groovy/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"title": "Simple \"@Grab\" annotations should use shorthand notation",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "2 min"
},
"tags": [
"groovy",
"convention"
],
"defaultSeverity": "Critical",
"ruleSpecification": "RSPEC-8322",
"sqKey": "S8322",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH"
},
"attribute": "CONVENTIONAL"
}
}
58 changes: 58 additions & 0 deletions rules/S8322/groovy/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
This rule raises an issue when a `@Grab` annotation uses the verbose format with named parameters (`group`, `module`, `version`) but no additional parameters are specified.

== Why is this an issue?

The `@Grab` annotation in Groovy supports two formats for specifying dependencies: a verbose format with named parameters and a concise shorthand format using the Maven coordinate style.

The verbose format requires explicitly naming each parameter:
```
@Grab(group='org.springframework', module='spring-orm', version='5.2.8.RELEASE')
```

The shorthand format uses the familiar Maven coordinate pattern:
```
@Grab('org.springframework:spring-orm:5.2.8.RELEASE')
```

When only the basic dependency information (group, module, version) is needed, the shorthand notation offers several advantages:

* *Improved readability*: The shorthand format is more compact and easier to scan visually
* *Familiar syntax*: It follows the same pattern used in Maven, Gradle, and other build tools
* *Reduced verbosity*: Less code to write and maintain
* *Consistency*: Aligns with common practices in the Java ecosystem

The verbose format should be reserved for cases where additional parameters are needed, such as `classifier`, `transitive`, `changing`, or `conf` options.

=== What is the potential impact?

Using the verbose format unnecessarily makes the code more verbose and harder to read. While this doesn't affect functionality, it reduces code quality and maintainability. The shorthand notation is more concise and follows established conventions from the Maven ecosystem, making the code more familiar to developers.

== How to fix it

Replace the verbose @Grab annotation with the shorthand notation using the 'group:module:version' format.

=== Code examples

==== Noncompliant code example

[source,groovy,diff-id=1,diff-type=noncompliant]
----
@Grab(group='org.springframework', module='spring-orm', version='5.2.8.RELEASE') // Noncompliant
import org.springframework.jdbc.core.JdbcTemplate
----

==== Compliant solution

[source,groovy,diff-id=1,diff-type=compliant]
----
@Grab('org.springframework:spring-orm:5.2.8.RELEASE')
import org.springframework.jdbc.core.JdbcTemplate
----

== Resources

=== Documentation

* Groovy Grape Documentation - https://groovy-lang.org/grape.html[Official documentation for Groovy's Grape dependency manager, including @Grab annotation usage]

* Groovy @Grab Annotation Reference - https://docs.groovy-lang.org/latest/html/documentation/grape.html#Grape-Annotation[Detailed reference for @Grab annotation syntax and parameters]
2 changes: 2 additions & 0 deletions rules/S8322/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}