Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

public class Main {
    public static Collection invokeForEach(Collection in, String methodName, Object[] args)
            throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        Iterator<Object> it = in.iterator();

        Class[] argsClasses = new Class[args.length];
        for (int i = 0; i < args.length; i++)
            argsClasses[i] = args[i].getClass();

        LinkedList<Object> out = new LinkedList<Object>();

        while (it.hasNext()) {
            Object obj = it.next();

            Method m = obj.getClass().getMethod(methodName, argsClasses);
            Object value2 = m.invoke(obj, args);
            out.add(value2);

        }
        return out;
    }
}