Example usage for java.lang Class getClasses

List of usage examples for java.lang Class getClasses

Introduction

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

Prototype

@CallerSensitive
public Class<?>[] getClasses() 

Source Link

Document

Returns an array containing Class objects representing all the public classes and interfaces that are 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("java.lang.String");

    Class[] classes = cls.getClasses();
    for (int i = 0; i < classes.length; i++) {
        System.out.println("Class found = " + classes[i].getName());
    }/*from w ww  . j a  v  a  2s .  co m*/

}

From source file:Main.java

public static void main(String[] args) {
    Class cls = Thread.class;
    System.out.println("Class = " + cls.getName());

    Class[] c = cls.getClasses();
    System.out.println("Classes = " + Arrays.asList(c));

    // returns an array of interfaces
    Class[] i = cls.getInterfaces();
    System.out.println("Interfaces = " + Arrays.asList(i));
}

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

public static int getIdByName(Context context, String className, String name) {
    String packageName = context.getPackageName();
    Class r = null;
    int id = 0;/*from w w  w  .ja v a 2  s . c o  m*/
    try {
        r = Class.forName(packageName + ".R");

        Class[] classes = r.getClasses();
        Class desireClass = null;

        for (int i = 0; i < classes.length; ++i) {
            if (classes[i].getName().split("\\$")[1].equals(className)) {
                desireClass = classes[i];
                break;
            }
        }
        if (classes.length == 0) {
            desireClass = Class.forName(packageName + ".R$" + className);
        }
        if (desireClass != null)
            id = desireClass.getField(name).getInt(desireClass);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }

    return id;
}

From source file:com.medigy.persist.util.HibernateUtil.java

/**
 * Creates a map of all classes implementing the {@link com.medigy.persist.reference.custom.CustomReferenceEntity}
 * interface and the respective cached enums
 *
 * @return/*  w w  w.  j  a  v a2 s .  c o m*/
 */
protected static Map<Class, Class<? extends CachedCustomReferenceEntity>> getCustomReferenceEntitiesAndRespectiveEnums(
        final Iterator classMappings) {
    final Map<Class, Class<? extends CachedCustomReferenceEntity>> customReferenceEntitiesAndCachesMap = new HashMap<Class, Class<? extends CachedCustomReferenceEntity>>();
    while (classMappings.hasNext()) {
        Class aClass = ((PersistentClass) classMappings.next()).getMappedClass(); //(Class) classMappings.next();
        if (CustomReferenceEntity.class.isAssignableFrom(aClass)) {
            for (final Class ic : aClass.getClasses()) {
                if (CachedCustomReferenceEntity.class.isAssignableFrom(ic)) {
                    if (ic.isEnum()) {
                        customReferenceEntitiesAndCachesMap.put(aClass, ic);
                    } else
                        throw new HibernateException(ic + " must be an enum since " + aClass + " is a "
                                + CachedCustomReferenceEntity.class.getName());

                    break;
                }
            }
            // if no cache is found, its ok since these are custom
        }
    }
    return customReferenceEntitiesAndCachesMap;
}

From source file:com.medigy.persist.util.HibernateUtil.java

protected static Map<Class, Class<? extends CachedReferenceEntity>> getReferenceEntitiesAndRespectiveEnums(
        final Iterator classMappings) {
    Map<Class, Class<? extends CachedReferenceEntity>> referenceEntitiesAndCachesMap = new HashMap<Class, Class<? extends CachedReferenceEntity>>();
    while (classMappings.hasNext()) {
        Class aClass = ((PersistentClass) classMappings.next()).getMappedClass(); //(Class) classMappings.next();
        if (ReferenceEntity.class.isAssignableFrom(aClass)) {
            boolean foundCache = false;
            for (final Class ic : aClass.getClasses()) {
                if (CachedReferenceEntity.class.isAssignableFrom(ic)) {
                    if (ic.isEnum()) {
                        referenceEntitiesAndCachesMap.put(aClass, ic);
                        foundCache = true;
                    } else
                        throw new HibernateException(ic + " must be an enum since " + aClass + " is a "
                                + ReferenceEntity.class.getName());

                    break;
                }//from   ww  w. j  a v  a2 s  .  c om

            }

            if (!foundCache)
                log.warn(aClass
                        + " is marked as a ReferenceEntity but does not contain a ReferenceEntityCache enum.");

            // TODO: find out how to ensure the new mapping for reference type is immutable and read only
            // final PersistentClass pClass = getClassMapping(aClass.getLabel());
        }
    }
    return referenceEntitiesAndCachesMap;

}

From source file:com.medallia.spider.api.StRenderer.java

/** @return the interface declared within the given class which is also annotated with the given annotation */
private static <X extends Annotation> Class<X> findInterfaceWithAnnotation(Map<Class<?>, Class<?>> methodMap,
        Class<?> clazz, Class<? extends Annotation> annotation) {
    Class<?> annotatedInterface = methodMap.get(clazz);
    if (annotatedInterface == null) {
        for (Class<?> c : CollUtils.concat(Arrays.asList(clazz.getClasses()),
                Arrays.asList(clazz.getDeclaredClasses()))) {
            Annotation i = c.getAnnotation(annotation);
            if (i != null) {
                annotatedInterface = c;/*from   w  w  w .  j a v a2 s . c  o  m*/
                methodMap.put(clazz, annotatedInterface);
                break;
            }
        }
    }
    @SuppressWarnings("unchecked")
    Class<X> x = (Class<X>) annotatedInterface;
    return x;
}

From source file:org.jasig.cas.util.annotation.AbstractAnnotationBeanPostProcessor.java

public final Object postProcessBeforeInitialization(final Object bean, final String beanName)
        throws BeansException {

    final List<Field> fields = new ArrayList<Field>();
    final Class<?> clazz = bean.getClass();
    final Class<?>[] classes = clazz.getClasses();

    addDeclaredFields(clazz, fields);//from ww  w  . j  ava2s. c  o m

    for (int i = 0; i < classes.length; i++) {
        addDeclaredFields(classes[i], fields);
    }

    try {
        for (final Field field : fields) {
            final boolean originalValue = field.isAccessible();
            field.setAccessible(true);
            final Annotation annotation = field.getAnnotation(getSupportedAnnotation());

            if (annotation != null) {
                processField(field, annotation, bean, beanName);
            }

            field.setAccessible(originalValue);
        }
    } catch (final IllegalAccessException e) {
        log.warn("Could not access field: " + e.getMessage(), e);
    }

    return bean;
}

From source file:org.jboss.arquillian.spring.integration.javaconfig.utils.DefaultConfigurationClassesProcessor.java

/**
 * Tries to find all default configuration classes for given testClass
 *
 * @param testClass - test class to check
 *
 * @return - list of all inner static classes marked with @Configuration annotation
 *///from  w w w.ja v  a2 s .c  o  m
private Class<?>[] defaultConfigurationClasses(Class testClass) {
    Class<?>[] configurationCandidates = testClass.getClasses();
    Set<Class<?>> configurationClasses = new HashSet<Class<?>>();

    for (Class<?> configurationCandidate : configurationCandidates) {
        if (configurationCandidate.isAnnotationPresent(Configuration.class)) {
            validateConfigurationCandidate(configurationCandidate);
            configurationClasses.add(configurationCandidate);
        }
    }

    return configurationClasses.toArray(new Class<?>[0]);
}

From source file:hu.bme.mit.sette.common.validator.reflection.ClassValidator.java

/**
 * Sets the required member class count for the Java class.
 *
 * @param memberClassCount/*from   w  w  w  .  j  av  a 2  s .c  o  m*/
 *            the required member class count for the Java class.
 * @return this object
 */
public ClassValidator memberClassCount(final int memberClassCount) {
    Validate.isTrue(memberClassCount >= 0,
            "The required member class count must be " + "a non-negative number");

    if (getSubject() != null) {
        Class<?> javaClass = getSubject();

        if (javaClass.getClasses().length != memberClassCount) {
            if (memberClassCount == 0) {
                this.addException("The Java class must not have " + "any member classes");
            } else if (memberClassCount == 1) {
                this.addException("The Java class must have " + "exactly 1 member class");
            } else {
                this.addException(String.format("The Java class must have " + "exactly %d member classes",
                        memberClassCount));
            }
        }
    }

    return this;
}