[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 <liangbowen@gf.com.cn>
Signed-off-by: liangbowen <liangbowen@gf.com.cn>
This commit is contained in:
liangbowen 2023-06-09 20:57:45 +08:00
parent 34e79b4195
commit 5f98539c82
13 changed files with 22 additions and 17 deletions

View File

@ -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++;

View File

@ -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

View File

@ -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;

View File

@ -531,7 +531,7 @@ public class KyuubiDatabaseMetaData implements SQLDatabaseMetaData {
@Override
public String getProcedureTerm() throws SQLException {
return new String("UDF");
return "UDF";
}
@Override

View File

@ -126,7 +126,7 @@ public class Utils {
break;
}
}
parts.add(sql.substring(off, sql.length()));
parts.add(sql.substring(off));
return parts;
}

View File

@ -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(

View File

@ -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

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -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);
});

View File

@ -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();
}