protected IFile getManifest(IPackageFragmentRoot[] roots, IJavaProject javaProject)
      throws CoreException {

    IFolder metaFolder = null;
    for (IPackageFragmentRoot root : roots) {
      if (!root.isArchive() && !root.isExternal()) {
        IResource resource = root.getResource();
        metaFolder = getMetaFolder(resource);
        if (metaFolder != null) {
          break;
        }
      }
    }

    // Otherwise look for manifest file in the java project:
    if (metaFolder == null) {
      metaFolder = getMetaFolder(javaProject.getProject());
    }

    if (metaFolder != null) {
      IResource[] members = metaFolder.members();
      if (members != null) {
        for (IResource mem : members) {
          if (MANIFEST_FILE.equals(mem.getName().toUpperCase()) && mem instanceof IFile) {
            return (IFile) mem;
          }
        }
      }
    }

    return null;
  }
  private static Change createPackageFragmentRootDeleteChange(IPackageFragmentRoot root)
      throws JavaModelException {
    IResource resource = root.getResource();
    if (resource != null && resource.isLinked()) {
      // XXX using this code is a workaround for jcore bug 31998
      // jcore cannot handle linked stuff
      // normally, we should always create DeletePackageFragmentRootChange
      CompositeChange composite =
          new DynamicValidationStateChange(
              RefactoringCoreMessages.DeleteRefactoring_delete_package_fragment_root);

      ClasspathChange change =
          ClasspathChange.removeEntryChange(root.getJavaProject(), root.getRawClasspathEntry());
      if (change != null) {
        composite.add(change);
      }
      Assert.isTrue(!Checks.isClasspathDelete(root)); // checked in preconditions
      composite.add(createDeleteChange(resource));

      return composite;
    } else {
      Assert.isTrue(!root.isExternal());
      // TODO remove the query argument
      return new DeletePackageFragmentRootChange(root, true, null);
    }
  }
 /** {@inheritDoc} */
 public long getTimeStamp() {
   IResource resource = root.getResource();
   if (resource != null && resource.exists()) {
     return resource.getLocalTimeStamp();
   } else {
     return IResource.NULL_STAMP;
   }
 }
예제 #4
0
 public Type(IType type) {
   if (type == null) throw new IllegalArgumentException("Invalid argument: JDT Type is null");
   this.type = type;
   packageName = type.getPackageFragment().getElementName();
   IPackageFragmentRoot packageRoot = (IPackageFragmentRoot) type.getPackageFragment().getParent();
   IFolder folder = (IFolder) packageRoot.getResource();
   sourceFolder = folder.getName();
   // sourceFolder = type.getfo
 }
 /**
  * Returns the path for this fragment in a OS friendly string. Note that in windows, the method
  * for getting the absolute path varies for source folders and jars
  *
  * @param fragmentRoot The fragment containing classes
  * @return The path of this fragment root, os-friendly
  */
 private String getPathFor(final IPackageFragmentRoot fragmentRoot) {
   final IResource resource = fragmentRoot.getResource();
   final IPath path;
   if (resource != null) {
     path = resource.getRawLocation();
   } else {
     path = fragmentRoot.getPath().makeAbsolute();
   }
   return path.toOSString();
 }
예제 #6
0
 /* (non-Javadoc)
  * Method declared on ViewerFilter.
  */
 public boolean select(Viewer viewer, Object parentElement, Object element) {
   if (element instanceof IPackageFragmentRoot) {
     IPackageFragmentRoot root = (IPackageFragmentRoot) element;
     if (root.isArchive()) {
       // don't filter out JARs contained in the project itself
       IResource resource = root.getResource();
       if (resource != null) {
         IProject jarProject = resource.getProject();
         IProject container = root.getJavaProject().getProject();
         return container.equals(jarProject);
       }
       return false;
     }
   }
   return true;
 }
예제 #7
0
  private static String fileArg(IProject project, IFile file) {
    String FILE_ARG_ERROR_PREFIX =
        "When trying to create clojure.main " + "file arg to launch, was " + "unable to ";

    IPath filePath = file.getLocation();

    IJavaProject javaProject = ClojureCore.getJavaProject(project);
    try {
      IPackageFragmentRoot filePFR = findPackageFragmentRoot(javaProject, filePath);
      if (filePFR != null) {
        IPath pfrPath = filePFR.getResource().getLocation();
        // TODO we can use .makeRelativeTo again when we decide not to support Eclipse 3.4 anymore
        //    			String classpathRelativeArg = filePath.makeRelativeTo(pfrPath).toString();
        String classpathRelativeArg = filePath.toString().substring(pfrPath.toString().length());
        if (classpathRelativeArg.startsWith("/")) {
          classpathRelativeArg = classpathRelativeArg.substring(1);
        }
        return " \"@/" + classpathRelativeArg + "\"";
      } else {
        CCWPlugin.logError(
            FILE_ARG_ERROR_PREFIX
                + " find package fragment root for file "
                + file
                + " in project "
                + project);
        return "";
      }
    } catch (JavaModelException jme) {
      CCWPlugin.logError(
          FILE_ARG_ERROR_PREFIX
              + " complete due to a JavaModelException finding package fragment root for file "
              + file
              + " in project "
              + project,
          jme);
      return "";
    }
  }
  private static boolean isRootAt(IPackageFragmentRoot root, IPath entry) {
    try {
      IClasspathEntry cpe = root.getRawClasspathEntry();
      if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
        IPath outputLocation = cpe.getOutputLocation();
        if (outputLocation == null) outputLocation = root.getJavaProject().getOutputLocation();

        IPath location =
            ResourcesPlugin.getWorkspace().getRoot().findMember(outputLocation).getLocation();
        if (entry.equals(location)) return true;
      }
    } catch (JavaModelException e) {
      JavaPlugin.log(e);
    }

    IResource resource = root.getResource();
    if (resource != null && entry.equals(resource.getLocation())) return true;

    IPath path = root.getPath();
    if (path != null && entry.equals(path)) return true;

    return false;
  }
 /** {@inheritDoc} */
 public IStorage getProgramResource(
     ProgramResourceLocator.File resourcePath, boolean createIfPossible) {
   IPath path =
       new Path(pathMapper.getModuleResourcePath(resourcePath.getModuleName()).getPathString())
           .append(resourcePath.getName());
   IFile resourceFile = ((IContainer) root.getResource()).getFile(path);
   if (createIfPossible && !resourceFile.exists()) {
     try {
       resourceFile.create(
           new InputStream() {
             @Override
             public int read() throws IOException {
               return -1;
             }
           },
           true,
           null);
     } catch (CoreException e) {
       Util.log(e, "Error creating file " + resourcePath);
     }
   }
   return resourceFile;
 }
예제 #10
0
 private static URL findResourceURL(
     IJavaProject javaProject, Set<IJavaProject> visited, boolean isFirstProject, String name) {
   if (visited.contains(javaProject)) return null;
   visited.add(javaProject);
   try {
     IPath outPath =
         javaProject
             .getProject()
             .getLocation()
             .removeLastSegments(1)
             .append(javaProject.getOutputLocation());
     outPath = outPath.addTrailingSeparator();
     {
       URL url = toURL(outPath.append(name));
       if (url != null) {
         return url;
       }
     }
     for (IPackageFragmentRoot fragment : javaProject.getPackageFragmentRoots()) {
       if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
         URL url = toURL(fragment.getResource().getLocation().append(name));
         if (url != null) {
           return url;
         }
       }
     }
     // urls.add(out);
     IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
     for (IClasspathEntry entry : entries) {
       switch (entry.getEntryKind()) {
         case IClasspathEntry.CPE_LIBRARY:
           {
             // TODO
             IClasspathEntry resolveEntry = JavaCore.getResolvedClasspathEntry(entry);
             File file = resolveEntry.getPath().toFile();
             IPath path = resolveEntry.getPath();
             if (!file.exists()) {
               String projectName = path.segment(0);
               IProject project =
                   javaProject.getProject().getWorkspace().getRoot().getProject(projectName);
               path = project.getLocation().append(path.removeFirstSegments(1));
             }
             String spec = "jar:file:" + path.toString() + "!/" + name;
             try {
               URL url2 = new URL(spec);
               url2.getContent();
               return url2;
             } catch (Exception e) {
             }
           }
           break;
         case IClasspathEntry.CPE_CONTAINER:
           break;
         case IClasspathEntry.CPE_VARIABLE:
           {
             {
               // TODO
               URL url = toURL(outPath.append(name));
               if (url != null) {
                 return url;
               }
             }
           }
           break;
         case IClasspathEntry.CPE_PROJECT:
           {
             if (isFirstProject || entry.isExported()) {
               URL url = findResourceURL(getJavaProject(entry), visited, false, name);
               if (url != null) {
                 return url;
               }
             }
             break;
           }
       }
     }
   } catch (JavaModelException e) {
     e.printStackTrace();
   }
   return null;
 }
  @Override
  public void run(IAction action) {
    if (action.getId() == FILE_LOCATION_ACTION) {
      // 文件
    } else if (action.getId() == FOLDER_LOCATION_ACTION) {
      // 文件夹
    } else {
      // 其他
    }

    if (selection instanceof IStructuredSelection) {
      IStructuredSelection sel = (IStructuredSelection) selection;
      Object obj = sel.getFirstElement();

      /*
       * Donot work on IWorkSpace
      if(obj instanceof IResource)
      {
      	IResource resource = (IResource) Platform.getAdapterManager().getAdapter(obj,IResource.class);
      	if (resource != null) {
      		try {
      			IPath path = resource.getRawLocation() ;
      			File winfile = path.toFile();
      			String winPath = winfile.getParent();

      			Runtime.getRuntime().exec("explorer " + winPath);
      		} catch (IOException e) {
      			e.printStackTrace();
      		}
      	}
      }
      */

      IPath path = null;
      System.out.println(obj.getClass());
      if (obj instanceof IFile) {
        IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj, IFile.class);
        path = file.getRawLocation();
      } else if (obj instanceof IFolder) {
        IFolder folder = (IFolder) Platform.getAdapterManager().getAdapter(obj, IFolder.class);
        path = folder.getRawLocation();
      } else if (obj instanceof IPackageFragmentRoot) {
        if (obj.getClass()
            .toString()
            .equals("class org.eclipse.jdt.internal.core.JarPackageFragmentRoot")) {
          MessageDialog.openInformation(
              workbenchpart.getSite().getShell(),
              "NeoFactory Enhance Plugins",
              "Sorry, this operation is not supported. \n\n"
                  + "JarPackageFragmentRoot extends from PackageFragmentRoot, \n"
                  + "PackageFragmentRoot implements IPackageFragmentRoot, \n"
                  + "so I cannot hide this menu as while as I need that shows on source packages.");
        } else {
          IPackageFragmentRoot packageFragRoot =
              (IPackageFragmentRoot)
                  Platform.getAdapterManager().getAdapter(obj, IPackageFragmentRoot.class);
          path = packageFragRoot.getResource().getRawLocation();
        }
      } else if (obj instanceof IPackageFragment) {
        IPackageFragment packageFrag =
            (IPackageFragment) Platform.getAdapterManager().getAdapter(obj, IPackageFragment.class);
        path = packageFrag.getResource().getRawLocation();
      } else if (obj instanceof ICompilationUnit) {
        ICompilationUnit unit =
            (ICompilationUnit) Platform.getAdapterManager().getAdapter(obj, ICompilationUnit.class);
        path = unit.getResource().getRawLocation();
      }

      if (path != null) {
        try {
          File winfile = path.toFile();
          String winPath = winfile.getParent();

          Runtime.getRuntime().exec("explorer " + winPath);
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }