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;

public class Main {
    public static Object containsClassAttributeInCollection(Collection in, String fieldName, Object value)
            throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        Iterator<Object> it = in.iterator();
        boolean isBoolean = (value instanceof Boolean || value == 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$

        while (it.hasNext()) {
            Object obj = it.next();
            Method m = obj.getClass().getMethod(methodName, new Class[] {});
            Object value2 = m.invoke(obj, null);
            if (value != null && value2 != null && value.equals(value2))
                return obj;
        }
        return null;
    }
}