Java Code Examples for org.eclipse.jdt.core.JavaModelException#isDoesNotExist()

The following examples show how to use org.eclipse.jdt.core.JavaModelException#isDoesNotExist() . 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: Storage2UriMapperJavaImpl.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @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);
	}
}
 
Example 2
Source File: JavaOutlinePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public boolean hasChildren(Object parent) {
	if (parent instanceof IParent) {
		IParent c= (IParent) parent;
		try {
			IJavaElement[] children= filter(c.getChildren());
			return (children != null && children.length > 0);
		} catch (JavaModelException x) {
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
			// don't log NotExist exceptions as this is a valid case
			// since we might have been posted and the element
			// removed in the meantime.
			if (JavaPlugin.isDebug() || !x.isDoesNotExist())
				JavaPlugin.log(x);
		}
	}
	return false;
}
 
Example 3
Source File: JavaOutlinePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public Object[] getChildren(Object parent) {
	if (parent instanceof IParent) {
		IParent c= (IParent) parent;
		try {
			return filter(c.getChildren());
		} catch (JavaModelException x) {
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=38341
			// don't log NotExist exceptions as this is a valid case
			// since we might have been posted and the element
			// removed in the meantime.
			if (JavaPlugin.isDebug() || !x.isDoesNotExist())
				JavaPlugin.log(x);
		}
	}
	return NO_CHILDREN;
}
 
Example 4
Source File: CompilationUnitEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the most narrow element including the given offset.  If <code>reconcile</code>
 * is <code>true</code> the editor's input element is reconciled in advance. If it is
 * <code>false</code> this method only returns a result if the editor's input element
 * does not need to be reconciled.
 *
 * @param offset the offset included by the retrieved element
 * @param reconcile <code>true</code> if working copy should be reconciled
 * @return the most narrow element which includes the given offset
 */
@Override
protected IJavaElement getElementAt(int offset, boolean reconcile) {
	ICompilationUnit unit= (ICompilationUnit)getInputJavaElement();

	if (unit != null) {
		try {
			if (reconcile) {
				JavaModelUtil.reconcile(unit);
				return unit.getElementAt(offset);
			} else if (unit.isConsistent())
				return unit.getElementAt(offset);

		} catch (JavaModelException x) {
			if (!x.isDoesNotExist())
			JavaPlugin.log(x.getStatus());
			// nothing found, be tolerant and go on
		}
	}

	return null;
}
 
Example 5
Source File: OverrideIndicatorLabelDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Note: This method is for internal use only. Clients should not call this method.
 * @param element The element to decorate
 * @return Resulting decorations (combination of JavaElementImageDescriptor.IMPLEMENTS
 * and JavaElementImageDescriptor.OVERRIDES)
 *
 * @noreference This method is not intended to be referenced by clients.
 */
public int computeAdornmentFlags(Object element) {
	if (element instanceof IMethod) {
		try {
			IMethod method= (IMethod) element;
			if (!method.getJavaProject().isOnClasspath(method)) {
				return 0;
			}
			int flags= method.getFlags();
			if (!method.isConstructor() && !Flags.isPrivate(flags) && !Flags.isStatic(flags)) {
				int res= getOverrideIndicators(method);
				if (res != 0 && Flags.isSynchronized(flags)) {
					return res | JavaElementImageDescriptor.SYNCHRONIZED;
				}
				return res;
			}
		} catch (JavaModelException e) {
			if (!e.isDoesNotExist()) {
				JavaPlugin.log(e);
			}
		}
	}
	return 0;
}
 
Example 6
Source File: OpenSuperImplementationAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean checkMethod(IMethod method) {
	try {
		int flags= method.getFlags();
		if (!Flags.isStatic(flags) && !Flags.isPrivate(flags)) {
			IType declaringType= method.getDeclaringType();
			if (SuperTypeHierarchyCache.hasInCache(declaringType)) {
				if (findSuperImplementation(method) == null) {
					return false;
				}
			}
			return true;
		}
	} catch (JavaModelException e) {
		if (!e.isDoesNotExist()) {
			JavaPlugin.log(e);
		}
	}
	return false;
}
 
Example 7
Source File: JdtBasedProcessorProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Construct a Classloader with the classpathentries from the provided and all upstream-projects,
 * except the output folders of the local project.
 */
protected URLClassLoader createClassLoaderForJavaProject(final IJavaProject projectToUse) {
  final LinkedHashSet<URL> urls = CollectionLiterals.<URL>newLinkedHashSet();
  try {
    this.collectClasspathURLs(projectToUse, urls, this.isOutputFolderIncluded(), CollectionLiterals.<IJavaProject>newHashSet());
  } catch (final Throwable _t) {
    if (_t instanceof JavaModelException) {
      final JavaModelException e = (JavaModelException)_t;
      boolean _isDoesNotExist = e.isDoesNotExist();
      boolean _not = (!_isDoesNotExist);
      if (_not) {
        JdtBasedProcessorProvider.LOG.error(e.getMessage(), e);
      }
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  }
  ClassLoader _parentClassLoader = this.getParentClassLoader();
  return new URLClassLoader(((URL[])Conversions.unwrapArray(urls, URL.class)), _parentClassLoader);
}
 
Example 8
Source File: JavaProjectsStateHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected IPackageFragmentRoot getJavaElement(final IFile file) {
	IJavaProject jp = JavaCore.create(file.getProject());
	if (!jp.exists())
		return null;
	IPackageFragmentRoot[] roots;
	try {
		roots = jp.getPackageFragmentRoots();
		for (IPackageFragmentRoot root : roots) {
			if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
				IResource resource2 = root.getUnderlyingResource();
				if (resource2.contains(file))
					return root;
			}
		}
	} catch (JavaModelException e) {
		if (!e.isDoesNotExist())
			log.error(e.getMessage(), e);
	}
	return null;
}
 
Example 9
Source File: JavaProjectsStateHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected List<String> getPackageFragmentRootHandles(IJavaProject project) {
	List<String> result = Lists.newArrayList();
	List<String> binaryAndNonLocalFragments = Lists.newArrayList();
	try {
		IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
		result = Lists.newArrayListWithCapacity(roots.length);
		for (IPackageFragmentRoot root : roots) {
			if (root != null && !JavaRuntime.newDefaultJREContainerPath().isPrefixOf(root.getRawClasspathEntry().getPath())) {
				if (root.getKind() == IPackageFragmentRoot.K_SOURCE && project.equals(root.getJavaProject())) {
					// treat local sources with higher priority
					// see Java behavior in SameClassNamesTest
					result.add(root.getHandleIdentifier());	
				} else {
					binaryAndNonLocalFragments.add(root.getHandleIdentifier());
				}
			}
		}
	} catch (JavaModelException e) {
		if (!e.isDoesNotExist()) {
			log.error("Cannot find rootHandles in project " + project.getProject().getName(), e);
		}
	}
	result.addAll(binaryAndNonLocalFragments);
	return result;
}
 
Example 10
Source File: Storage2UriMapperJavaImpl.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.4
 */
@Override
public Map<URI, IStorage> getAllEntries(IPackageFragmentRoot root) {
	try {
		IResource underlyingResource = root.getUnderlyingResource();
		if (underlyingResource instanceof IFolder) {
			return host.getAllEntries((IFolder) underlyingResource);
		}
	} catch (JavaModelException e) {
		if (!e.isDoesNotExist())
			log.error(e.getMessage(), e);
		return emptyMap();
	}
	PackageFragmentRootData data = getData(root);
	return data.uri2Storage;
}
 
Example 11
Source File: XtendUIValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Check
protected void checkAnnotationInSameProject(XAnnotation annotation) throws JavaModelException {
	try {
		if (annotationExtensions.isProcessed(annotation)) {
			JvmType annotationType = annotation.getAnnotationType();
			if (isSameProject(annotation, annotationType)) {
				error("The referenced active annotation cannot be used from within the same project.",XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, -1, ACTIVE_ANNOTATION_IN_SAME_CONTAINER);
			}
		}
	} catch(JavaModelException e) {
		if (!e.isDoesNotExist()) {
			throw e;
		}
	}
}
 
Example 12
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void run() {
	try {
		resolveParamNames.start();
		
		// Use the handle to find the top level type and then use the path the traverse to the correct nested type.
		//
		IType type = findTypeByHandleIdentifier();

		List<JvmFormalParameter> parameters = executable.getParameters();
		if (type != null) {
			IMethod javaMethod = findJavaMethod(type);
			int numberOfParameters = javaMethod.getNumberOfParameters();
			if (numberOfParameters != 0) {
				try {
					setParameterNames(javaMethod, parameters);
					return;
				} catch (JavaModelException ex) {
					if (!ex.isDoesNotExist())
						log.warn("IMethod.getParameterNames failed", ex);
				}
			}
		}

		// We generally should not ever get here.
		//
		synthesizeNames(parameters);
	} finally {
		resolveParamNames.stop();
	}
}
 
Example 13
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isExceptionToBeLogged(CoreException exception) {
	if (!(exception instanceof JavaModelException))
		return true;
	JavaModelException je= (JavaModelException)exception;
	if (!je.isDoesNotExist())
		return true;
	return false;
}
 
Example 14
Source File: JdtToBeBuiltComputer.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Handle all fragment roots that are on the classpath and not a source folder.
 */
private boolean shouldHandle(IPackageFragmentRoot root) {
	try {
		boolean result = !JavaRuntime.newDefaultJREContainerPath().isPrefixOf(root.getRawClasspathEntry().getPath());
		result &= (root.isArchive() || root.isExternal()); 
		return result;
	} catch (JavaModelException ex) {
		if (!ex.isDoesNotExist())
			log.error(ex.getMessage(), ex);
		return false;
	}
}
 
Example 15
Source File: JavaContentAssistInvocationContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Fallback to retrieve a core context and keyword proposals when no collector is available.
 * Runs code completion on the cu and collects keyword proposals. {@link #fKeywordProposals} is
 * non-<code>null</code> after this call.
 *
 * @since 3.3
 */
private void computeKeywordsAndContext() {
	ICompilationUnit cu= getCompilationUnit();
	if (cu == null) {
		if (fKeywordProposals == null)
			fKeywordProposals= new IJavaCompletionProposal[0];
		return;
	}

	CompletionProposalCollector collector= new CompletionProposalCollector(cu, true);
	collector.setIgnored(CompletionProposal.KEYWORD, false);

	try {
		cu.codeComplete(getInvocationOffset(), collector);
		if (fCoreContext == null)
			fCoreContext= collector.getContext();
		if (fKeywordProposals == null)
			fKeywordProposals= collector.getKeywordCompletionProposals();
		if (fLabelProvider == null)
			fLabelProvider= collector.getLabelProvider();
	} catch (JavaModelException x) {
		if (!x.isDoesNotExist() || cu.getJavaProject() == null || cu.getJavaProject().isOnClasspath(cu))
			JavaPlugin.log(x);
		if (fKeywordProposals == null)
			fKeywordProposals= new IJavaCompletionProposal[0];
	}
}
 
Example 16
Source File: ProjectAwareUniqueClassNameValidator.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean doCheckUniqueName(QualifiedName name, JvmDeclaredType type) {
	if (!super.doCheckUniqueName(name, type)) {
		return false;
	}

	try {
		return doCheckUniqueInProject(name, type);
	} catch (JavaModelException e) {
		if (!e.isDoesNotExist()) {
			LOG.error(e.getMessage(), e);
		}
		return true;
	}
}
 
Example 17
Source File: CleanUpAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isEnabled(IStructuredSelection selection) {
	Object[] selected= selection.toArray();
	for (int i= 0; i < selected.length; i++) {
		try {
			if (selected[i] instanceof IJavaElement) {
				IJavaElement elem= (IJavaElement)selected[i];
				if (elem.exists()) {
					switch (elem.getElementType()) {
						case IJavaElement.TYPE:
							return elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT; // for browsing perspective
						case IJavaElement.COMPILATION_UNIT:
							return true;
						case IJavaElement.IMPORT_CONTAINER:
							return true;
						case IJavaElement.PACKAGE_FRAGMENT:
						case IJavaElement.PACKAGE_FRAGMENT_ROOT:
							IPackageFragmentRoot root= (IPackageFragmentRoot)elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
							return (root.getKind() == IPackageFragmentRoot.K_SOURCE);
						case IJavaElement.JAVA_PROJECT:
							// https://bugs.eclipse.org/bugs/show_bug.cgi?id=65638
							return true;
					}
				}
			} else if (selected[i] instanceof LogicalPackage) {
				return true;
			} else if (selected[i] instanceof IWorkingSet) {
				IWorkingSet workingSet= (IWorkingSet) selected[i];
				return IWorkingSetIDs.JAVA.equals(workingSet.getId());
			}
		} catch (JavaModelException e) {
			if (!e.isDoesNotExist()) {
				JavaPlugin.log(e);
			}
		}
	}
	return false;
}
 
Example 18
Source File: JavaUIHelp.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public IContext getContext(Object target) {
	IContext context= HelpSystem.getContext(fId);
	if (fSelected != null && fSelected.length > 0) {
		try {
			context= new JavadocHelpContext(context, fSelected);
		} catch (JavaModelException e) {
			// since we are updating the UI with async exec it
			// can happen that the element doesn't exist anymore
			// but we are still showing it in the user interface
			if (!e.isDoesNotExist())
				JavaPlugin.log(e);
		}
	}
	return context;
}
 
Example 19
Source File: ClassFileEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void createSourceAttachmentControls(Composite composite, IPackageFragmentRoot root) throws JavaModelException {
	IClasspathEntry entry;
	try {
		entry= JavaModelUtil.getClasspathEntry(root);
	} catch (JavaModelException ex) {
		if (ex.isDoesNotExist())
			entry= null;
		else
			throw ex;
	}
	IPath containerPath= null;

	if (entry == null || root.getKind() != IPackageFragmentRoot.K_BINARY) {
		createLabel(composite, Messages.format(JavaEditorMessages.SourceAttachmentForm_message_noSource, BasicElementLabels.getFileName( fFile)));
		return;
	}

	IJavaProject jproject= root.getJavaProject();
	boolean canEditEncoding= true;
	if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
		containerPath= entry.getPath();
		ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
		IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
		if (initializer == null || container == null) {
			createLabel(composite, Messages.format(JavaEditorMessages.ClassFileEditor_SourceAttachmentForm_cannotconfigure, BasicElementLabels.getPathLabel(containerPath, false)));
			return;
		}
		String containerName= container.getDescription();
		IStatus status= initializer.getSourceAttachmentStatus(containerPath, jproject);
		if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
			createLabel(composite, Messages.format(JavaEditorMessages.ClassFileEditor_SourceAttachmentForm_notsupported, containerName));
			return;
		}
		if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
			createLabel(composite, Messages.format(JavaEditorMessages.ClassFileEditor_SourceAttachmentForm_readonly, containerName));
			return;
		}
		IStatus attributeStatus= initializer.getAttributeStatus(containerPath, jproject, IClasspathAttribute.SOURCE_ATTACHMENT_ENCODING);
		canEditEncoding= !(attributeStatus.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED || attributeStatus.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY);
		entry= JavaModelUtil.findEntryInContainer(container, root.getPath());
		Assert.isNotNull(entry);
	}


	Button button;

	IPath path= entry.getSourceAttachmentPath();
	if (path == null || path.isEmpty()) {
		String rootLabel= JavaElementLabels.getElementLabel(root, JavaElementLabels.ALL_DEFAULT);
		createLabel(composite, Messages.format(JavaEditorMessages.SourceAttachmentForm_message_noSourceAttachment, rootLabel));
		createLabel(composite, JavaEditorMessages.SourceAttachmentForm_message_pressButtonToAttach);
		createLabel(composite, null);

		button= createButton(composite, JavaEditorMessages.SourceAttachmentForm_button_attachSource);

	} else {
		createLabel(composite, Messages.format(JavaEditorMessages.SourceAttachmentForm_message_noSourceInAttachment, BasicElementLabels.getFileName(fFile)));
		createLabel(composite, JavaEditorMessages.SourceAttachmentForm_message_pressButtonToChange);
		createLabel(composite, null);

		button= createButton(composite, JavaEditorMessages.SourceAttachmentForm_button_changeAttachedSource);
	}

	button.addSelectionListener(getButtonListener(entry, containerPath, jproject, canEditEncoding));
}
 
Example 20
Source File: FindBrokenNLSKeysAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean canEnable(IStructuredSelection selection) {
	Object[] selected= selection.toArray();
	for (int i= 0; i < selected.length; i++) {
		try {
			if (selected[i] instanceof IJavaElement) {
				IJavaElement elem= (IJavaElement) selected[i];
				if (elem.exists()) {
					switch (elem.getElementType()) {
						case IJavaElement.TYPE:
							if (elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) {
								return true;
							}
							return false;
						case IJavaElement.COMPILATION_UNIT:
							return true;
						case IJavaElement.IMPORT_CONTAINER:
							return false;
						case IJavaElement.PACKAGE_FRAGMENT:
						case IJavaElement.PACKAGE_FRAGMENT_ROOT:
							IPackageFragmentRoot root= (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
							return (root.getKind() == IPackageFragmentRoot.K_SOURCE);
						case IJavaElement.JAVA_PROJECT:
							return true;
					}
				}
			} else if (selected[i] instanceof LogicalPackage) {
				return true;
			} else if (selected[i] instanceof IFile) {
				IFile file= (IFile)selected[i];
				if ("properties".equalsIgnoreCase(file.getFileExtension())) //$NON-NLS-1$
					return true;
			} else if (selected[i] instanceof IWorkingSet) {
				IWorkingSet workingSet= (IWorkingSet) selected[i];
				return IWorkingSetIDs.JAVA.equals(workingSet.getId());
			}
		} catch (JavaModelException e) {
			if (!e.isDoesNotExist()) {
				JavaPlugin.log(e);
			}
		}
	}
	return false;
}