use of com.orientechnologies.common.console.annotation.ConsoleParameter in project orientdb by orientechnologies.
the class OConsoleApplication method execute.
protected RESULT execute(String iCommand) {
iCommand = iCommand.trim();
if (iCommand.length() == 0)
// NULL LINE: JUMP IT
return RESULT.OK;
if (isComment(iCommand))
// COMMENT: JUMP IT
return RESULT.OK;
String[] commandWords = OStringParser.getWords(iCommand, wordSeparator);
for (String cmd : helpCommands) if (cmd.equals(commandWords[0])) {
if (iCommand.length() > cmd.length())
help(iCommand.substring(cmd.length() + 1));
else
help(null);
return RESULT.OK;
}
for (String cmd : exitCommands) if (cmd.equalsIgnoreCase(commandWords[0])) {
return RESULT.EXIT;
}
Method lastMethodInvoked = null;
final StringBuilder lastCommandInvoked = new StringBuilder(1024);
String commandLowerCase = "";
for (int i = 0; i < commandWords.length; i++) {
if (i > 0) {
commandLowerCase += " ";
}
commandLowerCase += commandWords[i].toLowerCase();
}
for (Entry<Method, Object> entry : getConsoleMethods().entrySet()) {
final Method m = entry.getKey();
final String methodName = m.getName();
final ConsoleCommand ann = m.getAnnotation(ConsoleCommand.class);
final StringBuilder commandName = new StringBuilder();
char ch;
int commandWordCount = 1;
for (int i = 0; i < methodName.length(); ++i) {
ch = methodName.charAt(i);
if (Character.isUpperCase(ch)) {
commandName.append(" ");
ch = Character.toLowerCase(ch);
commandWordCount++;
}
commandName.append(ch);
}
if (!commandLowerCase.equals(commandName.toString()) && !commandLowerCase.startsWith(commandName.toString() + " ")) {
if (ann == null)
continue;
String[] aliases = ann.aliases();
if (aliases == null || aliases.length == 0)
continue;
boolean aliasMatch = false;
for (String alias : aliases) {
if (iCommand.startsWith(alias.split(" ")[0])) {
aliasMatch = true;
commandWordCount = 1;
break;
}
}
if (!aliasMatch)
continue;
}
Object[] methodArgs;
// BUILD PARAMETERS
if (ann != null && !ann.splitInWords()) {
methodArgs = new String[] { iCommand.substring(iCommand.indexOf(' ') + 1) };
} else {
final int actualParamCount = commandWords.length - commandWordCount;
if (m.getParameterTypes().length > actualParamCount) {
// METHOD PARAMS AND USED PARAMS MISMATCH: CHECK FOR OPTIONALS
for (int paramNum = m.getParameterAnnotations().length - 1; paramNum > actualParamCount - 1; paramNum--) {
final Annotation[] paramAnn = m.getParameterAnnotations()[paramNum];
if (paramAnn != null)
for (int annNum = paramAnn.length - 1; annNum > -1; annNum--) {
if (paramAnn[annNum] instanceof ConsoleParameter) {
final ConsoleParameter annotation = (ConsoleParameter) paramAnn[annNum];
if (annotation.optional())
commandWords = OArrays.copyOf(commandWords, commandWords.length + 1);
break;
}
}
}
}
methodArgs = OArrays.copyOfRange(commandWords, commandWordCount, commandWords.length);
}
try {
m.invoke(entry.getValue(), methodArgs);
} catch (IllegalArgumentException e) {
lastMethodInvoked = m;
// GET THE COMMAND NAME
lastCommandInvoked.setLength(0);
for (int i = 0; i < commandWordCount; ++i) {
if (lastCommandInvoked.length() > 0)
lastCommandInvoked.append(" ");
lastCommandInvoked.append(commandWords[i]);
}
continue;
} catch (Exception e) {
if (e.getCause() != null)
onException(e.getCause());
else
e.printStackTrace(err);
return RESULT.ERROR;
}
return RESULT.OK;
}
if (lastMethodInvoked != null)
syntaxError(lastCommandInvoked.toString(), lastMethodInvoked);
error("\n!Unrecognized command: '%s'", iCommand);
return RESULT.ERROR;
}
Aggregations