From b80252c9fe06e959065bc6fd3ff38a3f715be411 Mon Sep 17 00:00:00 2001 From: Juan Manuel Leflet Estrada Date: Tue, 30 Jul 2024 13:24:13 +0200 Subject: [PATCH 1/7] Add rules for annotation inspection queries on location ANNOTATION Signed-off-by: Juan Manuel Leflet Estrada --- rule-example.yaml | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/rule-example.yaml b/rule-example.yaml index c67a94dc..b24ec8fd 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" From b589a7e9dbc009c5cebec8668a7b9bab37310042 Mon Sep 17 00:00:00 2001 From: Juan Manuel Leflet Estrada Date: Wed, 31 Jul 2024 13:36:45 +0200 Subject: [PATCH 2/7] Change value in annotation rule Signed-off-by: Juan Manuel Leflet Estrada --- rule-example.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rule-example.yaml b/rule-example.yaml index b24ec8fd..2b3d32d7 100644 --- a/rule-example.yaml +++ b/rule-example.yaml @@ -387,7 +387,7 @@ annotated: elements: - name: value - value: "/{id}" + value: "/\{id\}" - category: mandatory description: | This rule looks for a given annotation used with another annotation From b1995c5f3b4df84043f641c13350515f7b76a25b Mon Sep 17 00:00:00 2001 From: Juan Manuel Leflet Estrada Date: Wed, 7 Aug 2024 11:05:43 +0200 Subject: [PATCH 3/7] Change field Signed-off-by: Juan Manuel Leflet Estrada --- .../demo/ordermanagement/controller/CustomerController.java | 2 +- rule-example.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/rule-example.yaml b/rule-example.yaml index 2b3d32d7..a6f563ff 100644 --- a/rule-example.yaml +++ b/rule-example.yaml @@ -387,7 +387,7 @@ annotated: elements: - name: value - value: "/\{id\}" + value: "id" - category: mandatory description: | This rule looks for a given annotation used with another annotation From d14fa4ee842ff8370a59a66ce9de0655236c66f6 Mon Sep 17 00:00:00 2001 From: Juan Manuel Leflet Estrada Date: Tue, 13 Aug 2024 17:21:18 +0200 Subject: [PATCH 4/7] Fix tests Signed-off-by: Juan Manuel Leflet Estrada --- demo-output.yaml | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/demo-output.yaml b/demo-output.yaml index 9d0873a3..9237d636 100644 --- a/demo-output.yaml +++ b/demo-output.yaml @@ -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 From 7ccee826574bb8ac1dfc2c62fd02cb7e4bae42f6 Mon Sep 17 00:00:00 2001 From: Juan Manuel Leflet Estrada Date: Fri, 16 Aug 2024 13:51:01 +0200 Subject: [PATCH 5/7] Add documentation Signed-off-by: Juan Manuel Leflet Estrada --- docs/rules.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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: From 1325b2cf043e31a424a433b4bdc899f601859dd8 Mon Sep 17 00:00:00 2001 From: Juan Manuel Leflet Estrada Date: Fri, 30 Aug 2024 16:42:54 +0200 Subject: [PATCH 6/7] Fix broken gradle build Signed-off-by: Juan Manuel Leflet Estrada --- demo-output.yaml | 6 +++++- .../examples/gradle-multi-project-example/build.gradle | 9 +++++---- .../gradle/heroku/stage.gradle | 3 +-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/demo-output.yaml b/demo-output.yaml index 9237d636..fadfe84d 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 @@ -1048,6 +1048,10 @@ message: "" - uri: file:///examples/gradle-multi-project-example/gradle/wrapper/gradle-wrapper.jar message: "" + - uri: file:///examples/gradle-multi-project-example/template-core/build/libs/template-core.jar + message: "" + - uri: file:///examples/gradle-multi-project-example/template-server/build/libs/template-server.jar + message: "" - uri: file:///examples/inclusion-tests/pom.xml message: "" - uri: file:///examples/java-project/pom.xml 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 From 52c7fbba54aa1b6efdb90c7ae2001c45f6ff0c8c Mon Sep 17 00:00:00 2001 From: Juan Manuel Leflet Estrada Date: Mon, 2 Sep 2024 14:29:18 +0200 Subject: [PATCH 7/7] Fix demo output Signed-off-by: Juan Manuel Leflet Estrada --- demo-output.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/demo-output.yaml b/demo-output.yaml index fadfe84d..5bc15d6a 100644 --- a/demo-output.yaml +++ b/demo-output.yaml @@ -1048,10 +1048,6 @@ message: "" - uri: file:///examples/gradle-multi-project-example/gradle/wrapper/gradle-wrapper.jar message: "" - - uri: file:///examples/gradle-multi-project-example/template-core/build/libs/template-core.jar - message: "" - - uri: file:///examples/gradle-multi-project-example/template-server/build/libs/template-server.jar - message: "" - uri: file:///examples/inclusion-tests/pom.xml message: "" - uri: file:///examples/java-project/pom.xml