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

}

你可能感兴趣的:(java,eclipse,UI,.net,ide)