/** * Binds a free variable of the given class to the active closure that is the last one created in the current thread. * @param closedClass The type of the free variable to be bound to the active closure * @return A proxy of the same class of the passed class used to register all the invocation on the closed object */ public static <T> T of(Class<T> closedClass) { return bindClosure(closedClass, closedClass); }
/** * Creates a closure with four free variables and binds it to the current thread * @param type1 The type of the first free variable of the newly created closure * @param type2 The type of the second free variable of the newly created closure * @param type3 The type of the third free variable of the newly created closure * @param type4 The type of the fourth free variable of the newly created closure * @return The newly created closure */ public static <A, B, C, D> Closure4<A, B, C, D> closure(Class<A> type1, Class<B> type2, Class<C> type3, Class<D> type4) { return createClosure(type1, type2, type3, type4); }
/** * Defines a free variable of the given Class for the currently active closure * @param clazz The Class of the new argument * @return A placeholder that represent a free closure variable of the given Class */ public static <T> T var(Class<T> clazz) { return createClosureVarPlaceholder(clazz); }
switch (getClosureVarType(args[i])) { case VAR: objs[i] = (curriedVars != null && curriedVarsFlags[curriedParamCounter]) ? curriedVars[curriedParamCounter] : getClosureVarArgument(args[i]).evaluate(vars[varCounter++]); curriedParamCounter++; break;
/** * Binds an object to this closure. * @param closed The object that has to be bound to this closure * @param closedClass The actual class of the proxied object * @return An instance of the closedClass that is actually a proxy used to register all the invocation on the closed object */ public <T> T of(Object closed, Class<T> closedClass) { setClosed(closed); return createProxyClosure(this, closedClass); }
private void bindInvocation(Invokable invokable, Object[] args) { invokables.add(invokable); if (args != null) for (Object arg : args) { if (getClosureVarType(arg).isClosureVarPlaceholder()) freeVarsNumber++; } argsList.add(args); }
/** * Tests if the given object is actually a placeholder for a free variable of a closure * @param object The object to be tested * @return true if the given object is actually a placeholder for a free variable of a closure */ static ClosureVarType getClosureVarType(Object object) { if (object == null) return ClosuresFactory.ClosureVarType.FIXED; if (getClosureVarArgument(object) != null) return ClosuresFactory.ClosureVarType.VAR; if (createClosureArgumentPlaceholder(object.getClass()).equals(object)) return ClosuresFactory.ClosureVarType.FINAL_VAR; return ClosuresFactory.ClosureVarType.FIXED; }
public Object invoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException { if (registered) return method.invoke(proxy, args); registered = true; closure.bindInvocation(method, args); DelayedClosure.call(); Class<?> returnType = method.getReturnType(); if (returnType == Void.TYPE) { closure.closeUnhandledInvocations(); return null; } return createProxyClosure(closure, returnType); } }
/** * Creates a closure with three free variables and binds it to the current thread * @param type1 The type of the first free variable of the newly created closure * @param type2 The type of the second free variable of the newly created closure * @param type3 The type of the third free variable of the newly created closure * @return The newly created closure */ public static <A, B, C> Closure3<A, B, C> closure(Class<A> type1, Class<B> type2, Class<C> type3) { return createClosure(type1, type2, type3); }
/** * Binds an object to the active closure that is the last one created in the current thread. * @param closed The object that has to be bound to the active closure * @param closedClass The actual class of the proxied object * @return An instance of the closedClass that is actually a proxy used to register all the invocation on the closed object */ public static <T> T of(T closed, Class<T> closedClass) { return bindClosure(closed, closedClass); }
/** * Creates a generic (not typed) closure and binds it to the current thread * @return The newly created closure */ public static Closure closure() { return createClosure(); }
/** * Creates a closure with a single free variable and binds it to the current thread * @param type1 The type of the free parameter of the newly created closure * @return The newly created closure */ public static <A> Closure1<A> closure(Class<A> type1) { return createClosure(type1); }
/** * Creates a closure with two free variables and binds it to the current thread * @param type1 The type of the first free variable of the newly created closure * @param type2 The type of the second free variable of the newly created closure * @return The newly created closure */ public static <A, B> Closure2<A, B> closure(Class<A> type1, Class<B> type2) { return createClosure(type1, type2); }
/** * Creates a DelayedClosure */ public DelayedClosure() { closure = createClosure(); closureResult = new ClosureResult<T>() { public T get() { return result; }}; CURRENT_DELAYED.set(this); }