public List<String> onTabComplete( CommandSender commandSender, Command command, String s, String[] strings) { for (TabCompleter completer : delegateCompleters) { List<String> list = completer.onTabComplete(commandSender, command, s, strings); if (list != null) return list; } String expression = strings[strings.length - 1]; TreeSet<String> result = new TreeSet<String>(); Caller caller = plugin.getCallerService().getCaller(commandSender); WorkspaceService service = plugin.getWorkspaceService(); String workspaceName = service.getWorkspaceName(commandSender); Workspace workspace = service.getWorkspace(workspaceName); LinkedList<String> tokens = new LinkedList<String>(); boolean needHelp = expression.endsWith("?"); if (needHelp) expression = expression.substring(0, expression.length() - 1); Collections.addAll(tokens, expression.split("\\.")); if (expression.endsWith(".")) tokens.add(""); if (needHelp) { getHelp(caller, workspace, tokens); return Collections.singletonList(expression); } String firstToken = tokens.pollFirst(); if (firstToken == null) firstToken = ""; MetaClass callerScriptMetaClass = InvokerHelper.getMetaClass(CallerScript.class); MetaClass workspaceMetaClass = InvokerHelper.getMetaClass(Workspace.class); Map workspaceVars = null; if (workspace != null) workspaceVars = workspace.getBinding().getVariables(); Map globalVars = service.getBinding().getVariables(); PreparedScriptProperties properties = new PreparedScriptProperties(); properties.setCaller(caller); properties.setServer(plugin.getServer()); properties.setWorkspace(workspace); if (tokens.isEmpty()) { // get current method or class for (MetaProperty metaProperty : callerScriptMetaClass.getProperties()) { String name = metaProperty.getName(); if (name.contains(firstToken)) result.add(name); } for (String name : service.getImportTabCompleteClasses().keySet()) { if (name.contains(firstToken)) result.add(name); } for (MetaMethod metaMethod : callerScriptMetaClass.getMetaMethods()) { if (metaMethod.getDeclaringClass().getTheClass().equals(Object.class)) continue; String name = metaMethod.getName(); if (name.contains(firstToken)) { String methodEnd = "("; if (metaMethod.isValidMethod(new Class[] {Closure.class})) methodEnd = "{"; else if (metaMethod.getParameterTypes().length == 0) methodEnd = "()"; result.add(name + methodEnd); } int args = metaMethod.getParameterTypes().length; if ((name.startsWith("get") && args == 0 || name.startsWith("set") && args == 1) && name.length() > 3) { String propertyName = getPropertyName(name); if (propertyName != null && propertyName.contains(firstToken)) result.add(propertyName); } } for (MetaMethod metaMethod : workspaceMetaClass.getMetaMethods()) { if (metaMethod.getDeclaringClass().getTheClass().equals(Object.class)) continue; String name = metaMethod.getName(); if (name.contains(firstToken)) { String methodEnd = "("; if (metaMethod.isValidMethod(new Class[] {Closure.class})) methodEnd = "{"; else if (metaMethod.getParameterTypes().length == 0) methodEnd = "()"; result.add(name + methodEnd); } } for (Method method : CallerScript.class.getMethods()) { if (method.getDeclaringClass().equals(Object.class)) continue; String name = method.getName(); if (name.contains(firstToken)) { String methodEnd = "("; Class<?>[] types = method.getParameterTypes(); if (types.length == 1 && Closure.class.isAssignableFrom(types[0])) methodEnd = "{"; else if (types.length == 0) methodEnd = "()"; result.add(name + methodEnd); int args = method.getParameterTypes().length; if ((name.startsWith("get") && args == 0 || name.startsWith("set") && args == 1) && name.length() > 3) { String propertyName = getPropertyName(name); if (propertyName != null && propertyName.contains(firstToken)) result.add(propertyName); } } } for (Method method : Workspace.class.getMethods()) { if (method.getDeclaringClass().equals(Object.class)) continue; String name = method.getName(); if (name.contains(firstToken)) { String methodEnd = "("; Class<?>[] types = method.getParameterTypes(); if (types.length == 1 && Closure.class.isAssignableFrom(types[0])) methodEnd = "{"; else if (types.length == 0) methodEnd = "()"; result.add(name + methodEnd); } } if (workspaceVars != null) for (Object key : workspaceVars.keySet()) { String name = key.toString(); if (name.contains(firstToken)) result.add(name); } if (globalVars != null) for (Object key : globalVars.keySet()) { String name = key.toString(); if (name.contains(firstToken)) result.add(name); } for (GroovyObject modifier : CallerScript.getDynamicModifiers()) { Object[] params = {properties}; try { Map<?, ?> map = (Map) modifier.getMetaClass().invokeMethod(modifier, "getPropertyMapFor", params); for (Object key : map.keySet()) { String name = key.toString(); if (name.contains(firstToken)) result.add(name); } } catch (Exception ignored) { } try { Map<?, ?> map = (Map) modifier.getMetaClass().invokeMethod(modifier, "getMethodMapFor", params); for (Object key : map.keySet()) { String name = key.toString(); if (name.contains(firstToken)) result.add(name + "("); } } catch (Exception ignored) { } } if (globalVars != null) for (Object key : globalVars.keySet()) { String name = key.toString(); if (name.contains(firstToken)) result.add(name); } return new ArrayList<String>(result); } // get metaclass of first token MetaClass metaClass = getFirstTokenMeta( caller, firstToken, commandSender, service, callerScriptMetaClass, workspaceMetaClass, workspace, workspaceVars, globalVars, properties); boolean classHook = tokens.size() <= 1 && service.getImportTabCompleteClasses().containsKey(firstToken); if (metaClass == null) return null; metaClass = skipTokens(tokens, metaClass); if (metaClass == null) return null; // select property or method of last metaclass String token = tokens.pollFirst(); Class theClass = metaClass.getTheClass(); String inputPrefix = expression.substring(0, expression.lastIndexOf('.')) + "."; for (MetaProperty metaProperty : metaClass.getProperties()) { String name = metaProperty.getName(); if (name.startsWith(token)) result.add(inputPrefix + name); } for (MetaMethod metaMethod : metaClass.getMetaMethods()) { if (metaMethod.getDeclaringClass().getTheClass().equals(Object.class)) continue; String name = metaMethod.getName(); if (name.startsWith(token)) { String methodEnd = "("; if (metaMethod.isValidMethod(new Class[] {Closure.class})) methodEnd = "{"; else if (metaMethod.getNativeParameterTypes().length == 0) methodEnd = "()"; result.add(inputPrefix + name + methodEnd); } int args = metaMethod.getParameterTypes().length; if ((name.startsWith("get") && args == 0 || name.startsWith("set") && args == 1) && name.length() > 3) { String propertyName = getPropertyName(name); if (propertyName != null && propertyName.startsWith(token)) result.add(inputPrefix + propertyName); } } for (Method method : theClass.getMethods()) { if (method.getDeclaringClass().equals(Object.class)) continue; String name = method.getName(); if (name.startsWith(token)) { String methodEnd = "("; Class<?>[] types = method.getParameterTypes(); if (types.length == 1 && Closure.class.isAssignableFrom(types[0])) methodEnd = "{"; if (types.length == 0) methodEnd = "()"; result.add(inputPrefix + name + methodEnd); } int args = method.getParameterTypes().length; if ((name.startsWith("get") && args == 0 || name.startsWith("set") && args == 1) && name.length() > 3) { String propertyName = getPropertyName(name); if (propertyName != null && propertyName.startsWith(token)) result.add(inputPrefix + propertyName); } } if (Enum.class.isAssignableFrom(theClass)) { Enum[] enumValues = getEnumValues(theClass); if (enumValues != null) for (Enum anEnum : enumValues) { String name = anEnum.name(); if (name.startsWith(token)) result.add(inputPrefix + name); } } if (classHook) { for (MetaProperty metaProperty : InvokerHelper.getMetaClass(Class.class).getProperties()) { String name = metaProperty.getName(); if (name.startsWith(token)) result.add(inputPrefix + name); } for (Method method : Class.class.getMethods()) { if (method.getDeclaringClass().equals(Object.class)) continue; String name = method.getName(); if (name.startsWith(token)) { String methodEnd = "("; Class<?>[] types = method.getParameterTypes(); if (types.length == 1 && Closure.class.isAssignableFrom(types[0])) methodEnd = "{"; if (types.length == 0) methodEnd = "()"; result.add(inputPrefix + name + methodEnd); } int args = method.getParameterTypes().length; if ((name.startsWith("get") && args == 0 || name.startsWith("set") && args == 1) && name.length() > 3) { String propertyName = getPropertyName(name); if (propertyName != null && propertyName.startsWith(token)) result.add(inputPrefix + propertyName); } } } return new ArrayList<String>(result); }
private MetaClass getFirstTokenMeta( Caller caller, String firstToken, CommandSender commandSender, WorkspaceService service, MetaClass callerScriptMetaClass, MetaClass workspaceMetaClass, Workspace workspace, Map workspaceVars, Map globalVars, PreparedScriptProperties properties) { if (firstToken.isEmpty() || propertyPattern.matcher(firstToken).matches()) { if (firstToken.equals("me")) return InvokerHelper.getMetaClass(commandSender.getClass()); if (firstToken.equals("this")) return InvokerHelper.getMetaClass(CallerScript.class); if (firstToken.equals("_")) { Object result = caller.getLastResult(); if (result == null) return null; return InvokerHelper.getMetaClass(result.getClass()); } Class aClass = service.getImportTabCompleteClasses().get(firstToken); if (aClass != null) return InvokerHelper.getMetaClass(aClass); MetaProperty property = callerScriptMetaClass.getMetaProperty(firstToken); if (property != null) return InvokerHelper.getMetaClass(property.getType()); if (workspaceVars != null) for (Object key : workspaceVars.keySet()) { String name = key.toString(); if (name.contains(firstToken)) { Object val = workspaceVars.get(key); return InvokerHelper.getMetaClass(val.getClass()); } } if (globalVars != null) for (Object key : globalVars.keySet()) { String name = key.toString(); if (name.contains(firstToken)) { Object val = globalVars.get(key); return InvokerHelper.getMetaClass(val.getClass()); } } if (workspace != null) { for (GroovyObject modifier : CallerScript.getDynamicModifiers()) { Object[] params = {properties}; try { Map<?, ?> map = (Map) modifier.getMetaClass().invokeMethod(modifier, "getPropertyMapFor", params); Object value = map.get(firstToken); if (!(value instanceof Class)) return null; return InvokerHelper.getMetaClass((Class) value); } catch (Exception ignored) { } } } } else if (methodPattern.matcher(firstToken).matches()) { String curMethodName = firstToken.substring(0, firstToken.indexOf('(')); for (MetaMethod metaMethod : callerScriptMetaClass.getMetaMethods()) { String name = metaMethod.getName(); if (name.equals(curMethodName)) { return InvokerHelper.getMetaClass(metaMethod.getReturnType()); } } for (MetaMethod metaMethod : workspaceMetaClass.getMetaMethods()) { String name = metaMethod.getName(); if (name.equals(curMethodName)) { return InvokerHelper.getMetaClass(metaMethod.getReturnType()); } } for (GroovyObject modifier : CallerScript.getDynamicModifiers()) { Object[] params = {properties}; try { Map<?, ?> map = (Map) modifier.getMetaClass().invokeMethod(modifier, "getMethodMapFor", params); Object value = map.get(curMethodName); if (!(value instanceof Class)) return null; return InvokerHelper.getMetaClass(value); } catch (Exception ignored) { } } } else if (firstToken.endsWith("\'") || firstToken.endsWith("\"")) { return InvokerHelper.getMetaClass(String.class); } return null; }