From 1d943c9f58d34be03638577e69eb185c7c22be25 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 2 Jun 2022 21:16:56 +0200 Subject: [PATCH] [jOOQ/jOOQ#13399] jOOQ-checker should system property defaults that apply to the entire checked code --- .../src/main/java/org/jooq/checker/Tools.java | 12 +++++++++++ .../jOOQ-checker-framework-example/pom.xml | 4 ++++ .../checker/SQLDialectCheckerTests.java | 21 ++++++++++--------- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/jOOQ-checker/src/main/java/org/jooq/checker/Tools.java b/jOOQ-checker/src/main/java/org/jooq/checker/Tools.java index a050c02920..99d18f01b7 100644 --- a/jOOQ-checker/src/main/java/org/jooq/checker/Tools.java +++ b/jOOQ-checker/src/main/java/org/jooq/checker/Tools.java @@ -47,6 +47,7 @@ import java.io.PrintWriter; import java.util.EnumSet; import java.util.function.Function; import java.util.function.Supplier; +import java.util.stream.Stream; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; @@ -92,6 +93,9 @@ final class Tools { EnumSet allowed = EnumSet.noneOf(SQLDialect.class); EnumSet required = EnumSet.noneOf(SQLDialect.class); + defaults("org.jooq.checker.dialects.allow", allowed); + defaults("org.jooq.checker.dialects.require", required); + boolean evaluateRequire = true; while (enclosing != null) { Allow allow = enclosing.getAnnotation(Allow.class); @@ -157,6 +161,14 @@ final class Tools { return null; } + private static void defaults(String property, EnumSet set) { + Stream.of(System.getProperty(property, "").split(",")) + .map(String::trim) + .filter(s -> !s.isEmpty()) + .map(s -> SQLDialect.valueOf(s.toUpperCase())) + .forEach(set::add); + } + static final T checkPlainSQL( MethodInvocationTree node, Supplier enclosingSupplier, diff --git a/jOOQ-examples/jOOQ-checker-framework-example/pom.xml b/jOOQ-examples/jOOQ-checker-framework-example/pom.xml index 064d4434e0..f0ffd22f81 100644 --- a/jOOQ-examples/jOOQ-checker-framework-example/pom.xml +++ b/jOOQ-examples/jOOQ-checker-framework-example/pom.xml @@ -68,6 +68,9 @@ org.jooq.checker.SQLDialectChecker + + -J-Dorg.jooq.checker.dialects.allow=HSQLDB + @@ -99,6 +102,7 @@ -XDcompilePolicy=simple -Xplugin:ErrorProne + -XDorg.jooq.checker.dialects.allow=HSQLDB diff --git a/jOOQ-examples/jOOQ-checker-framework-example/src/main/java/org/jooq/example/checker/SQLDialectCheckerTests.java b/jOOQ-examples/jOOQ-checker-framework-example/src/main/java/org/jooq/example/checker/SQLDialectCheckerTests.java index 2fbb561497..410523863f 100644 --- a/jOOQ-examples/jOOQ-checker-framework-example/src/main/java/org/jooq/example/checker/SQLDialectCheckerTests.java +++ b/jOOQ-examples/jOOQ-checker-framework-example/src/main/java/org/jooq/example/checker/SQLDialectCheckerTests.java @@ -13,16 +13,17 @@ import org.jooq.impl.DSL; // The class requires the H2, MYSQL, and POSTGRES_9_5 families // The inherited @Allow annotation from the package allows only the MYSQL family, though. +// The system property also allows HSQLDB @Require({ H2, MYSQL, POSTGRES_9_5 }) public class SQLDialectCheckerTests { - // @Allow = MYSQL (inherited from package) + // @Allow = { HSQLDB (inherited from the system property), MYSQL (inherited from package) } // @Require = { H2, MYSQL, POSTGRES_9_5 } - public static void doesntCompileBecauseOnlyMySQLIsAllowed() { + public static void compilesBecauseOnlyHSQLDBIsAllowed() { DSL.array(2); } - // @Allow = { MYSQL (inherited from package), POSTGRES_9_4 } + // @Allow = { HSQLDB (inherited from the system property), MYSQL (inherited from package), POSTGRES_9_4 } // @Require = { POSTGRES_9_4 } @Allow(POSTGRES_9_4) @Require(POSTGRES_9_4) @@ -30,7 +31,7 @@ public class SQLDialectCheckerTests { DSL.cube(DSL.inline(1)); } - // @Allow = { MYSQL (inherited from package), POSTGRES_9_5 } + // @Allow = { HSQLDB (inherited from the system property), MYSQL (inherited from package), POSTGRES_9_5 } // @Require = { POSTGRES_9_5 } @Allow(POSTGRES_9_5) @Require(POSTGRES_9_5) @@ -38,7 +39,7 @@ public class SQLDialectCheckerTests { DSL.cube(DSL.inline(1)); } - // @Allow = { MYSQL (inherited from package), POSTGRES } + // @Allow = { HSQLDB (inherited from the system property), MYSQL (inherited from package), POSTGRES } // @Require = { POSTGRES } @Allow(POSTGRES) @Require(POSTGRES) @@ -46,7 +47,7 @@ public class SQLDialectCheckerTests { DSL.cube(DSL.inline(1)); } - // @Allow = { MYSQL (inherited from package), POSTGRES } + // @Allow = { HSQLDB (inherited from the system property), MYSQL (inherited from package), POSTGRES } // @Require = { POSTGRES_9_4 } @Allow(POSTGRES) @Require(POSTGRES_9_4) @@ -54,7 +55,7 @@ public class SQLDialectCheckerTests { DSL.cube(DSL.inline(1)); } - // @Allow = { MYSQL (inherited from package), POSTGRES_9_4 } + // @Allow = { HSQLDB (inherited from the system property), MYSQL (inherited from package), POSTGRES_9_4 } // @Require = { POSTGRES } @Allow(POSTGRES_9_4) @Require(POSTGRES) @@ -62,14 +63,14 @@ public class SQLDialectCheckerTests { DSL.lateral(DSL.dual()); } - // @Allow = { H2, MYSQL (inherited from package) } + // @Allow = { H2, HSQLDB (inherited from the system property), MYSQL (inherited from package) } // @Require = { H2, MYSQL, POSTGRES_9_5 } (inherited from class) @Allow(H2) public static void doesntCompileBecauseMYSQLIsNotSupported() { DSL.array(2); } - // @Allow = { H2, MYSQL (inherited from package) } + // @Allow = { H2, HSQLDB (inherited from the system property), MYSQL (inherited from package) } // @Require = { H2 } @Allow(H2) @Require(H2) @@ -77,7 +78,7 @@ public class SQLDialectCheckerTests { DSL.array(2); } - // @Allow = { H2, MYSQL (inherited from package) } + // @Allow = { H2, HSQLDB (inherited from the system property), MYSQL (inherited from package) } // @Require = { H2 } @Allow(H2) @Require({ H2, ORACLE })