PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 157 lines | 96 code | 4 blank | 57 comment | 25 complexity | 9a74cc88f725445df2d10fa667237789 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.ArrayList;
  13. import org.eclipse.core.resources.IContainer;
  14. import org.eclipse.core.resources.IResource;
  15. import org.eclipse.core.runtime.IStatus;
  16. import org.eclipse.core.runtime.Path;
  17. import org.aspectj.org.eclipse.jdt.core.IJavaElement;
  18. import org.aspectj.org.eclipse.jdt.core.IJavaModelStatus;
  19. import org.aspectj.org.eclipse.jdt.core.IJavaModelStatusConstants;
  20. import org.aspectj.org.eclipse.jdt.core.IJavaProject;
  21. import org.aspectj.org.eclipse.jdt.core.IPackageFragment;
  22. import org.aspectj.org.eclipse.jdt.core.IPackageFragmentRoot;
  23. import org.aspectj.org.eclipse.jdt.core.JavaConventions;
  24. import org.aspectj.org.eclipse.jdt.core.JavaCore;
  25. import org.aspectj.org.eclipse.jdt.core.JavaModelException;
  26. import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
  27. import org.aspectj.org.eclipse.jdt.internal.core.util.Messages;
  28. import org.aspectj.org.eclipse.jdt.internal.core.util.Util;
  29. /**
  30. * This operation creates a new package fragment under a given package fragment root.
  31. * The following must be specified: <ul>
  32. * <li>the package fragment root
  33. * <li>the package name
  34. * </ul>
  35. * <p>Any needed folders/package fragments are created.
  36. * If the package fragment already exists, this operation has no effect.
  37. * The result elements include the <code>IPackageFragment</code> created and any side effect
  38. * package fragments that were created.
  39. *
  40. * <p>NOTE: A default package fragment exists by default for a given root.
  41. *
  42. * <p>Possible exception conditions: <ul>
  43. * <li>Package fragment root is read-only
  44. * <li>Package fragment's name is taken by a simple (non-folder) resource
  45. * </ul>
  46. */
  47. public class CreatePackageFragmentOperation extends JavaModelOperation {
  48. /**
  49. * The fully qualified, dot-delimited, package name.
  50. */
  51. protected String[] pkgName;
  52. /**
  53. * When executed, this operation will create a package fragment with the given name
  54. * under the given package fragment root. The dot-separated name is broken into
  55. * segments. Intermediate folders are created as required for each segment.
  56. * If the folders already exist, this operation has no effect.
  57. */
  58. public CreatePackageFragmentOperation(IPackageFragmentRoot parentElement, String packageName, boolean force) {
  59. super(null, new IJavaElement[]{parentElement}, force);
  60. this.pkgName = packageName == null ? null : Util.getTrimmedSimpleNames(packageName);
  61. }
  62. /**
  63. * Execute the operation - creates the new package fragment and any
  64. * side effect package fragments.
  65. *
  66. * @exception JavaModelException if the operation is unable to complete
  67. */
  68. protected void executeOperation() throws JavaModelException {
  69. try {
  70. JavaElementDelta delta = null;
  71. PackageFragmentRoot root = (PackageFragmentRoot) getParentElement();
  72. beginTask(Messages.operation_createPackageFragmentProgress, this.pkgName.length);
  73. IContainer parentFolder = (IContainer) root.getResource();
  74. String[] sideEffectPackageName = CharOperation.NO_STRINGS;
  75. ArrayList results = new ArrayList(this.pkgName.length);
  76. char[][] inclusionPatterns = root.fullInclusionPatternChars();
  77. char[][] exclusionPatterns = root.fullExclusionPatternChars();
  78. int i;
  79. for (i = 0; i < this.pkgName.length; i++) {
  80. String subFolderName = this.pkgName[i];
  81. sideEffectPackageName = Util.arrayConcat(sideEffectPackageName, subFolderName);
  82. IResource subFolder = parentFolder.findMember(subFolderName);
  83. if (subFolder == null) {
  84. createFolder(parentFolder, subFolderName, force);
  85. parentFolder = parentFolder.getFolder(new Path(subFolderName));
  86. IPackageFragment addedFrag = root.getPackageFragment(sideEffectPackageName);
  87. if (!Util.isExcluded(parentFolder, inclusionPatterns, exclusionPatterns)) {
  88. if (delta == null) {
  89. delta = newJavaElementDelta();
  90. }
  91. delta.added(addedFrag);
  92. }
  93. results.add(addedFrag);
  94. } else {
  95. parentFolder = (IContainer) subFolder;
  96. }
  97. worked(1);
  98. }
  99. if (results.size() > 0) {
  100. this.resultElements = new IJavaElement[results.size()];
  101. results.toArray(this.resultElements);
  102. if (delta != null) {
  103. addDelta(delta);
  104. }
  105. }
  106. } finally {
  107. done();
  108. }
  109. }
  110. /**
  111. * Possible failures: <ul>
  112. * <li>NO_ELEMENTS_TO_PROCESS - the root supplied to the operation is
  113. * <code>null</code>.
  114. * <li>INVALID_NAME - the name provided to the operation
  115. * is <code>null</code> or is not a valid package fragment name.
  116. * <li>READ_ONLY - the root provided to this operation is read only.
  117. * <li>NAME_COLLISION - there is a pre-existing resource (file)
  118. * with the same name as a folder in the package fragment's hierarchy.
  119. * <li>ELEMENT_NOT_PRESENT - the underlying resource for the root is missing
  120. * </ul>
  121. * @see IJavaModelStatus
  122. * @see JavaConventions
  123. */
  124. public IJavaModelStatus verify() {
  125. IJavaElement parentElement = getParentElement();
  126. if (parentElement == null) {
  127. return new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS);
  128. }
  129. String packageName = this.pkgName == null ? null : Util.concatWith(this.pkgName, '.');
  130. IJavaProject project = parentElement.getJavaProject();
  131. if (this.pkgName == null || (this.pkgName.length > 0 && JavaConventions.validatePackageName(packageName, project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true)).getSeverity() == IStatus.ERROR)) {
  132. return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, packageName);
  133. }
  134. IPackageFragmentRoot root = (IPackageFragmentRoot) getParentElement();
  135. if (root.isReadOnly()) {
  136. return new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, root);
  137. }
  138. IContainer parentFolder = (IContainer) root.getResource();
  139. int i;
  140. for (i = 0; i < this.pkgName.length; i++) {
  141. IResource subFolder = parentFolder.findMember(this.pkgName[i]);
  142. if (subFolder != null) {
  143. if (subFolder.getType() != IResource.FOLDER) {
  144. return new JavaModelStatus(
  145. IJavaModelStatusConstants.NAME_COLLISION,
  146. Messages.bind(Messages.status_nameCollision, subFolder.getFullPath().toString()));
  147. }
  148. parentFolder = (IContainer) subFolder;
  149. }
  150. }
  151. return JavaModelStatus.VERIFIED_OK;
  152. }
  153. }