I'm a little lost here! Can someone give me a hint?
package com.codegym.task.task35.task3507;
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/*
What is ClassLoader?
*/
public class Solution {
public static void main(String[] args) throws ClassNotFoundException {
Set<? extends Animal> allAnimals = getAllAnimals(Solution.class.getProtectionDomain().getCodeSource().getLocation().getPath() + Solution.class.getPackage().getName().replaceAll("[.]", "/") + "/data");
System.out.println(allAnimals);
}
public static <T extends Animal> Set<T> getAllAnimals(String pathToAnimals) throws ClassNotFoundException {
Set<T> set = new HashSet<>();
ClassLoader loader = new ClassLoader() {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
return super.loadClass(name);
}
};
File file = new File(pathToAnimals);
String[] classFiles = file.list();
for (String fileName : classFiles){
Object fileClass = loader.loadClass(fileName);
if (fileClass instanceof Animal){
set.add((T)fileClass);
}
}
return set;
}
}