1 19 20 package org.netbeans.modules.form.project; 21 22 import java.io.InputStream ; 23 import java.net.URL ; 24 import org.netbeans.api.java.classpath.ClassPath; 25 import org.openide.ErrorManager; 26 import org.openide.filesystems.FileObject; 27 import org.openide.filesystems.FileStateInvalidException; 28 29 39 40 class ProjectClassLoader extends ClassLoader { 41 42 private ClassLoader projectClassLoaderDelegate; 43 private ClassPath sources; 44 private ClassLoader systemClassLoader; 45 46 private ProjectClassLoader(ClassLoader projectClassLoaderDelegate, ClassPath sources) { 47 this.projectClassLoaderDelegate = projectClassLoaderDelegate; 48 this.sources = sources; 49 this.systemClassLoader = (ClassLoader ) org.openide.util.Lookup.getDefault().lookup(ClassLoader .class); 50 } 51 52 static ClassLoader getUpToDateClassLoader(FileObject fileInProject, ClassLoader clSoFar) { 53 ClassLoader existingCL = clSoFar instanceof ProjectClassLoader ? 54 ((ProjectClassLoader)clSoFar).projectClassLoaderDelegate : clSoFar; 55 ClassPath classPath = ClassPath.getClassPath(fileInProject, ClassPath.EXECUTE); 56 ClassLoader actualCL = classPath != null ? classPath.getClassLoader(true) : null; 57 if (actualCL == existingCL) 58 return clSoFar; 59 if (actualCL == null) 60 return null; 61 return new ProjectClassLoader(actualCL, ClassPath.getClassPath(fileInProject, ClassPath.SOURCE)); 62 } 63 64 protected Class findClass(String name) throws ClassNotFoundException { 65 if (name.startsWith("org.apache.commons.logging.")) { try { 67 return systemClassLoader.loadClass(name); 68 } catch (ClassNotFoundException cnfex) { 69 } 71 } 72 Class c = null; 73 String filename = name.replace('.', '/').concat(".class"); URL url = projectClassLoaderDelegate.getResource(filename); 75 if (url != null) { 76 try { 77 InputStream is = url.openStream(); 78 byte[] data = null; 79 int first; 80 int available = is.available(); 81 while ((first = is.read()) != -1) { 82 int length = is.available(); 83 if (length != available) { length++; 85 } 86 byte[] b = new byte[length]; 87 b[0] = (byte) first; 88 int count = 1; 89 while (count < length) { 90 int read = is.read(b, count, length - count); 91 assert (read != -1); 92 count += read; 93 } 94 if (data == null) { 95 data = b; 96 } 97 else { 98 byte[] temp = new byte[data.length + count]; 99 System.arraycopy(data, 0, temp, 0, data.length); 100 System.arraycopy(b, 0, temp, data.length, count); 101 data = temp; 102 } 103 } 104 int dot = name.lastIndexOf('.'); 105 if (dot != -1) { String packageName = name.substring(0, dot); 107 Package pakcage = getPackage(packageName); 108 if (pakcage == null) { 109 definePackage(packageName, null, null, null, null, null, null, null); 111 } 112 } 113 c = defineClass(name, data, 0, data.length); 114 } 115 catch (Exception ex) { 116 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 117 } 118 } 119 else if (ClassPathUtils.getClassLoadingType(name) == ClassPathUtils.SYSTEM_CLASS) { 120 c = systemClassLoader.loadClass(name); 123 } 124 if (c == null) 125 throw new ClassNotFoundException (name); 126 return c; 127 } 128 129 protected URL findResource(String name) { 130 FileObject fo = sources.findResource(name); 131 if (fo != null) { 132 try { 133 return fo.getURL(); 134 } catch (FileStateInvalidException ex) { 135 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 136 } 137 } 138 return projectClassLoaderDelegate.getResource(name); 139 } 140 } 141 | Popular Tags |