Java Code Examples for org.eclipse.jdt.core.IJavaElement#exists()

The following examples show how to use org.eclipse.jdt.core.IJavaElement#exists() . 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: PackageExplorerPart.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Object findInputElement() {
	if (getRootMode() == WORKING_SETS_AS_ROOTS) {
		return fWorkingSetModel;
	} else {
		Object input= getSite().getPage().getInput();
		if (input instanceof IWorkspace) {
			return JavaCore.create(((IWorkspace)input).getRoot());
		} else if (input instanceof IContainer) {
			IJavaElement element= JavaCore.create((IContainer)input);
			if (element != null && element.exists())
				return element;
			return input;
		}
		//1GERPRT: ITPJUI:ALL - Packages View is empty when shown in Type Hierarchy Perspective
		// we can't handle the input
		// fall back to show the workspace
		return JavaCore.create(JavaPlugin.getWorkspace().getRoot());
	}
}
 
Example 2
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isDeleteAvailable(final IJavaElement element) {
	if (!element.exists())
		return false;
	if (element instanceof IJavaModel || element instanceof IJavaProject)
		return false;
	if (element.getParent() != null && element.getParent().isReadOnly())
		return false;
	if (element instanceof IPackageFragmentRoot) {
		IPackageFragmentRoot root= (IPackageFragmentRoot) element;
		if (root.isExternal() || Checks.isClasspathDelete(root)) // TODO: rename isClasspathDelete
			return false;

		if (root.getResource().equals(root.getJavaProject().getProject()))
			return false;
	}
	if (element.getResource() == null && !RefactoringAvailabilityTester.isWorkingCopyElement(element))
		return false;
	if (element instanceof IMember && ((IMember) element).isBinary())
		return false;
	return true;
}
 
Example 3
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected RefactoringStatus verifyDestination(IJavaElement javaElement) throws JavaModelException {
	Assert.isNotNull(javaElement);
	if (!fCheckDestination) {
		return new RefactoringStatus();
	}
	if (!javaElement.exists()) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_cannot1);
	}
	if (javaElement instanceof IJavaModel) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_jmodel);
	}
	if (!(javaElement instanceof IJavaProject)) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_src2proj);
	}
	if (javaElement.isReadOnly()) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_src2writable);
	}
	if (ReorgUtils.isPackageFragmentRoot(javaElement.getJavaProject())) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_src2nosrc);
	}
	return new RefactoringStatus();
}
 
Example 4
Source File: PackagesViewHierarchicalContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Object getHierarchicalParent(IPackageFragment fragment) {
	IJavaElement parent= fragment.getParent();

	if ((parent instanceof IPackageFragmentRoot) && parent.exists()) {
		IPackageFragmentRoot root= (IPackageFragmentRoot) parent;
		if (root.isArchive() || root.isExternal() || !fragment.exists()) {
			return findNextLevelParentByElementName(fragment);
		} else {
			IResource resource= fragment.getResource();
			if ((resource != null) && (resource instanceof IFolder)) {
				IFolder folder= (IFolder) resource;
				IResource res= folder.getParent();

				IJavaElement el= JavaCore.create(res);
				if (el != null) {
					return el;
				} else {
					return res;
				}
			}
		}
	}
	return parent;
}
 
Example 5
Source File: LaunchPipelineShortcut.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
private static LaunchableResource toLaunchableResource(IResource resource) {
  if (resource == null) {
    return null;
  }
  IJavaElement javaElement = resource.getAdapter(IJavaElement.class);
  if (javaElement != null && javaElement.exists() && javaElement instanceof ICompilationUnit) {
    ICompilationUnit compilationUnit = (ICompilationUnit) javaElement;
    IType javaType = compilationUnit.findPrimaryType();
    if (javaType == null) {
      return null;
    }
    IMethod mainMethod = javaType.getMethod(
        "main", new String[] {Signature.createTypeSignature("String[]", false)});
    return new LaunchableResource(resource, mainMethod, javaType);
  }
  return new LaunchableResource(resource);
}
 
Example 6
Source File: ClassFileEditorInputFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public IAdaptable createElement(IMemento memento) {
	String identifier= memento.getString(KEY);
	if (identifier == null)
		return null;

	IJavaElement element= JavaCore.create(identifier);
	try {
		if (!element.exists() && element instanceof IClassFile) {
			/*
			 * Let's try to find the class file,
			 * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=83221
			 */
			IClassFile cf= (IClassFile)element;
			IType type= cf.getType();
			IJavaProject project= element.getJavaProject();
			if (project != null) {
				type= project.findType(type.getFullyQualifiedName());
				if (type == null)
					return null;
				element= type.getParent();
			}
		}
		return EditorUtility.getEditorInput(element);
	} catch (JavaModelException x) {
		// Don't report but simply return null
		return null;
	}
}
 
Example 7
Source File: MembersView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the closest Java element which can be used as input for
 * this part and has the given Java element as child.
 *
 * @param 	je 	the Java element for which to search the closest input
 * @return	the closest Java element used as input for this part, or <code>null</code>
 */
@Override
protected IJavaElement findInputForJavaElement(IJavaElement je) {
	if (je == null || !je.exists() || (je.getJavaProject() != null && !je.getJavaProject().isOnClasspath(je)))
		return null;

	switch (je.getElementType()) {
		case IJavaElement.TYPE:
			IType type= ((IType)je).getDeclaringType();
			if (type == null)
				return je;
			else
				return findInputForJavaElement(type);
		case IJavaElement.COMPILATION_UNIT:
			return getTypeForCU((ICompilationUnit)je);
		case IJavaElement.CLASS_FILE:
			return findInputForJavaElement(((IClassFile)je).getType());
		case IJavaElement.IMPORT_DECLARATION:
			return findInputForJavaElement(je.getParent());
		case IJavaElement.PACKAGE_DECLARATION:
		case IJavaElement.IMPORT_CONTAINER:
			IJavaElement parent= je.getParent();
			if (parent instanceof ICompilationUnit) {
				return getTypeForCU((ICompilationUnit)parent);
			}
			else if (parent instanceof IClassFile)
				return findInputForJavaElement(parent);
			return null;
		default:
			if (je instanceof IMember)
				return findInputForJavaElement(((IMember)je).getDeclaringType());
	}
	return null;
}
 
Example 8
Source File: TypeHierarchyViewPart.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void updateHistoryEntries() {
	for (int i= fInputHistory.size() - 1; i >= 0; i--) {
		IJavaElement[] entries= fInputHistory.get(i);
		for (int j= 0; j < entries.length; j++) {
			IJavaElement elem= entries[j];
			if (elem != null && !elem.exists()) {
				fInputHistory.remove(i);
				break;
			}
		}
	}
	fHistoryDropDownAction.setEnabled(!fInputHistory.isEmpty());
}
 
Example 9
Source File: InferTypeArgumentsRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
	final String clone= arguments.getAttribute(ATTRIBUTE_CLONE);
	if (clone != null) {
		fAssumeCloneReturnsSameType= Boolean.valueOf(clone).booleanValue();
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_CLONE));
	final String leave= arguments.getAttribute(ATTRIBUTE_LEAVE);
	if (leave != null) {
		fLeaveUnconstrainedRaw= Boolean.valueOf(leave).booleanValue();
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_LEAVE));
	int count= 1;
	final List<IJavaElement> elements= new ArrayList<IJavaElement>();
	String handle= null;
	String attribute= JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + count;
	final RefactoringStatus status= new RefactoringStatus();
	while ((handle= arguments.getAttribute(attribute)) != null) {
		final IJavaElement element= JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false);
		if (element == null || !element.exists())
			return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INFER_TYPE_ARGUMENTS);
		else
			elements.add(element);
		count++;
		attribute= JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + count;
	}
	fElements= elements.toArray(new IJavaElement[elements.size()]);
	if (elements.isEmpty())
		return JavaRefactoringDescriptorUtil.createInputFatalStatus(null, getName(), IJavaRefactorings.INFER_TYPE_ARGUMENTS);
	if (!status.isOK())
		return status;
	return new RefactoringStatus();
}
 
Example 10
Source File: JavaWorkingSetUpdater.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void checkElementExistence(IWorkingSet workingSet) {
	List<IAdaptable> elements= new ArrayList<IAdaptable>(Arrays.asList(workingSet.getElements()));
	boolean changed= false;
	for (Iterator<IAdaptable> iter= elements.iterator(); iter.hasNext();) {
		IAdaptable element= iter.next();
		boolean remove= false;
		if (element instanceof IJavaElement) {
			IJavaElement jElement= (IJavaElement)element;
			// If we have directly a project then remove it when it
			// doesn't exist anymore. However if we have a sub element
			// under a project only remove the element if the parent
			// project is open. Otherwise we would remove all elements
			// in closed projects.
			if (jElement instanceof IJavaProject) {
				remove= !jElement.exists();
			} else {
				final IJavaProject javaProject= jElement.getJavaProject();
				final boolean isProjectOpen= javaProject != null ? javaProject.getProject().isOpen() : true;
				remove= isProjectOpen && !jElement.exists();
			}
		} else if (element instanceof IResource) {
			IResource resource= (IResource)element;
			// See comments above
			if (resource instanceof IProject) {
				remove= !resource.exists();
			} else {
				IProject project= resource.getProject();
				remove= (project != null ? project.isOpen() : true) && !resource.exists();
			}
		}
		if (remove) {
			iter.remove();
			changed= true;
		}
	}
	if (changed) {
		workingSet.setElements(elements.toArray(new IAdaptable[elements.size()]));
	}
}
 
Example 11
Source File: ConvertJavaCodeHandler.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
private Set<ICompilationUnit> getCompilationUnits(IStructuredSelection structuredSelection) {
	Set<ICompilationUnit> result = new HashSet<ICompilationUnit>();
	for (Object element : structuredSelection.toList()) {
		if (element instanceof IJavaElement) {
			IJavaElement elem = (IJavaElement) element;
			if (elem.exists()) {
				collectCompilationUnits(elem, result);

			}
		}
	}
	return result;
}
 
Example 12
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public RefactoringStatus initialize(JavaRefactoringArguments arguments) {
	final RefactoringStatus status= new RefactoringStatus();
	int memberCount= 0;
	String value= arguments.getAttribute(ATTRIBUTE_MEMBERS);
	if (value != null && !"".equals(value)) {//$NON-NLS-1$
		try {
			memberCount= Integer.parseInt(value);
		} catch (NumberFormatException exception) {
			return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_MEMBERS));
		}
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_MEMBERS));
	String handle= null;
	List<IJavaElement> elements= new ArrayList<IJavaElement>();
	for (int index= 0; index < memberCount; index++) {
		final String attribute= JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (index + 1);
		handle= arguments.getAttribute(attribute);
		if (handle != null && !"".equals(handle)) { //$NON-NLS-1$
			final IJavaElement element= JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false);
			if (element == null || !element.exists())
				status.merge(JavaRefactoringDescriptorUtil.createInputWarningStatus(element, getProcessorId(), getRefactoringId()));
			else
				elements.add(element);
		} else
			return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, attribute));
	}
	fJavaElements= elements.toArray(new IJavaElement[elements.size()]);
	status.merge(super.initialize(arguments));
	return status;
}
 
Example 13
Source File: ParentChecker.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isDescendantOf(IResource subResource, IJavaElement superElement) {
	IResource parent= subResource.getParent();
	while(parent != null){
		IJavaElement el= JavaCore.create(parent);
		if (el != null && el.exists() && el.equals(superElement)) {
			return true;
		}
		parent= parent.getParent();
	}
	return false;
}
 
Example 14
Source File: PackageFragmentRootReorgChange.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected int getUpdateModelFlags(boolean isCopy) throws JavaModelException{
	final int destination= IPackageFragmentRoot.DESTINATION_PROJECT_CLASSPATH;
	final int replace= IPackageFragmentRoot.REPLACE;
	final int originating;
	final int otherProjects;
	if (isCopy){
		originating= 0; //ORIGINATING_PROJECT_CLASSPATH does not apply to copy
		otherProjects= 0;//OTHER_REFERRING_PROJECTS_CLASSPATH does not apply to copy
	} else{
		originating= IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH;
		otherProjects= IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH;
	}

	IJavaElement javaElement= JavaCore.create(getDestination());
	if (javaElement == null || !javaElement.exists()) {
		return replace | originating;
	}

	if (fUpdateClasspathQuery == null) {
		return replace | originating | destination;
	}

	IJavaProject[] referencingProjects= JavaElementUtil.getReferencingProjects(getRoot());
	if (referencingProjects.length <= 1) {
		return replace | originating | destination;
	}

	boolean updateOtherProjectsToo= fUpdateClasspathQuery.confirmManipulation(getRoot(), referencingProjects);
	if (updateOtherProjectsToo) {
		return replace | originating | destination | otherProjects;
	} else {
		return replace | originating | destination;
	}
}
 
Example 15
Source File: CleanUpAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void collectCompilationUnits(Object element, Collection<IJavaElement> result) {
	try {
		if (element instanceof IJavaElement) {
			IJavaElement elem= (IJavaElement)element;
			if (elem.exists()) {
				switch (elem.getElementType()) {
					case IJavaElement.TYPE:
						if (elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) {
							result.add(elem.getParent());
						}
						break;
					case IJavaElement.COMPILATION_UNIT:
						result.add(elem);
						break;
					case IJavaElement.IMPORT_CONTAINER:
						result.add(elem.getParent());
						break;
					case IJavaElement.PACKAGE_FRAGMENT:
						collectCompilationUnits((IPackageFragment)elem, result);
						break;
					case IJavaElement.PACKAGE_FRAGMENT_ROOT:
						collectCompilationUnits((IPackageFragmentRoot)elem, result);
						break;
					case IJavaElement.JAVA_PROJECT:
						IPackageFragmentRoot[] roots= ((IJavaProject)elem).getPackageFragmentRoots();
						for (int k= 0; k < roots.length; k++) {
							collectCompilationUnits(roots[k], result);
						}
						break;
				}
			}
		} else if (element instanceof LogicalPackage) {
			IPackageFragment[] packageFragments= ((LogicalPackage)element).getFragments();
			for (int k= 0; k < packageFragments.length; k++) {
				IPackageFragment pack= packageFragments[k];
				if (pack.exists()) {
					collectCompilationUnits(pack, result);
				}
			}
		} else if (element instanceof IWorkingSet) {
			IWorkingSet workingSet= (IWorkingSet) element;
			IAdaptable[] elements= workingSet.getElements();
			for (int j= 0; j < elements.length; j++) {
				collectCompilationUnits(elements[j], result);
			}
		}
	} catch (JavaModelException e) {
		if (JavaModelUtil.isExceptionToBeLogged(e))
			JavaPlugin.log(e);
	}
}
 
Example 16
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;
}
 
Example 17
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
	fSelfInitializing= true;
	final String handle= arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
	if (handle != null) {
		final IJavaElement element= JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false);
		if (element == null || !element.exists() || element.getElementType() != IJavaElement.COMPILATION_UNIT)
			return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.CONVERT_ANONYMOUS);
		else {
			fCu= (ICompilationUnit) element;
		}
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
	final String name= arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME);
	if (name != null && !"".equals(name)) //$NON-NLS-1$
		fClassName= name;
	else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME));
	final String visibility= arguments.getAttribute(ATTRIBUTE_VISIBILITY);
	if (visibility != null && !"".equals(visibility)) {//$NON-NLS-1$
		int flag= 0;
		try {
			flag= Integer.parseInt(visibility);
		} catch (NumberFormatException exception) {
			return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_VISIBILITY));
		}
		fVisibility= flag;
	}
	final String selection= arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
	if (selection != null) {
		int offset= -1;
		int length= -1;
		final StringTokenizer tokenizer= new StringTokenizer(selection);
		if (tokenizer.hasMoreTokens())
			offset= Integer.valueOf(tokenizer.nextToken()).intValue();
		if (tokenizer.hasMoreTokens())
			length= Integer.valueOf(tokenizer.nextToken()).intValue();
		if (offset >= 0 && length >= 0) {
			fSelectionStart= offset;
			fSelectionLength= length;
		} else
			return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION}));
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION));
	final String declareStatic= arguments.getAttribute(ATTRIBUTE_STATIC);
	if (declareStatic != null) {
		fDeclareStatic= Boolean.valueOf(declareStatic).booleanValue();
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_STATIC));
	final String declareFinal= arguments.getAttribute(ATTRIBUTE_FINAL);
	if (declareFinal != null) {
		fDeclareFinal= Boolean.valueOf(declareStatic).booleanValue();
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_FINAL));
	return new RefactoringStatus();
}
 
Example 18
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected RefactoringStatus verifyDestination(IJavaElement javaElement) throws JavaModelException {
	Assert.isNotNull(javaElement);
	if (!fCheckDestination)
		return new RefactoringStatus();
	if (!javaElement.exists())
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_doesnotexist0);
	if (javaElement instanceof IJavaModel)
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_jmodel);

	if (javaElement.isReadOnly())
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_readonly);

	if (!javaElement.isStructureKnown())
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_structure);

	if (javaElement instanceof IOpenable) {
		IOpenable openable= (IOpenable) javaElement;
		if (!openable.isConsistent())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_inconsistent);
	}

	if (javaElement instanceof IPackageFragmentRoot) {
		IPackageFragmentRoot root= (IPackageFragmentRoot) javaElement;
		if (root.isArchive())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_archive);
		if (root.isExternal())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_external);
	}

	if (ReorgUtils.isInsideCompilationUnit(javaElement)) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_cannot);
	}

	IContainer destinationAsContainer= getDestinationAsContainer();
	if (destinationAsContainer == null || isChildOfOrEqualToAnyFolder(destinationAsContainer))
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_not_this_resource);

	if (containsLinkedResources() && !ReorgUtils.canBeDestinationForLinkedResources(javaElement))
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_linked);
	return new RefactoringStatus();
}
 
Example 19
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
	final String selection = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
	if (selection != null) {
		int offset = -1;
		int length = -1;
		final StringTokenizer tokenizer = new StringTokenizer(selection);
		if (tokenizer.hasMoreTokens()) {
			offset = Integer.valueOf(tokenizer.nextToken()).intValue();
		}
		if (tokenizer.hasMoreTokens()) {
			length = Integer.valueOf(tokenizer.nextToken()).intValue();
		}
		if (offset >= 0 && length >= 0) {
			fSelectionStart = offset;
			fSelectionLength = length;
		} else {
			return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION }));
		}
	} else {
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION));
	}
	final String handle = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
	if (handle != null) {
		final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false);
		if (element == null || !element.exists() || element.getElementType() != IJavaElement.COMPILATION_UNIT) {
			return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.EXTRACT_LOCAL_VARIABLE);
		} else {
			fCu = (ICompilationUnit) element;
		}
	} else {
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
	}
	final String name = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME);
	if (name != null && !"".equals(name)) {
		fTempName = name;
	} else {
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME));
	}
	final String replace = arguments.getAttribute(ATTRIBUTE_REPLACE);
	if (replace != null) {
		fReplaceAllOccurrences = Boolean.valueOf(replace).booleanValue();
	} else {
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REPLACE));
	}
	final String declareFinal = arguments.getAttribute(ATTRIBUTE_FINAL);
	if (declareFinal != null) {
		fDeclareFinal = Boolean.valueOf(declareFinal).booleanValue();
	} else {
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_FINAL));
	}
	return new RefactoringStatus();
}
 
Example 20
Source File: JavaElementReferenceConverter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Throws a <code>ParameterValueConversionException</code> if the java
 * element reference string identifies an element that does not exist.
 *
 * @param javaElement
 *            an element to check for existence
 * @throws ParameterValueConversionException
 */
private void assertExists(IJavaElement javaElement) throws ParameterValueConversionException {
	if ((javaElement == null) || (!javaElement.exists())) {
		throw new ParameterValueConversionException("parameterValue must reference an existing IJavaElement"); //$NON-NLS-1$
	}
}