Skip to content

Commit

Permalink
<fix>(console): fix console code scan issue, fix link help. (#707)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyonRay authored Nov 18, 2022
1 parent a5db61d commit e5d6aaa
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 86 deletions.
11 changes: 4 additions & 7 deletions src/main/java/console/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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<Binding> keymap = lineReader.getKeyMaps().get(LineReader.MAIN);
keymap.bind(new Reference("beginning-of-line"), "\033[1~");
Expand All @@ -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(
"["
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/console/ConsoleInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 5 additions & 23 deletions src/main/java/console/NonInteractiveConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand All @@ -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);
Expand All @@ -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],
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/console/client/ConsoleClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public StringsCompleterIgnoreCase(Iterable<String> strings) {
}
}

@Override
public void complete(
LineReader reader, final ParsedLine commandLine, final List<Candidate> candidates) {
if (commandLine == null || candidates == null) {
Expand Down
24 changes: 2 additions & 22 deletions src/main/java/console/command/model/HelpInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/console/common/ConsoleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -414,6 +414,7 @@ public CommandTokenizer(Reader r) {
quoteChar('"');
}

@Override
public void parseNumbers() {}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/console/contract/ConsoleContractFace.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
42 changes: 20 additions & 22 deletions src/main/java/console/contract/ConsoleContractImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> textList = new ArrayList<String>();
List<String> textList = new ArrayList<>();
while ((line = reader.readLine()) != null) {
textList.add(line);
}
Expand All @@ -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");

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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 =
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/console/contract/utils/ContractCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/console/precompiled/PrecompiledImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public void remove(String sql) throws Exception {

private boolean checkTableExistence(Map<String, List<String>> descTable) {
return descTable.size() != 0
&& !descTable.get(PrecompiledConstant.KEY_FIELD_NAME).get(0).equals("");
&& !descTable.get(PrecompiledConstant.KEY_FIELD_NAME).get(0).isEmpty();
}

@Override
Expand Down Expand Up @@ -612,8 +612,8 @@ public String getPwd() {
private Tuple2<Integer, Integer> 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<BfsInfo> children = bfsService.list(absolutePath);
for (int i = 0; i < children.size(); i++) {
String thisPrefix = "";
Expand Down

0 comments on commit e5d6aaa

Please sign in to comment.