Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import java.util.Map;

public class Main {
    /**
     * Returns the generic class of multi-value objects.
     * 
     * @param p
     *          Field to examine
     * @return The Class<?> of generic type if any, otherwise null
     */
    public static Class<?> getGenericMultivalueType(final Field p) {
        if (p.getType() instanceof Class<?>) {
            final Type genericType = p.getGenericType();
            if (genericType != null && genericType instanceof ParameterizedType) {
                final ParameterizedType pt = (ParameterizedType) genericType;
                if (pt.getActualTypeArguments() != null && pt.getActualTypeArguments().length > 0) {
                    if (((Class<?>) pt.getRawType()).isAssignableFrom(Map.class)) {
                        if (pt.getActualTypeArguments()[1] instanceof Class<?>) {
                            return (Class<?>) pt.getActualTypeArguments()[1];
                        } else if (pt.getActualTypeArguments()[1] instanceof ParameterizedType)
                            return (Class<?>) ((ParameterizedType) pt.getActualTypeArguments()[1]).getRawType();
                    } else if (pt.getActualTypeArguments()[0] instanceof Class<?>) {
                        return (Class<?>) pt.getActualTypeArguments()[0];
                    } else if (pt.getActualTypeArguments()[0] instanceof ParameterizedType)
                        return (Class<?>) ((ParameterizedType) pt.getActualTypeArguments()[0]).getRawType();
                }
            } else if (p.getType().isArray())
                return p.getType().getComponentType();
        }
        return null;
    }
}