Java tutorial
/* * Created 2007/01/31 * Copyright (C) 2003-2009 Naoki Iwami (naoki@limy.org) * * This file is part of Limy Eclipse Plugin. * * Limy Eclipse Plugin is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Limy Eclipse Plugin is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Limy Eclipse Plugin. If not, see <http://www.gnu.org/licenses/>. */ package org.limy.eclipse.qalab.common; import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Set; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jface.preference.IPreferenceStore; /** * QALabp??i[NX?B * @author Naoki Iwami */ public class LimyQalabEnvironment { // ------------------------ Fields /** v?WFNgpXgA */ private final IPreferenceStore store; /** v?WFNg */ private IProject[] projects; /** v?WFNgpNX??[_ */ private ClassLoader projectClassLoader; /** \?[XpX?iQ?v?WFNg?j */ private Collection<IPath> sourcePathsWithRef; /** \?[XpX?iQ?v?WFNg??j */ private Collection<IPath> sourcePathsWithoutRef; /** ?CJavav?WFNg */ private IJavaProject javaProject; /** binpX */ private Collection<IPath> binPaths; // ------------------------ Constructors /** * LimyQalabEnvironmentCX^X?\z?B * @param projects v?WFNg * @param store v?WFNgpXgA */ public LimyQalabEnvironment(IProject[] projects, IPreferenceStore store) { super(); this.projects = projects; this.store = store; } // ------------------------ Public Methods /** * @return */ public IPreferenceStore getStore() { return store; // return LimyQalabPluginUtils.createQalabStore(getMainProject()); } /** * ?Cv?WFNg?B * @return ?Cv?WFNg */ public IProject getProject() { return projects[0]; } /** * ?CJavav?WFNg?B * @return ?CJavav?WFNg */ public IJavaProject getJavaProject() { if (javaProject == null) { javaProject = JavaCore.create(projects[0]); } return javaProject; } /** * Javav?WFNg\?[XfBNg?B * @param enableRef Q?v?WFNg? * @return * @throws CoreException */ public Collection<IPath> getSourcePaths(boolean enableRef) throws CoreException { if (sourcePathsWithRef == null) { sourcePathsWithRef = privateGetSourcePaths(true); sourcePathsWithoutRef = privateGetSourcePaths(false); } return enableRef ? sourcePathsWithRef : sourcePathsWithoutRef; } /** * Javav?WFNg\?[XfBNg?ieXgfBNg??j?B * @return Javav?WFNg\?[XfBNg?ieXgfBNg??j * @throws CoreException */ public Collection<IPath> getMainSourcePaths() throws CoreException { return getAllSourcePaths(false); } /** * Javav?WFNgeXg\?[XfBNg?B * @return Javav?WFNg\?[XfBNg * @throws CoreException */ public Collection<IPath> getTestSourcePaths() throws CoreException { return getAllSourcePaths(true); } /** * binfBNg?B * @param enableRef Q?v?WFNg? * @return binfBNg * @throws JavaModelException */ public Collection<IPath> getBinPaths(boolean enableRef) throws JavaModelException { if (binPaths == null) { Collection<IPath> paths = privateGetBinPaths(this, enableRef); String enableProject = store.getString(LimyQalabConstants.SUB_PROJECT_NAMES); binPaths = new ArrayList<IPath>(); for (IPath path : paths) { String projectName = path.segment(0); // v?WFNg?Tuv?WFNgtarget if (projectName.equals(getMainProject().getName()) || enableProject.indexOf(projectName + "\n") >= 0) { binPaths.add(path); } } } return binPaths; } /** * LQ?v?WFNg?B * @return LQ?v?WFNg * @throws CoreException */ public Collection<IProject> getEnableReferencedProjects() throws CoreException { Collection<IProject> results = new ArrayList<IProject>(); String enableProject = store.getString(LimyQalabConstants.SUB_PROJECT_NAMES); for (IProject refProject : getMainProject().getReferencedProjects()) { if (enableProject.indexOf(refProject.getName() + "\n") >= 0) { results.add(refProject); } } return results; } // ------------------------ Getter/Setter Methods /** * v?WFNgpNX??[_?B * @return v?WFNgpNX??[_ */ public ClassLoader getProjectClassLoader() { return projectClassLoader; } /** * v?WFNgpNX??[_??B * @param projectClassLoader v?WFNgpNX??[_ */ public void setProjectClassLoader(ClassLoader projectClassLoader) { this.projectClassLoader = projectClassLoader; } public File getTempFile(String relativePath) { return LimyQalabUtils.createTempFile(getMainProject(), relativePath); } // ------------------------ Private Methods /** * ?Cv?WFNg?B * @return ?Cv?WFNg */ private IProject getMainProject() { return projects[0]; } /** * \?[XfBNg?B * @param enableRef Q?v?WFNg? * @return \?[XfBNg * @throws JavaModelException JavafO */ private Collection<IPath> privateGetSourcePaths(boolean enableRef) throws JavaModelException { Collection<IPath> results = new ArrayList<IPath>(); IJavaProject project = getJavaProject(); for (IJavaElement element : getAllJavaElements(project)) { if (enableRef && getStore().getBoolean(LimyQalabConstants.ENABLE_REFPROJECT)) { results.add(element.getPath()); } else { // v?WFNgL?? if (element.getJavaProject().equals(project)) { results.add(element.getPath()); } } } return results; } /** * Javav?WFNgS\?[XpX?iQ?v?WFNg?j?B * @param project Javav?WFNg * @return S\?[XpX * @throws JavaModelException JavafO */ private Collection<IJavaElement> getAllJavaElements(IJavaProject project) throws JavaModelException { Collection<IJavaElement> results = new ArrayList<IJavaElement>(); IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots(); // v?WFNgS\?[XpXQ?v?WFNg?[v for (IPackageFragmentRoot root : roots) { if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { results.add(root); } } return results; } /** * pXeXgpfBNg?B * @param location pX * @return eXgpfBNg?^ */ private boolean isTestPath(IPath location) { for (String segment : location.removeFirstSegments(1).segments()) { if ("test".equals(segment)) { return true; } } return false; } /** * Javav?WFNg?o?fBNg?B * @param env * @param enableRef Q?v?WFNg? * @return Javav?WFNg\?[XfBNg * @throws JavaModelException JavafO */ private Collection<IPath> privateGetBinPaths(LimyQalabEnvironment env, boolean enableRef) throws JavaModelException { IJavaProject project = env.getJavaProject(); Set<IPath> results = new HashSet<IPath>(); if (enableRef && store.getBoolean(LimyQalabConstants.ENABLE_REFPROJECT)) { // Q?v?WFNgL?? IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots(); for (IPackageFragmentRoot refRoot : roots) { if (refRoot.getKind() == IPackageFragmentRoot.K_SOURCE) { // Q?v?WFNg?[v IJavaProject refProject = refRoot.getJavaProject(); LimyQalabUtils.appendProjectBinPaths(refProject, results); } } } else { // ftHgv?WFNg?ofBNg LimyQalabUtils.appendProjectBinPaths(project, results); } return results; } /** * Javav?WFNg\?[XfBNg?B * @param enableTestPath trueeXgpX?Afalse?CpX * @return Javav?WFNg\?[XfBNg * @throws CoreException */ private Collection<IPath> getAllSourcePaths(boolean enableTestPath) throws CoreException { String enableProject = store.getString(LimyQalabConstants.SUB_PROJECT_NAMES); Collection<IPath> results = new ArrayList<IPath>(); Collection<IPath> paths = getSourcePaths(true); for (IPath path : paths) { if (isTestPath(path) == enableTestPath) { String projectName = path.segment(0); // v?WFNg?Tuv?WFNgcopy if (projectName.equals(getMainProject().getName()) || enableProject.indexOf(projectName + "\n") >= 0) { results.add(path); } } } return results; } }