package org.flymine.util;

import java.lang.reflect.Field;

/**
 * Provides utility methods for working with Java types and reflection
 *
 * @author Mark Woodbridge
 * @author Richard Smith
 */
public class TypeUtil
{
    private TypeUtil() {
    }

    /**
     * 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
     */ 
    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;
    }

    /**
     * Returns the container class for a given primitive type
     *
     * @param c one of the 8 primitive types
     * @return the corresponding container class
     */
    public static Class toContainerType(Class c) {
        if (c.equals(Integer.TYPE)) {
            return Integer.class;
        }
        if (c.equals(Boolean.TYPE)) {
            return Boolean.class;
        }
        if (c.equals(Double.TYPE)) {
            return Double.class;
        }
        if (c.equals(Float.TYPE)) {
            return Float.class;
        }
        if (c.equals(Long.TYPE)) {
            return Long.class;
        }
        if (c.equals(Short.TYPE)) {
            return Short.class;
        }
        if (c.equals(Byte.TYPE)) {
            return Byte.class;
        }
        if (c.equals(Character.TYPE)) {
            return Character.class;
        }
        return c;
    }
}

@@ -20,16 +20,96 @@
 .util;%0A%0A
+import java.util.Arrays;%0Aimport java.util.Collection;%0Aimport java.util.HashSet;%0A
 import j
@@ -905,32 +905,494 @@
 eturn f;%0A    %7D%0A%0A
+    /**%0A     * Gets the Fields of a Class%0A     *%0A     * @param c the Class%0A     * @return the fields in this class%0A     */%0A    public static Collection getFields(Class c) %7B%0A        Collection fields = new HashSet();%0A        do %7B%0A            for (int i = 0; i %3C c.getDeclaredFields().length; i++) %7B%0A                fields.addAll(Arrays.asList(c.getDeclaredFields()));%0A            %7D%0A        %7D while ((c = c.getSuperclass()) != null);%0A        return fields;%0A    %7D%0A%0A
     /**%0A     * R

add getFields to return all fields of a class