Java Reflection Interface Get getInterfacesForClass(Class type)

Here you can find the source of getInterfacesForClass(Class type)

Description

A simple but efficient method for getting the interfaces for a class type, this has some shortcuts for the common types like maps, lists, etc.
Only returns the interfaces for the current type and not for all nested types

License

Apache License

Parameter

Parameter Description
type any class type

Return

the list of interfaces (empty if none)

Declaration

public static List<Class<?>> getInterfacesForClass(Class<?> type) 

Method Source Code

//package com.java2s;
/**//from  www.  j a v  a2 s.  c o  m
 * $Id: ConstructorUtils.java 61 2009-09-25 11:14:16Z azeckoski $
 * $URL: http://reflectutils.googlecode.com/svn/trunk/src/main/java/org/azeckoski/reflectutils/ConstructorUtils.java $
 * FieldUtils.java - genericdao - May 19, 2008 10:10:15 PM - azeckoski
 **************************************************************************
 * Copyright (c) 2008 Aaron Zeckoski
 * Licensed under the Apache License, Version 2.0
 * 
 * A copy of the Apache License has been included in this 
 * distribution and is available at: http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * Aaron Zeckoski (azeckoski @ gmail.com) (aaronz @ vt.edu) (aaron @ caret.cam.ac.uk)
 */

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    /**
     * A simple but efficient method for getting the interfaces for a class type,
     * this has some shortcuts for the common types like maps, lists, etc.<br/>
     * Only returns the interfaces for the current type and not for all nested types
     * 
     * @param type any class type
     * @return the list of interfaces (empty if none)
     */
    public static List<Class<?>> getInterfacesForClass(Class<?> type) {
        ArrayList<Class<?>> interfaces = new ArrayList<Class<?>>();
        // find the actual interfaces from the class itself
        for (Class<?> iface : type.getInterfaces()) {
            interfaces.add(iface);
        }
        // add in the collection interface if this is a collection
        if (isClassCollection(type)) {
            if (isClassList(type)) {
                interfaces.add(List.class);
            } else if (Set.class.isAssignableFrom(type)) {
                interfaces.add(Set.class);
            }
            interfaces.add(Collection.class);
        } else if (isClassMap(type)) {
            interfaces.add(Map.class);
        }
        return interfaces;
    }

    /**
     * @param type any class
     * @return true if this class is a collection (e.g. {@link Collection}, {@link HashSet}, {@link Vector})
     */
    public static boolean isClassCollection(Class<?> type) {
        checkNull(type);
        boolean collection = false;
        if (Collection.class.isAssignableFrom(type)) {
            collection = true;
        }
        return collection;
    }

    /**
     * @param type any class
     * @return true if this class is a list (e.g. {@link List}, {@link ArrayList})
     */
    public static boolean isClassList(Class<?> type) {
        checkNull(type);
        boolean list = false;
        if (List.class.isAssignableFrom(type)) {
            list = true;
        }
        return list;
    }

    /**
     * @param type any class
     * @return true if this class is a map (e.g. {@link Map}, {@link HashMap})
     */
    public static boolean isClassMap(Class<?> type) {
        checkNull(type);
        boolean collection = false;
        if (Map.class.isAssignableFrom(type)) {
            collection = true;
        }
        return collection;
    }

    private static void checkNull(Class<?> type) {
        if (type == null) {
            throw new IllegalArgumentException("class type cannot be null to check the type");
        }
    }
}

Related

  1. getInterfaces(Class c)
  2. getInterfaces(Class clazz)
  3. getInterfaces(Class clazz)
  4. getInterfaces(final Class type)
  5. getInterfaces(Object owner, String[] interfaceStrings)
  6. getInterfacesList(Class clazz)
  7. getParentAllInterfaces(Class pojoClass)
  8. getPortNameAndSuffixFromInterfaceName( String intfName)