Пример #1
0
 public ASTReader(IJavaProject iJavaProject, IProgressMonitor monitor) {
   if (monitor != null)
     monitor.beginTask("Parsing selected Java Project", getNumberOfCompilationUnits(iJavaProject));
   systemObject = new SystemObject();
   examinedProject = iJavaProject;
   try {
     IPackageFragmentRoot[] iPackageFragmentRoots = iJavaProject.getPackageFragmentRoots();
     for (IPackageFragmentRoot iPackageFragmentRoot : iPackageFragmentRoots) {
       IJavaElement[] children = iPackageFragmentRoot.getChildren();
       for (IJavaElement child : children) {
         if (child.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
           IPackageFragment iPackageFragment = (IPackageFragment) child;
           ICompilationUnit[] iCompilationUnits = iPackageFragment.getCompilationUnits();
           for (ICompilationUnit iCompilationUnit : iCompilationUnits) {
             if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException();
             systemObject.addClasses(parseAST(iCompilationUnit));
             if (monitor != null) monitor.worked(1);
           }
         }
       }
     }
   } catch (JavaModelException e) {
     e.printStackTrace();
   }
   if (monitor != null) monitor.done();
 }
 /**
  * Initializes the source folder field with a valid package fragment root. The package fragment
  * root is computed from the given Java element.
  *
  * @param elem the Java element used to compute the initial package fragment root used as the
  *     source folder
  */
 protected void initContainerPage(IJavaElement elem) {
   IPackageFragmentRoot initRoot = null;
   if (elem != null) {
     initRoot = JavaModelUtil.getPackageFragmentRoot(elem);
     try {
       if (initRoot == null || initRoot.getKind() != IPackageFragmentRoot.K_SOURCE) {
         IJavaProject jproject = elem.getJavaProject();
         if (jproject != null) {
           initRoot = null;
           if (jproject.exists()) {
             IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots();
             for (int i = 0; i < roots.length; i++) {
               if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                 initRoot = roots[i];
                 break;
               }
             }
           }
           if (initRoot == null) {
             initRoot = jproject.getPackageFragmentRoot(jproject.getResource());
           }
         }
       }
     } catch (JavaModelException e) {
       JavaPlugin.log(e);
     }
   }
   setPackageFragmentRoot(initRoot, true);
 }
  protected List<String> getSourceFolders(IProject project) {
    List<String> result = new ArrayList<String>();
    IJavaProject javaProject = JavaCore.create(project);
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    try {
      for (IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) {
        if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
          IPath path = packageFragmentRoot.getPath();
          IFolder folder = root.getFolder(path);
          String location = folder.getLocation().toString();

          if (!location.contains("src-gen")) {
            result.add(location);
          }
        }
      }

      for (IProject referencedProject : javaProject.getProject().getReferencedProjects()) {
        if (referencedProject.isAccessible() && referencedProject.hasNature(JavaCore.NATURE_ID)) {
          result.addAll(getSourceFolders(referencedProject));
        }
      }

    } catch (JavaModelException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();

    } catch (CoreException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return result;
  }
 protected IPackageFragmentRoot getFragmentRoot(IJavaElement elem) {
   IPackageFragmentRoot initRoot = null;
   if (elem != null) {
     initRoot = (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
     try {
       if (initRoot == null || initRoot.getKind() != IPackageFragmentRoot.K_SOURCE) {
         IJavaProject jproject = elem.getJavaProject();
         if (jproject != null) {
           initRoot = null;
           if (jproject.exists()) {
             IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots();
             for (int i = 0; i < roots.length; i++) {
               if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                 initRoot = roots[i];
                 break;
               }
             }
           }
           if (initRoot == null) {
             initRoot = jproject.getPackageFragmentRoot(jproject.getResource());
           }
         }
       }
     } catch (JavaModelException e) {
       // TODO
       e.printStackTrace();
     }
   }
   return initRoot;
 }
 public static IPackageFragmentRoot getSourceFolder(IJavaProject javaProject, String name)
     throws JavaModelException {
   IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
   for (int i = 0; i < roots.length; i++) {
     if (!roots[i].isArchive() && roots[i].getElementName().equals(name)) return roots[i];
   }
   return null;
 }
Пример #6
0
  /**
   * This method will return the first source folder of the Java project
   *
   * @param javaProject - the java project where the source folder needs to be indentified
   * @return
   * @throws JavaModelException
   */
  public static IPackageFragmentRoot getSourceFolder(IJavaProject javaProject)
      throws JavaModelException {
    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
      if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
        return root;
      }
    }

    return null;
  }
  private static IPackageFragmentRoot[] findRootsInProject(IPath entry, IJavaProject project) {
    ArrayList<IPackageFragmentRoot> result = new ArrayList<IPackageFragmentRoot>();

    try {
      IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
      for (int i = 0; i < roots.length; i++) {
        IPackageFragmentRoot packageFragmentRoot = roots[i];
        if (isRootAt(packageFragmentRoot, entry)) result.add(packageFragmentRoot);
      }
    } catch (Exception e) {
      JavaPlugin.log(e);
    }

    return result.toArray(new IPackageFragmentRoot[result.size()]);
  }
 /*
  * @param projects a collection of IJavaProject
  * @return Array of IPackageFragmentRoot, one element for each packageFragmentRoot which lies within a project in <code> projects </code> .
  */
 private static IPackageFragmentRoot[] getAllScopeElements(
     IJavaProject project, boolean onlySourceRoots) throws JavaModelException {
   Collection<IJavaProject> referencingProjects = getReferencingProjects(project);
   List<IPackageFragmentRoot> result = new ArrayList<IPackageFragmentRoot>();
   for (Iterator<IJavaProject> it = referencingProjects.iterator(); it.hasNext(); ) {
     IJavaProject javaProject = it.next();
     IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
     // Add all package fragment roots except archives
     for (int i = 0; i < roots.length; i++) {
       IPackageFragmentRoot root = roots[i];
       if (!onlySourceRoots || root.getKind() == IPackageFragmentRoot.K_SOURCE) result.add(root);
     }
   }
   return result.toArray(new IPackageFragmentRoot[result.size()]);
 }
 /** @since 2.4 */
 private void updateCache(IJavaProject project) {
   Set<PackageFragmentRootData> datas = newHashSet();
   try {
     if (project.exists() && project.getProject().isAccessible()) {
       for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
         boolean isCachable = root.isArchive() || root.isExternal();
         if (isCachable) datas.add(getCachedData(root));
       }
     }
   } catch (JavaModelException e) {
     if (!e.isDoesNotExist())
       log.error("Error getting package fragments roots of " + project.getElementName(), e);
   } finally {
     clearCache(project, datas);
   }
 }
  public void testPreferredPackageManager() throws JavaModelException {
    BatchArtifactType type = BatchArtifactType.BATCHLET;

    IJavaProject javaProject = EclipseUtil.getJavaProject(project);

    javaProject.getPackageFragmentRoots()[0].createPackageFragment(
        "batch.artifact", true, new NullProgressMonitor());

    PreferredPackageManager.savePreferredPackage(project, type, TEST_PACKAGE_NAME);

    IPackageFragment suggestedPackageName =
        PreferredPackageManager.getPackageSuggestion(project, type);

    assertEquals(
        "Unexpected suggested package name",
        TEST_PACKAGE_NAME,
        suggestedPackageName.getPath().toString());
  }
Пример #11
0
 public static int getNumberOfCompilationUnits(IJavaProject iJavaProject) {
   int numberOfCompilationUnits = 0;
   try {
     IPackageFragmentRoot[] iPackageFragmentRoots = iJavaProject.getPackageFragmentRoots();
     for (IPackageFragmentRoot iPackageFragmentRoot : iPackageFragmentRoots) {
       IJavaElement[] children = iPackageFragmentRoot.getChildren();
       for (IJavaElement child : children) {
         if (child.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
           IPackageFragment iPackageFragment = (IPackageFragment) child;
           ICompilationUnit[] iCompilationUnits = iPackageFragment.getCompilationUnits();
           numberOfCompilationUnits += iCompilationUnits.length;
         }
       }
     }
   } catch (JavaModelException e) {
     e.printStackTrace();
   }
   return numberOfCompilationUnits;
 }
 private String[] findDependenciesFor(final IJavaProject javaProject) {
   try {
     final IPackageFragmentRoot[] fragmentRoots = javaProject.getPackageFragmentRoots();
     final Collection<String> rootPaths = new ArrayList<String>(fragmentRoots.length);
     for (int i = 0; i < fragmentRoots.length; i++) {
       final String path = getPathFor(fragmentRoots[i]);
       if (path != null) {
         rootPaths.add(path);
       }
     }
     return rootPaths.toArray(new String[rootPaths.size()]);
   } catch (final JavaModelException ex) {
     FeatureEditorPlugin.instance()
         .log(
             IStatus.WARNING,
             "Could not get package fragment roots for project "
                 + javaProject.getProject().getName());
     return new String[0];
   }
 }
Пример #13
0
 private void createReport(IProgressMonitor monitor) throws CoreException, IOException {
   final int work = session.getScope().size();
   monitor.beginTask(
       NLS.bind(CoreMessages.ExportingSession_task, session.getDescription()), work * 2);
   final SessionAnalyzer analyzer = new SessionAnalyzer();
   final IJavaModelCoverage modelCoverage =
       analyzer.processSession(session, new SubProgressMonitor(monitor, work));
   final IReportVisitor formatter = createFormatter();
   formatter.visitInfo(analyzer.getSessionInfos(), analyzer.getExecutionData());
   final IReportGroupVisitor modelgroup = formatter.visitGroup(session.getDescription());
   for (IJavaProject project : modelCoverage.getProjects()) {
     final IReportGroupVisitor projectgroup = modelgroup.visitGroup(project.getElementName());
     for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
       final IBundleCoverage coverage = (IBundleCoverage) modelCoverage.getCoverageFor(root);
       if (coverage != null) {
         projectgroup.visitBundle(coverage, createSourceFileLocator(root));
         monitor.worked(1);
       }
     }
   }
   formatter.visitEnd();
   monitor.done();
 }
Пример #14
0
  protected IPackageFragment choosePackage() {
    List<IJavaElement> packagesList = new LinkedList<IJavaElement>();
    try {
      IProject actualProject =
          ResourcesPlugin.getWorkspace().getRoot().getProject(getContainerFullPath().segment(0));
      IJavaProject javaProject =
          JavaModelManager.getJavaModelManager().getJavaModel().getJavaProject(actualProject);
      IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
      for (int k = 0; k < roots.length; k++) {
        if (roots[k].getKind() == IPackageFragmentRoot.K_SOURCE) {
          IJavaElement[] children = roots[k].getChildren();
          for (int i = 0; i < children.length; i++) {
            packagesList.add(children[i]);
          }
        }
      }
    } catch (JavaModelException e) {
      // JTourBusPlugin.log(e);
      e.printStackTrace();
    }
    IJavaElement[] packages = packagesList.toArray(new IJavaElement[packagesList.size()]);

    ElementListSelectionDialog dialog =
        new ElementListSelectionDialog(
            getShell(), new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
    dialog.setIgnoreCase(false);
    dialog.setTitle(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_title);
    dialog.setMessage(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_description);
    dialog.setEmptyListMessage(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_empty);
    dialog.setFilter(_packageDialogField.getText());
    dialog.setElements(packages);
    if (dialog.open() == Window.OK) {
      return (IPackageFragment) dialog.getFirstResult();
    }
    return null;
  }
Пример #15
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;
 }