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:org.lockss.devtools.plugindef.EditableDefinablePlugin.java

Collection getKnownCacheExceptions() {
    HashSet exceptions = new HashSet();
    // always add cache success
    exceptions.add(CacheSuccess.class.getName());
    Class ce = CacheException.class;
    Class[] ce_classes = ce.getDeclaredClasses();
    for (int ic = 0; ic < ce_classes.length; ic++) {
        try {// w w w.  j ava2 s . c o  m
            if (ce_classes[ic].newInstance() instanceof CacheException) {
                exceptions.add(ce_classes[ic].getName());
            }
        } catch (IllegalAccessException ex) {
        } catch (InstantiationException ex) {
        }
    }
    return exceptions;
}

From source file:com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.java

public void register(ContainerBuilder containerBuilder, LocatableProperties props)
        throws ConfigurationException {
    if (LOG.isInfoEnabled()) {
        LOG.info("Parsing configuration file [" + configFileName + "]");
    }//  ww w  .j  av a  2s. c o m
    Map<String, Node> loadedBeans = new HashMap<String, Node>();
    for (Document doc : documents) {
        Element rootElement = doc.getDocumentElement();
        NodeList children = rootElement.getChildNodes();
        int childSize = children.getLength();

        for (int i = 0; i < childSize; i++) {
            Node childNode = children.item(i);

            if (childNode instanceof Element) {
                Element child = (Element) childNode;

                final String nodeName = child.getNodeName();

                if ("bean".equals(nodeName)) {
                    String type = child.getAttribute("type");
                    String name = child.getAttribute("name");
                    String impl = child.getAttribute("class");
                    String onlyStatic = child.getAttribute("static");
                    String scopeStr = child.getAttribute("scope");
                    boolean optional = "true".equals(child.getAttribute("optional"));
                    Scope scope = Scope.SINGLETON;
                    if ("default".equals(scopeStr)) {
                        scope = Scope.DEFAULT;
                    } else if ("request".equals(scopeStr)) {
                        scope = Scope.REQUEST;
                    } else if ("session".equals(scopeStr)) {
                        scope = Scope.SESSION;
                    } else if ("singleton".equals(scopeStr)) {
                        scope = Scope.SINGLETON;
                    } else if ("thread".equals(scopeStr)) {
                        scope = Scope.THREAD;
                    }

                    if (StringUtils.isEmpty(name)) {
                        name = Container.DEFAULT_NAME;
                    }

                    try {
                        Class cimpl = ClassLoaderUtil.loadClass(impl, getClass());
                        Class ctype = cimpl;
                        if (StringUtils.isNotEmpty(type)) {
                            ctype = ClassLoaderUtil.loadClass(type, getClass());
                        }
                        if ("true".equals(onlyStatic)) {
                            // Force loading of class to detect no class def found exceptions
                            cimpl.getDeclaredClasses();
                            containerBuilder.injectStatics(cimpl);
                        } else {
                            if (containerBuilder.contains(ctype, name)) {
                                Location loc = LocationUtils
                                        .getLocation(loadedBeans.get(ctype.getName() + name));
                                if (throwExceptionOnDuplicateBeans) {
                                    throw new ConfigurationException("Bean type " + ctype + " with the name "
                                            + name + " has already been loaded by " + loc, child);
                                }
                            }

                            // Force loading of class to detect no class def found exceptions
                            cimpl.getDeclaredConstructors();

                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Loaded type:" + type + " name:" + name + " impl:" + impl);
                            }
                            containerBuilder.factory(ctype, name,
                                    new LocatableFactory(name, ctype, cimpl, scope, childNode), scope);
                        }
                        loadedBeans.put(ctype.getName() + name, child);
                    } catch (Throwable ex) {
                        if (!optional) {
                            throw new ConfigurationException(
                                    "Unable to load bean: type:" + type + " class:" + impl, ex, childNode);
                        } else {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Unable to load optional class: #0", impl);
                            }
                        }
                    }
                } else if ("constant".equals(nodeName)) {
                    String name = child.getAttribute("name");
                    String value = child.getAttribute("value");
                    props.setProperty(name, value, childNode);
                } else if (nodeName.equals("unknown-handler-stack")) {
                    List<UnknownHandlerConfig> unknownHandlerStack = new ArrayList<UnknownHandlerConfig>();
                    NodeList unknownHandlers = child.getElementsByTagName("unknown-handler-ref");
                    int unknownHandlersSize = unknownHandlers.getLength();

                    for (int k = 0; k < unknownHandlersSize; k++) {
                        Element unknownHandler = (Element) unknownHandlers.item(k);
                        unknownHandlerStack.add(new UnknownHandlerConfig(unknownHandler.getAttribute("name")));
                    }

                    if (!unknownHandlerStack.isEmpty())
                        configuration.setUnknownHandlerStack(unknownHandlerStack);
                }
            }
        }
    }
}

From source file:org.evosuite.setup.TestClusterGenerator.java

private void addDeclaredClasses(Set<Class<?>> targetClasses, Class<?> currentClass) {
    for (Class<?> c : currentClass.getDeclaredClasses()) {
        logger.info("Adding declared class {}", c);
        targetClasses.add(c);//from ww w . j a  va  2s  .c  om
        addDeclaredClasses(targetClasses, c);
    }
}

From source file:se.crisp.codekvast.agent.daemon.codebase.CodeBaseScanner.java

void findTrackedMethods(CodeBase codeBase, Set<String> packages, Set<String> excludePackages, Class<?> clazz) {
    if (clazz.isInterface()) {
        log.debug("Ignoring interface {}", clazz);
        return;//from w ww.j ava2 s  .c om
    }

    log.debug("Analyzing {}", clazz);
    MethodAnalyzer methodAnalyzer = codeBase.getConfig().getMethodAnalyzer();
    try {
        Method[] declaredMethods = clazz.getDeclaredMethods();
        Method[] methods = clazz.getMethods();
        Method[] allMethods = new Method[declaredMethods.length + methods.length];
        System.arraycopy(declaredMethods, 0, allMethods, 0, declaredMethods.length);
        System.arraycopy(methods, 0, allMethods, declaredMethods.length, methods.length);

        for (Method method : allMethods) {
            SignatureStatus status = methodAnalyzer.apply(method);

            // Some AOP frameworks (e.g., Guice) push methods from a base class down to subclasses created in runtime.
            // We need to map those back to the original declaring signature, or else the original declared method will look unused.

            MethodSignature thisSignature = SignatureUtils.makeMethodSignature(clazz, method);

            MethodSignature declaringSignature = SignatureUtils.makeMethodSignature(
                    findDeclaringClass(method.getDeclaringClass(), method, packages), method);

            if (shouldExcludeSignature(declaringSignature, excludePackages)) {
                status = SignatureStatus.EXCLUDED_BY_PACKAGE_NAME;
            }
            codeBase.addSignature(thisSignature, declaringSignature, status);
        }

        for (Class<?> innerClass : clazz.getDeclaredClasses()) {
            findTrackedMethods(codeBase, packages, excludePackages, innerClass);
        }
    } catch (NoClassDefFoundError e) {
        log.warn("Cannot analyze {}: {}", clazz, e.toString());
    }
}