get Field Type - Java Reflection

Java examples for Reflection:Field Get

Description

get Field Type

Demo Code


//package com.java2s;
import java.lang.reflect.*;

import java.util.HashMap;

public class Main {
    private static final HashMap<String, Class<?>> primitives = new HashMap<String, Class<?>>();

    public static Class<?> getFieldType(final Class<?> fieldContainer,
            final String fieldName) {
        Class<?> t = null;/*w  ww  .  j a v a  2 s.c o  m*/
        final Field[] fields = fieldContainer.getDeclaredFields();
        for (final Field field : fields)
            if (field.getName().equalsIgnoreCase(fieldName)) {
                if (field.getType().isPrimitive())
                    t = primitives.get(field.getType().getName());
                else
                    t = field.getType();
                break;
            }
        return t;
    }
}

Related Tutorials