Java Reflection Super Class getParents(final Class Class)

Here you can find the source of getParents(final Class Class)

Description

get Parents

License

Open Source License

Declaration

public static Class<?>[] getParents(final Class<?> Class) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.HashMap;

import java.util.List;

public class Main {
    private static final HashMap<Class<?>, Class<?>[]> cachedClass = new HashMap<Class<?>, Class<?>[]>();
    private static final HashMap<Class<?>, HashMap<Class<?>, Class<?>[]>> cachedClassWithDelimiter = new HashMap<Class<?>, HashMap<Class<?>, Class<?>[]>>();

    public static Class<?>[] getParents(final Class<?> Class) {
        return getParents(Class, null);
    }// w  ww. j  ava2s .  co  m

    public static Class<?>[] getParents(final Class<?> Class, Class<?> delimiter) {
        HashMap<Class<?>, Class<?>[]> classHash;
        final Class<?> index;
        if (delimiter != null) {
            if ((classHash = cachedClassWithDelimiter.get(Class)) == null)
                cachedClassWithDelimiter.put(Class, classHash = new HashMap<Class<?>, Class<?>[]>());

            index = delimiter;
        } else {
            classHash = cachedClass;
            delimiter = Object.class;
            index = Class;
        }

        Class<?>[] classes;
        if ((classes = classHash.get(index)) == null) {
            final List<Class<?>> _classes = new ArrayList<Class<?>>();

            Class<?> parent = Class;
            while ((parent = parent.getSuperclass()) != delimiter && parent != null)
                _classes.add(parent);

            classes = _classes.toArray(new Class<?>[_classes.size()]);
            classHash.put(index, classes);
        }

        return classes;
    }
}

Related

  1. getAllSuperclasses(Class cls)
  2. getAllSuperclasses(Class clazz)
  3. getAllSuperClasses(Class clazz)
  4. getAllSuperClasses(Class clz)
  5. getAllSuperTypeNames(Class aClass)
  6. getSuper(Class clazz, Set result)
  7. getSuperClasses(Class clazz, Set result)
  8. getSuperClasses(Class clz, boolean reverseOrder)
  9. getSuperclasses(Class from)