Java Code Examples for org.eclipse.jdt.core.IPackageFragment#equals()

The following examples show how to use org.eclipse.jdt.core.IPackageFragment#equals() . 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: MoveCompilationUnitChange.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private IPackageFragment[] createDestination(IPackageFragmentRoot root, IPackageFragment destination, IProgressMonitor pm) throws JavaModelException {
	String packageName= destination.getElementName();
	String[] split= packageName.split("\\."); //$NON-NLS-1$

	ArrayList<IPackageFragment> created= new ArrayList<>();

	StringBuilder name= new StringBuilder();
	name.append(split[0]);
	for (int i= 0; i < split.length; i++) {
		IPackageFragment fragment= root.getPackageFragment(name.toString());
		if (!fragment.exists()) {
			created.add(fragment);
		}

		if (fragment.equals(destination)) {
			root.createPackageFragment(name.toString(), true, pm);
			return created.toArray(new IPackageFragment[created.size()]);
		}

		name.append("."); //$NON-NLS-1$
		name.append(split[i + 1]);
	}

	return null;
}
 
Example 2
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected RefactoringStatus verifyDestination(IJavaElement destination) throws JavaModelException {
	RefactoringStatus superStatus= super.verifyDestination(destination);
	if (superStatus.hasFatalError())
		return superStatus;

	Object commonParent= new ParentChecker(getResources(), getJavaElements()).getCommonParent();
	if (destination.equals(commonParent))
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_parent);
	IContainer destinationAsContainer= getDestinationAsContainer();
	if (destinationAsContainer != null && (destinationAsContainer.equals(commonParent) || commonParent instanceof IPackageFragmentRoot
			&& destinationAsContainer.equals(((IPackageFragmentRoot) commonParent).getResource())))
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_parent);
	IPackageFragment destinationAsPackage= getDestinationAsPackageFragment();
	if (destinationAsPackage != null && (destinationAsPackage.equals(commonParent)))
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_parent);

	if (cannotUpdateReferencesForDestination())
		superStatus.addInfo(RefactoringCoreMessages.ReorgPolicyFactory_noJavaUpdates);

	return superStatus;
}
 
Example 3
Source File: MoveCompilationUnitChange.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IPackageFragment[] createDestination(IPackageFragmentRoot root, IPackageFragment destination, IProgressMonitor pm) throws JavaModelException {
	String packageName= destination.getElementName();
	String[] split= packageName.split("\\."); //$NON-NLS-1$

	ArrayList<IPackageFragment> created= new ArrayList<IPackageFragment>();

	StringBuffer name= new StringBuffer();
	name.append(split[0]);
	for (int i= 0; i < split.length; i++) {
		IPackageFragment fragment= root.getPackageFragment(name.toString());
		if (!fragment.exists()) {
			created.add(fragment);
		}

		if (fragment.equals(destination)) {
			root.createPackageFragment(name.toString(), true, pm);
			return created.toArray(new IPackageFragment[created.size()]);
		}

		name.append("."); //$NON-NLS-1$
		name.append(split[i + 1]);
	}

	return null;
}
 
Example 4
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Evaluates if a member in the focus' element hierarchy is visible from
 * elements in a package.
 * @param member The member to test the visibility for
 * @param pack The package of the focus element focus
 * @return returns <code>true</code> if the member is visible from the package
 * @throws JavaModelException thrown when the member can not be accessed
 */
public static boolean isVisibleInHierarchy(IMember member, IPackageFragment pack) throws JavaModelException {
	int type= member.getElementType();
	if  (type == IJavaElement.INITIALIZER ||  (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { //$NON-NLS-1$
		return false;
	}

	int otherflags= member.getFlags();

	IType declaringType= member.getDeclaringType();
	if (Flags.isPublic(otherflags) || Flags.isProtected(otherflags) || (declaringType != null && isInterfaceOrAnnotation(declaringType))) {
		return true;
	} else if (Flags.isPrivate(otherflags)) {
		return false;
	}

	IPackageFragment otherpack= (IPackageFragment) member.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
	return (pack != null && pack.equals(otherpack));
}
 
Example 5
Source File: OverwriteHelper.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean canOverwrite(ICompilationUnit cu) {
	if (fDestination instanceof IPackageFragment){
		IPackageFragment destination= (IPackageFragment)fDestination;
		return ! destination.equals(cu.getParent()) && destination.getCompilationUnit(cu.getElementName()).exists();
	} else {
		return willOverwrite(ReorgUtils.getResource(cu));
	}
}
 
Example 6
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected RefactoringStatus verifyDestination(IJavaElement destination) throws JavaModelException {
	RefactoringStatus superStatus= super.verifyDestination(destination);
	if (superStatus.hasFatalError()) {
		return superStatus;
	}

	Object commonParent= new ParentChecker(getResources(), getJavaElements()).getCommonParent();
	if (destination.equals(commonParent)) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_parent);
	}
	IContainer destinationAsContainer= getDestinationAsContainer();
	if (destinationAsContainer != null && (destinationAsContainer.equals(commonParent) || commonParent instanceof IPackageFragmentRoot
			&& destinationAsContainer.equals(((IPackageFragmentRoot) commonParent).getResource()))) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_parent);
	}
	IPackageFragment destinationAsPackage= getDestinationAsPackageFragment();
	if (destinationAsPackage != null && (destinationAsPackage.equals(commonParent))) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_parent);
	}

	if (cannotUpdateReferencesForDestination()) {
		superStatus.addInfo(RefactoringCoreMessages.ReorgPolicyFactory_noJavaUpdates);
	}

	return superStatus;
}
 
Example 7
Source File: MoveCuUpdateCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean isInAnotherFragmentOfSamePackage(ICompilationUnit cu, IPackageFragment pack) {
	if (!(cu.getParent() instanceof IPackageFragment)) {
		return false;
	}
	IPackageFragment cuPack = (IPackageFragment) cu.getParent();
	return !cuPack.equals(pack) && JavaModelUtil.isSamePackage(cuPack, pack);
}
 
Example 8
Source File: OverwriteHelper.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean canOverwrite(ICompilationUnit cu) {
	if (fDestination instanceof IPackageFragment){
		IPackageFragment destination= (IPackageFragment)fDestination;
		return ! destination.equals(cu.getParent()) && destination.getCompilationUnit(cu.getElementName()).exists();
	} else {
		return willOverwrite(ReorgUtils.getResource(cu));
	}
}
 
Example 9
Source File: GotoPackageAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void gotoPackage(IPackageFragment p) {
	fPackageExplorer.selectReveal(new StructuredSelection(p));
	if (!p.equals(getSelectedElement())) {
		MessageDialog.openInformation(fPackageExplorer.getSite().getShell(),
			getDialogTitle(),
			Messages.format(PackagesMessages.PackageExplorer_element_not_present, JavaElementLabels.getElementLabel(p, JavaElementLabels.ALL_DEFAULT)));
	}
}
 
Example 10
Source File: PackagesViewFlatContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void addElement(IPackageFragment frag) {
	String key= getKey(frag);
	LogicalPackage lp= fMapToLogicalPackage.get(key);

	if(lp != null && lp.belongs(frag)){
		lp.add(frag);
		return;
	}

	IPackageFragment fragment= fMapToPackageFragments.get(key);
	if(fragment != null){
		//must create a new LogicalPackage
		if(!fragment.equals(frag)){
			lp= new LogicalPackage(fragment);
			lp.add(frag);
			fMapToLogicalPackage.put(key, lp);

			//@Improve: should I replace this with a refresh?
			postRemove(fragment);
			postAdd(lp);

			return;
		}
	}

	else {
		fMapToPackageFragments.put(key, frag);
		postAdd(frag);
	}
}
 
Example 11
Source File: PackagesViewHierarchicalContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private LogicalPackage createLogicalPackage(IPackageFragment pkgFragment) {
	if(!fInputIsProject)
		return null;

	List<IPackageFragment> fragments= new ArrayList<IPackageFragment>();
	try {
		IPackageFragmentRoot[] roots= pkgFragment.getJavaProject().getPackageFragmentRoots();
		for (int i= 0; i < roots.length; i++) {
			IPackageFragmentRoot root= roots[i];
			IPackageFragment fragment= root.getPackageFragment(pkgFragment.getElementName());
			if(fragment.exists() && !fragment.equals(pkgFragment))
				fragments.add(fragment);
		}
		if(!fragments.isEmpty()) {
			LogicalPackage logicalPackage= new LogicalPackage(pkgFragment);
			fMapToLogicalPackage.put(getKey(pkgFragment), logicalPackage);
			Iterator<IPackageFragment> iter= fragments.iterator();
			while(iter.hasNext()){
				IPackageFragment f= iter.next();
				if(logicalPackage.belongs(f)){
					logicalPackage.add(f);
					fMapToLogicalPackage.put(getKey(f), logicalPackage);
				}
			}

			return logicalPackage;
		}

	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}

	return null;
}
 
Example 12
Source File: MoveCuUpdateCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean isInAnotherFragmentOfSamePackage(ICompilationUnit cu, IPackageFragment pack) {
	if (! (cu.getParent() instanceof IPackageFragment))
		return false;
	IPackageFragment cuPack= (IPackageFragment) cu.getParent();
	return ! cuPack.equals(pack) && JavaModelUtil.isSamePackage(cuPack, pack);
}
 
Example 13
Source File: LogicalPackagesProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Combines packages with same names into a logical package which will
 * be added to the resulting array. If a package is not yet in this content
 * provider then the package fragment is added to the resulting array.
 *
 * @param packageFragments the package fragments to combine
 * @return an array with combined (logical) packages and package fragments
 */
protected Object[] combineSamePackagesIntoLogialPackages(IPackageFragment[] packageFragments) {

	if (!fCompoundState)
		return packageFragments;

	List<IAdaptable> newChildren= new ArrayList<IAdaptable>();

	for (int i= 0; i < packageFragments.length; i++) {
		IPackageFragment fragment=  packageFragments[i];

		if (fragment == null)
			continue;

		LogicalPackage lp= findLogicalPackage(fragment);

		if (lp != null) {
			if (lp.belongs(fragment)) {
				lp.add(fragment);
			}
			if(!newChildren.contains(lp))
				newChildren.add(lp);

		} else {
			String key= getKey(fragment);
			IPackageFragment frag= fMapToPackageFragments.get(key);
			if (frag != null && !fragment.equals(frag)) {
				lp= new LogicalPackage(frag);
				lp.add(fragment);
				newChildren.remove(frag);
				newChildren.add(lp);
				fMapToLogicalPackage.put(key, lp);
				fMapToPackageFragments.remove(frag);
			} else {
				fMapToPackageFragments.put(key, fragment);
				newChildren.add(fragment);
			}
		}
	}
	return newChildren.toArray();
}