Java Code Examples for org.eclipse.jdt.core.IJavaModel#getJavaProjects()

The following examples show how to use org.eclipse.jdt.core.IJavaModel#getJavaProjects() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaModelMerger.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the dependent projects of the specified project.
 *
 * @param set
 *            the project set
 * @param project
 *            the project to get its dependent projects
 */
private void getDependentProjects(final Set<IProject> set, final IProject project) {
	Assert.isNotNull(set);
	Assert.isNotNull(project);
	final IJavaModel model= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
	if (model != null) {
		try {
			final String name= project.getName();
			final IJavaProject[] projects= model.getJavaProjects();
			for (int index= 0; index < projects.length; index++) {
				final String[] names= projects[index].getRequiredProjectNames();
				for (int offset= 0; offset < names.length; offset++) {
					if (name.equals(names[offset]))
						set.add(projects[index].getProject());
				}
			}
		} catch (JavaModelException exception) {
			JavaPlugin.log(exception);
		}
	}
}
 
Example 2
Source File: CompilationUnitDocumentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Fuzzy search for Java project in the workspace that matches
 * the given path.
 *
 * @param path the path to match
 * @return the matching Java project or <code>null</code>
 * @since 3.2
 */
private IJavaProject findJavaProject(IPath path) {
	if (path == null)
		return null;

	String[] pathSegments= path.segments();
	IJavaModel model= JavaCore.create(JavaPlugin.getWorkspace().getRoot());
	IJavaProject[] projects;
	try {
		projects= model.getJavaProjects();
	} catch (JavaModelException e) {
		return null; // ignore - use default JRE
	}
	for (int i= 0; i < projects.length; i++) {
		IPath projectPath= projects[i].getProject().getFullPath();
		String projectSegment= projectPath.segments()[0];
		for (int j= 0; j < pathSegments.length; j++)
			if (projectSegment.equals(pathSegments[j]))
				return projects[i];
	}
	return null;
}
 
Example 3
Source File: JavaEditorBreadcrumb.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns all accessible projects of the given Java model.
 *
 * @param model the Java model
 * @return the accessible projects of the given model
 */
private Object[] getAccessibleProjects(IJavaModel model) {
	IJavaProject[] javaProjects;
	Object[] nonJavaResources;
	try {
		javaProjects= model.getJavaProjects();
		nonJavaResources= model.getNonJavaResources();
	} catch (JavaModelException e) {
		return fParent.getChildren(model);
	}
	ArrayList<IAdaptable> result= new ArrayList<IAdaptable>(javaProjects.length + nonJavaResources.length);
	for (int i= 0; i < nonJavaResources.length; i++) {
		IProject project= (IProject)nonJavaResources[i];
		if (project.isAccessible())
			result.add(project);
	}
	for (int i= 0; i < javaProjects.length; i++) {
		IJavaProject javaProject= javaProjects[i];
		if (javaProject.getProject().isAccessible())
			result.add(javaProject);
	}
	return result.toArray(new Object[result.size()]);
}
 
Example 4
Source File: VariableBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean doesChangeRequireFullBuild(List<String> removed, List<String> changed) {
	try {
		IJavaModel model= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
		IJavaProject[] projects= model.getJavaProjects();
		for (int i= 0; i < projects.length; i++) {
			IClasspathEntry[] entries= projects[i].getRawClasspath();
			for (int k= 0; k < entries.length; k++) {
				IClasspathEntry curr= entries[k];
				if (curr.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
					String var= curr.getPath().segment(0);
					if (removed.contains(var) || changed.contains(var)) {
						return true;
					}
				}
			}
		}
	} catch (JavaModelException e) {
		return true;
	}
	return false;
}
 
Example 5
Source File: JavaProjectHelper.java    From testability-explorer with Apache License 2.0 5 votes vote down vote up
public IJavaProject[] getJavaProjects() {
  IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
  IJavaModel javaModel = JavaCore.create(workspaceRoot);
  IJavaProject[] javaProjects = null;
  try {
    javaProjects = javaModel.getJavaProjects();
  } catch (JavaModelException e) {
    logger.logException("Error getting Java Projects", e);
  }
  return javaProjects;
}
 
Example 6
Source File: BuildPathSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds a source attachment for a new archive in the existing classpaths.
 * @param elem The new classpath entry
 * @return A path to be taken for the source attachment or <code>null</code>
 */
public static IPath guessSourceAttachment(CPListElement elem) {
	if (elem.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
		return null;
	}
	IJavaProject currProject= elem.getJavaProject(); // can be null
	try {
		IJavaModel jmodel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
		IJavaProject[] jprojects= jmodel.getJavaProjects();
		for (int i= 0; i < jprojects.length; i++) {
			IJavaProject curr= jprojects[i];
			if (!curr.equals(currProject)) {
				IClasspathEntry[] entries= curr.getRawClasspath();
				for (int k= 0; k < entries.length; k++) {
					IClasspathEntry entry= entries[k];
					if (entry.getEntryKind() == elem.getEntryKind()
						&& entry.getPath().equals(elem.getPath())) {
						IPath attachPath= entry.getSourceAttachmentPath();
						if (attachPath != null && !attachPath.isEmpty()) {
							return attachPath;
						}
					}
				}
			}
		}
	} catch (JavaModelException e) {
		JavaPlugin.log(e.getStatus());
	}
	return null;
}
 
Example 7
Source File: BuildPathSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds a javadoc location for a new archive in the existing classpaths.
 * @param elem The new classpath entry
 * @return A javadoc location found in a similar classpath entry or <code>null</code>.
 */
public static String guessJavadocLocation(CPListElement elem) {
	if (elem.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
		return null;
	}
	IJavaProject currProject= elem.getJavaProject(); // can be null
	try {
		// try if the jar itself contains the source
		IJavaModel jmodel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
		IJavaProject[] jprojects= jmodel.getJavaProjects();
		for (int i= 0; i < jprojects.length; i++) {
			IJavaProject curr= jprojects[i];
			if (!curr.equals(currProject)) {
				IClasspathEntry[] entries= curr.getRawClasspath();
				for (int k= 0; k < entries.length; k++) {
					IClasspathEntry entry= entries[k];
					if (entry.getEntryKind() == elem.getEntryKind() && entry.getPath().equals(elem.getPath())) {
						IClasspathAttribute[] attributes= entry.getExtraAttributes();
						for (int n= 0; n < attributes.length; n++) {
							IClasspathAttribute attrib= attributes[n];
							if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attrib.getName())) {
								return attrib.getValue();
							}
						}
					}
				}
			}
		}
	} catch (JavaModelException e) {
		JavaPlugin.log(e.getStatus());
	}
	return null;
}
 
Example 8
Source File: OthersWorkingSetUpdater.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void updateElements() {
	Assert.isTrue(fWorkingSet != null && fWorkingSetModel != null); // init and addWorkingSet have happend

	IWorkingSet[] activeWorkingSets= fWorkingSetModel.getActiveWorkingSets();

	List<IAdaptable> result= new ArrayList<IAdaptable>();
	Set<IResource> projects= new HashSet<IResource>();
	for (int i= 0; i < activeWorkingSets.length; i++) {
		if (activeWorkingSets[i] == fWorkingSet) continue;
		IAdaptable[] elements= activeWorkingSets[i].getElements();
		for (int j= 0; j < elements.length; j++) {
			IAdaptable element= elements[j];
			IResource resource= (IResource)element.getAdapter(IResource.class);
			if (resource != null && resource.getType() == IResource.PROJECT) {
				projects.add(resource);
			}
		}
	}
	IJavaModel model= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
	try {
		IJavaProject[] jProjects= model.getJavaProjects();
		for (int i= 0; i < jProjects.length; i++) {
			if (!projects.contains(jProjects[i].getProject()))
				result.add(jProjects[i]);
		}
		Object[] rProjects= model.getNonJavaResources();
		for (int i= 0; i < rProjects.length; i++) {
			if (!projects.contains(rProjects[i]))
				result.add((IProject) rProjects[i]);
		}
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}
	fWorkingSet.setElements(CollectionsUtil.toArray(result, IAdaptable.class));
}
 
Example 9
Source File: FullSourceWorkspaceModelTests.java    From dacapobench with Apache License 2.0 4 votes vote down vote up
public void testFindType() throws CoreException {

    IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
    IJavaProject[] existingProjects = model.getJavaProjects();

    try {
      // close existing projects
      for (int i = 0, length = existingProjects.length; i < length; i++) {
        existingProjects[i].getProject().close(null);
        if (DACAPO_PRINT && i % 4 == 0)
          System.out.print(".");
      }

      // get 20 projects
      int max = 20;
      IJavaProject[] projects = new IJavaProject[max];
      for (int i = 0; i < max; i++) {
        projects[i] = createJavaProject("FindType" + i);
        if (DACAPO_PRINT && i % 4 == 0)
          System.out.print(".");
      }
      AbstractJavaModelTests.waitUntilIndexesReady();
      if (DACAPO_PRINT)
        System.out.print(".");
      AbstractJavaModelTests.waitForAutoBuild();

      try {
        model.close();
        for (int j = 0; j < max; j++) {
          projects[j].findType("java.lang.Object");
          if (DACAPO_PRINT && j % 4 == 0)
            System.out.print(".");
        }
      } finally {
        for (int i = 0; i < max; i++) {
          projects[i].getProject().delete(false, null);
        }
      }
    } finally {
      // reopen existing projects
      for (int i = 0, length = existingProjects.length; i < length; i++) {
        existingProjects[i].getProject().open(null);
      }
    }
  }
 
Example 10
Source File: BinaryRefactoringHistoryWizard.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Checks whether the archive referenced by the package fragment root is not
 * shared with multiple java projects in the workspace.
 *
 * @param root
 *            the package fragment root
 * @param monitor
 *            the progress monitor to use
 * @return the status of the operation
 */
private static RefactoringStatus checkPackageFragmentRoots(final IPackageFragmentRoot root, final IProgressMonitor monitor) {
	final RefactoringStatus status= new RefactoringStatus();
	try {
		monitor.beginTask(JarImportMessages.JarImportWizard_prepare_import, 100);
		final IWorkspaceRoot workspace= ResourcesPlugin.getWorkspace().getRoot();
		if (workspace != null) {
			final IJavaModel model= JavaCore.create(workspace);
			if (model != null) {
				try {
					final URI uri= getLocationURI(root.getRawClasspathEntry());
					if (uri != null) {
						final IJavaProject[] projects= model.getJavaProjects();
						final IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 100, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL);
						try {
							subMonitor.beginTask(JarImportMessages.JarImportWizard_prepare_import, projects.length * 100);
							for (int index= 0; index < projects.length; index++) {
								final IPackageFragmentRoot[] roots= projects[index].getPackageFragmentRoots();
								final IProgressMonitor subsubMonitor= new SubProgressMonitor(subMonitor, 100, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL);
								try {
									subsubMonitor.beginTask(JarImportMessages.JarImportWizard_prepare_import, roots.length);
									for (int offset= 0; offset < roots.length; offset++) {
										final IPackageFragmentRoot current= roots[offset];
										if (!current.equals(root) && current.getKind() == IPackageFragmentRoot.K_BINARY) {
											final IClasspathEntry entry= current.getRawClasspathEntry();
											if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
												final URI location= getLocationURI(entry);
												if (uri.equals(location))
													status.addFatalError(Messages.format(JarImportMessages.JarImportWizard_error_shared_jar, new String[] { JavaElementLabels.getElementLabel(current.getJavaProject(), JavaElementLabels.ALL_DEFAULT) }));
											}
										}
										subsubMonitor.worked(1);
									}
								} finally {
									subsubMonitor.done();
								}
							}
						} finally {
							subMonitor.done();
						}
					}
				} catch (CoreException exception) {
					status.addError(exception.getLocalizedMessage());
				}
			}
		}
	} finally {
		monitor.done();
	}
	return status;
}
 
Example 11
Source File: StandardJavaElementContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Evaluates all Java projects of a given {@link IJavaModel}. Clients can override this method.
 *
 * @param jm the Java model
 * @return the projects
 * @throws JavaModelException thrown if accessing the model failed
 */
protected Object[] getJavaProjects(IJavaModel jm) throws JavaModelException {
	return jm.getJavaProjects();
}