THRIFT-5584: use gradle toolchain to specify Java 11 with --release 8 (#2606)

* use gradle toolchain to specify Java 11 with --release 8
* use newer syntax
* fix spotless apply
* remove legacy apply and bump version of spot bugs
* migrate pmd to new plugin syntax
diff --git a/lib/java/build.gradle b/lib/java/build.gradle
index 8206411..c8fc27c 100644
--- a/lib/java/build.gradle
+++ b/lib/java/build.gradle
@@ -39,6 +39,7 @@
     id 'java-library'
     id 'maven-publish'
     id 'signing'
+    id 'pmd'
     id 'com.github.johnrengelman.shadow' version '6.1.0'
     id "com.github.spotbugs" version "4.7.1"
     id "com.diffplug.spotless" version "6.5.2"
diff --git a/lib/java/gradle.properties b/lib/java/gradle.properties
index dfa29fe..b375fad 100644
--- a/lib/java/gradle.properties
+++ b/lib/java/gradle.properties
@@ -27,10 +27,10 @@
 # Dependency versions
 httpclient.version=4.5.13
 httpcore.version=4.4.15
-slf4j.version=1.7.35
+slf4j.version=1.7.36
 servlet.version=4.0.1
 tomcat.embed.version=9.0.43
 junit.version=5.8.2
 mockito.version=1.10.19
 javax.annotation.version=1.3.2
-commons-lang3.version=3.12
+commons-lang3.version=3.12.0
diff --git a/lib/java/gradle/codeQualityChecks.gradle b/lib/java/gradle/codeQualityChecks.gradle
index b8d13f9..0ce47f7 100644
--- a/lib/java/gradle/codeQualityChecks.gradle
+++ b/lib/java/gradle/codeQualityChecks.gradle
@@ -5,11 +5,9 @@
 
 dependencies {
     spotbugs configurations.spotbugsPlugins.dependencies
-    spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.11.0'
+    spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.12.0'
 }
 
-apply plugin: 'com.github.spotbugs'
-
 // see https://spotbugs-gradle-plugin.netlify.app/com/github/spotbugs/snom/spotbugsextension
 spotbugs {
     ignoreFailures = true
@@ -28,8 +26,6 @@
     }
 }
 
-apply plugin: 'pmd'
-
 pmd {
     ignoreFailures = true
     toolVersion = '6.0.0'
@@ -46,9 +42,7 @@
 
 spotless {
     java {
-        target project.fileTree(project.rootDir) {
-          include 'src/**/*.java'
-        }
+        target 'src/**/*.java'
         googleJavaFormat()
     }
 }
diff --git a/lib/java/gradle/environment.gradle b/lib/java/gradle/environment.gradle
index 224f10f..f25f3e6 100644
--- a/lib/java/gradle/environment.gradle
+++ b/lib/java/gradle/environment.gradle
@@ -29,13 +29,8 @@
 ext.installPath = property('install.path')
 ext.installJavadocPath = property('install.javadoc.path')
 
-ext.thriftRoot = file('../..')
-
-if (hasProperty('thrift.compiler')) {
-    ext.thriftCompiler = property('thrift.compiler')
-} else {
-    ext.thriftCompiler = "$thriftRoot/compiler/cpp/thrift"
-}
+ext.thriftRoot = rootProject.file('../..')
+ext.thriftCompiler = findProperty('thrift.compiler') ?: "$thriftRoot/compiler/cpp/thrift"
 
 ext.mvnRepo = property('mvn.repo')
 ext.apacheRepo = property('apache.repo')
@@ -50,6 +45,7 @@
 ext.junitVersion = property('junit.version')
 ext.mockitoVersion = property('mockito.version')
 ext.javaxAnnotationVersion = property('javax.annotation.version')
+ext.commonsLang3Version = property('commons-lang3.version')
 
 // In this section you declare where to find the dependencies of your project
 repositories {
@@ -69,7 +65,7 @@
     implementation "org.apache.httpcomponents:httpcore:${httpcoreVersion}"
     implementation "javax.servlet:javax.servlet-api:${servletVersion}"
     implementation "javax.annotation:javax.annotation-api:${javaxAnnotationVersion}"
-    implementation "org.apache.commons:commons-lang3:3.12.0"
+    implementation "org.apache.commons:commons-lang3:${commonsLang3Version}"
 
     testImplementation "org.junit.jupiter:junit-jupiter:${junitVersion}"
     testImplementation "org.mockito:mockito-all:${mockitoVersion}"
diff --git a/lib/java/gradle/sourceConfiguration.gradle b/lib/java/gradle/sourceConfiguration.gradle
index 97ce1b9..044a114 100644
--- a/lib/java/gradle/sourceConfiguration.gradle
+++ b/lib/java/gradle/sourceConfiguration.gradle
@@ -21,21 +21,27 @@
 // ----------------------------------------------------------------------------
 // Compiler configuration details
 
-// These two properties are still needed on JDK8, and possibly used directly by
-// plugins. However, the '--release' option added below makes these two
-// properties redundant when building with JDK9 or later.
-sourceCompatibility = '1.8'
-targetCompatibility = '1.8'
+// We are using Java 11 toolchain to compile.
+// This enables decoupling from the Java version that gradle runs, from
+// the actual JDK version for the project. For more details, see
+// https://docs.gradle.org/current/userguide/toolchains.html
+//
+// The '--release' option added below makes sure that even if we are using
+// the toolchain version > 8, the final artifact is at version 8. There is
+// also a runtime CI that's based on Java 8 to ensure that.
+java {
+    toolchain {
+        languageVersion = JavaLanguageVersion.of(11)
+    }
+}
 
-tasks.withType(JavaCompile) {
+tasks.withType(JavaCompile).configureEach {
     options.encoding = 'UTF-8'
     options.debug = true
     options.deprecation = true
     // the following is to build with Java 8 specifications, even when building with JDK9 or later
-    if (JavaVersion.current() > JavaVersion.VERSION_1_8) {
-        options.compilerArgs.addAll(['--release', '8'])
-    }
-    options.compilerArgs.addAll([
+    options.release = 8
+    options.compilerArgs += [
             '-Werror',
             '-Xlint:deprecation',
             '-Xlint:cast',
@@ -44,7 +50,7 @@
             '-Xlint:finally',
             '-Xlint:overrides',
             // we can't enable -Xlint:unchecked just yet
-    ])
+    ]
 }
 
 // ----------------------------------------------------------------------------
diff --git a/lib/java/src/crossTest/java/org/apache/thrift/test/TestClient.java b/lib/java/src/crossTest/java/org/apache/thrift/test/TestClient.java
index 32ac88f..3c54988 100644
--- a/lib/java/src/crossTest/java/org/apache/thrift/test/TestClient.java
+++ b/lib/java/src/crossTest/java/org/apache/thrift/test/TestClient.java
@@ -19,6 +19,7 @@
 
 package org.apache.thrift.test;
 
+import java.io.ByteArrayOutputStream;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -26,6 +27,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.IntStream;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.thrift.TApplicationException;
 import org.apache.thrift.TException;
@@ -299,24 +301,7 @@
         /** BINARY TEST */
         try {
           System.out.print("testBinary(-128...127) = ");
-          byte[] data =
-              new byte[] {
-                -128, -127, -126, -125, -124, -123, -122, -121, -120, -119, -118, -117, -116, -115,
-                -114, -113, -112, -111, -110, -109, -108, -107, -106, -105, -104, -103, -102, -101,
-                -100, -99, -98, -97, -96, -95, -94, -93, -92, -91, -90, -89, -88, -87, -86, -85,
-                -84, -83, -82, -81, -80, -79, -78, -77, -76, -75, -74, -73, -72, -71, -70, -69, -68,
-                -67, -66, -65, -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53, -52, -51,
-                -50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34,
-                -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17,
-                -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3,
-                4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
-                26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
-                47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
-                68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
-                89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
-                108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
-                125, 126, 127
-              };
+          byte[] data = getBytesData();
           ByteBuffer bin = testClient.testBinary(ByteBuffer.wrap(data));
           bin.mark();
           byte[] bytes = new byte[bin.limit() - bin.position()];
@@ -823,4 +808,10 @@
 
     System.exit(returnCode);
   }
+
+  private static byte[] getBytesData() {
+    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(256);
+    IntStream.range(-128, 128).forEach(byteArrayOutputStream::write);
+    return byteArrayOutputStream.toByteArray();
+  }
 }