Description

This library provides methods to search for classes without using reflection, in fact, without even loading the classes. This is particularly important when the class has static initializers that cause behavior that cannot be undone. Additionally, framework libraries don't always have apriori knowledge of all classes which must be managed.

Find All Implementors of an Interface

You can use the following code snippet to find all implementors of an interface.

        MetaClassLoader classLoader = MetaRegistry.SINGLETON.getClassLoader(getClass().getClassLoader());
        Collection<? extends MetaClass> implementations = classLoader.findAllImplementors(FullInterface.class.getCanonicalName());

Find All Classes in a jar with a Annotation

You can use the following code snippets to find all classes with a particular annotation in a jar with a marker META-INF file.

    
    public void findAnnotatedClassesFromJar(MetaClassLoader classLoader) {
        for(MetaClassLocation location : classLoader.getClassLocationsWithMetaFile("persistence.xml")) {
            findAnnotatedClassesFromLocation(location);
        }
    }

    private void findAnnotatedClassesFromLocation(MetaClassLocation location) {
        for(MetaClass mc : location.getMetaClasses()) {
            MetaAnnotation ma = mc.getAnnotation("javax.persistence.Entity");
            if(ma!=null) {
                workOnClass(mc);
            }
        }
    }