PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/aspectj-1.6.9/aspectjtools1.6.9/org/aspectj/org/eclipse/jdt/internal/core/JarPackageFragmentRoot.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 308 lines | 197 code | 20 blank | 91 comment | 44 complexity | 8346d9e258638e61fabe72ec3988a940 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2007 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.org.eclipse.jdt.internal.core;
  12. import java.util.*;
  13. import java.util.zip.ZipEntry;
  14. import java.util.zip.ZipFile;
  15. import org.eclipse.core.resources.IResource;
  16. import org.eclipse.core.resources.ResourcesPlugin;
  17. import org.eclipse.core.runtime.CoreException;
  18. import org.eclipse.core.runtime.IPath;
  19. import org.aspectj.org.eclipse.jdt.core.*;
  20. import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
  21. import org.aspectj.org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject;
  22. import org.aspectj.org.eclipse.jdt.internal.core.util.Util;
  23. /**
  24. * A package fragment root that corresponds to a .jar or .zip.
  25. *
  26. * <p>NOTE: The only visible entries from a .jar or .zip package fragment root
  27. * are .class files.
  28. * <p>NOTE: A jar package fragment root may or may not have an associated resource.
  29. *
  30. * @see org.aspectj.org.eclipse.jdt.core.IPackageFragmentRoot
  31. * @see org.aspectj.org.eclipse.jdt.internal.core.JarPackageFragmentRootInfo
  32. */
  33. public class JarPackageFragmentRoot extends PackageFragmentRoot {
  34. public final static ArrayList EMPTY_LIST = new ArrayList();
  35. /**
  36. * The path to the jar file
  37. * (a workspace relative path if the jar is internal,
  38. * or an OS path if the jar is external)
  39. */
  40. protected final IPath jarPath;
  41. /**
  42. * Constructs a package fragment root which is the root of the Java package directory hierarchy
  43. * based on a JAR file that is not contained in a <code>IJavaProject</code> and
  44. * does not have an associated <code>IResource</code>.
  45. */
  46. protected JarPackageFragmentRoot(IPath jarPath, JavaProject project) {
  47. super(null, project);
  48. this.jarPath = jarPath;
  49. }
  50. /**
  51. * Constructs a package fragment root which is the root of the Java package directory hierarchy
  52. * based on a JAR file.
  53. */
  54. protected JarPackageFragmentRoot(IResource resource, JavaProject project) {
  55. super(resource, project);
  56. this.jarPath = resource.getFullPath();
  57. }
  58. /**
  59. * Compute the package fragment children of this package fragment root.
  60. * These are all of the directory zip entries, and any directories implied
  61. * by the path of class files contained in the jar of this package fragment root.
  62. * Has the side effect of opening the package fragment children.
  63. */
  64. protected boolean computeChildren(OpenableElementInfo info, Map newElements) throws JavaModelException {
  65. ArrayList vChildren= new ArrayList();
  66. final int JAVA = 0;
  67. final int NON_JAVA = 1;
  68. ZipFile jar= null;
  69. try {
  70. jar= getJar();
  71. HashtableOfArrayToObject packageFragToTypes= new HashtableOfArrayToObject();
  72. // always create the default package
  73. packageFragToTypes.put(CharOperation.NO_STRINGS, new ArrayList[] { EMPTY_LIST, EMPTY_LIST });
  74. for (Enumeration e= jar.entries(); e.hasMoreElements();) {
  75. ZipEntry member= (ZipEntry) e.nextElement();
  76. initPackageFragToTypes(packageFragToTypes, member.getName(), member.isDirectory());
  77. }
  78. //loop through all of referenced packages, creating package fragments if necessary
  79. // and cache the entry names in the infos created for those package fragments
  80. for (int i = 0, length = packageFragToTypes.keyTable.length; i < length; i++) {
  81. String[] pkgName = (String[]) packageFragToTypes.keyTable[i];
  82. if (pkgName == null) continue;
  83. ArrayList[] entries= (ArrayList[]) packageFragToTypes.get(pkgName);
  84. JarPackageFragment packFrag= (JarPackageFragment) getPackageFragment(pkgName);
  85. JarPackageFragmentInfo fragInfo= new JarPackageFragmentInfo();
  86. int resLength= entries[NON_JAVA].size();
  87. if (resLength == 0) {
  88. packFrag.computeNonJavaResources(CharOperation.NO_STRINGS, packFrag, fragInfo, jar.getName());
  89. } else {
  90. String[] resNames= new String[resLength];
  91. entries[NON_JAVA].toArray(resNames);
  92. packFrag.computeNonJavaResources(resNames, packFrag, fragInfo, jar.getName());
  93. }
  94. packFrag.computeChildren(fragInfo, entries[JAVA]);
  95. newElements.put(packFrag, fragInfo);
  96. vChildren.add(packFrag);
  97. }
  98. } catch (CoreException e) {
  99. if (e instanceof JavaModelException) throw (JavaModelException)e;
  100. throw new JavaModelException(e);
  101. } finally {
  102. JavaModelManager.getJavaModelManager().closeZipFile(jar);
  103. }
  104. IJavaElement[] children= new IJavaElement[vChildren.size()];
  105. vChildren.toArray(children);
  106. info.setChildren(children);
  107. return true;
  108. }
  109. /**
  110. * Returns a new element info for this element.
  111. */
  112. protected Object createElementInfo() {
  113. return new JarPackageFragmentRootInfo();
  114. }
  115. /**
  116. * A Jar is always K_BINARY.
  117. */
  118. protected int determineKind(IResource underlyingResource) {
  119. return IPackageFragmentRoot.K_BINARY;
  120. }
  121. /**
  122. * Returns true if this handle represents the same jar
  123. * as the given handle. Two jars are equal if they share
  124. * the same zip file.
  125. *
  126. * @see Object#equals
  127. */
  128. public boolean equals(Object o) {
  129. if (this == o)
  130. return true;
  131. if (o instanceof JarPackageFragmentRoot) {
  132. JarPackageFragmentRoot other= (JarPackageFragmentRoot) o;
  133. return this.jarPath.equals(other.jarPath);
  134. }
  135. return false;
  136. }
  137. public String getElementName() {
  138. return this.jarPath.lastSegment();
  139. }
  140. /**
  141. * Returns the underlying ZipFile for this Jar package fragment root.
  142. *
  143. * @exception CoreException if an error occurs accessing the jar
  144. */
  145. public ZipFile getJar() throws CoreException {
  146. return JavaModelManager.getJavaModelManager().getZipFile(getPath());
  147. }
  148. /**
  149. * @see IPackageFragmentRoot
  150. */
  151. public int getKind() {
  152. return IPackageFragmentRoot.K_BINARY;
  153. }
  154. /**
  155. * Returns an array of non-java resources contained in the receiver.
  156. */
  157. public Object[] getNonJavaResources() throws JavaModelException {
  158. // We want to show non java resources of the default package at the root (see PR #1G58NB8)
  159. Object[] defaultPkgResources = ((JarPackageFragment) getPackageFragment(CharOperation.NO_STRINGS)).storedNonJavaResources();
  160. int length = defaultPkgResources.length;
  161. if (length == 0)
  162. return defaultPkgResources;
  163. Object[] nonJavaResources = new Object[length];
  164. for (int i = 0; i < length; i++) {
  165. JarEntryResource nonJavaResource = (JarEntryResource) defaultPkgResources[i];
  166. nonJavaResources[i] = nonJavaResource.clone(this);
  167. }
  168. return nonJavaResources;
  169. }
  170. public PackageFragment getPackageFragment(String[] pkgName) {
  171. return new JarPackageFragment(this, pkgName);
  172. }
  173. /**
  174. * @see IPackageFragmentRoot
  175. */
  176. public IPath getPath() {
  177. if (isExternal()) {
  178. return this.jarPath;
  179. } else {
  180. return super.getPath();
  181. }
  182. }
  183. public IResource getResource() {
  184. if (this.resource == null) {
  185. this.resource = JavaModel.getTarget(ResourcesPlugin.getWorkspace().getRoot(), this.jarPath, false);
  186. }
  187. if (this.resource instanceof IResource) {
  188. return super.getResource();
  189. } else {
  190. // external jar
  191. return null;
  192. }
  193. }
  194. /**
  195. * @see IJavaElement
  196. */
  197. public IResource getUnderlyingResource() throws JavaModelException {
  198. if (isExternal()) {
  199. if (!exists()) throw newNotPresentException();
  200. return null;
  201. } else {
  202. return super.getUnderlyingResource();
  203. }
  204. }
  205. public int hashCode() {
  206. return this.jarPath.hashCode();
  207. }
  208. private void initPackageFragToTypes(HashtableOfArrayToObject packageFragToTypes, String entryName, boolean isDirectory) {
  209. int lastSeparator = isDirectory ? entryName.length()-1 : entryName.lastIndexOf('/');
  210. String[] pkgName = Util.splitOn('/', entryName, 0, lastSeparator);
  211. String[] existing = null;
  212. int length = pkgName.length;
  213. int existingLength = length;
  214. while (existingLength >= 0) {
  215. existing = (String[]) packageFragToTypes.getKey(pkgName, existingLength);
  216. if (existing != null) break;
  217. existingLength--;
  218. }
  219. JavaModelManager manager = JavaModelManager.getJavaModelManager();
  220. IJavaProject project = getJavaProject();
  221. for (int i = existingLength; i < length; i++) {
  222. if (Util.isValidFolderNameForPackage(pkgName[i], project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true))) {
  223. System.arraycopy(existing, 0, existing = new String[i+1], 0, i);
  224. existing[i] = manager.intern(pkgName[i]);
  225. packageFragToTypes.put(existing, new ArrayList[] { EMPTY_LIST, EMPTY_LIST });
  226. } else {
  227. // non-Java resource folder
  228. if (!isDirectory) {
  229. ArrayList[] children = (ArrayList[]) packageFragToTypes.get(existing);
  230. if (children[1/*NON_JAVA*/] == EMPTY_LIST) children[1/*NON_JAVA*/] = new ArrayList();
  231. children[1/*NON_JAVA*/].add(entryName);
  232. }
  233. return;
  234. }
  235. }
  236. if (isDirectory)
  237. return;
  238. // add classfile info amongst children
  239. ArrayList[] children = (ArrayList[]) packageFragToTypes.get(pkgName);
  240. if (org.aspectj.org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entryName)) {
  241. if (children[0/*JAVA*/] == EMPTY_LIST) children[0/*JAVA*/] = new ArrayList();
  242. String fileName = entryName.substring(lastSeparator + 1);
  243. children[0/*JAVA*/].add(fileName);
  244. } else {
  245. if (children[1/*NON_JAVA*/] == EMPTY_LIST) children[1/*NON_JAVA*/] = new ArrayList();
  246. children[1/*NON_JAVA*/].add(entryName);
  247. }
  248. }
  249. /**
  250. * @see IPackageFragmentRoot
  251. */
  252. public boolean isArchive() {
  253. return true;
  254. }
  255. /**
  256. * @see IPackageFragmentRoot
  257. */
  258. public boolean isExternal() {
  259. return getResource() == null;
  260. }
  261. /**
  262. * Jars and jar entries are all read only
  263. */
  264. public boolean isReadOnly() {
  265. return true;
  266. }
  267. /**
  268. * Returns whether the corresponding resource or associated file exists
  269. */
  270. protected boolean resourceExists() {
  271. if (this.isExternal()) {
  272. return
  273. JavaModel.getTarget(
  274. ResourcesPlugin.getWorkspace().getRoot(),
  275. this.getPath(), // don't make the path relative as this is an external archive
  276. true) != null;
  277. } else {
  278. return super.resourceExists();
  279. }
  280. }
  281. protected void toStringAncestors(StringBuffer buffer) {
  282. if (isExternal())
  283. // don't show project as it is irrelevant for external jar files.
  284. // also see https://bugs.eclipse.org/bugs/show_bug.cgi?id=146615
  285. return;
  286. super.toStringAncestors(buffer);
  287. }
  288. }