private static int checkSingleArgument( String subroutineName, Class expectedType, ArgumentsList arguments, List<LispObject> args, int argsCounter, int i) { try { if (!(expectedType.isInstance(args.get(argsCounter)))) { if (expectedType.equals(LispList.class) && args.get(argsCounter).equals(LispSymbol.ourNil)) { arguments.setValue(i, LispList.list()); return argsCounter + 1; } if (expectedType.equals(LispSymbol.class) && args.get(argsCounter).equals(LispSymbol.ourNil)) { arguments.setValue(i, LispSymbol.ourNil); return argsCounter + 1; } if (arguments.isOptional(i)) return -1; throw new WrongTypeArgumentException(expectedType.getSimpleName(), args.get(argsCounter)); } arguments.setValue(i, args.get(argsCounter)); return argsCounter + 1; } catch (IndexOutOfBoundsException e) { if (arguments.isOptional(i)) return -1; throw new WrongNumberOfArgumentsException(subroutineName, i); } }
private static int checkParameterizedType( String subroutineName, ParameterizedType expectedType, ArgumentsList arguments, List<LispObject> args, int argsCounter, int i) { Type rawType = expectedType.getRawType(); Type expectedTypeArguments = expectedType.getActualTypeArguments()[0]; try { if (((Class) rawType).isInstance(args.get(argsCounter))) { Type actualTypeArguments = ((ParameterizedType) (Type) args.get(argsCounter).getClass()) .getActualTypeArguments()[0]; if (!expectedTypeArguments.equals(actualTypeArguments)) { throw new WrongTypeArgumentException( ((Class) rawType).getSimpleName() + "<" + ((Class) expectedTypeArguments).getSimpleName() + ">", args.get(argsCounter)); } arguments.setValue(i, args.get(argsCounter)); return argsCounter + 1; } else { if (arguments.isOptional(i)) return -1; throw new WrongTypeArgumentException(expectedType.toString(), args.get(argsCounter)); } } catch (IndexOutOfBoundsException e) { if (arguments.isOptional(i)) return -1; throw new WrongNumberOfArgumentsException(subroutineName, i); } }
private static int checkArray( Class expectedType, ArgumentsList arguments, List<LispObject> args, int argsCounter, int i) { Class componentType = expectedType.getComponentType(); ArrayList<LispObject> array = new ArrayList<>(); while (argsCounter != args.size()) { if (!componentType.isInstance(args.get(argsCounter))) throw new WrongTypeArgumentException(componentType.toString(), args.get(argsCounter)); array.add(args.get(argsCounter)); ++argsCounter; } arguments.setValue(i, customizeArrayList(componentType, array)); return argsCounter; }
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; }