站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题在Eclipse插件开发中使用URLClassLoader加载JavaProject中的类

在Eclipse插件开发中使用URLClassLoader加载JavaProject中的类

摘要: 在Eclipse插件开发中使用URLClassLoader加载JavaProject中的类

昨天在开发一个给Java Class添加static final long serialVersionUID属性的Eclipse插件时需要用到获取选中的Java类型(IType)对应的Class,由于是第一次写插件,花了不少时间来看帮助和相关资料才解决这个问题,最后总结了一下,写了一个ClassHelper类,方便以后再写其他插件时可以使用。

ClassHelper.java


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 elvis
*/
public final class ClassHelper {

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

/**
* get the ClassLoader of java project specified.
*
* @param project IJavaProject
* @return ClassLoader 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 Class in java project
*
* @param project IJavaProject
* @param className name of class to load
* @return Class
* @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 IType to Class
*
* @param type IType
* @return Class
* @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;
}
}



↑返回目录
前一篇: Eclipse快速上手EJB -- 3. 一对一的双向关系的CMR(3)
后一篇: 用 Runtime Spy 调整 Eclipse 的启动性能,第 1 部分