⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jarclassloader.java

📁 java写的多功能文件编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      while (entries.hasMoreElements())      {        ZipEntry entry = (ZipEntry) entries.nextElement();        String name = entry.getName().toLowerCase();        if (name.endsWith(".jext-script") || name.endsWith(".py"))        {          try          {            BufferedReader in = new BufferedReader(                                new InputStreamReader(zipFile.getInputStream(entry)));            String line;            StringBuffer buf = new StringBuffer();            while ((line = in.readLine()) != null)              buf.append(line).append('\n');            if (name.endsWith(".jext-script"))              org.jext.scripting.dawn.Run.execute(buf.toString(), parent, false);            else              org.jext.scripting.python.Run.execute(buf.toString(), parent);          } catch (IOException ioe) { }        }      }    }  }  public ZipFile getZipFile()  {    return zipFile;  }  // private members  /*   * Loading of plugin classes is deferred until all JARs   * are loaded - this is necessary because a plugin might   * depend on classes stored in other JARs.   */  private static ArrayList classLoaders = new ArrayList();  private int index;  private ArrayList pluginClasses = new ArrayList();//  replaced fileName attribute with URL as fileName was not used, and URL is now used for package sealing.  private URL url;  private ZipFile zipFile;  private void loadAllPlugins()  {    for (int i = 0; i < pluginClasses.size(); i++)    {      String name = (String) pluginClasses.get(i);      try      {        loadPluginClass(name);      } catch (Throwable t) {        String[] args = { name };        System.err.println(Jext.getProperty("jar.error.init", args));        t.printStackTrace();      }    }  }  private void loadPluginClass(String name) throws Exception  {    name = Utilities.fileToClass(name);    //if ("yes".equals(Jext.getProperty("plugin." + name + ".disabled")))    if (!isEnabled(name))    {      String[] args = { Jext.getProperty("plugin." + name + ".name") };      System.err.println(Jext.getProperty("jar.disabled", args));      return;    }    Plugin[] plugins = Jext.getPlugins();    for (int i = 0; i < plugins.length; i++)    {      if (plugins[i].getClass().getName().equals(name))      {        String[] args = { name };        System.err.println(Jext.getProperty("jar.error.duplicateName", args));        return;      }    }    // Check dependencies    if (!checkDependencies(name))      return;    Class clazz = loadClass(name, true);    int modifiers = clazz.getModifiers();    if (Plugin.class.isAssignableFrom(clazz) &&            !Modifier.isInterface(modifiers) &&            !Modifier.isAbstract(modifiers))    {      Plugin plugin = (Plugin) clazz.newInstance();      Jext.addPlugin(plugin);      int dot = name.lastIndexOf('.');      name = name.substring((dot == -1 ? 0 : dot + 1));      String[] args = { Jext.getProperty("plugin." + name + ".name") };      System.out.println(Jext.getProperty("jar.loaded", args));    }  }  private boolean checkDependencies(String name)  {    int i = 0;    StringBuffer deps = new StringBuffer();    boolean ok = true;    String dep;    while ((dep = Jext.getProperty("plugin." + name + ".depend." + i++)) != null)    {      int index = dep.indexOf(' ');      if (index == -1)      {        deps.append(dep);        deps.append('\n');        ok = false;        continue;      }      String what = dep.substring(0, index);      String arg = dep.substring(index + 1);      String[] args2 = new String[1];      if (what.equals("jext"))        args2[0] = Jext.BUILD; //Utilities.buildToVersion(arg);      else        args2[0] = arg;      deps.append(Jext.getProperty("jar.what." + what, args2));      deps.append('\n');      if (what.equals("jdk"))      {        if (System.getProperty("java.version").compareTo(arg) < 0)          ok = false;      } else if (what.equals("deprecateJDK")) {        if (System.getProperty("java.version").compareTo(arg) >= 0)          ok = false;      } else if (what.equals("jext")) {        if (Jext.BUILD.compareTo(arg) < 0)          ok = false;      } else if (what.equals("os")) {        ok = (System.getProperty("os.name").indexOf(arg) != -1);      } else if (what.equals("class")) {        try        {          loadClass(arg, false);        } catch (Exception e) {          ok = false;        }      } else        ok = false;    }    if (!ok && Jext.getProperty("plugin." + name + ".disabled") == null)    {      int dot = name.lastIndexOf('.');      name = name.substring((dot == -1 ? 0 : dot + 1));      String[] _args = { Jext.getProperty("plugin." + name + ".name"), deps.toString() };      int response = JOptionPane.showConfirmDialog(null,                                 Jext.getProperty("plugin.disable.question", _args),                                 Jext.getProperty("plugin.disable.title"),                                 JOptionPane.YES_NO_OPTION,                                 JOptionPane.QUESTION_MESSAGE);      //Jext.setProperty("plugin." + name + ".disabled", response == 0 ? "yes" : "no");      setEnabled(name, response == 0 ? false : true);    }    return ok;  }  private Class findOtherClass(String clazz, boolean resolveIt) throws ClassNotFoundException  {    for (int i = 0; i < classLoaders.size(); i++)    {      JARClassLoader loader = (JARClassLoader) classLoaders.get(i);      Class cls = loader.loadClassFromZip(clazz, resolveIt, false);      if (cls != null)        return cls;    }    /* Defer to whoever loaded us (such as JShell, Echidna, etc) */    ClassLoader loader = getClass().getClassLoader();    if (loader != null)      return loader.loadClass(clazz);    /* Doesn't exist in any other plugin, look in system classes */    return findSystemClass(clazz);  }  private Class loadClassFromZip(String clazz, boolean resolveIt,          boolean doDepencies) throws ClassNotFoundException  {    Class cls = findLoadedClass(clazz);    if (cls != null)    {      if (resolveIt)        resolveClass(cls);      return cls;    }    String name = Utilities.classToFile(clazz);    try    {      ZipEntry entry = zipFile.getEntry(name);      if (entry == null)      {        if (doDepencies)          return findOtherClass(clazz, resolveIt);        else          return null;      }      InputStream in = zipFile.getInputStream(entry);      int len = (int) entry.getSize();      byte[] data = new byte[len];      int success = 0;      int offset = 0;      while (success < len)      {        len -= success;        offset += success;        success = in.read(data, offset, len);        if (success == -1)        {          String[] args = { clazz, zipFile.getName()};          System.err.println(Jext.getProperty("jar.error.zip", args));          throw new ClassNotFoundException(clazz);        }      }            int dot = clazz.lastIndexOf('.');      String pkgName = (dot < 0) ? null : name.replace('/', '.').substring(0, dot);      if (pkgName != null && getPackage(pkgName) == null)      {        Package p = definePackage(pkgName, null, null, null, null, null, null, url);      }//end if there is a Package but it has not yet been defined       cls = defineClass(clazz, data, 0, data.length);      if (resolveIt)        resolveClass(cls);      return cls;    } catch (IOException io) {      throw new ClassNotFoundException(clazz);    }  }}// End of JARCLassLoader.java

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -