Include compile options

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2, Sig, Sigo 3 years ago committed by GitHub
parent bcaa025af2
commit d17b0635f5
  1. BIN
      bin/TestPlugin.jar
  2. BIN
      plugins/TestPlugin.class
  3. BIN
      plugins/TestPlugin.jar
  4. 66
      src/sig/PluginLoader.java

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -1,33 +1,85 @@
package sig;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
public class PluginLoader {
public static void LoadPlugins() {
Path p = Path.of("..","plugins");
try {
Files.walk(p).filter((f)->Files.isRegularFile(f)&&f.getFileName().toString().contains(".jar")).forEach((f)->{
LoadPlugin(f);
Files.walk(p).filter((f)->Files.isRegularFile(f)&&f.getFileName().toString().contains(".java")).forEach((f)->{
System.out.println(LoadPlugin(f));
});
} catch (IOException e) {
e.printStackTrace();
}
}
private static void LoadPlugin(Path f) {
public static List getClasseNames(String jarName) {
boolean debug=true;
ArrayList classes = new ArrayList();
if (debug)
System.out.println("Jar " + jarName );
try {
JarInputStream jarFile = new JarInputStream(new FileInputStream(
jarName));
JarEntry jarEntry;
while (true) {
jarEntry = jarFile.getNextJarEntry();
if (jarEntry == null) {
break;
}
if (jarEntry.getName().endsWith(".class")) {
if (debug)
System.out.println("Found "
+ jarEntry.getName().replaceAll("/", "\\."));
classes.add(jarEntry.getName().replaceAll("/", "\\."));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return classes;
}
private static String LoadPlugin(Path f) {
try {
System.out.println("Loading "+f);
URL l = f.toUri().toURL();
System.out.println(" Compiling "+f.getParent().toAbsolutePath()+"...");
Process process = Runtime.getRuntime().exec(new String[]{"javac","../plugins/TestPlugin.java"});
//Process process = Runtime.getRuntime().exec("jar cfm TestPlugin.jar ../manifest .");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder line=new StringBuilder();
String lin;
while ((lin = reader.readLine()) != null) {
line.append(lin).append("\n");
}
process = Runtime.getRuntime().exec(new String[]{"jar","cfm","TestPlugin.jar","../manifest","../plugins/TestPlugin.class"});
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((lin = reader.readLine()) != null) {
line.append(lin).append("\n");
}
URL l = Paths.get(f.getFileName().toString().replace(".java","")+".jar").toUri().toURL();
URLClassLoader loader = new URLClassLoader(new URL[]{l});
Object newPlugin = Class.forName(f.getFileName().toString().replace(".jar",""),true,loader).getDeclaredConstructor().newInstance();
} catch (MalformedURLException | InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
Object newPlugin = Class.forName(f.getFileName().toString().replace(".java",""),true,loader).getDeclaredConstructor().newInstance();
return line.toString();
} catch (IllegalArgumentException | SecurityException | IOException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException e) {
e.printStackTrace();
}
return "";
}
}

Loading…
Cancel
Save