Пример #1
0
 public void runString(String input) {
   try {
     i++;
     JavaSourceCompiler javaSourceCompiler = new JavaSourceCompilerImpl();
     JavaSourceCompiler.CompilationUnit compilationUnit =
         javaSourceCompiler.createCompilationUnit();
     if (!dependencies.isEmpty()) {
       compilationUnit.addClassPathEntries(dependencies);
     }
     String complete =
         "import net.aufdemrand.denizen.utilities.debugging.dB;\npublic class CodeTester"
             + i
             + " extends net.aufdemrand.denizen.utilities.dExternalRunnable {\n"
             + "public void unload() {}\npublic void load() {}\n"
             + "public void run() {\n"
             + input
             + "\n}\n}";
     compilationUnit.addJavaSource("CodeTester" + i, complete);
     ClassLoader classLoader = javaSourceCompiler.compile(compilationUnit);
     Class<? extends dExternalRunnable> load =
         (Class<? extends dExternalRunnable>) classLoader.loadClass("CodeTester" + i);
     dExternalRunnable loadedClass = load.newInstance();
     loadedClass.load();
     loadedClass.run();
     loadedClass.unload();
   } catch (Exception e) {
     dB.echoError(e);
   }
 }
Пример #2
0
  @SuppressWarnings("unchecked")
  public void loader() {

    dB.log("Loading external dependencies for run-time compiler.");
    dependencies = new ArrayList<String>();
    try {
      File file =
          new File(
              denizen.getDataFolder()
                  + File.separator
                  + "externals"
                  + File.separator
                  + "dependencies");
      for (File f : file.listFiles(jarFilter)) {
        dependencies.add(f.getPath());
        dB.log("Loaded  " + f.getName());
      }
    } catch (Exception error) {
      dB.log("No dependencies to load or error loading dependencies: " + error.getMessage());
    }
    dB.log("Loading plugins as dependencies for run-time compiler.");
    for (File f : pluginsFolder.listFiles(jarFilter)) {
      dependencies.add(f.getPath());
      dB.log("Loaded  " + f.getName());
    }
    dB.log("Attempting to add CraftBukkit as dependency for run-time compiler.");
    try {
      File file = new File(".");
      for (File f : file.listFiles(jarFilter)) {
        dependencies.add(f.getPath());
        dB.log("Loaded  " + f.getName());
      }
    } catch (Exception error) {
      dB.log("Could not find CraftBukkit jar or error reading it: " + error.getMessage());
    }

    if (!externalsFolder.exists() || externalsFolder.list().length == 0) {
      return;
    }
    try {
      File file = new File(denizen.getDataFolder() + File.separator + "externals");
      File[] files = file.listFiles(javaFilter);
      if (files != null && files.length > 0) {
        for (File f : files) {
          String fileName = f.getName();

          dB.log("Processing '" + fileName + "'... ");

          JavaSourceCompiler javaSourceCompiler = new JavaSourceCompilerImpl();
          JavaSourceCompiler.CompilationUnit compilationUnit =
              javaSourceCompiler.createCompilationUnit();
          if (!dependencies.isEmpty()) {
            compilationUnit.addClassPathEntries(dependencies);
          }

          try {
            compilationUnit.addJavaSource(
                fileName.replace(".java", ""), readFile(f.getAbsolutePath()));
            ClassLoader classLoader = javaSourceCompiler.compile(compilationUnit);
            Class<dExternal> load =
                (Class<dExternal>) classLoader.loadClass(fileName.replace(".java", ""));
            dExternal loadedClass = load.newInstance();
            loadedClass.load();
            loadedExternals.add(loadedClass);
          } catch (Exception e) {
            if (e instanceof IllegalStateException) {
              dB.echoError("No JDK found! External .java files will not be loaded.");
              dB.echoError(e);
            } else {
              dB.echoError(ChatColor.RED + "Error compiling " + fileName + "!");
              dB.echoError(e);
            }
          }
        }
        dB.echoApproval("All externals loaded!");
      }
    } catch (Exception error) {
      dB.echoError(error);
    }
  }