
pluginloader.PluginLoader Maven / Gradle / Ivy
package pluginloader;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import pluginloader.load.PluginClassLoader;
import pluginloader.load.PluginCompiler;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* This class loads plugins and instantiates them
*/
public class PluginLoader {
public static class LoadingException extends Exception{
public LoadingException(String message) {
super(message);
}
public LoadingException(Throwable cause) {
super(cause);
}
}
private static class ClassInfo{
private final String className;
private final String packageName;
private ClassInfo(String className, String packageName) {
this.className = className;
this.packageName = packageName;
}
public String getFullQualifiedClassName(){
String packagePath = "";
if (packageName != null){
packagePath = String.format("%s.", packageName);
}
return String.format("%s%s", packagePath, className);
}
}
/**
* Compiles a plugin given as a java class in source code form and loads it into the Java Runtime.
* It only loads classes that extend the given java class.
* After successfully loading it, an instance of each found class that extends the given class
* will be returned to the caller.
* If a class already exists, it will be updated if its code has changed. If the code has
* not changed, the loading is skipped and a new instance of the class is returned.
* @param plugin The string containing the source code of the plugin class
* @param pluginInterface The interface/class the plugin class has to implement/extend
* @return OBJECT The new instance of the class
* @throws LoadingException When something goes wrong while compiling or loading the class
*/
public
© 2015 - 2025 Weber Informatics LLC | Privacy Policy