📄 jarclassloader.java
字号:
/* * JARClassLoader.java - Loads classes from JAR files * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 1999, 2000, 2001, 2002 Slava Pestov * Portions copyright (C) 1999 mike dillon * Portions copyright (C) 2002 Marco Hunsicker * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package org.gjt.sp.jedit;//{{{ Importsimport java.io.*;import java.lang.reflect.Modifier;import java.net.*;import java.util.*;import java.util.jar.*;import java.util.zip.*;import org.gjt.sp.jedit.gui.DockableWindowManager;import org.gjt.sp.util.Log;//}}}/** * A class loader implementation that loads classes from JAR files. * @author Slava Pestov * @version $Id: JARClassLoader.java,v 1.22 2003/02/07 17:42:30 spestov Exp $ */public class JARClassLoader extends ClassLoader{ //{{{ JARClassLoader constructor /** * This constructor creates a class loader for loading classes from all * plugins. For example BeanShell uses one of these so that scripts can * use plugin classes. */ public JARClassLoader() { } //}}} //{{{ JARClassLoader constructor public static long scanTime; public static long startTime; public JARClassLoader(String path) throws IOException { long time = System.currentTimeMillis(); this.path = path; zipFile = new JarFile(path); definePackages(); jar = new EditPlugin.JAR(path,this); Enumeration entires = zipFile.entries(); while(entires.hasMoreElements()) { ZipEntry entry = (ZipEntry)entires.nextElement(); String name = entry.getName(); String lname = name.toLowerCase(); if(lname.equals("actions.xml")) pluginResources.add(entry); if(lname.equals("dockables.xml")) pluginResources.add(entry); else if(lname.endsWith(".props")) jEdit.loadProps(zipFile.getInputStream(entry),true); else if(name.endsWith(".class")) { classHash.put(MiscUtilities.fileToClass(name),this); if(name.endsWith("Plugin.class")) pluginClasses.add(name); } } jEdit.addPluginJAR(jar); scanTime += (System.currentTimeMillis() - time); } //}}} //{{{ loadClass() method /** * @exception ClassNotFoundException if the class could not be found */ public Class loadClass(String clazz, boolean resolveIt) throws ClassNotFoundException { // see what JARClassLoader this class is in Object obj = classHash.get(clazz); if(obj == NO_CLASS) { // we remember which classes we don't exist // because BeanShell tries loading all possible // <imported prefix>.<class name> combinations throw new ClassNotFoundException(clazz); } else if(obj instanceof ClassLoader) { JARClassLoader classLoader = (JARClassLoader)obj; return classLoader._loadClass(clazz,resolveIt); } // if it's not in the class hash, and not marked as // non-existent, try loading it from the CLASSPATH try { Class cls; /* Defer to whoever loaded us (such as JShell, * Echidna, etc) */ ClassLoader parentLoader = getClass().getClassLoader(); if (parentLoader != null) cls = parentLoader.loadClass(clazz); else cls = findSystemClass(clazz); return cls; } catch(ClassNotFoundException cnf) { // remember that this class doesn't exist for // future reference classHash.put(clazz,NO_CLASS); throw cnf; } } //}}} //{{{ getResourceAsStream() method public InputStream getResourceAsStream(String name) { if(zipFile == null) return null; try { ZipEntry entry = zipFile.getEntry(name); if(entry == null) return getSystemResourceAsStream(name); else return zipFile.getInputStream(entry); } catch(IOException io) { Log.log(Log.ERROR,this,io); return null; } } //}}} //{{{ getResource() method public URL getResource(String name) { if(zipFile == null) return null; ZipEntry entry = zipFile.getEntry(name); if(entry == null) return getSystemResource(name); try { return new URL(getResourceAsPath(name)); } catch(MalformedURLException mu) { Log.log(Log.ERROR,this,mu); return null; } } //}}} //{{{ getResourceAsPath() method public String getResourceAsPath(String name) { if(zipFile == null) return null; if(!name.startsWith("/")) name = "/" + name; return "jeditresource:/" + MiscUtilities.getFileName( jar.getPath()) + "!" + name; } //}}} //{{{ closeZipFile() method /** * Closes the ZIP file. This plugin will no longer be usable * after this. * @since jEdit 2.6pre1 */ public void closeZipFile() { if(zipFile == null) return; try { zipFile.close(); } catch(IOException io) { Log.log(Log.ERROR,this,io); } zipFile = null; } //}}} //{{{ getZipFile() method /** * Returns the ZIP file associated with this class loader. * @since jEdit 3.0final */ public ZipFile getZipFile() { return zipFile; } //}}} //{{{ startAllPlugins() method void startAllPlugins() { long time = System.currentTimeMillis(); boolean ok = true; for(int i = 0; i < pluginClasses.size(); i++) { String name = (String)pluginClasses.get(i); name = MiscUtilities.fileToClass(name); try { ok &= loadPluginClass(name); } catch(Throwable t) { ok = false; Log.log(Log.ERROR,this,"Error while starting plugin " + name); Log.log(Log.ERROR,this,t); jar.addPlugin(new EditPlugin.Broken(name)); String[] args = { t.toString() }; jEdit.pluginError(jar.getPath(), "plugin-error.start-error",args); } } startTime += (System.currentTimeMillis() - time); time = System.currentTimeMillis(); if(!ok) { // don't load actions and dockables if plugin didn't load. return; } try { for(int i = 0; i < pluginResources.size(); i++) { ZipEntry entry = (ZipEntry)pluginResources.get(i); String name = entry.getName(); if(name.equalsIgnoreCase("actions.xml")) { jEdit.loadActions( path + "!actions.xml", new BufferedReader(new InputStreamReader( zipFile.getInputStream(entry))), jar.getActions()); } else if(name.equalsIgnoreCase("dockables.xml")) { DockableWindowManager.loadDockableWindows( path + "!dockables.xml", new BufferedReader(new InputStreamReader( zipFile.getInputStream(entry))), jar.getActions()); } } } catch(IOException io) { Log.log(Log.ERROR,jEdit.class,"Cannot load" + " plugin " + MiscUtilities.getFileName(path)); Log.log(Log.ERROR,jEdit.class,io); String[] args = { io.toString() }; jEdit.pluginError(path,"plugin-error.load-error",args); } scanTime += (System.currentTimeMillis() - time); } //}}} //{{{ Private members // used to mark non-existent classes in class hash private static final Object NO_CLASS = new Object(); private static Hashtable classHash = new Hashtable(); private String path; private EditPlugin.JAR jar; private ArrayList pluginResources = new ArrayList(); private ArrayList pluginClasses = new ArrayList(); private JarFile zipFile; //{{{ loadPluginClass() method private boolean loadPluginClass(String name) throws Exception { // Check if a plugin with the same name is already loaded EditPlugin[] plugins = jEdit.getPlugins(); for(int i = 0; i < plugins.length; i++) { if(plugins[i].getClass().getName().equals(name)) { jEdit.pluginError(jar.getPath(), "plugin-error.already-loaded",null); return false; } } /* This is a bit silly... but WheelMouse seems to be * unmaintained so the best solution is to add a hack here. */ if(name.equals("WheelMousePlugin") && OperatingSystem.hasJava14()) { jar.addPlugin(new EditPlugin.Broken(name)); jEdit.pluginError(jar.getPath(),"plugin-error.obsolete",null); return false; } // Check dependencies if(!checkDependencies(name)) { jar.addPlugin(new EditPlugin.Broken(name)); return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -