Java tutorial
/* * Created 2007/08/29 * 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.Arrays; import java.util.Collection; import java.util.HashSet; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; 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; /** * JavaA?[eBeBNX?B * @author Naoki Iwami */ public final class LimyQalabJavaUtils { /** * private constructor */ private LimyQalabJavaUtils() { } /** * Javav?WFNgSCu?iJava binpXQ?v?WFNgExportCu?j?B * @param project Javav?WFNg * @return Cu?pX * @throws CoreException */ public static Collection<String> getJavaLibraries(IJavaProject project) throws CoreException { Collection<String> results = new HashSet<String>(); for (IPackageFragmentRoot fragment : project.getAllPackageFragmentRoots()) { String location = getExternalLocation(fragment); if (location != null) { results.add(location); } } // TODO ? Eclipse Plugin v?WFNg???A // ev?WFNgEclipseAjar // export?A?iAccessG?[?j // ptOprKv for (IProject refProject : project.getProject().getReferencedProjects()) { boolean isPlugin = Arrays.asList(refProject.getDescription().getNatureIds()) .indexOf("org.eclipse.pde.PluginNature") >= 0; if (isPlugin) { IPackageFragmentRoot[] roots = JavaCore.create(refProject).getAllPackageFragmentRoots(); for (IPackageFragmentRoot fragment : roots) { String location = getExternalLocation(fragment); if (location != null) { results.add(location); } } } } return results; } /** * PackageFragmentRoot?pX?B * @param fragment PackageFragmentRoot * @return * @throws CoreException */ public static String getExternalLocation(IPackageFragmentRoot fragment) throws CoreException { String location = null; if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) { // v?WFNgQ?v?WFNg\?[XfBNg IPath outputLocation = fragment.getRawClasspathEntry().getOutputLocation(); if (outputLocation != null) { // \?[XpXL?o?w?? location = LimyQalabUtils.createFullPath(fragment.getJavaProject(), outputLocation); } else { // \?[XpXL?o?w???Av?WFNgftHg?o?gp location = LimyQalabUtils.createFullPath(fragment.getJavaProject(), fragment.getJavaProject().getOutputLocation()); } } else { // v?WFNgclasspathQ?v?WFNgExportCu IResource resource = fragment.getResource(); if (resource != null) { location = resource.getLocation().toString(); } else { // Variablewjart@Cresource = null IPath path = fragment.getRawClasspathEntry().getPath(); if (!path.toString().startsWith("org.eclipse.jdt.launching.JRE_CONTAINER")) { // JREOJARt@C?iVariablew?jNXpX location = fragment.getPath().toString(); } } } return location; } public static IPackageFragmentRoot getPackageFragmentRoot(IJavaElement el) { // if (el.getElementType() == IJavaElement.COMPILATION_UNIT) { // return (PackageFragmentRoot)((IPackageFragment)el.getParent()).getParent(); // } // if (el.getElementType() == IJavaElement.PACKAGE_FRAGMENT) { // return (PackageFragmentRoot)el.getParent(); // } // if (el.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) { // return (PackageFragmentRoot)el; // } return el.getJavaProject().getPackageFragmentRoot(el.getResource()); } /** * JavavfbinfBNg?B * <p> * pbP?[Wvf : binfBNgTufBNg<br> * \?[XfBNg : binfBNg<br> * </p> * @param el Javavf * @return binfBNg?i?pX?j * @throws CoreException */ public static String getBinDirPath(IJavaElement el) throws CoreException { if (el.getElementType() == IJavaElement.PACKAGE_FRAGMENT) { IPackageFragmentRoot root = (IPackageFragmentRoot) el.getParent(); String path = LimyQalabJavaUtils.getExternalLocation(root); return new File(path, el.getElementName().replace('.', '/')).getAbsolutePath(); } if (el.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) { return LimyQalabJavaUtils.getExternalLocation((IPackageFragmentRoot) el); } return null; } }