Did anyone solve the problem?
package com.codegym.task.task35.task3507;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
/*
What is ClassLoader?
*/
public class Solution {
public static void main(String[] args) {
Set<? extends Animal> allAnimals = getAllAnimals(Solution.class.getProtectionDomain().getCodeSource().getLocation().getPath() + Solution.class.getPackage().getName().replaceAll("[.]", "/") + "/data");
System.out.println(allAnimals);
Set<? extends Animal> allAnimals2 = getAllAnimals(Solution.class.getProtectionDomain().getCodeSource().getLocation().getPath() + Solution.class.getPackage().getName().replaceAll("[.]", "/"));
System.out.println(allAnimals2);
}
public static Set<? extends Animal> getAllAnimals(String pathToAnimals) {
// 1. get all java class at path
// 2. if else ... add class with no argument constructor and inheriting Animal to set
File dir = new File(pathToAnimals);
Set<Animal> result = new HashSet<>();
try {
URL url = dir.toURI().toURL();
URLClassLoader urlClassLoader = URLClassLoader.newInstance(new URL[]{url});
for (String className : walkClassName(dir)) {
try {
Class<?> clazz = urlClassLoader.loadClass(className);
Constructor<?> ct = clazz.getConstructor();
if (Modifier.isPublic(ct.getModifiers()) && Animal.class.isAssignableFrom(clazz)) {
Animal animal = (Animal) clazz.newInstance();
result.add(animal);
}
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) {
// e.printStackTrace();
}
}
} catch (URISyntaxException | MalformedURLException ignored) {
}
return result;
}
public static ArrayList<String> walkClassName(File dir) throws URISyntaxException {
String path = new File(Solution.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath();
ArrayList<String> result = new ArrayList<>();
for (File file : Objects.requireNonNull(dir.listFiles())) {
if (file.isFile() && (file.getName().endsWith(".class"))) {
String filePath = file.getAbsolutePath();
filePath = filePath.substring(0, filePath.length() - 6);
result.add(filePath.substring(
path.length() + 1
).replace(File.separator, "."));
}
}
return result;
}
public static ArrayList<String> walkAllClassName(File dir) throws URISyntaxException {
String path = new File(Solution.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath();
Stack<File> stack = new Stack<>();
ArrayList<String> result = new ArrayList<>();
stack.add(dir);
while (!stack.isEmpty()) {
File probe = stack.pop();
File[] files = probe.listFiles();
if (files == null) {
continue;
}
for (File file : files) {
if (file.isDirectory()) {
stack.add(file);
} else if (file.isFile() && file.getName().endsWith(".class")) {
String filePath = file.getAbsolutePath();
filePath = filePath.substring(0, filePath.length() - 6);
result.add(filePath.substring(
path.length() + 1
).replace(File.separator, "."));
}
}
}
return result;
}
}