Example usage for java.lang Class getDeclaredClasses

List of usage examples for java.lang Class getDeclaredClasses

Introduction

In this page you can find the example usage for java.lang Class getDeclaredClasses.

Prototype

@CallerSensitive
public Class<?>[] getDeclaredClasses() throws SecurityException 

Source Link

Document

Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    Class cls = Class.forName("MyClass");

    Class[] classes = cls.getDeclaredClasses();
    for (int i = 0; i < classes.length; i++) {
        System.out.println("Class = " + classes[i].getName());
    }//from   ww  w.  j a  v a2  s.  c  o  m

}

From source file:Main.java

public static void printMemberClasses(final Class dataType) {
    final Class[] nestedClasses = dataType.getClasses();
    final Class[] declaredNestedClasses = dataType.getDeclaredClasses();
    final Class[] nestedInterfaces = dataType.getInterfaces();
    final Class declaringClass = dataType.getDeclaringClass();

    System.out.println("Member Class infor for: " + dataType.getName());
    System.out.println("Nested Classes: " + Arrays.asList(nestedClasses));
    System.out.println("Declared Nested Classes: " + Arrays.asList(declaredNestedClasses));
    System.out.println("Interfaces: " + Arrays.asList(nestedInterfaces));
    System.out.println("Declaring Class: " + declaringClass);
}

From source file:Main.java

/**
 * Because of a BUG of Android (API 13-),
 * get signature info by using "getPackageArchiveInfo" of "PackageManager"
 * always causes "NullPointerException".
 * Lack of code in method "getPackageArchiveInfo":
 *     if ((flags & GET_SIGNATURES) != 0)
 *     {/*from   ww  w. j  ava 2s . com*/
 *         packageParser.collectCertificates(pkg, 0);
 *     }
 */
@SuppressWarnings("unchecked")
public static PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) {
    try {
        Class packageParserClass = Class.forName("android.content.pm.PackageParser");
        Class[] innerClasses = packageParserClass.getDeclaredClasses();
        Class packageParserPackageClass = null;
        for (Class innerClass : innerClasses) {
            if (0 == innerClass.getName().compareTo("android.content.pm.PackageParser$Package")) {
                packageParserPackageClass = innerClass;
                break;
            }
        }
        Constructor packageParserConstructor = packageParserClass.getConstructor(String.class);
        Method parsePackageMethod = packageParserClass.getDeclaredMethod("parsePackage", File.class,
                String.class, DisplayMetrics.class, int.class);
        Method collectCertificatesMethod = packageParserClass.getDeclaredMethod("collectCertificates",
                packageParserPackageClass, int.class);
        Method generatePackageInfoMethod = packageParserClass.getDeclaredMethod("generatePackageInfo",
                packageParserPackageClass, int[].class, int.class, long.class, long.class);
        packageParserConstructor.setAccessible(true);
        parsePackageMethod.setAccessible(true);
        collectCertificatesMethod.setAccessible(true);
        generatePackageInfoMethod.setAccessible(true);

        Object packageParser = packageParserConstructor.newInstance(archiveFilePath);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        displayMetrics.setToDefaults();

        final File sourceFile = new File(archiveFilePath);

        Object pkg = parsePackageMethod.invoke(packageParser, sourceFile, archiveFilePath, displayMetrics, 0);
        if (pkg == null) {
            return null;
        }

        if ((flags & PackageManager.GET_SIGNATURES) != 0) {
            collectCertificatesMethod.invoke(packageParser, pkg, 0);
        }

        return (PackageInfo) generatePackageInfoMethod.invoke(null, pkg, null, flags, 0, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static <T> T getPrivateConstantField(Class<?> outerClass, String innerClassName, String fieldname,
        Class<T> returnType) throws Exception {
    // Get all inner classes
    Class<?> innerClasses[] = outerClass.getDeclaredClasses();
    // find the inner class that matches the order
    Class<?> innerClass = null;

    for (int index = 0; index < innerClasses.length; index++) {
        if (innerClassName.equals(innerClasses[index].getSimpleName())) {
            innerClass = innerClasses[index];
        }/*from  w  w w .j av  a 2  s.  c  om*/
    }
    T returnValue = null;
    if (innerClass != null) {
        Field field;
        field = innerClass.getDeclaredField(fieldname);
        field.setAccessible(true);
        returnValue = (T) field.get(innerClass);
    }
    return returnValue;
}

From source file:Main.java

public static Object getNewObject(Class<?> outerClass, String innerClassName, Class<?> parameterTypes[],
        Object parameters[]) throws Exception {
    // Get all inner classes
    Class<?> innerClasses[] = outerClass.getDeclaredClasses();
    // find the inner class that matches the order
    Constructor<?> constructor = null;
    for (int index = 0; index < innerClasses.length; index++) {
        if (innerClassName.equals(innerClasses[index].getSimpleName())) {
            constructor = innerClasses[index].getConstructor(parameterTypes);
        }//  w ww.  j  a v a  2 s .  c  o m
    }
    if (constructor != null) {
        constructor.setAccessible(true);
        Object obj = constructor.newInstance(parameters);
        return obj;
    }
    return null;
}

From source file:Main.java

public static <T> T getPrivateMethod(Class<?> outerClass, String innerClassName, Object obj, String methodName,
        Class<?> parameterTypes[], Object parameters[], Class<T> returnType) throws Exception {
    // Get all inner classes
    Class<?> innerClasses[] = outerClass.getDeclaredClasses();
    // find the inner class that matches the order
    Class<?> innerClass = null;

    for (int index = 0; index < innerClasses.length; index++) {
        if (innerClassName.equals(innerClasses[index].getSimpleName())) {
            innerClass = innerClasses[index];
        }//w  w w.  j a  v a2  s.c  om
    }
    T returnValue = null;
    if (innerClass != null) {
        Method method = innerClass.getDeclaredMethod(methodName, parameterTypes);
        method.setAccessible(true);
        returnValue = (T) method.invoke(obj, parameters);
    }
    return returnValue;
}

From source file:org.springframework.test.context.support.AnnotationConfigContextLoaderUtils.java

/**
 * Detect the default configuration classes for the supplied test class.
 * <p>The returned class array will contain all static nested classes of
 * the supplied class that meet the requirements for {@code @Configuration}
 * class implementations as specified in the documentation for
 * {@link Configuration @Configuration}.
 * <p>The implementation of this method adheres to the contract defined in the
 * {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
 * SPI. Specifically, this method uses introspection to detect default
 * configuration classes that comply with the constraints required of
 * {@code @Configuration} class implementations. If a potential candidate
 * configuration class does not meet these requirements, this method will log a
 * debug message, and the potential candidate class will be ignored.
 * @param declaringClass the test class that declared {@code @ContextConfiguration}
 * @return an array of default configuration classes, potentially empty but
 * never {@code null}/*w  w  w.ja  va2s.c o  m*/
 */
public static Class<?>[] detectDefaultConfigurationClasses(Class<?> declaringClass) {
    Assert.notNull(declaringClass, "Declaring class must not be null");

    List<Class<?>> configClasses = new ArrayList<>();

    for (Class<?> candidate : declaringClass.getDeclaredClasses()) {
        if (isDefaultConfigurationClassCandidate(candidate)) {
            configClasses.add(candidate);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug(String.format(
                        "Ignoring class [%s]; it must be static, non-private, non-final, and annotated "
                                + "with @Configuration to be considered a default configuration class.",
                        candidate.getName()));
            }
        }
    }

    if (configClasses.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info(String.format(
                    "Could not detect default configuration classes for test class [%s]: "
                            + "%s does not declare any static, non-private, non-final, nested classes "
                            + "annotated with @Configuration.",
                    declaringClass.getName(), declaringClass.getSimpleName()));
        }
    }

    return configClasses.toArray(new Class<?>[configClasses.size()]);
}

From source file:org.copperengine.ext.wfrepo.classpath.ClasspathWorkflowRepository.java

static Set<Class<?>> findWorkflowClasses(final List<String> wfPackages, final ClassLoader cl) throws Exception {
    final ClassPath cp = ClassPath.from(cl);
    final Set<Class<?>> set = new HashSet<Class<?>>();
    for (String wfPackage : wfPackages) {
        final ImmutableSet<com.google.common.reflect.ClassPath.ClassInfo> x = cp
                .getTopLevelClassesRecursive(wfPackage);
        for (com.google.common.reflect.ClassPath.ClassInfo ci : x) {
            final Class<?> c = cl.loadClass(ci.getName());
            set.add(c);/*from  www .j a  v  a  2s . c  o  m*/
            set.addAll(Arrays.asList(c.getDeclaredClasses()));
            loadAnonymousInnerClasses(cl, set, c);
        }
    }
    return set;
}

From source file:com.palantir.ptoss.util.Reflections.java

/**
 * Gets all inner classes from a given class that are assignable from the target class.
 * @param klass type to query for inner-classes.
 * @param targetClass interface or class that inner classes must be assignable from to be
 * returned.//w  ww . j a  va2s .  c o  m
 * @return all inner classes in <code>klass</code> that are assignable from
 * <code>targetClass</code>
 * @see Class#isAssignableFrom(Class)
 * @see Class#getDeclaredClasses()
 */
public static List<Class<?>> getTypesOfType(Class<?> klass, Class<?> targetClass) {
    List<Class<?>> classes = Lists.newArrayList();
    for (Class<?> cl : klass.getDeclaredClasses()) {
        if (targetClass.isAssignableFrom(cl)) {
            classes.add(cl);
        }
    }
    return classes;
}

From source file:org.bonitasoft.engine.io.IOUtil.java

public static Map<String, byte[]> getResources(final Class<?>... classes) throws IOException {
    if (classes == null || classes.length == 0) {
        final String message = "No classes available";
        throw new IOException(message);
    }/*from w  w  w .j a  v a 2 s .com*/
    final Map<String, byte[]> resources = new HashMap<>();
    for (final Class<?> clazz : classes) {
        resources.put(clazz.getName().replace(".", "/") + ".class", getClassData(clazz));
        for (final Class<?> internalClass : clazz.getDeclaredClasses()) {
            resources.put(internalClass.getName().replace(".", "/") + ".class", getClassData(internalClass));
        }
    }
    return resources;
}