get Collection Type - Android java.util

Android examples for java.util:Collection

Description

get Collection Type

Demo Code

/* /*from  w  ww. ja v  a  2s .c  o  m*/
 * Cleandroid Framework 
 * @author: Douraid Arfaoui <douraid.arfaoui@gmail.com>
 *
 * Copyright (c) 2015, Douraid Arfaoui, or third-party contributors as 
 * indicated by the @author tags or express copyright attribution 
 * statements applied by the authors. 
 * 
 * This copyrighted material is made available to anyone wishing to use, modify, 
 * copy, or redistribute it subject to the terms and conditions of the Apache 2 
 * License, as published by the Apache Software Foundation.  
 * 
 */
//package com.java2s;

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

public class Main {
    public static Class<?> getCollectionType(Field field) {
        if (field.getGenericType() instanceof ParameterizedType) {
            ParameterizedType type = (ParameterizedType) field
                    .getGenericType();
            return (Class<?>) type.getActualTypeArguments()[0];
        }
        return Object.class;
    }

    public static Class<?> getCollectionType(Method method) {
        if (method.getGenericReturnType() instanceof ParameterizedType) {
            ParameterizedType type = (ParameterizedType) method
                    .getGenericReturnType();
            return (Class<?>) type.getActualTypeArguments()[0];
        }
        return Object.class;
    }
}

Related Tutorials