1 19 20 package org.netbeans.modules.form.project; 21 import java.io.InputStream ; 22 import java.net.URL ; 23 import org.openide.ErrorManager; 24 25 33 34 final class FormClassLoader extends ClassLoader { 35 36 private ClassLoader systemClassLoader; 37 private ClassLoader projectClassLoader; 38 39 FormClassLoader(ClassLoader projectClassLoader) { 40 this.systemClassLoader = (ClassLoader ) org.openide.util.Lookup.getDefault().lookup(ClassLoader .class); 41 this.projectClassLoader = projectClassLoader; 42 } 43 44 ClassLoader getProjectClassLoader() { 45 return projectClassLoader; 46 } 47 48 protected Class findClass(String name) throws ClassNotFoundException { 49 int type = ClassPathUtils.getClassLoadingType(name); 50 if (type == ClassPathUtils.UNSPECIFIED_CLASS) { 51 if (projectClassLoader == null) 52 throw new ClassNotFoundException (ClassPathUtils.getBundleString("MSG_NullClassPath")); return projectClassLoader.loadClass(name); 54 } 55 if (type == ClassPathUtils.SYSTEM_CLASS) 56 return systemClassLoader.loadClass(name); 57 59 Class c = null; 60 String filename = name.replace('.', '/').concat(".class"); URL url = systemClassLoader.getResource(filename); 62 if (url == null && projectClassLoader != null) 63 url = projectClassLoader.getResource(filename); 64 if (url != null) { 65 try { 66 InputStream is = url.openStream(); 67 byte[] data = null; 68 int first; 69 int available = is.available(); 70 while ((first = is.read()) != -1) { 71 int length = is.available(); 72 if (length != available) { length++; 74 } 75 byte[] b = new byte[length]; 76 b[0] = (byte) first; 77 int count = 1; 78 while (count < length) { 79 int read = is.read(b, count, length - count); 80 assert (read != -1); 81 count += read; 82 } 83 if (data == null) { 84 data = b; 85 } 86 else { 87 byte[] temp = new byte[data.length + count]; 88 System.arraycopy(data, 0, temp, 0, data.length); 89 System.arraycopy(b, 0, temp, data.length, count); 90 data = temp; 91 } 92 } 93 int dot = name.lastIndexOf('.'); 94 if (dot != -1) { String packageName = name.substring(0, dot); 96 Package pakcage = getPackage(packageName); 97 if (pakcage == null) { 98 definePackage(packageName, null, null, null, null, null, null, null); 100 } 101 } 102 c = defineClass(name, data, 0, data.length); 103 } 104 catch (Exception ex) { 105 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 106 } 107 } 108 if (c == null) 109 throw new ClassNotFoundException (name); 110 111 return c; 112 } 113 114 public URL getResource(String name) { 115 URL url = projectClassLoader != null ? projectClassLoader.getResource(name) : null; 116 if (url == null) 117 url = systemClassLoader.getResource(name); 118 return url; 119 } 120 } 121 | Popular Tags |