public static LispObject evaluate(String name, Environment environment, List<LispObject> args) { for (Class c : getSubroutineContainers()) { for (Method m : c.getMethods()) { Subroutine annotation = m.getAnnotation(Subroutine.class); if (annotation == null || !annotation.value().equals(name)) continue; if (!Arrays.asList(mySpecialForms).contains(c)) { if (!environment.areArgumentsEvaluated()) { for (int i = 0, dataSize = args.size(); i < dataSize; i++) { args.set(i, args.get(i).evaluate(environment)); } } else { environment.setArgumentsEvaluated(false); } } ArgumentsList arguments = parseArguments(m, environment, args); checkArguments(name, arguments, args); try { return (LispObject) m.invoke(null, arguments.getValues()); } catch (IllegalAccessException | InvocationTargetException e) { if (e.getCause().getMessage() == null) e.getCause().printStackTrace(); else System.err.println(e.getCause().getMessage()); Throwable cause = getCause(e); if (cause instanceof LispThrow) throw (LispThrow) cause; if (cause instanceof VoidVariableException && TestMode.TEST && c == Key.class) { System.err.println("Skip keymap errors in test mode"); return LispSymbol.ourNil; } if (cause instanceof LispException) throw (LispException) cause; cause.printStackTrace(); throw new LispException(e.getCause().getMessage()); } } } throw new InternalException(JelispBundle.message("unknown.subroutine", name)); }
private static ArgumentsList parseArguments( Method m, Environment environment, List<LispObject> args) { Type[] parametersTypes = m.getGenericParameterTypes(); Annotation[][] parametersAnnotations = m.getParameterAnnotations(); if (parametersAnnotations.length != parametersTypes.length) { throw new InternalException(JelispBundle.message("subroutine.args.properties.mismatch")); } ArgumentsList arguments = new ArgumentsList(); setOptional(arguments, parametersAnnotations, parametersTypes); int nActual = args.size(); if (parametersTypes.length != 0) { if (parametersTypes[0].equals(Environment.class)) { ++nActual; arguments.setValue(0, environment); } } if ((nActual < arguments.getRequiredSize()) || (nActual > parametersTypes.length && !m.isVarArgs())) throw new WrongNumberOfArgumentsException(m.getAnnotation(Subroutine.class).value(), nActual); return arguments; }