自定义一个ClassLoader.

自定义一个ClassLoader.本文主要是要装载一个Eclipse Plugin的class.主要是classpath设置.注意这里的classname要用包名+类名的形式.而在下面的源代码中的默认构造函数中,来设置classpath. 你也可以使用第二个构造函数来自己设置classpath.在这里用的是为一个JavaProject设置classpath.

public class  CustomClassLoader  extends  ClassLoader  {

    
/** scanned class path */
    
private Vector fPathItems;

    
/**
     * CustomClassLoader constructor comment.
     
*/

    
public CustomClassLoader() {
        String classpath 
= System.getProperty("java.class.path");
        
final IJavaProject create = JavaCore.create(getProject());
        
try {
            
// /add resolved classpath.
            final IClasspathEntry[] resolvedClasspath = create.getResolvedClasspath(true);
            
for (IClasspathEntry entry : resolvedClasspath) {
                classpath 
+= ";" + entry.getPath().toOSString();
            }

            
final String workspacepath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
            
// / add this project's output classpath.
            classpath += ";" + workspacepath + create.getOutputLocation().toOSString();
            
final String[] requiredProjectNames = create.getRequiredProjectNames();
            
// / add required project to classpath.
            for (String string : requiredProjectNames) {
                classpath 
+= ";" + workspacepath + "/" + string;
            }

        }
 catch (Exception e) {
            e.printStackTrace();
        }

        scanPath(classpath);
    }


    
public CustomClassLoader(String classpath) {
        scanPath(classpath);
    }


    
public URL getResource(String name) {
        
return ClassLoader.getSystemResource(name);
    }


    
public InputStream getResourceAsStream(String name) {
        
return ClassLoader.getSystemResourceAsStream(name);
    }


    @SuppressWarnings(
"unchecked")
    
private void scanPath(String classPath) {
        String separator 
= System.getProperty("path.separator");
        fPathItems 
= new Vector(10);
        StringTokenizer st 
= new StringTokenizer(classPath, separator);
        
while (st.hasMoreTokens()) {
            fPathItems.addElement(st.nextToken());
        }

    }


    @SuppressWarnings(
"unchecked")
    
public synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {

        Class c 
= findLoadedClass(name);
        
if (c != null)
            
return c;
        
//
        
// Delegate the loading of excluded classes to the
        
// standard class loader.
        
//
        try {
            c 
= findSystemClass(name);
            
return c;
        }
 catch (ClassNotFoundException e) {
            c 
= null;
            
// keep searching
        }

        
if (c == null{
            
byte[] data = lookupClassData(name);
            
if (data == null)
                
throw new ClassNotFoundException();
            c 
= defineClass(name, data, 0, data.length);
        }

        
if (resolve)
            resolveClass(c);
        
return c;
    }


    
private byte[] lookupClassData(String className) throws ClassNotFoundException {
        
byte[] data = null;
        
for (int i = 0; i < fPathItems.size(); i++{
            String path 
= (String) fPathItems.elementAt(i);
            String fileName 
= className.replace('.''/'+ ".class";
            
if (isJar(path)) {
                data 
= loadJarData(path, fileName);
            }
 else {
                data 
= loadFileData(path, fileName);
            }

            
if (data != null)
                
return data;
        }

        
throw new ClassNotFoundException(className);
    }


    
boolean isJar(String pathEntry) {
        
return pathEntry.endsWith(".jar"|| pathEntry.endsWith(".zip");
    }


    
private byte[] loadFileData(String path, String fileName) {
        File file 
= new File(path, fileName);
        
if (file.exists()) {
            
return getClassData(file);
        }

        
return null;
    }


    
private byte[] getClassData(File f) {
        
try {
            FileInputStream stream 
= new FileInputStream(f);
            ByteArrayOutputStream out 
= new ByteArrayOutputStream(1000);
            
byte[] b = new byte[1000];
            
int n;
            
while ((n = stream.read(b)) != -1)
                out.write(b, 
0, n);
            stream.close();
            out.close();
            
return out.toByteArray();

        }
 catch (IOException e) {
        }

        
return null;
    }


    
private byte[] loadJarData(String path, String fileName) {
        ZipFile zipFile 
= null;
        InputStream stream 
= null;
        File archive 
= new File(path);
        
if (!archive.exists())
            
return null;
        
try {
            zipFile 
= new ZipFile(archive);
        }
 catch (IOException io) {
            
return null;
        }

        ZipEntry entry 
= zipFile.getEntry(fileName);
        
if (entry == null)
            
return null;
        
int size = (int) entry.getSize();
        
try {
            stream 
= zipFile.getInputStream(entry);
            
byte[] data = new byte[size];
            
int pos = 0;
            
while (pos < size) {
                
int n = stream.read(data, pos, data.length - pos);
                pos 
+= n;
            }

            zipFile.close();
            
return data;
        }
 catch (IOException e) {
        }
 finally {
            
try {
                
if (stream != null)
                    stream.close();
            }
 catch (IOException e) {
            }

        }

        
return null;
    }


    
public IProject getProject() {
        return null;      
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值