<commit_before>package org.flymine.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.lang.reflect.Field;

/**
 * Provides utility methods for working with Java types and reflection
 *
 * @author <NAME>
 * @author <NAME>
 */
public class TypeUtil
{
    private TypeUtil() {
    }

    /**
     * Returns the value of a public or protected Field of an Object given the field name
     *
     * @param o the Object
     * @param fieldName the name of the relevant Field
     * @return the value of the Field
     * @throws IllegalAccessException if the field is inaccessible
     */ 
    public static Object getFieldValue(Object o, String fieldName) throws IllegalAccessException {
        Field f  = getField(o.getClass(), fieldName);
        f.setAccessible(true);
        return f.get(o);
    }

    /**
     * Returns the Field object of a Class given the field name
     *
     * @param c the Class
     * @param fieldName the name of the relevant field
     * @return the Field, or null if the field is not found
     */ 
    public static Field getField(Class c, String fieldName) {
        Field f = null;
        boolean found = false;
        do {
            try {
                f = c.getDeclaredField(fieldName);
                found = true;
            } catch (NoSuchFieldException e) {
                c = c.getSuperclass();
            }
        } while(c != null && !found);    
        return f;
    }

    /**
     * Gets the Fields of a Class
     *
     * @param c the Class
     * @return the fields in this class
     */
    public static Collection getFields(Class c) {
        Collection fields = new HashSet();
        do {
            for (int i = 0; i < c.getDeclaredFields().length; i++) {
                fields.addAll(Arrays.asList(c.getDeclaredFields()));
            }
        } while ((c = c.getSuperclass()) != null);
        return fields;
    }

    /**
     * Get the type of the elements of a collection
     *
     * @param col the collection
     * @return the Class of the elements of the collection
     */
    public static Class getElementType(Collection col) {
        if (col == null) {
            throw new NullPointerException("Collection cannot be null");
        }<commit_msg>added methods to set field values and get the getter methods of a bean<commit_after>package org.flymine.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.IntrospectionException;

/**
 * Provides utility methods for working with Java types and reflection
 *
 * @author <NAME>
 * @author <NAME>
 */
public class TypeUtil
{
    private TypeUtil() {
    }

    /**
     * Returns the value of a public or protected Field of an Object given the field name
     *
     * @param o the Object
     * @param fieldName the name of the relevant Field
     * @return the value of the Field
     * @throws IllegalAccessException if the field is inaccessible
     */ 
    public static Object getFieldValue(Object o, String fieldName) throws IllegalAccessException {
        Field f  = getField(o.getClass(), fieldName);
        f.setAccessible(true);
        return f.get(o);
    }

    /**
     * Sets the value of a public or protected Field of an Object given the field name
     *
     * @param o the Object
     * @param fieldName the name of the relevant Field
     * @param fieldValue the value of the Field
     * @throws IllegalAccessException if the field is inaccessible
     */ 
    public static void setFieldValue(Object o, String fieldName, Object fieldValue) 
        throws IllegalAccessException {
        Field f  = getField(o.getClass(), fieldName);
        f.setAccessible(true);
        f.set(o, fieldValue);
    }

    /**
     * Returns the Field object of a Class given the field name
     *
     * @param c the Class
     * @param fieldName the name of the relevant field
     * @return the Field, or null if the field is not found
     */ 
    public static Field getField(Class c, String fieldName) {
        Field f = null;
        boolean found = false;
        do {
            try {
                f = c.getDeclaredField(fieldName);
                found = true;
            } catch (NoSuchFieldException e) {
                c = c.getSuperclass();
            }
        } while(c != null && !found);    
        return f;
    }

    /**
     * Gets the Fields of a Class
     *
     * @param c the Class
     * @return the fields in this class
     */
    public static Collection getFields(Class c) {
        Collection fields = new HashSet();
        do {
            for (int i = 0; i < c.getDeclaredFields().length; i++) {
                fields.addAll(Arrays.asList(c.getDeclaredFields()));
            }
        } while ((c = c.getSuperclass()) != null);
        return fields;
    }
    
    /**
     * Gets the getter methods for the bean properties of a class
     *
     * @param c the Class
     * @return an array of the getter methods
     * @throws IntrospectionException if an error occurs
     */
    public static Method[] getGetters(Class c) throws IntrospectionException {
        PropertyDescriptor[] pd = Introspector.getBeanInfo(c).getPropertyDescriptors();
        Collection getters = new HashSet();
        for (int i = 0; i < pd.length; i++) {
            Method getter = pd[i].getReadMethod();
            if (!getter.getName().equals("getClass")) {
                getters.add(getter);
            }
        }
        return (Method[]) getters.toArray(new Method[] {});
    }

    /**
     * Get the type of the elements of a collection
     *
     * @param col the collection
     * @return the Class of the elements of the collection
     */
    public static Class getElementType(Collection col) {
        if (col == null) {
            throw new NullPointerException("Collection cannot be null");
        }
{'fraction_non_alphanumeric': 0.09084956621378294, 'fraction_numerical': 0.0004910787362907187, 'mean_word_length': 2.147861926841834, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3515866', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1633, 'n_words': 596}