From e5d6aaa40858cdf71d003398d3e9535efefddf54 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Fri, 18 Nov 2022 10:26:10 +0800 Subject: [PATCH] (console): fix console code scan issue, fix link help. (#707) --- src/main/java/console/Console.java | 11 ++--- src/main/java/console/ConsoleInitializer.java | 2 +- .../java/console/NonInteractiveConsole.java | 28 +++---------- .../console/client/ConsoleClientImpl.java | 4 +- .../command/category/ContractOpCommand.java | 2 +- .../completer/StringsCompleterIgnoreCase.java | 1 + .../java/console/command/model/HelpInfo.java | 24 +---------- .../java/console/common/ConsoleUtils.java | 3 +- .../console/contract/ConsoleContractFace.java | 4 +- .../console/contract/ConsoleContractImpl.java | 42 +++++++++---------- .../contract/utils/ContractCompiler.java | 4 +- .../console/precompiled/PrecompiledImpl.java | 6 +-- 12 files changed, 45 insertions(+), 86 deletions(-) diff --git a/src/main/java/console/Console.java b/src/main/java/console/Console.java index 4f036df7..2cdaaecc 100644 --- a/src/main/java/console/Console.java +++ b/src/main/java/console/Console.java @@ -28,8 +28,6 @@ public class Console { private static final Logger logger = LoggerFactory.getLogger(Console.class); - public static int INPUT_FLAG = 0; - public static LineReader createLineReader(ConsoleInitializer consoleInitializer) throws IOException { if (consoleInitializer.isDisableAutoCompleter()) { @@ -42,13 +40,12 @@ public static LineReader createLineReader(ConsoleInitializer consoleInitializer) public static void main(String[] args) { LineReader lineReader = null; - Scanner sc = null; + Scanner sc; ConsoleInitializer consoleInitializer = null; try { consoleInitializer = new ConsoleInitializer(); consoleInitializer.init(args); lineReader = createLineReader(consoleInitializer); - sc = new Scanner(System.in); if (!consoleInitializer.isDisableAutoCompleter() && lineReader != null) { KeyMap keymap = lineReader.getKeyMaps().get(LineReader.MAIN); keymap.bind(new Reference("beginning-of-line"), "\033[1~"); @@ -69,12 +66,12 @@ public static void main(String[] args) { while (true) { try { - if (lineReader == null || !consoleInitializer.isDisableAutoCompleter()) { + if (lineReader == null) { System.out.println("Console can not read commands."); break; } String request; - if (INPUT_FLAG == 0 && !consoleInitializer.isDisableAutoCompleter()) { + if (!consoleInitializer.isDisableAutoCompleter()) { request = lineReader.readLine( "[" @@ -97,7 +94,7 @@ public static void main(String[] args) { if (params.length < 1) { continue; } - if ("".equals(params[0].trim())) { + if (params[0].trim().isEmpty()) { continue; } // execute the command diff --git a/src/main/java/console/ConsoleInitializer.java b/src/main/java/console/ConsoleInitializer.java index dc2059b3..9d3824fb 100644 --- a/src/main/java/console/ConsoleInitializer.java +++ b/src/main/java/console/ConsoleInitializer.java @@ -65,7 +65,7 @@ public void init(String[] args) throws ConfigException { this.authFace = new AuthImpl(client); } - private class AccountInfo { + private static class AccountInfo { private String accountFileFormat; private String accountFile; private String password; diff --git a/src/main/java/console/NonInteractiveConsole.java b/src/main/java/console/NonInteractiveConsole.java index f553e5d8..2dee6557 100644 --- a/src/main/java/console/NonInteractiveConsole.java +++ b/src/main/java/console/NonInteractiveConsole.java @@ -19,15 +19,6 @@ public class NonInteractiveConsole { private static final Logger logger = LoggerFactory.getLogger(NonInteractiveConsole.class); - public static boolean isNumeric(String str) { - try { - new Integer(str); - } catch (Exception e) { - return false; - } - return true; - } - public static void main(String[] args) { if (args.length == 0 || args[0].equals("-h") || args[0].equals("--help")) { @@ -39,18 +30,9 @@ public static void main(String[] args) { String[] params = null; try { consoleInitializer = new ConsoleInitializer(); - if (args.length > 0 && isNumeric(args[0])) { - String[] groupId = new String[1]; - params = new String[args.length - 1]; - System.arraycopy(args, 1, params, 0, params.length); - // set the groupId - System.arraycopy(args, 0, groupId, 0, 1); - consoleInitializer.init(groupId); - } else { - params = new String[args.length]; - System.arraycopy(args, 0, params, 0, params.length); - consoleInitializer.init(new String[] {}); - } + params = new String[args.length]; + System.arraycopy(args, 0, params, 0, params.length); + consoleInitializer.init(new String[] {}); } catch (Exception e) { System.out.println(e.getMessage()); logger.error(" message: {}, e: ", e.getMessage(), e); @@ -63,7 +45,7 @@ public static void main(String[] args) { String[] command = params[0].split(" "); CommandInfo commandInfo = null; // execute the command - if (command != null && command.length > 1) { + if (command.length > 1) { commandInfo = SupportedCommand.getCommandInfo( command[0], @@ -84,7 +66,7 @@ public static void main(String[] args) { commandInfo.callCommand(consoleInitializer, inputParamString, null); } else { String[] paramWithoutQuotation = new String[params.length]; - for (Integer i = 0; i < params.length; i++) { + for (int i = 0; i < params.length; i++) { String param = params[i]; paramWithoutQuotation[i] = param; // Remove the quotes around the input parameters diff --git a/src/main/java/console/client/ConsoleClientImpl.java b/src/main/java/console/client/ConsoleClientImpl.java index 695bef7d..c2a9e129 100644 --- a/src/main/java/console/client/ConsoleClientImpl.java +++ b/src/main/java/console/client/ConsoleClientImpl.java @@ -224,7 +224,7 @@ public void getTransactionByHashWithProof(String[] params) throws Exception { String transactionWithProof = client.getTransaction(nodeName, transactionHash, true).getResult().toString(); - if (Objects.isNull(transactionWithProof) || "".equals(transactionWithProof)) { + if (Objects.isNull(transactionWithProof) || transactionWithProof.isEmpty()) { System.out.println("This transaction hash doesn't exist."); return; } @@ -247,7 +247,7 @@ public void getTransactionReceiptByHashWithProof(String[] params) throws Excepti return; } - if (Objects.isNull(transactionReceiptWithProof) || "".equals(transactionReceiptWithProof)) { + if (Objects.isNull(transactionReceiptWithProof) || transactionReceiptWithProof.isEmpty()) { System.out.println("This transaction hash doesn't exist."); return; } diff --git a/src/main/java/console/command/category/ContractOpCommand.java b/src/main/java/console/command/category/ContractOpCommand.java index 9a629f4f..9480c216 100644 --- a/src/main/java/console/command/category/ContractOpCommand.java +++ b/src/main/java/console/command/category/ContractOpCommand.java @@ -113,7 +113,7 @@ public static void setIsWasm(boolean wasm) { (consoleInitializer, params, pwd) -> consoleInitializer .getConsoleContractFace() - .listDeployContractAddress(consoleInitializer, params, pwd), + .listDeployContractAddress(consoleInitializer, params), 1, 2, true); diff --git a/src/main/java/console/command/completer/StringsCompleterIgnoreCase.java b/src/main/java/console/command/completer/StringsCompleterIgnoreCase.java index 6158e803..bb2b4385 100644 --- a/src/main/java/console/command/completer/StringsCompleterIgnoreCase.java +++ b/src/main/java/console/command/completer/StringsCompleterIgnoreCase.java @@ -40,6 +40,7 @@ public StringsCompleterIgnoreCase(Iterable strings) { } } + @Override public void complete( LineReader reader, final ParsedLine commandLine, final List candidates) { if (commandLine == null || candidates == null) { diff --git a/src/main/java/console/command/model/HelpInfo.java b/src/main/java/console/command/model/HelpInfo.java index fa3b3f39..e56b8799 100644 --- a/src/main/java/console/command/model/HelpInfo.java +++ b/src/main/java/console/command/model/HelpInfo.java @@ -266,8 +266,8 @@ public static void linkHelp() { System.out.println( "* path[Required] -- The BFS path of link, link must locate under '/apps'."); System.out.println("* contractAddress[Required] -- The address of a contract."); - System.out.println("Example: (if in evm)\nln /apps/Name/Version 0x1234567890"); - System.out.println("Example: (if in wasm)\nln /apps/Name/Version /apps/test/HelloWorld"); + System.out.println("Example: (if in evm)\nln /apps/Name 0x1234567890"); + System.out.println("Example: (if in wasm)\nln /apps/Name /apps/test/HelloWorld"); } public static void addObserverHelp() { @@ -377,26 +377,6 @@ public static void showDescHelp() { System.out.println("* tableName -- The name of the table."); } - public static void promptNoFunc(String contractName, String funcName, int lenParams) { - if (lenParams <= 1) { - System.out.println( - "The method " - + funcName - + " with " - + lenParams - + " parameter" - + " is undefined of the contract."); - } else { - System.out.println( - "The method " - + funcName - + " with " - + lenParams - + " parameters" - + " is undefined of the contract."); - } - } - public static void loadAccountHelp() { System.out.println("Load account for the transaction signature"); System.out.println("Usage: \nloadAccount accountPath accountFormat"); diff --git a/src/main/java/console/common/ConsoleUtils.java b/src/main/java/console/common/ConsoleUtils.java index 6df2cbf7..4c9e18e9 100644 --- a/src/main/java/console/common/ConsoleUtils.java +++ b/src/main/java/console/common/ConsoleUtils.java @@ -63,7 +63,7 @@ public static void printJson(String jsonStr) { } public static String formatJson(String jsonStr) { - if (null == jsonStr || "".equals(jsonStr)) return ""; + if (null == jsonStr || jsonStr.isEmpty()) return ""; jsonStr = jsonStr.replace("\\n", ""); StringBuilder sb = new StringBuilder(); char last = '\0'; @@ -414,6 +414,7 @@ public CommandTokenizer(Reader r) { quoteChar('"'); } + @Override public void parseNumbers() {} } diff --git a/src/main/java/console/contract/ConsoleContractFace.java b/src/main/java/console/contract/ConsoleContractFace.java index 36615cfb..ab4ca5f8 100644 --- a/src/main/java/console/contract/ConsoleContractFace.java +++ b/src/main/java/console/contract/ConsoleContractFace.java @@ -12,6 +12,6 @@ public interface ConsoleContractFace { void listAbi(ConsoleInitializer consoleInitializer, String[] params, String pwd) throws Exception; - void listDeployContractAddress( - ConsoleInitializer consoleInitializer, String[] params, String pwd) throws Exception; + void listDeployContractAddress(ConsoleInitializer consoleInitializer, String[] params) + throws Exception; } diff --git a/src/main/java/console/contract/ConsoleContractImpl.java b/src/main/java/console/contract/ConsoleContractImpl.java index 661c4d9e..aa91376d 100644 --- a/src/main/java/console/contract/ConsoleContractImpl.java +++ b/src/main/java/console/contract/ConsoleContractImpl.java @@ -401,16 +401,24 @@ public TransactionResponse deployWasm( private synchronized void writeLog(String contractName, String contractAddress) { contractName = ConsoleUtils.removeSolSuffix(contractName); - BufferedReader reader = null; + + File logFile = new File(Common.ContractLogFileName); try { - File logFile = new File(Common.ContractLogFileName); if (!logFile.exists() && !logFile.createNewFile()) { System.out.println("Failed to create log file: " + Common.ContractLogFileName); return; } - reader = new BufferedReader(new FileReader(Common.ContractLogFileName)); + } catch (IOException e) { + System.out.println("Failed to create log file: " + Common.ContractLogFileName); + logger.error("create file exception", e); + return; + } + try (BufferedReader reader = + new BufferedReader(new FileReader(Common.ContractLogFileName)); + PrintWriter pw = + new PrintWriter(new FileWriter(Common.ContractLogFileName, true)); ) { String line; - List textList = new ArrayList(); + List textList = new ArrayList<>(); while ((line = reader.readLine()) != null) { textList.add(line); } @@ -431,22 +439,15 @@ private synchronized void writeLog(String contractName, String contractAddress) return; } } - PrintWriter pw = new PrintWriter(new FileWriter(Common.ContractLogFileName, true)); for (; i < textList.size(); i++) { pw.println(textList.get(i)); } pw.flush(); - pw.close(); } } catch (IOException e) { System.out.println("Read deploylog.txt failed."); + logger.error("Read deploylog.txt failed.", e); return; - } finally { - try { - reader.close(); - } catch (IOException e) { - System.out.println("Close deploylog.txt failed."); - } } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @@ -461,22 +462,19 @@ private synchronized void writeLog(String contractName, String contractAddress) + contractName + " " + contractAddress; - try { - File logFile = new File(Common.ContractLogFileName); + try (PrintWriter pw = new PrintWriter(new FileWriter(Common.ContractLogFileName, true))) { if (!logFile.exists() && !logFile.createNewFile()) { System.out.println("Failed to create file " + Common.ContractLogFileName); } - PrintWriter pw = new PrintWriter(new FileWriter(Common.ContractLogFileName, true)); pw.println(log); pw.flush(); - pw.close(); } catch (IOException e) { System.out.println(e.getMessage()); logger.error(" message: {}, e: {}", e.getMessage(), e); - return; } } + @Override public void getDeployLog(String[] params) throws Exception { String queryRecordNumber = ""; int recordNumber = Common.QueryLogCount; @@ -528,10 +526,10 @@ public void getDeployLog(String[] params) throws Exception { stringBuilder.append(textList.get(i)); stringBuilder.append(ls); } - if ("".equals(stringBuilder.toString())) { + if (stringBuilder.toString().isEmpty()) { System.out.println("Empty set."); } else { - System.out.println(stringBuilder.toString()); + System.out.println(stringBuilder); } } catch (Exception e) { logger.error(" load {} failed, e: {}", Common.ContractLogFileName, e); @@ -786,7 +784,7 @@ private void sendTransaction( System.out.println("Return message: " + response.getReturnMessage()); ConsoleUtils.printReturnResults(response.getResults()); ConsoleUtils.singleLine(); - if (response.getEvents() != null && !response.getEvents().equals("")) { + if (response.getEvents() != null && !response.getEvents().isEmpty()) { System.out.println("Event logs"); System.out.println("Event: " + response.getEvents()); } @@ -898,8 +896,8 @@ public void listAbi(ConsoleInitializer consoleInitializer, String[] params, Stri } @Override - public void listDeployContractAddress( - ConsoleInitializer consoleInitializer, String[] params, String pwd) throws Exception { + public void listDeployContractAddress(ConsoleInitializer consoleInitializer, String[] params) + throws Exception { boolean isWasm = consoleInitializer.getClient().isWASM(); String contractNameOrPath = ConsoleUtils.resolvePath(params[1]); String contractName = diff --git a/src/main/java/console/contract/utils/ContractCompiler.java b/src/main/java/console/contract/utils/ContractCompiler.java index 6b592dbc..adac77ce 100644 --- a/src/main/java/console/contract/utils/ContractCompiler.java +++ b/src/main/java/console/contract/utils/ContractCompiler.java @@ -197,7 +197,7 @@ public static AbiAndBin compileSolToBinAndAbi( boolean isContractParallelAnalysis) throws IOException, CompileContractException { SolidityCompiler.Option libraryOption = null; - if (librariesOption != null && !librariesOption.equals("")) { + if (librariesOption != null && !librariesOption.isEmpty()) { libraryOption = new SolidityCompiler.CustomOption("libraries", librariesOption); } @@ -234,7 +234,7 @@ public static AbiAndBin compileSolToBinAndAbi( !res.isFailed(), res.getOutput(), res.getErrors()); - if (res.isFailed() || "".equals(res.getOutput())) { + if (res.isFailed() || res.getOutput().isEmpty()) { throw new CompileContractException(" Compile error: " + res.getErrors()); } diff --git a/src/main/java/console/precompiled/PrecompiledImpl.java b/src/main/java/console/precompiled/PrecompiledImpl.java index b9c9824d..53e4106c 100644 --- a/src/main/java/console/precompiled/PrecompiledImpl.java +++ b/src/main/java/console/precompiled/PrecompiledImpl.java @@ -295,7 +295,7 @@ public void remove(String sql) throws Exception { private boolean checkTableExistence(Map> descTable) { return descTable.size() != 0 - && !descTable.get(PrecompiledConstant.KEY_FIELD_NAME).get(0).equals(""); + && !descTable.get(PrecompiledConstant.KEY_FIELD_NAME).get(0).isEmpty(); } @Override @@ -612,8 +612,8 @@ public String getPwd() { private Tuple2 travelBfs( String absolutePath, String prefix, int deep, int limit) throws ContractException { if (deep >= limit) return new Tuple2<>(0, 0); - Integer dirCount = 0; - Integer contractCount = 0; + int dirCount = 0; + int contractCount = 0; List children = bfsService.list(absolutePath); for (int i = 0; i < children.size(); i++) { String thisPrefix = "";