Java Code Examples for groovy.lang.Closure#getParameterTypes()

The following examples show how to use groovy.lang.Closure#getParameterTypes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClosureBackedRuleAction.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private List<Class<?>> parseInputTypes(Closure<?> closure) {
    Class<?>[] parameterTypes = closure.getParameterTypes();

    if (parameterTypes.length == 0) {
        throw new RuleActionValidationException("Rule action closure must declare at least one parameter.");
    }

    List<Class<?>> inputTypes = Lists.newArrayList();

    if (parameterTypes[0] != subjectType) {
        throw new RuleActionValidationException(String.format("First parameter of rule action closure must be of type '%s'.", subjectType.getSimpleName()));
    }

    for (Class<?> parameterType : Arrays.asList(parameterTypes).subList(1, parameterTypes.length)) {
        inputTypes.add(parameterType);
    }
    return inputTypes;
}
 
Example 2
Source File: DefaultJsonGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected ClosureConverter(Class<?> type, Closure<?> closure) {
    if (type == null) {
        throw new NullPointerException("Type parameter must not be null");
    }
    if (closure == null) {
        throw new NullPointerException("Closure parameter must not be null");
    }

    int paramCount = closure.getMaximumNumberOfParameters();
    if (paramCount < 1) {
        throw new IllegalArgumentException("Closure must accept at least one parameter");
    }
    Class<?> param1 = closure.getParameterTypes()[0];
    if (!param1.isAssignableFrom(type)) {
        throw new IllegalArgumentException("Expected first parameter to be of type: " + type.toString());
    }
    if (paramCount > 1) {
        Class<?> param2 = closure.getParameterTypes()[1];
        if (!param2.isAssignableFrom(String.class)) {
            throw new IllegalArgumentException("Expected second parameter to be of type: " + String.class.toString());
        }
    }
    this.type = type;
    this.closure = closure;
    this.paramCount = paramCount;
}
 
Example 3
Source File: StringGroovyMethods.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a replacement corresponding to the matched pattern for {@link org.codehaus.groovy.runtime.StringGroovyMethods#replaceAll(CharSequence,Pattern,Closure)}.
 * <p>
 * The closure take parameter:
 * <ul>
 * <li>Whole of match if the pattern include no capturing group</li>
 * <li>Object[] of capturing groups if the closure takes Object[] as parameter</li>
 * <li>List of capturing groups</li>
 * </ul>
 *
 * @param matcher the matcher object used for matching
 * @param closure specified with replaceAll() to get replacement
 * @return replacement correspond replacement for a match
 */
private static String getReplacement(final Matcher matcher, final Closure closure) {
    if (!hasGroup(matcher)) {
        return InvokerHelper.toString(closure.call(matcher.group()));
    }

    int count = matcher.groupCount();
    List<String> groups = new ArrayList<String>();
    for (int i = 0; i <= count; i++) {
        groups.add(matcher.group(i));
    }

    if (closure.getParameterTypes().length == 1
            && closure.getParameterTypes()[0] == Object[].class) {
        return InvokerHelper.toString(closure.call(groups.toArray()));
    }
    return InvokerHelper.toString(closure.call(groups));
}
 
Example 4
Source File: ClosureBackedRuleAction.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private List<Class<?>> parseInputTypes(Closure<?> closure) {
    Class<?>[] parameterTypes = closure.getParameterTypes();

    if (parameterTypes.length == 0) {
        throw new RuleActionValidationException("Rule action closure must declare at least one parameter.");
    }

    List<Class<?>> inputTypes = Lists.newArrayList();

    if (parameterTypes[0] != subjectType) {
        throw new RuleActionValidationException(String.format("First parameter of rule action closure must be of type '%s'.", subjectType.getSimpleName()));
    }

    for (Class<?> parameterType : Arrays.asList(parameterTypes).subList(1, parameterTypes.length)) {
        inputTypes.add(parameterType);
    }
    return inputTypes;
}
 
Example 5
Source File: GroovyBeanDefinitionReader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected void applyPropertyToBeanDefinition(String name, Object value) {
	if (value instanceof GString) {
		value = value.toString();
	}
	if (addDeferredProperty(name, value)) {
		return;
	}
	else if (value instanceof Closure) {
		GroovyBeanDefinitionWrapper current = this.currentBeanDefinition;
		try {
			Closure callable = (Closure) value;
			Class<?> parameterType = callable.getParameterTypes()[0];
			if (Object.class == parameterType) {
				this.currentBeanDefinition = new GroovyBeanDefinitionWrapper("");
				callable.call(this.currentBeanDefinition);
			}
			else {
				this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(null, parameterType);
				callable.call((Object) null);
			}

			value = this.currentBeanDefinition.getBeanDefinition();
		}
		finally {
			this.currentBeanDefinition = current;
		}
	}
	this.currentBeanDefinition.addProperty(name, value);
}
 
Example 6
Source File: GroovyBeanDefinitionReader.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected void applyPropertyToBeanDefinition(String name, Object value) {
	if (value instanceof GString) {
		value = value.toString();
	}
	if (addDeferredProperty(name, value)) {
		return;
	}
	else if (value instanceof Closure) {
		GroovyBeanDefinitionWrapper current = this.currentBeanDefinition;
		try {
			Closure callable = (Closure) value;
			Class<?> parameterType = callable.getParameterTypes()[0];
			if (Object.class == parameterType) {
				this.currentBeanDefinition = new GroovyBeanDefinitionWrapper("");
				callable.call(this.currentBeanDefinition);
			}
			else {
				this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(null, parameterType);
				callable.call((Object) null);
			}

			value = this.currentBeanDefinition.getBeanDefinition();
		}
		finally {
			this.currentBeanDefinition = current;
		}
	}
	this.currentBeanDefinition.addProperty(name, value);
}
 
Example 7
Source File: DefaultComponentMetadataHandler.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void executeRuleClosure(ModuleComponentResolveMetaData metadata, ComponentMetadataDetails details, Closure<?> closure) {
    Class<?>[] parameterTypes = closure.getParameterTypes();
    if (parameterTypes.length == 0) {
        throw new InvalidUserCodeException("A component metadata rule needs to have at least one parameter.");
    }

    List<Object> args = Lists.newArrayList();

    if (parameterTypes[0].isAssignableFrom(ComponentMetadataDetails.class)) {
        args.add(details);
    } else {
        throw new InvalidUserCodeException(
                String.format("First parameter of a component metadata rule needs to be of type '%s'.",
                        ComponentMetadataDetails.class.getSimpleName()));
    }

    for (Class<?> parameterType : Arrays.asList(parameterTypes).subList(1, parameterTypes.length)) {
        if (parameterType == IvyModuleDescriptor.class) {
            if (!(metadata instanceof IvyModuleResolveMetaData)) {
                return;
            }
            IvyModuleResolveMetaData ivyMetadata = (IvyModuleResolveMetaData) metadata;
            args.add(new DefaultIvyModuleDescriptor(ivyMetadata.getExtraInfo(), ivyMetadata.getBranch(), ivyMetadata.getStatus()));
        } else {
            throw new InvalidUserCodeException(String.format("Unsupported parameter type for component metadata rule: %s", parameterType.getName()));
        }
    }
    closure.call(args.toArray());
}
 
Example 8
Source File: GroovyBeanDefinitionReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void applyPropertyToBeanDefinition(String name, Object value) {
	if (value instanceof GString) {
		value = value.toString();
	}
	if (addDeferredProperty(name, value)) {
		return;
	}
	else if (value instanceof Closure) {
		GroovyBeanDefinitionWrapper current = this.currentBeanDefinition;
		try {
			Closure callable = (Closure) value;
			Class<?> parameterType = callable.getParameterTypes()[0];
			if (Object.class == parameterType) {
				this.currentBeanDefinition = new GroovyBeanDefinitionWrapper("");
				callable.call(this.currentBeanDefinition);
			}
			else {
				this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(null, parameterType);
				callable.call((Object) null);
			}

			value = this.currentBeanDefinition.getBeanDefinition();
		}
		finally {
			this.currentBeanDefinition = current;
		}
	}
	this.currentBeanDefinition.addProperty(name, value);
}
 
Example 9
Source File: GroovyBeanDefinitionReader.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
protected void applyPropertyToBeanDefinition(String name, Object value) {
	if (value instanceof GString) {
		value = value.toString();
	}
	if (addDeferredProperty(name, value)) {
		return;
	}
	else if (value instanceof Closure) {
		GroovyBeanDefinitionWrapper current = this.currentBeanDefinition;
		try {
			Closure callable = (Closure) value;
			Class<?> parameterType = callable.getParameterTypes()[0];
			if (parameterType.equals(Object.class)) {
				this.currentBeanDefinition = new GroovyBeanDefinitionWrapper("");
				callable.call(this.currentBeanDefinition);
			}
			else {
				this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(null, parameterType);
				callable.call((Object) null);
			}

			value = this.currentBeanDefinition.getBeanDefinition();
		}
		finally {
			this.currentBeanDefinition = current;
		}
	}
	this.currentBeanDefinition.addProperty(name, value);
}
 
Example 10
Source File: GroovyBeanDefinitionReader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected void applyPropertyToBeanDefinition(String name, Object value) {
	if (value instanceof GString) {
		value = value.toString();
	}
	if (addDeferredProperty(name, value)) {
		return;
	}
	else if (value instanceof Closure) {
		GroovyBeanDefinitionWrapper current = this.currentBeanDefinition;
		try {
			Closure callable = (Closure) value;
			Class<?> parameterType = callable.getParameterTypes()[0];
			if (Object.class == parameterType) {
				this.currentBeanDefinition = new GroovyBeanDefinitionWrapper("");
				callable.call(this.currentBeanDefinition);
			}
			else {
				this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(null, parameterType);
				callable.call((Object) null);
			}

			value = this.currentBeanDefinition.getBeanDefinition();
		}
		finally {
			this.currentBeanDefinition = current;
		}
	}
	this.currentBeanDefinition.addProperty(name, value);
}
 
Example 11
Source File: DockerizedExecHandle.java    From gradle-dockerized-test-plugin with Apache License 2.0 5 votes vote down vote up
private void invokeIfNotNull(Closure closure, Object... args) {
    if (closure != null) {
        int l = closure.getParameterTypes().length;
        Object[] nargs;
        if (l < args.length) {
            nargs = new Object[l];
            System.arraycopy(args, 0, nargs, 0, l);
        } else {
            nargs = args;
        }
        closure.call(nargs);
    }
}
 
Example 12
Source File: Memoize.java    From groovy with Apache License 2.0 5 votes vote down vote up
MemoizeFunction(final MemoizeCache<Object, ?> cache, Closure<V> closure) {
    super(closure.getOwner());
    this.cache = coerce(cache);
    this.closure = closure;
    parameterTypes = closure.getParameterTypes();
    maximumNumberOfParameters = closure.getMaximumNumberOfParameters();
}
 
Example 13
Source File: CurriedClosure.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the curried closure.
 *
 * @param index the position where the parameters should be injected (-ve for lazy)
 * @param uncurriedClosure the closure to be called after the curried parameters are injected
 * @param arguments the supplied parameters
 */
public CurriedClosure(int index, Closure<V> uncurriedClosure, Object... arguments) {
    super(uncurriedClosure.clone());
    curriedParams = arguments;
    this.index = index;
    final int origMaxLen = uncurriedClosure.getMaximumNumberOfParameters();
    maximumNumberOfParameters = origMaxLen - arguments.length;
    Class[] classes = uncurriedClosure.getParameterTypes();
    Class lastType = classes.length == 0 ? null : classes[classes.length-1];
    if (lastType != null && lastType.isArray()) {
        varargType = lastType;
    }

    if (!isVararg()) {
        // perform some early param checking for non-vararg case
        if (index < 0) {
            // normalise
            this.index += origMaxLen;
            minParamsExpected = 0;
        } else {
            minParamsExpected = index + arguments.length;
        }
        if (maximumNumberOfParameters < 0) {
            throw new IllegalArgumentException("Can't curry " + arguments.length + " arguments for a closure with " + origMaxLen + " parameters.");
        }
        if (index < 0) {
            if (index < -origMaxLen || index > -arguments.length)
                throw new IllegalArgumentException("To curry " + arguments.length + " argument(s) expect index range " +
                        (-origMaxLen) + ".." + (-arguments.length) + " but found " + index);
        } else if (index > maximumNumberOfParameters) {
            throw new IllegalArgumentException("To curry " + arguments.length + " argument(s) expect index range 0.." +
                    maximumNumberOfParameters + " but found " + index);
        }
    } else {
        minParamsExpected = 0;
    }
}
 
Example 14
Source File: DefaultComponentMetadataHandler.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void executeRuleClosure(ModuleComponentResolveMetaData metadata, ComponentMetadataDetails details, Closure<?> closure) {
    Class<?>[] parameterTypes = closure.getParameterTypes();
    if (parameterTypes.length == 0) {
        throw new InvalidUserCodeException("A component metadata rule needs to have at least one parameter.");
    }

    List<Object> args = Lists.newArrayList();

    if (parameterTypes[0].isAssignableFrom(ComponentMetadataDetails.class)) {
        args.add(details);
    } else {
        throw new InvalidUserCodeException(
                String.format("First parameter of a component metadata rule needs to be of type '%s'.",
                        ComponentMetadataDetails.class.getSimpleName()));
    }

    for (Class<?> parameterType : Arrays.asList(parameterTypes).subList(1, parameterTypes.length)) {
        if (parameterType == IvyModuleDescriptor.class) {
            if (!(metadata instanceof IvyModuleResolveMetaData)) {
                return;
            }
            IvyModuleResolveMetaData ivyMetadata = (IvyModuleResolveMetaData) metadata;
            args.add(new DefaultIvyModuleDescriptor(ivyMetadata.getExtraInfo(), ivyMetadata.getBranch(), ivyMetadata.getStatus()));
        } else {
            throw new InvalidUserCodeException(String.format("Unsupported parameter type for component metadata rule: %s", parameterType.getName()));
        }
    }
    closure.call(args.toArray());
}
 
Example 15
Source File: ClosureMetaMethod.java    From groovy with Apache License 2.0 4 votes vote down vote up
public AnonymousMetaMethod(Closure closure, String name, Class declaringClass) {
    super(closure.getParameterTypes());
    this.closure = closure;
    this.name = name;
    this.declaringClass = declaringClass;
}
 
Example 16
Source File: ClosureStaticMetaMethod.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param name The name of the MetaMethod
 * @param declaringClass The class which declared the MetaMethod
 * @param c The closure that this ClosureMetaMethod will invoke when called
 */
public ClosureStaticMetaMethod(String name, Class declaringClass, Closure c) {
    this(name, declaringClass, c, c.getParameterTypes());
}