`
glacier3
  • 浏览: 386248 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

ClassUtil eclipse plugin development

阅读更多
package com.nantian.iwapdesigner.transactioneditor.util;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.part.FileEditorInput;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.ide.eclipse.beans.core.internal.model.Bean;
import org.springframework.ide.eclipse.beans.core.model.IBean;
import org.springframework.ide.eclipse.beans.core.model.IBeansConfig;
import org.springframework.ide.eclipse.beans.core.model.IBeansProject;

import com.nantian.iwapdesigner.transexplorer.model.IWAPBeansModel;

/**
 * Class Helper to load class of java project.
 * 
 * @author elvis refelction part
 *  
 * 
 */
public class ClassUtil {
	private static final String PROTOCAL_PREFIX = "file:///";

	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;
	}

	public static ClassLoader getProjectClassLoader(IJavaProject project)
			throws CoreException, MalformedURLException {
		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);
	}

	private static String computeForURLClassLoader(String classpath) {
		if (!classpath.endsWith("/")) {
			File file = new File(classpath);
			if (file.exists() && file.isDirectory()) {
				classpath = classpath.concat("/");
			}
		}
		return classpath;
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics