|
| 1 | +package jwf.debugport.internal; |
| 2 | + |
| 3 | +import android.text.TextUtils; |
| 4 | + |
| 5 | +import java.lang.annotation.Annotation; |
| 6 | +import java.lang.reflect.Method; |
| 7 | +import java.util.StringTokenizer; |
| 8 | + |
| 9 | +import jwf.debugport.annotations.Command; |
| 10 | + |
| 11 | +/** |
| 12 | + * Created by jason on 5/15/16. |
| 13 | + */ |
| 14 | +public class CommandHelpInfo implements Comparable<CommandHelpInfo> { |
| 15 | + private final Class mCommandClass; |
| 16 | + private Command.Help mHelpAnnotation; |
| 17 | + private Command mCommandAnnotation; |
| 18 | + |
| 19 | + public CommandHelpInfo(Class commandClass) { |
| 20 | + mCommandClass = commandClass; |
| 21 | + if (commandClass.isAnnotationPresent(Command.class)) { |
| 22 | + mCommandAnnotation = (Command) commandClass.getAnnotation(Command.class); |
| 23 | + } |
| 24 | + if (commandClass.isAnnotationPresent(Command.Help.class)) { |
| 25 | + mHelpAnnotation = (Command.Help) commandClass.getAnnotation(Command.Help.class); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public String getCommandGroup() { |
| 30 | + if (mHelpAnnotation != null && !TextUtils.isEmpty(mHelpAnnotation.group())) { |
| 31 | + return mHelpAnnotation.group(); |
| 32 | + } |
| 33 | + if (mCommandAnnotation != null && !TextUtils.isEmpty(mCommandAnnotation.group())) { |
| 34 | + return mCommandAnnotation.group(); |
| 35 | + } |
| 36 | + return Command.GROUP_OTHER; |
| 37 | + } |
| 38 | + |
| 39 | + public void appendHelpInfoForMethods(StringBuilder target, int nameStartCol, int helpStartCol, int maxCols) { |
| 40 | + Method[] methods = mCommandClass.getMethods(); |
| 41 | + boolean isFirst = true; |
| 42 | + for (Method method : methods) { |
| 43 | + if (!method.getName().equals("invoke") || !method.isAnnotationPresent(Command.Help.class)) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + // separate each item with a new line. |
| 48 | + if (!isFirst) { |
| 49 | + target.append("\n"); |
| 50 | + } |
| 51 | + isFirst = false; |
| 52 | + |
| 53 | + target.append(Utils.spaces(nameStartCol)); |
| 54 | + String signature = getSignature(method); |
| 55 | + target.append(signature); |
| 56 | + target.append("\n"); |
| 57 | + |
| 58 | + appendHelpText(target, method.getAnnotation(Command.Help.class).value(), helpStartCol, maxCols); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public void appendHelpInfoForClass(StringBuilder target, int nameStartCol, int helpStartCol, int maxCols) { |
| 63 | + target.append(Utils.spaces(nameStartCol)); |
| 64 | + if (!TextUtils.isEmpty(mHelpAnnotation.format())) { |
| 65 | + target.append(mHelpAnnotation.format()); |
| 66 | + } else { |
| 67 | + target.append(mCommandClass.getSimpleName()); |
| 68 | + } |
| 69 | + target.append("\n"); |
| 70 | + appendHelpText(target, mHelpAnnotation.value(), helpStartCol, maxCols); |
| 71 | + } |
| 72 | + |
| 73 | + String getSignature(Method method) { |
| 74 | + Annotation[][] paramAnnotations = method.getParameterAnnotations(); |
| 75 | + Class<?>[] params = method.getParameterTypes(); |
| 76 | + StringBuilder signatureBuilder = new StringBuilder(); |
| 77 | + signatureBuilder.append(mCommandClass.getSimpleName()); |
| 78 | + signatureBuilder.append("("); |
| 79 | + for (int j = 2; j < params.length; j++) { |
| 80 | + // first two params are Interpreter and CallStack, ignore those. |
| 81 | + if (j != 2) { |
| 82 | + signatureBuilder.append(", "); |
| 83 | + } |
| 84 | + |
| 85 | + String name = params[j].getSimpleName(); |
| 86 | + if (j == params.length - 1 && method.isVarArgs() && params[j].isArray()) { |
| 87 | + signatureBuilder.append(name.replace("[]", "")); |
| 88 | + signatureBuilder.append("..."); |
| 89 | + } else { |
| 90 | + signatureBuilder.append(name); |
| 91 | + } |
| 92 | + |
| 93 | + Annotation[] annotations = paramAnnotations[j]; |
| 94 | + Command.ParamName paramAnnotation = null; |
| 95 | + for (Annotation a : annotations) { |
| 96 | + if (a instanceof Command.ParamName) { |
| 97 | + paramAnnotation = (Command.ParamName) a; |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + if (paramAnnotation != null) { |
| 102 | + if (!TextUtils.isEmpty(paramAnnotation.value())) { |
| 103 | + signatureBuilder.append(" "); |
| 104 | + signatureBuilder.append(paramAnnotation.value()); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + signatureBuilder.append(")"); |
| 109 | + return signatureBuilder.toString(); |
| 110 | + } |
| 111 | + |
| 112 | + public static void appendHelpText(StringBuilder target, String helpText, int helpStartCol, int maxCols) { |
| 113 | + int cols = helpStartCol; |
| 114 | + target.append(Utils.spaces(helpStartCol)); |
| 115 | + |
| 116 | + StringTokenizer tokens = new StringTokenizer(helpText, " "); |
| 117 | + while (tokens.hasMoreTokens()) { |
| 118 | + String token = tokens.nextToken(); |
| 119 | + target.append(token); |
| 120 | + if (tokens.hasMoreTokens()) { |
| 121 | + target.append(" "); |
| 122 | + } |
| 123 | + cols += token.length() + 1; |
| 124 | + if (cols >= maxCols && tokens.hasMoreTokens()) { |
| 125 | + cols = helpStartCol; |
| 126 | + target.append("\n"); |
| 127 | + target.append(Utils.spaces(helpStartCol)); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @SuppressWarnings("NullableProblems") |
| 133 | + @Override |
| 134 | + public int compareTo(CommandHelpInfo another) { |
| 135 | + if (another == null) { |
| 136 | + return -1; |
| 137 | + } |
| 138 | + String myGroup = getCommandGroup(); |
| 139 | + String theirGroup = another.getCommandGroup(); |
| 140 | + int groupCompare = myGroup.compareTo(theirGroup); |
| 141 | + if (groupCompare != 0) { |
| 142 | + // "Other" group should always come last. |
| 143 | + if (myGroup.equals(Command.GROUP_OTHER)) { |
| 144 | + return 1; |
| 145 | + } |
| 146 | + if (theirGroup.equals(Command.GROUP_OTHER)) { |
| 147 | + return - 1; |
| 148 | + } |
| 149 | + return groupCompare; |
| 150 | + } |
| 151 | + return mCommandClass.getSimpleName().compareTo(another.mCommandClass.getSimpleName()); |
| 152 | + } |
| 153 | + |
| 154 | + public Class getCommandClass() { |
| 155 | + return mCommandClass; |
| 156 | + } |
| 157 | +} |
0 commit comments