Skip to content

Commit

Permalink
🐛 Add rules for annotation inspection queries on location ANNOTATION (#…
Browse files Browse the repository at this point in the history
…683)

- Fixes #655

---------

Signed-off-by: Juan Manuel Leflet Estrada <[email protected]>
  • Loading branch information
jmle authored Sep 25, 2024
1 parent 974a5d2 commit 6f80215
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 16 deletions.
38 changes: 33 additions & 5 deletions demo-output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
incidents:
- uri: file:///examples/gradle-multi-project-example/build.gradle
message: dependency junit.junit with 4.12 is bad and you should feel bad for using it
codeSnip: " 1 apply plugin: 'idea'\n 2 \n 3 ext {\n 4 log4jVersion = '2.9.1'\n 5 }\n 6 \n 7 buildscript {\n 8 repositories {\n 9 jcenter()\n10 }\n11 dependencies {"
codeSnip: " 1 apply plugin: 'idea'\n 2 \n 3 ext {\n 4 log4jVersion = '2.9.1'\n 5 }\n 6 \n 7 buildscript {\n 8 repositories {\n 9 jcenter()\n10 mavenCentral()\n11 }"
lineNumber: 0
variables:
name: junit.junit
Expand Down Expand Up @@ -934,7 +934,7 @@
category: mandatory
incidents:
- uri: file:///examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/config/PersistenceConfig.java
message: Annotation instpection 01
message: Annotation inspection 01
codeSnip: "17 import org.springframework.transaction.annotation.EnableTransactionManagement;\n18 \n19 import io.konveyor.demo.config.ApplicationConfiguration;\n20 \n21 @Configuration\n22 @EnableJpaRepositories(basePackages = {\n23 \"io.konveyor.demo.ordermanagement.repository\"\n24 })\n25 @EnableTransactionManagement\n26 @EnableSpringDataWebSupport\n27 public class PersistenceConfig {\n28 \n29 \n30 \t@Bean\n31 public LocalContainerEntityManagerFactoryBean entityManagerFactory() {\n32 final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();\n33 em.setDataSource(dataSource());\n34 em.setPackagesToScan(\"io.konveyor.demo.ordermanagement.model\");\n35 em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());\n36 em.setJpaProperties(additionalProperties());\n37 "
lineNumber: 27
variables:
Expand All @@ -948,7 +948,7 @@
category: mandatory
incidents:
- uri: file:///examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/config/PersistenceConfig.java
message: Annotation instpection 02
message: Annotation inspection 02
codeSnip: "46 dataSource.setUrl(config.getProperty(\"jdbc.url\"));\n47 dataSource.setUsername(config.getProperty(\"jdbc.user\"));\n48 dataSource.setPassword(config.getProperty(\"jdbc.password\"));\n49 \n50 return dataSource;\n51 }\n52 \n53 @Bean\n54 public PlatformTransactionManager transactionManager() {\n55 final JpaTransactionManager transactionManager = new JpaTransactionManager();\n56 transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());\n57 return transactionManager;\n58 }\n59 \n60 @Bean\n61 public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {\n62 return new PersistenceExceptionTranslationPostProcessor();\n63 }\n64 \n65 final Properties additionalProperties() {\n66 \tApplicationConfiguration config = new ApplicationConfiguration();"
lineNumber: 56
variables:
Expand All @@ -962,14 +962,42 @@
category: mandatory
incidents:
- uri: file:///examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/controller/CustomerController.java
message: Annotation instpection 03
codeSnip: "11 import org.springframework.web.bind.annotation.GetMapping;\n12 import org.springframework.web.bind.annotation.PathVariable;\n13 import org.springframework.web.bind.annotation.RequestMapping;\n14 import org.springframework.web.bind.annotation.RestController;\n15 \n16 @RestController\n17 @RequestMapping(\"/customers\")\n18 public class CustomerController {\n19 \t\n20 \t@Autowired\n21 \tprivate CustomerService customerService;\n22 \t\n23 \tprivate static Logger logger = Logger.getLogger( CustomerController.class.getName() );\n24 \t\n25 \t@GetMapping(value = \"/{id}\", produces = MediaType.APPLICATION_JSON_VALUE)\n26 public Customer getById(@PathVariable(\"id\") Long id) {\n27 \t\tCustomer c = customerService.findById(id);\n28 \t\tif (c == null) {\n29 \t\t\tthrow new ResourceNotFoundException(\"Requested order doesn't exist\");\n30 \t\t}\n31 \t\tlogger.debug(\"Returning element: \" + c);"
message: Annotation inspection 03
codeSnip: "11 import org.springframework.web.bind.annotation.GetMapping;\n12 import org.springframework.web.bind.annotation.PathVariable;\n13 import org.springframework.web.bind.annotation.RequestMapping;\n14 import org.springframework.web.bind.annotation.RestController;\n15 \n16 @RestController\n17 @RequestMapping(\"/customers\")\n18 public class CustomerController {\n19 \t\n20 \t@Autowired\n21 \tprivate CustomerService customerService;\n22 \t\n23 \tprivate static Logger logger = Logger.getLogger( CustomerController.class.getName() );\n24 \t\n25 \t@GetMapping(value = \"id\", produces = MediaType.APPLICATION_JSON_VALUE)\n26 public Customer getById(@PathVariable(\"id\") Long id) {\n27 \t\tCustomer c = customerService.findById(id);\n28 \t\tif (c == null) {\n29 \t\t\tthrow new ResourceNotFoundException(\"Requested order doesn't exist\");\n30 \t\t}\n31 \t\tlogger.debug(\"Returning element: \" + c);"
lineNumber: 21
variables:
file: file:///examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/controller/CustomerController.java
kind: Field
name: customerService
package: io.konveyor.demo.ordermanagement.controller
java-annotation-inspection-04:
description: |
This rule looks for a given annotation used with some given properties (elements)
category: mandatory
incidents:
- uri: file:///examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/controller/CustomerController.java
message: Annotation inspection 04
codeSnip: "15 \n16 @RestController\n17 @RequestMapping(\"/customers\")\n18 public class CustomerController {\n19 \t\n20 \t@Autowired\n21 \tprivate CustomerService customerService;\n22 \t\n23 \tprivate static Logger logger = Logger.getLogger( CustomerController.class.getName() );\n24 \t\n25 \t@GetMapping(value = \"id\", produces = MediaType.APPLICATION_JSON_VALUE)\n26 public Customer getById(@PathVariable(\"id\") Long id) {\n27 \t\tCustomer c = customerService.findById(id);\n28 \t\tif (c == null) {\n29 \t\t\tthrow new ResourceNotFoundException(\"Requested order doesn't exist\");\n30 \t\t}\n31 \t\tlogger.debug(\"Returning element: \" + c);\n32 \t\treturn c;\n33 \t}\n34 \t\n35 \t@RequestMapping"
lineNumber: 25
variables:
file: file:///examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/controller/CustomerController.java
kind: Method
name: GetMapping
package: io.konveyor.demo.ordermanagement.controller
java-annotation-inspection-05:
description: |
This rule looks for a given annotation used with another annotation
category: mandatory
incidents:
- uri: file:///examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/config/PersistenceConfig.java
message: Annotation inspection 05
codeSnip: "11 import org.springframework.data.web.config.EnableSpringDataWebSupport;\n12 import org.springframework.jdbc.datasource.DriverManagerDataSource;\n13 import org.springframework.orm.jpa.JpaTransactionManager;\n14 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;\n15 import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;\n16 import org.springframework.transaction.PlatformTransactionManager;\n17 import org.springframework.transaction.annotation.EnableTransactionManagement;\n18 \n19 import io.konveyor.demo.config.ApplicationConfiguration;\n20 \n21 @Configuration\n22 @EnableJpaRepositories(basePackages = {\n23 \"io.konveyor.demo.ordermanagement.repository\"\n24 })\n25 @EnableTransactionManagement\n26 @EnableSpringDataWebSupport\n27 public class PersistenceConfig {\n28 \n29 \n30 \t@Bean\n31 public LocalContainerEntityManagerFactoryBean entityManagerFactory() {"
lineNumber: 21
variables:
file: file:///examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/config/PersistenceConfig.java
kind: Class
name: Configuration
package: io.konveyor.demo.ordermanagement.config
java-downloaded-maven-artifact:
description: |
This rule tests the application downloaded from maven artifact
Expand Down
16 changes: 15 additions & 1 deletion docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,26 @@ class MyApplication {
The structure of the `annotated` YAML element is the following:
```yaml
annotated:
pattern: a Java regex to match the fully qualified name of the annotation (mandatory)
pattern: a Java regex to match the fully qualified name of the annotation (optional)
elements: an array of elements to match within the annotation (optional)
- name: the exact name of the element to match against
value: a Java regex to match the value of the element
```

It is also possible to match an annotation with specific elements, without having to specify the symbol it annotates.
The following example would also match on the `@Bean` annotation in the same code as the last example. Note that
the only element specified with a `pattern` is the annotation itself:
```yaml
when:
java.referenced:
location: ANNOTATION
pattern: org.framework.Bean
annotated:
elements:
- name: url
value: "http://www.example.com"
```

##### Java Locations

The java provider allows scoping the search down to certain source code locations. Any one of the following search locations can be used to scope down java searches:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CustomerController {

private static Logger logger = Logger.getLogger( CustomerController.class.getName() );

@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value = "id", produces = MediaType.APPLICATION_JSON_VALUE)
public Customer getById(@PathVariable("id") Long id) {
Customer c = customerService.findById(id);
if (c == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ ext {
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
}
// dependencies {
// classpath 'com.gradleup.shadow:shadow-gradle-plugin:2.0.3'
// }
}

allprojects {
Expand All @@ -24,7 +25,7 @@ apply from: file('gradle/check.gradle')
apply from: file('gradle/heroku/clean.gradle')

subprojects {
apply plugin: 'com.github.johnrengelman.shadow'
// apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'

group = "io.jeffchao.${rootProject.name}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
task stage(dependsOn: ['clean', 'shadowJar'])
task stage(dependsOn: ['clean'])

task copyToLib(type: Copy) {
from "$buildDir/libs"
into "$rootProject.buildDir/libs"
}
copyToLib.dependsOn(shadowJar)
stage.dependsOn(copyToLib)
33 changes: 30 additions & 3 deletions rule-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@
- category: mandatory
description: |
This rule looks for a given class annotated with a given annotation
message: "Annotation instpection 01"
message: "Annotation inspection 01"
ruleID: java-annotation-inspection-01
when:
java.referenced:
Expand All @@ -356,7 +356,7 @@
- category: mandatory
description: |
This rule looks for a given method annotated with a given annotation
message: "Annotation instpection 02"
message: "Annotation inspection 02"
ruleID: java-annotation-inspection-02
when:
java.referenced:
Expand All @@ -367,11 +367,38 @@
- category: mandatory
description: |
This rule looks for a given field annotated with a given annotation
message: "Annotation instpection 03"
message: "Annotation inspection 03"
ruleID: java-annotation-inspection-03
when:
java.referenced:
pattern: io.konveyor.demo.ordermanagement.service.CustomerService
location: FIELD
annotated:
pattern: org.springframework.beans.factory.annotation.Autowired
- category: mandatory
description: |
This rule looks for a given annotation used with some given properties (elements)
message: "Annotation inspection 04"
ruleID: java-annotation-inspection-04
when:
java.referenced:
pattern: org.springframework.web.bind.annotation.GetMapping
location: ANNOTATION
annotated:
elements:
- name: value
value: "id"
- category: mandatory
description: |
This rule looks for a given annotation used with another annotation
message: "Annotation inspection 05"
ruleID: java-annotation-inspection-05
when:
java.referenced:
pattern: org.springframework.context.annotation.Configuration
location: ANNOTATION
annotated:
pattern: org.springframework.data.jpa.repository.config.EnableJpaRepositories
elements:
- name: basePackages
value: "io.konveyor.demo.ordermanagement.repository"

0 comments on commit 6f80215

Please sign in to comment.