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 extractField(Collection in, String fieldName, Class type) throws SecurityException,
            NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        Iterator<Object> it = in.iterator();
        boolean isBoolean = (type == Boolean.class || type == boolean.class);
        String methodName = (isBoolean) ? "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) //$NON-NLS-1$
                : "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); //$NON-NLS-1$
        LinkedList<Object> out = new LinkedList<Object>();

        while (it.hasNext()) {
            Object obj = it.next();
            Method m = obj.getClass().getMethod(methodName, new Class[] {});
            Object value2 = m.invoke(obj, null);
            out.add(value2);

        }
        return out;
    }
}