Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2010 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
 
 package org.eclipse.jdt.internal.core;
 
 import java.io.File;
 import java.util.Map;
 
 import  org.eclipse.core.resources.IFile;
 import  org.eclipse.core.resources.IFolder;
 import  org.eclipse.core.resources.IProject;
 import  org.eclipse.core.resources.IResource;
 import  org.eclipse.core.resources.IWorkspace;
 import  org.eclipse.core.resources.ResourcesPlugin;
 import  org.eclipse.core.runtime.Assert;
 import  org.eclipse.core.runtime.IPath;
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.IStatus;
 import  org.eclipse.core.runtime.Path;
Implementation of IJavaModel. The Java Model maintains a cache of active IJavaProjects in a workspace. A Java Model is specific to a workspace. To retrieve a workspace's model, use the getJavaModel() method.

See also:
IJavaModel
 
 public class JavaModel extends Openable implements IJavaModel {

A set of java.io.Files used as a cache of external jars that are known to be existing. Note this cache is kept for the whole session.
 
 	public static HashSet existingExternalFiles = new HashSet();

A set of external files (existingExternalFiles) which have been confirmed as file (i.e. which returns true to java.io.File.isFile(). Note this cache is kept for the whole session.
 
 	public static HashSet existingExternalConfirmedFiles = new HashSet();

Constructs a new Java Model on the given workspace. Note that only one instance of JavaModel handle should ever be created. One should only indirect through JavaModelManager#getJavaModel() to get access to it.

Throws:
Error if called more than once
 
 protected JavaModel() throws Error {
 	super(null);
 }
 protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pmMap newElements, IResource underlyingResource)	/*throws JavaModelException*/ {
 
 	// determine my children
 	IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
 	int length = projects.length;
 	IJavaElement[] children = new IJavaElement[length];
 	int index = 0;
 	for (int i = 0; i < lengthi++) {
 		IProject project = projects[i];
 		if (JavaProject.hasJavaNature(project)) {
 			children[index++] = getJavaProject(project);
 		}
 	}
 	if (index < length)
 		System.arraycopy(children, 0, children = new IJavaElement[index], 0, index);
 	info.setChildren(children);
 
 	newElements.put(thisinfo);
 
 	return true;
 }
 /*
  * @see IJavaModel
  */
 public boolean contains(IResource resource) {
 	switch (resource.getType()) {
 		case IResource.ROOT:
 		case IResource.PROJECT:
 			return true;
 	}
 	// file or folder
 	IJavaProject[] projects;
	try {
		projects = getJavaProjects();
catch (JavaModelException e) {
		return false;
	}
	for (int i = 0, length = projects.lengthi < lengthi++) {
		JavaProject project = (JavaProject)projects[i];
		if (!project.contains(resource)) {
			return false;
		}
	}
	return true;

See also:
IJavaModel
public void copy(IJavaElement[] elementsIJavaElement[] containersIJavaElement[] siblingsString[] renamingsboolean force, IProgressMonitor monitorthrows JavaModelException {
	if (elements != null && elements.length > 0 && elements[0] != null && elements[0].getElementType() < .) {
		runOperation(new CopyResourceElementsOperation(elementscontainersforce), elementssiblingsrenamingsmonitor);
else {
		runOperation(new CopyElementsOperation(elementscontainersforce), elementssiblingsrenamingsmonitor);
	}
Returns a new element info for this element.
protected Object createElementInfo() {
	return new JavaModelInfo();

See also:
IJavaModel
public void delete(IJavaElement[] elementsboolean force, IProgressMonitor monitorthrows JavaModelException {
	if (elements != null && elements.length > 0 && elements[0] != null && elements[0].getElementType() < .) {
		new DeleteResourceElementsOperation(elementsforce).runOperation(monitor);
else {
		new DeleteElementsOperation(elementsforce).runOperation(monitor);
	}
public boolean equals(Object o) {
	if (!(o instanceof JavaModel)) return false;
	return super.equals(o);

See also:
IJavaElement
public int getElementType() {
	return JAVA_MODEL;
Flushes the cache of external files known to be existing.
public static void flushExternalFileCache() {
/*
 * @see JavaElement
 */
	switch (token.charAt(0)) {
		case JEM_JAVAPROJECT:
			if (!memento.hasMoreTokens()) return this;
			String projectName = memento.nextToken();
			JavaElement project = (JavaElement)getJavaProject(projectName);
			return project.getHandleFromMemento(mementoowner);
	}
	return null;

See also:
JavaElement.getHandleMemento(StringBuffer)
protected void getHandleMemento(StringBuffer buff) {
Returns the char that marks the start of this handles contribution to a memento.
protected char getHandleMementoDelimiter(){
	Assert.isTrue(false"Should not be called"); //$NON-NLS-1$
	return 0;

See also:
IJavaModel
public IJavaProject getJavaProject(String projectName) {
	return new JavaProject(ResourcesPlugin.getWorkspace().getRoot().getProject(projectName), this);
Returns the active Java project associated with the specified resource, or null if no Java project yet exists for the resource.

Throws:
IllegalArgumentException if the given resource is not one of an IProject, IFolder, or IFile.
public IJavaProject getJavaProject(IResource resource) {
	switch(resource.getType()){
		case IResource.FOLDER:
			return new JavaProject(((IFolder)resource).getProject(), this);
		case IResource.FILE:
			return new JavaProject(((IFile)resource).getProject(), this);
		case IResource.PROJECT:
			return new JavaProject((IProject)resourcethis);
		default:
	}

See also:
IJavaModel
	ArrayList list = getChildrenOfType(JAVA_PROJECT);
	IJavaProject[] arraynew IJavaProject[list.size()];
	list.toArray(array);
	return array;

See also:
IJavaModel
/*
 * @see IJavaElement
 */
public IPath getPath() {
	return Path.ROOT;
/*
 * @see IJavaElement
 */
public IResource resource(PackageFragmentRoot root) {
	return ResourcesPlugin.getWorkspace().getRoot();

See also:
IOpenable
public IResource getUnderlyingResource() {
	return null;
Returns the workbench associated with this object.
public IWorkspace getWorkspace() {
	return ResourcesPlugin.getWorkspace();

See also:
IJavaModel
public void move(IJavaElement[] elementsIJavaElement[] containersIJavaElement[] siblingsString[] renamingsboolean force, IProgressMonitor monitorthrows JavaModelException {
	if (elements != null && elements.length > 0 && elements[0] != null && elements[0].getElementType() < .) {
		runOperation(new MoveResourceElementsOperation(elementscontainersforce), elementssiblingsrenamingsmonitor);
else {
		runOperation(new MoveElementsOperation(elementscontainersforce), elementssiblingsrenamingsmonitor);
	}

See also:
IJavaModel.refreshExternalArchives(IJavaElement[], IProgressMonitor)
public void refreshExternalArchives(IJavaElement[] elementsScope, IProgressMonitor monitorthrows JavaModelException {
	if (elementsScope == null){
		elementsScope = new IJavaElement[] { this };
	}
	JavaModelManager.getJavaModelManager().getDeltaProcessor().checkExternalArchiveChanges(elementsScopemonitor);

See also:
IJavaModel
public void rename(IJavaElement[] elementsIJavaElement[] destinationsString[] renamingsboolean force, IProgressMonitor monitorthrows JavaModelException {
	if (elements != null && elements.length > 0 && elements[0] != null && elements[0].getElementType() < .) {
		op = new RenameResourceElementsOperation(elementsdestinationsrenamingsforce);
else {
		op = new RenameElementsOperation(elementsdestinationsrenamingsforce);
	}
	op.runOperation(monitor);
Configures and runs the MultiOperation.
protected void runOperation(MultiOperation opIJavaElement[] elementsIJavaElement[] siblingsString[] renamings, IProgressMonitor monitorthrows JavaModelException {
	op.setRenamings(renamings);
	if (siblings != null) {
		for (int i = 0; i < elements.lengthi++) {
			op.setInsertBefore(elements[i], siblings[i]);
		}
	}
	op.runOperation(monitor);

Private:
Debugging purposes
protected void toStringInfo(int tabStringBuffer bufferObject infoboolean showResolvedInfo) {
	buffer.append(tabString(tab));
	buffer.append("Java Model"); //$NON-NLS-1$
	if (info == null) {
		buffer.append(" (not open)"); //$NON-NLS-1$
	}
Helper method - returns the targeted item (IResource if internal or java.io.File if external), or null if unbound Internal items must be referred to using container relative paths.
public static Object getTarget(IPath pathboolean checkResourceExistence) {
	Object target = getWorkspaceTarget(path); // Implicitly checks resource existence
	if (target != null)
		return target;
	return getExternalTarget(pathcheckResourceExistence);
public static IResource getWorkspaceTarget(IPath path) {
	if (path == null || path.getDevice() != null)
		return null;
	IWorkspace workspace = ResourcesPlugin.getWorkspace();
	if (workspace == null)
		return null;
	return workspace.getRoot().findMember(path);
public static Object getExternalTarget(IPath pathboolean checkResourceExistence) {
	if (path == null)
		return null;
	ExternalFoldersManager externalFoldersManager = JavaModelManager.getExternalManager();
	Object linkedFolder = externalFoldersManager.getFolder(path);
	if (linkedFolder != null) {
		if (checkResourceExistence) {
			// check if external folder is present
			File externalFile = new File(path.toOSString());
			if (!externalFile.isDirectory()) {
				return null;
			}
		}
		return linkedFolder;
	}
	File externalFile = new File(path.toOSString());
	if (!checkResourceExistence) {
		return externalFile;
else if (existingExternalFilesContains(externalFile)) {
		return externalFile;
else {
			..println("(" + Thread.currentThread() + ") [JavaModel.getTarget(...)] Checking existence of " + path.toString()); //$NON-NLS-1$ //$NON-NLS-2$
		}
		if (externalFile.isFile()) { // isFile() checks for existence (it returns false if a directory)
			// cache external file
			return externalFile;
		}
	}
	return null;
private synchronized static void existingExternalFilesAdd(File externalFile) {
private synchronized static boolean existingExternalFilesContains(File externalFile) {
	return .contains(externalFile);
Helper method - returns whether an object is afile (i.e. which returns true to java.io.File.isFile().
public static boolean isFile(Object target) {
	return getFile(target) != null;
Helper method - returns the file item (i.e. which returns true to java.io.File.isFile(), or null if unbound
public static synchronized File getFile(Object target) {
		return (Filetarget;
	if (target instanceof File) {
		File f = (Filetarget;
		if (f.isFile()) {
			return f;
		}
	}
	return null;
protected IStatus validateExistence(IResource underlyingResource) {
	// Java model always exists
New to GrepCode? Check out our FAQ X