Created
January 20, 2023 18:23
-
-
Save EronAlves1996/2f129bcaa79dca55868f83e8af36b43a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.eron; | |
import java.io.File; | |
import java.io.IOException; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Parameter; | |
import java.net.MalformedURLException; | |
import java.net.URISyntaxException; | |
import java.net.URL; | |
import java.net.URLClassLoader; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Enumeration; | |
import java.util.Iterator; | |
public class Main { | |
public static void main(String[] args) throws IOException, URISyntaxException { | |
// GET ALL CLASSPATH LOCATIONS | |
Enumeration<URL> resources = ClassLoader.getSystemClassLoader().getResources(""); | |
Iterator<URL> urlIterator = resources.asIterator(); | |
// ITERATE ON THESE LOCATIONS AND TAKE ONLY CLASS FILES AND PREFIX BEFORE PACKAGE DIR | |
ArrayList<File> files = null; | |
String dir = null; | |
while(urlIterator.hasNext()){ | |
dir = urlIterator.next().getFile(); | |
File file1 = new File(dir); | |
files = iteratorScan(file1); | |
} | |
final String dirFinal = dir; | |
// INSTANTIATE NEW CLASSLOADER, SPECIFICATING FILE PROTOCOL AND POINTING TO THE FOLDER WHERE PACKAGE | |
// IS LOCATED | |
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{ | |
new URL(dirFinal.startsWith("file:") ? dirFinal : "file:" + dirFinal) | |
}); | |
// ITERATE FOR ALL CLASSES AND DO THE OPERATIONS | |
files.forEach(file ->{ | |
try { | |
// GET THE ABSOLUTE PATH OF CLASS FILE AND REMOVE THE PATH BEFORE PACKAGE, THE ".class" EXTENSION AND | |
// REPLACE "/" FOR "." | |
String fullQualifiedName = file.getAbsolutePath() | |
.replace(dirFinal, "") | |
.replace(".class", "").replace("/", "."); | |
// LOAD THE CLASS | |
Class<?> aClass = urlClassLoader.loadClass(fullQualifiedName); | |
// INSTANTIATE AND TEST CLASS | |
if(aClass.getName().endsWith("Logger")){ | |
Object[] test = new Object[1]; | |
Arrays.stream(aClass.getConstructors()).forEach(constructor -> { | |
Parameter[] parameters = constructor.getParameters(); | |
if(parameters.length == 0){ | |
try{ | |
test[0] = constructor.newInstance(); | |
} catch (InvocationTargetException e) { | |
throw new RuntimeException(e); | |
} catch (InstantiationException e) { | |
throw new RuntimeException(e); | |
} catch (IllegalAccessException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
}); | |
Logger log = (Logger) test[0]; | |
try{ | |
log.log("Testing"); | |
log.log("Anyways again"); | |
} catch(NullPointerException ex){} | |
} | |
// OBLIGATORY TRY/CATCH BLOCK FOR POSSIBLE EXCEPTIONS | |
} catch (ClassNotFoundException e) { | |
throw new RuntimeException(e); | |
} | |
}); | |
} | |
public static ArrayList<File> iteratorScan(File dirOrF){ | |
ArrayList<File> files = new ArrayList<>(); | |
Arrays.stream(dirOrF.listFiles()).forEach(f -> { | |
if (f.isDirectory()){ | |
iteratorScan(f).forEach(files::add); | |
} else { | |
files.add(f); | |
} | |
}); | |
return files; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment