Java Class Load getClasses(Class infoClass)

Here you can find the source of getClasses(Class infoClass)

Description

Get classes for a class.

License

Open Source License

Parameter

Parameter Description
infoClass Class for which classes are wanted.

Return

String array of classes.

Declaration


public static String[] getClasses(Class infoClass) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

import java.util.*;

public class Main {
    /** Get classes for a class.
     *//from   w w  w  .  ja  v a 2 s  . co m
     *   @param    infoClass    Class for which classes are wanted.
     *
     *   @return            String array of classes.
     *
     */

    public static String[] getClasses(Class infoClass) {
        String result[] = null;
        Object[] classes = null;

        // Get list of internal classes, both
        // public and private.

        classes = infoClass.getDeclaredClasses();

        // Return null result if no classes.

        if ((classes == null) || (classes.length == 0))
            return result;

        // Sort classes in ascending order by class name.

        Comparator comparator = new Comparator() {
            public int compare(Object o1, Object o2) {
                return o1.toString().compareTo(o2.toString());
            }
        };

        Arrays.sort(classes, comparator);

        result = new String[classes.length];

        // Create "result" array containing list
        // of class names.

        for (int i = 0; i < classes.length; i++) {
            Object aClass = classes[i];

            // Throw away the leading "class" etc.
            // modifier.

            result[i] = trimClassName(aClass.toString());

            int firstBlank = result[i].indexOf(' ');

            if (firstBlank != -1) {
                result[i] = result[i].substring(firstBlank + 1);
            }
        }
        // Return string array of class names.
        return result;
    }

    /**
     *   Removes class modifiers, leaving only the class name.
     *
     *   @param   name   The class name plus modifiers, if any.
     *
     *   @return         The class name stripped of modifiers.
     *
     */

    public static String trimClassName(String name) {
        String result;
        // Find last period in class name.

        int lastPeriod = name.lastIndexOf('.');

        // If found, strip everything
        // before it.  The result is the
        // unmodified class name.
        // e.g.,
        // a.b.c.simple -> simple .
        if (lastPeriod != -1) {
            result = name.substring(lastPeriod + 1);
        } else {
            result = name;
        }
        // Return trimmed class name.
        return result;
    }
}

Related

  1. getClass(String fullClassName)
  2. getClass(String name)
  3. getClass(String name)
  4. getClass(String upnpDataType)
  5. getClassDescriptor(Class type)
  6. getClasses(Class t)
  7. getClasses(Object... objects)
  8. getClasses(String input)
  9. getClassesFromPaths(String[] classpaths)