如何在没有库的情况下获取所有扩展类

发布于 2025-02-07 15:12:46 字数 687 浏览 1 评论 0原文

在我的项目中,我有一个类City和其他扩展它的类(伦敦paris ...)。我想实例化所有扩展city(城市的子类)的类,而无需使用任何外部库。

我想避免使用这样的构造函数手动实例化课程:

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

类:

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

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

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";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謌踐踏愛綪 2025-02-14 15:12:46

这是一个解决方案,可以正常工作,但是您可以添加一些增强功能(使其与间接超级类别一起使用。.),这个想法是:查找项目目录中的所有类(按名称)。在此之后,实现了所有这些类:

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()));

    }
}

输出

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

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

图像阐明文件路径和Java类的内容

从另一个堆栈答案中复制了一些代码,然后从这里进行: https://dzone.com/articles/get-all-classes-within package

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文