(转)收藏一段代码,备用

package ksource.eclipse.util;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.JavaRuntime;

/**
* Class Helper to load class of java project.
* @author <a href=mailto:elvis_qy@yahoo.com.cn>elvis</a>
*/
public final class ClassHelper {

private static final String PROTOCAL_PREFIX = "file:///";

/**
* get the <code>ClassLoader</code> of java project specified.
*
* @param project <code>IJavaProject</code>
* @return <code>ClassLoader</code> of java project
* @throws CoreException
* @throws MalformedURLException
*/
public static ClassLoader getProjectClassLoader(IJavaProject project)
throws CoreException,MalformedURLException {

//compute the project classpaths
//REVIEW: Are the classpaths returned by computeDefaultRuntimeClassPath enough to load class?
String[] classPaths = JavaRuntime.computeDefaultRuntimeClassPath(project);

URL[] urls = new URL[classPaths.length];
for (int i = 0; i < classPaths.length; i++) {
urls[i] = new URL(PROTOCAL_PREFIX
+ computeForURLClassLoader(classPaths[i]));
}
return new URLClassLoader(urls);
}

/**
* load <code>Class</code> in java project
*
* @param project <code>IJavaProject</code>
* @param className name of class to load
* @return <code>Class</code>
* @throws ClassNotFoundException
* @throws CoreException
* @throws MalformedURLException
*/
public static Class loadClass(IJavaProject project, String className)
throws CoreException, ClassNotFoundException,MalformedURLException {
ClassLoader loader = getProjectClassLoader(project);
Class clazz = loader.loadClass(className);
loader = null;
return clazz;
}

/**
* transform the <code>IType</code> to <code>Class</code>
*
* @param type <code>IType</code>
* @return <code>Class</code>
* @throws ClassNotFoundException
* @throws MalformedURLException
*/
public static Class typeToClass(IType type) throws CoreException,
ClassNotFoundException,MalformedURLException {
try {
if (null != type && (type.isClass() || type.isInterface())) {
String className = type.getFullyQualifiedName($);
return loadClass(type.getJavaProject(), className);
}
} catch (JavaModelException e) {
e.printStackTrace();
}
return null;
}

/**
* because of URLClassLoader assumes any URL not ends with / to refer to a
* jar file. so we need to append / to classpath if it is a folder but not
* ends with /.
*
* @param classpath
* @return
*/
private static String computeForURLClassLoader(String classpath) {
if (!classpath.endsWith("/")) {
File file = new File(classpath);
if (file.exists() && file.isDirectory()) {
classpath = classpath.concat("/");
}
}
return classpath;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值