From 6f80215ccbc6a5c9c226f5a1260ab51b101dfd4f Mon Sep 17 00:00:00 2001 From: Juan Manuel Leflet Estrada Date: Wed, 25 Sep 2024 09:44:26 +0200 Subject: [PATCH] :bug: Add rules for annotation inspection queries on location ANNOTATION (#683) - Fixes #655 --------- Signed-off-by: Juan Manuel Leflet Estrada --- demo-output.yaml | 38 ++++++++++++++++--- docs/rules.md | 16 +++++++- .../controller/CustomerController.java | 2 +- .../gradle-multi-project-example/build.gradle | 9 +++-- .../gradle/heroku/stage.gradle | 3 +- rule-example.yaml | 33 ++++++++++++++-- 6 files changed, 85 insertions(+), 16 deletions(-) diff --git a/demo-output.yaml b/demo-output.yaml index 9d0873a3..5bc15d6a 100644 --- a/demo-output.yaml +++ b/demo-output.yaml @@ -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 @@ -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: @@ -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: @@ -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 diff --git a/docs/rules.md b/docs/rules.md index f26939bf..b786de3b 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -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: diff --git a/external-providers/java-external-provider/examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/controller/CustomerController.java b/external-providers/java-external-provider/examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/controller/CustomerController.java index 7726cc85..e61f5074 100644 --- a/external-providers/java-external-provider/examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/controller/CustomerController.java +++ b/external-providers/java-external-provider/examples/customers-tomcat-legacy/src/main/java/io/konveyor/demo/ordermanagement/controller/CustomerController.java @@ -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) { diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/build.gradle b/external-providers/java-external-provider/examples/gradle-multi-project-example/build.gradle index d81723d2..2026e66e 100644 --- a/external-providers/java-external-provider/examples/gradle-multi-project-example/build.gradle +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/build.gradle @@ -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 { @@ -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}" diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/heroku/stage.gradle b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/heroku/stage.gradle index 819e4e83..98f3b68d 100644 --- a/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/heroku/stage.gradle +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/heroku/stage.gradle @@ -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) \ No newline at end of file diff --git a/rule-example.yaml b/rule-example.yaml index c67a94dc..a6f563ff 100644 --- a/rule-example.yaml +++ b/rule-example.yaml @@ -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: @@ -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: @@ -367,7 +367,7 @@ - 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: @@ -375,3 +375,30 @@ 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"