src/de/plpt/ArgumentParser/ArgumentParser.java
/**
	 * Parses a given command and looks for a matching regex definition in executableObject.
	 * When a regex method definition matches command string, the method is executed with regex group parameters
	 * which were parsed to strongly typed values if possible.
	 * Parameters of defined Methods can only be primitive values or primitive array in Object wrapper array type
	 *
	 * @param command InputCommand from Terminal
	 * @param <T>     Return type of matching Method defined in executable Object.
	 * @param args    optional object argument which can be passed to the destination method. if input is string and
	 *                destination is typed, then the params are converted into destination type
	 * @return Result value (return type) of matching Method in executableObject. In worst case use as Object an call
	 * Object#toString() on it.
	 * @throws ArgumentParserException    Parser exception raised when there are problems
	 *                                    parsing string and executing Methods.
	 * @throws IntervalViolationException Raised when a given Integer interval is violated
	 */
Parses a given command and looks for a matching regex definition in executableObject.
When a regex method definition matches command string, the method is executed with regex group parameters
which were parsed to strongly typed values if possible.
Parameters of defined Methods can only be primitive values or primitive array in Object wrapper array type

@param command InputCommand from Terminal
@param      Return type of matching Method defined in executable Object.
@param args    optional object argument which can be passed to the destination method. if input is string and
destination is typed, then the params are converted into destination type
@return Result value (return type) of matching Method in executableObject. In worst case use as Object an call
Object#toString() on it.
@throws ArgumentParserException    Parser exception raised when there are problems
parsing string and executing Methods.
@throws IntervalViolationException Raised when a given Integer interval is violated
Parses a given command and looks for a matching regex definition in executableObject . When a regex method definition matches command string the method is executed with regex group parameters which were parsed to strongly typed values if possible . Parameters of defined Methods can only be primitive values or primitive array in Object wrapper array type @param command InputCommand from Terminal @param Return type of matching Method defined in executable Object . @param args optional object argument which can be passed to the destination method . if input is string and destination is typed then the params are converted into destination type @return Result value ( return type ) of matching Method in executableObject . In worst case use as Object an call Object#toString () on it . @throws ArgumentParserException Parser exception raised when there are problems parsing string and executing Methods . @throws IntervalViolationException Raised when a given Integer interval is violated
@SuppressWarnings("unchecked")
	public <T> T parse(String command, Object... args) throws ArgumentParserException, IntervalViolationException {
		Method meth = getMatchingMethod(command);
		if (meth == null) {
			Method mm = getStartsWithMethod(command);
			if (mm != null) {
				String regex = mm.getAnnotation(CommandInfo.class).command();
				throw new IllegalArgumentException(
						String.format("Command '%s' does not match regex '%s'", command, regex));
			}
			String uncd = "Unknown Command";
			if (showHelp) {
				uncd += "\n" + getHelpString();
			}
			throw new ArgumentParserException(uncd);
		}
		CommandInfo anno = meth.getAnnotation(CommandInfo.class);
		String annoCommand = anno.command();
		Pattern pattern = Pattern.compile(annoCommand);
		Matcher matcher = pattern.matcher(command);
		if (!matcher.matches())
			throw new ArgumentParserException(
					String.format("Command '%s' does not match pattern '%s'", command, annoCommand));
		Object[] values = new Object[meth.getParameterCount()];
		int indexOffset = 0;
		for (int i = 0; i < meth.getParameterCount(); i++) {
			Class<?> type = meth.getParameterTypes()[i];
			Annotation[] paramAnno = meth.getParameterAnnotations()[i];
			int currentOffset = i + indexOffset;
			if (matcher.groupCount() - 1 >= currentOffset) {
				if (!type.isArray()) {
					values[i] = processNormalParameter(matcher, currentOffset, type, paramAnno);
				} else if ((paramAnno.length > 0)) {
					Object[] array = processArrayParameter(matcher, currentOffset, type, paramAnno[0]);
					values[i] = array;
					indexOffset += array.length - 1;
				} else {
					throw new ArgumentParserException("Given type is an array but no array lenght is defined!");
				}
			} else if (args.length > currentOffset - matcher.groupCount()) {
				values[i] = parseValue(args[currentOffset - matcher.groupCount()].toString(), type);
			}
		}
		try {
			return (T) (meth.invoke(executableObject, values));
		} catch (IllegalAccessException e) {
			throw new ArgumentParserException("Illegal Access on executable object: " + e.getMessage(), e);
		} catch (InvocationTargetException e) {
			if (e.getCause() == null) {
				throw new ArgumentParserException(
						String.format("Method '%s' cannot be invoked: %s", meth.getName(), e.getMessage()), e);
			} else {
				throw new ArgumentParserExecutionException("There is an error inside invoked Method", e.getCause());
			}
		}
	}
@ SuppressWarnings ( "unchecked" ) public < T > T parse ( String command , Object ... args ) throws ArgumentParserException , IntervalViolationException { Method meth = getMatchingMethod ( command ) ; if ( meth == null ) { Method mm = getStartsWithMethod ( command ) ; if ( mm != null ) { String regex = mm . getAnnotation ( CommandInfo . class ) . command ( ) ; throw new IllegalArgumentException ( String . format ( "Command '%s' does not match regex '%s'" , command , regex ) ) ; } String uncd = "Unknown Command" ; if ( showHelp ) { uncd += "\n" + getHelpString ( ) ; } throw new ArgumentParserException ( uncd ) ; } CommandInfo anno = meth . getAnnotation ( CommandInfo . class ) ; String annoCommand = anno . command ( ) ; Pattern pattern = Pattern . compile ( annoCommand ) ; Matcher matcher = pattern . matcher ( command ) ; if ( ! matcher . matches ( ) ) throw new ArgumentParserException ( String . format ( "Command '%s' does not match pattern '%s'" , command , annoCommand ) ) ; Object [ ] values = new Object [ meth . getParameterCount ( ) ] ; int indexOffset = 0 ; for ( int i = 0 ; i < meth . getParameterCount ( ) ; i ++ ) { Class < ? > type = meth . getParameterTypes ( ) [ i ] ; Annotation [ ] paramAnno = meth . getParameterAnnotations ( ) [ i ] ; int currentOffset = i + indexOffset ; if ( matcher . groupCount ( ) - 1 >= currentOffset ) { if ( ! type . isArray ( ) ) { values [ i ] = processNormalParameter ( matcher , currentOffset , type , paramAnno ) ; } else if ( ( paramAnno . length > 0 ) ) { Object [ ] array = processArrayParameter ( matcher , currentOffset , type , paramAnno [ 0 ] ) ; values [ i ] = array ; indexOffset += array . length - 1 ; } else { throw new ArgumentParserException ( "Given type is an array but no array lenght is defined!" ) ; } } else if ( args . length > currentOffset - matcher . groupCount ( ) ) { values [ i ] = parseValue ( args [ currentOffset - matcher . groupCount ( ) ] . toString ( ) , type ) ; } } try { return ( T ) ( meth . invoke ( executableObject , values ) ) ; } catch ( IllegalAccessException e ) { throw new ArgumentParserException ( "Illegal Access on executable object: " + e . getMessage ( ) , e ) ; } catch ( InvocationTargetException e ) { if ( e . getCause ( ) == null ) { throw new ArgumentParserException ( String . format ( "Method '%s' cannot be invoked: %s" , meth . getName ( ) , e . getMessage ( ) ) , e ) ; } else { throw new ArgumentParserExecutionException ( "There is an error inside invoked Method" , e . getCause ( ) ) ; } } }
Parses a given command and looks for a matching regex definition in executableObject.
Parses a given command and looks for a matching regex definition in executableObject .