簡體   English   中英

如何在沒有庫的情況下獲取所有擴展類

[英]How to get all extended classes without libraries

在我的項目中,我有一個類City和其他擴展它的類( LondonParis ...)。 我想在不使用任何外部庫的情況下實例化所有擴展City的類(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";
    }
}

這是一個解決方案,它可以工作,但您可以添加一些增強功能(使其與間接超類一起工作..),想法是:找到項目目錄中的所有類,迭代這些類並查找擴展給定類的所有類(按名字)。 之后實例化所有這些類:

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM