This is an automated email from the ASF dual-hosted git repository.

bowenliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new 5f98539c8 [KYUUBI #4944] [MINOR] Code improvement for Java
5f98539c8 is described below

commit 5f98539c8263b2e413055a2e514b2faaab88fb02
Author: liangbowen <[email protected]>
AuthorDate: Fri Jun 9 20:57:45 2023 +0800

    [KYUUBI #4944] [MINOR] Code improvement for Java
    
    ### _Why are the changes needed?_
    
    - To satisfied the code scanning suggestion of Java language by CodeQL, 
with no feature changes
      - Ignored error status of call
        - https://github.com/apache/kyuubi/security/code-scanning/88
      - Inefficient empty string test
          - https://github.com/apache/kyuubi/security/code-scanning/87
      - Inefficient String constructor
          - https://github.com/apache/kyuubi/security/code-scanning/84
      - Missing Override annotation
          - https://github.com/apache/kyuubi/security/code-scanning/78
          - https://github.com/apache/kyuubi/security/code-scanning/79
          - https://github.com/apache/kyuubi/security/code-scanning/80
          - https://github.com/apache/kyuubi/security/code-scanning/81
          - https://github.com/apache/kyuubi/security/code-scanning/82
          - https://github.com/apache/kyuubi/security/code-scanning/83
      - Useless toString on String
          - https://github.com/apache/kyuubi/security/code-scanning/108
      - Use of default toString()
          - https://github.com/apache/kyuubi/security/code-scanning/107
      - Unread local variable
          - https://github.com/apache/kyuubi/security/code-scanning/96
      - Random used only once
          - https://github.com/apache/kyuubi/security/code-scanning/192
          - https://github.com/apache/kyuubi/security/code-scanning/191
      - Missing enum case in switch
          - https://github.com/apache/kyuubi/security/code-scanning/193
    - redundant usages of length when calling substring
    
    ### _How was this patch tested?_
    - [ ] Add some test cases that check the changes thoroughly including 
negative and positive cases if possible
    
    - [ ] Add screenshots for manual tests if appropriate
    
    - [x] [Run 
test](https://kyuubi.readthedocs.io/en/master/develop_tools/testing.html#running-tests)
 locally before make a pull request
    
    Closes #4944 from bowenliang123/improve-jdbc.
    
    Closes #4944
    
    b1b4dfa03 [liangbowen] substring
    0caefc646 [liangbowen] substring
    9dab41b57 [liangbowen] substring
    a340df36e [liangbowen] style
    94be380e8 [liangbowen] code improvement for java
    
    Authored-by: liangbowen <[email protected]>
    Signed-off-by: liangbowen <[email protected]>
---
 .../src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java         | 4 +---
 .../src/main/java/org/apache/hive/beeline/KyuubiCommands.java        | 4 ++--
 .../src/main/java/org/apache/kyuubi/jdbc/KyuubiHiveDriver.java       | 3 ++-
 .../java/org/apache/kyuubi/jdbc/hive/KyuubiDatabaseMetaData.java     | 2 +-
 .../src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java             | 2 +-
 .../java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java  | 4 ++--
 .../src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnBuffer.java  | 3 ++-
 .../src/main/java/org/apache/kyuubi/jdbc/hive/common/Date.java       | 2 ++
 .../src/main/java/org/apache/kyuubi/jdbc/hive/common/Timestamp.java  | 2 ++
 .../java/org/apache/kyuubi/jdbc/hive/common/TimestampTZUtil.java     | 2 +-
 .../src/test/java/org/apache/kyuubi/jdbc/hive/TestJdbcDriver.java    | 5 +++--
 .../src/main/java/org/apache/kyuubi/client/RestClient.java           | 2 +-
 .../src/main/java/org/apache/kyuubi/client/RetryableRestClient.java  | 4 ++--
 13 files changed, 22 insertions(+), 17 deletions(-)

diff --git 
a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java 
b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java
index b3a2fa307..073870a47 100644
--- 
a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java
+++ 
b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java
@@ -22,7 +22,6 @@ import java.io.InputStream;
 import java.sql.Driver;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.Iterator;
 import java.util.List;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Options;
@@ -174,8 +173,7 @@ public class KyuubiBeeLine extends BeeLine {
       return 1;
     }
     if (!commands.isEmpty()) {
-      for (Iterator<String> i = commands.iterator(); i.hasNext(); ) {
-        String command = i.next().toString();
+      for (String command : commands) {
         debug(loc("executing-command", command));
         if (!dispatch(command)) {
           code++;
diff --git 
a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java 
b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java
index 9557f2567..fdd14d8cb 100644
--- 
a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java
+++ 
b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java
@@ -357,7 +357,7 @@ public class KyuubiCommands extends Commands {
    */
   private void addCmdPart(List<String> cmdList, StringBuilder command, String 
cmdpart) {
     if (cmdpart.endsWith("\\")) {
-      command.append(cmdpart.substring(0, cmdpart.length() - 1)).append(";");
+      command.append(cmdpart, 0, cmdpart.length() - 1).append(";");
       return;
     } else {
       command.append(cmdpart);
@@ -422,6 +422,7 @@ public class KyuubiCommands extends Commands {
     return null;
   }
 
+  @Override
   public boolean connect(Properties props) throws IOException {
     String url =
         getProperty(
@@ -507,7 +508,6 @@ public class KyuubiCommands extends Commands {
 
   @Override
   public String handleMultiLineCmd(String line) throws IOException {
-    int[] startQuote = {-1};
     Character mask =
         (System.getProperty("jline.terminal", 
"").equals("jline.UnsupportedTerminal"))
             ? null
diff --git 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/KyuubiHiveDriver.java 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/KyuubiHiveDriver.java
index 3b874ba2e..66b797087 100644
--- 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/KyuubiHiveDriver.java
+++ 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/KyuubiHiveDriver.java
@@ -24,6 +24,7 @@ import java.util.Properties;
 import java.util.jar.Attributes;
 import java.util.jar.Manifest;
 import java.util.logging.Logger;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.kyuubi.jdbc.hive.JdbcConnectionParams;
 import org.apache.kyuubi.jdbc.hive.KyuubiConnection;
 import org.apache.kyuubi.jdbc.hive.KyuubiSQLException;
@@ -137,7 +138,7 @@ public class KyuubiHiveDriver implements Driver {
       host = "";
     }
     String port = Integer.toString(params.getPort());
-    if (host.equals("")) {
+    if (StringUtils.isEmpty(host)) {
       port = "";
     } else if (port.equals("0") || port.equals("-1")) {
       port = DEFAULT_PORT;
diff --git 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiDatabaseMetaData.java
 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiDatabaseMetaData.java
index f5e29f8e7..c6ab3a277 100644
--- 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiDatabaseMetaData.java
+++ 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiDatabaseMetaData.java
@@ -531,7 +531,7 @@ public class KyuubiDatabaseMetaData implements 
SQLDatabaseMetaData {
 
   @Override
   public String getProcedureTerm() throws SQLException {
-    return new String("UDF");
+    return "UDF";
   }
 
   @Override
diff --git 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java
index ac9b29664..ef723ea30 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java
@@ -126,7 +126,7 @@ public class Utils {
           break;
       }
     }
-    parts.add(sql.substring(off, sql.length()));
+    parts.add(sql.substring(off));
     return parts;
   }
 
diff --git 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java
 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java
index bfa5e632e..948fd3334 100644
--- 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java
+++ 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java
@@ -22,7 +22,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.apache.kyuubi.shaded.curator.framework.CuratorFramework;
@@ -111,7 +111,7 @@ class ZooKeeperHiveClientHelper {
     try (CuratorFramework zooKeeperClient = getZkClient(connParams)) {
       List<String> serverHosts = getServerHosts(connParams, zooKeeperClient);
       // Now pick a server node randomly
-      String serverNode = serverHosts.get(new 
Random().nextInt(serverHosts.size()));
+      String serverNode = 
serverHosts.get(ThreadLocalRandom.current().nextInt(serverHosts.size()));
       updateParamsWithZKServerNode(connParams, zooKeeperClient, serverNode);
     } catch (Exception e) {
       throw new ZooKeeperHiveClientException(
diff --git 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnBuffer.java
 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnBuffer.java
index e703cb1f0..bd5124f95 100644
--- 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnBuffer.java
+++ 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnBuffer.java
@@ -228,8 +228,9 @@ public class ColumnBuffer extends AbstractList<Object> {
         return stringVars.get(index);
       case BINARY_TYPE:
         return binaryVars.get(index).array();
+      default:
+        return null;
     }
-    return null;
   }
 
   @Override
diff --git 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/Date.java 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/Date.java
index 1b49c268a..720c7517f 100644
--- 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/Date.java
+++ 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/Date.java
@@ -65,6 +65,7 @@ public class Date implements Comparable<Date> {
     return localDate.format(PRINT_FORMATTER);
   }
 
+  @Override
   public int hashCode() {
     return localDate.hashCode();
   }
@@ -164,6 +165,7 @@ public class Date implements Comparable<Date> {
   }
 
   /** Return a copy of this object. */
+  @Override
   public Object clone() {
     // LocalDateTime is immutable.
     return new Date(this.localDate);
diff --git 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/Timestamp.java
 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/Timestamp.java
index cdb6b10ce..7e02835b7 100644
--- 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/Timestamp.java
+++ 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/Timestamp.java
@@ -95,6 +95,7 @@ public class Timestamp implements Comparable<Timestamp> {
     return localDateTime.format(PRINT_FORMATTER);
   }
 
+  @Override
   public int hashCode() {
     return localDateTime.hashCode();
   }
@@ -207,6 +208,7 @@ public class Timestamp implements Comparable<Timestamp> {
   }
 
   /** Return a copy of this object. */
+  @Override
   public Object clone() {
     // LocalDateTime is immutable.
     return new Timestamp(this.localDateTime);
diff --git 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/TimestampTZUtil.java
 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/TimestampTZUtil.java
index a938e1688..be16926cb 100644
--- 
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/TimestampTZUtil.java
+++ 
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/TimestampTZUtil.java
@@ -98,7 +98,7 @@ public class TimestampTZUtil {
     Matcher matcher = SINGLE_DIGIT_PATTERN.matcher(s);
     if (matcher.find()) {
       int index = matcher.start() + 1;
-      s = s.substring(0, index) + "0" + s.substring(index, s.length());
+      s = s.substring(0, index) + "0" + s.substring(index);
     }
     return s;
   }
diff --git 
a/kyuubi-hive-jdbc/src/test/java/org/apache/kyuubi/jdbc/hive/TestJdbcDriver.java
 
b/kyuubi-hive-jdbc/src/test/java/org/apache/kyuubi/jdbc/hive/TestJdbcDriver.java
index 228ad00ee..efdf73092 100644
--- 
a/kyuubi-hive-jdbc/src/test/java/org/apache/kyuubi/jdbc/hive/TestJdbcDriver.java
+++ 
b/kyuubi-hive-jdbc/src/test/java/org/apache/kyuubi/jdbc/hive/TestJdbcDriver.java
@@ -24,6 +24,7 @@ import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.nio.file.Files;
 import java.util.Arrays;
 import java.util.Collection;
 import org.junit.AfterClass;
@@ -67,14 +68,14 @@ public class TestJdbcDriver {
   public static void setUpBeforeClass() throws Exception {
     file = new File(System.getProperty("user.dir") + File.separator + 
"Init.sql");
     if (!file.exists()) {
-      file.createNewFile();
+      Files.createFile(file.toPath());
     }
   }
 
   @AfterClass
   public static void cleanUpAfterClass() throws Exception {
     if (file != null) {
-      file.delete();
+      Files.deleteIfExists(file.toPath());
     }
   }
 
diff --git 
a/kyuubi-rest-client/src/main/java/org/apache/kyuubi/client/RestClient.java 
b/kyuubi-rest-client/src/main/java/org/apache/kyuubi/client/RestClient.java
index 6447d5477..e6d1d9674 100644
--- a/kyuubi-rest-client/src/main/java/org/apache/kyuubi/client/RestClient.java
+++ b/kyuubi-rest-client/src/main/java/org/apache/kyuubi/client/RestClient.java
@@ -114,7 +114,7 @@ public class RestClient implements IRestClient {
               contentBody = new FileBody((File) payload);
               break;
             default:
-              throw new RuntimeException("Unsupported multi part type:" + 
multiPart);
+              throw new RuntimeException("Unsupported multi part type:" + 
multiPart.getType());
           }
           entityBuilder.addPart(s, contentBody);
         });
diff --git 
a/kyuubi-rest-client/src/main/java/org/apache/kyuubi/client/RetryableRestClient.java
 
b/kyuubi-rest-client/src/main/java/org/apache/kyuubi/client/RetryableRestClient.java
index dcd052aca..d13151c2e 100644
--- 
a/kyuubi-rest-client/src/main/java/org/apache/kyuubi/client/RetryableRestClient.java
+++ 
b/kyuubi-rest-client/src/main/java/org/apache/kyuubi/client/RetryableRestClient.java
@@ -22,7 +22,7 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.util.List;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.kyuubi.client.exception.RetryableKyuubiRestException;
 import org.slf4j.Logger;
@@ -44,7 +44,7 @@ public class RetryableRestClient implements InvocationHandler 
{
   private RetryableRestClient(List<String> uris, RestClientConf conf) {
     this.conf = conf;
     this.uris = uris;
-    this.currentUriIndex = new 
Random(System.currentTimeMillis()).nextInt(uris.size());
+    this.currentUriIndex = ThreadLocalRandom.current().nextInt(uris.size());
     newRestClient();
   }
 

Reply via email to