noob
noob

Reputation: 3

How to get all extended classes without libraries

Within my project I have a class City and other classes that extend it (London,Paris...). I want to instantiate all the classes that extend City (all the subclasses of City) without using any external library.

I want to avoid instantiating classes manually using the constructors like that:

List<City> cities = new ArrayList<City>();
cities.add( new London() );
cities.add( new California() );
cities.add( new Chicago() );

Classes:

public abstract Class City{
    public abstract String getName();
}
    
public Class London{
    public String getName(){
        return "London";
    }
}

public Class Paris{
    public String getName(){
        return "Paris";
    }
}

Upvotes: 0

Views: 210

Answers (1)

Oussama ZAGHDOUD
Oussama ZAGHDOUD

Reputation: 2169

This is a solution,it works but you can add some enhancement (make it work with indirect superClasses ..), the idea is : find all the classes in the project directory , iterate those classes and look for all classes that extends a given class (by name). After that instanciate all these classes :

package x;

import java.util.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.*;
import java.net.URL;

public class MyClass {

    /**
     * Scans all classes accessible from the context class loader which belong to
     * the given package and subpackages.
     *
     * @param packageName The base package
     * @return The classes
     * @throws ClassNotFoundException
     * @throws IOException
     */
    private static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        assert classLoader != null;
        String path = packageName.replace('.', '/');
        Enumeration<URL> resources = classLoader.getResources(path);
        List<File> dirs = new ArrayList<File>();
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            dirs.add(new File(resource.getFile()));
        }
        ArrayList<Class> classes = new ArrayList<Class>();
        for (File directory : dirs) {
            classes.addAll(findClasses(directory, packageName));
        }
        return classes.toArray(new Class[classes.size()]);
    }

    /**
     * Recursive method used to find all classes in a given directory and subdirs.
     *
     * @param directory   The base directory
     * @param packageName The package name for classes found inside the base
     *                    directory
     * @return The classes
     * @throws ClassNotFoundException
     */
    private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException {
        List<Class> classes = new ArrayList<Class>();
        if (!directory.exists()) {
            return classes;
        }
        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                assert !file.getName().contains(".");
                classes.addAll(findClasses(file, packageName + "." + file.getName()));
            } else if (file.getName().endsWith(".class")) {

//              System.out.println(packageName + '.' + file.getName().substring(0, file.getName().length() - 6));

                classes.add(
                        Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
            }
        }
        return classes;
    }

    public static void main(String[] args) throws ClassNotFoundException {
        List<City> cities = new ArrayList<City>();
        List<Class> classes = findClasses(new File("C:\\Users\\za.oussama\\Desktop\\Dossiers\\ws\\xx\\bin\\x"), "x");
        // print all class names within the project directory
        classes.forEach(System.out::println);
        // instanciate the classes who extends the class City : 
        // you can make it dynamic adding the class name to the method parameter 
        classes.stream().filter(clas -> clas.getSuperclass().getSimpleName().equals("City")).forEach(cl -> {
            try {
                City city = (City) cl.getDeclaredConstructor().newInstance();
                cities.add(city);
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });
        System.out.println();
        cities.forEach(c -> System.out.println(" Instance created from class : " + c.getName()));

    }
}

Output

class x.City
class x.London
class x.MyClass
class x.Paris

 Instance created from class : London
 Instance created from class : Paris

Image to clarify the path of files and the content of java classes enter image description here

some code is copied from another stack answer and from here : https://dzone.com/articles/get-all-classes-within-package.

Upvotes: 2

Related Questions