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

The following examples show how to use org.eclipse.jdt.core.IPackageFragment#isDefaultPackage() . 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: CorrectPackageDeclarationProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public String getName() {
	ICompilationUnit cu= getCompilationUnit();
	IPackageFragment parentPack= (IPackageFragment) cu.getParent();
	try {
		IPackageDeclaration[] decls= cu.getPackageDeclarations();
		if (parentPack.isDefaultPackage() && decls.length > 0) {
			return Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_remove_description, BasicElementLabels.getJavaElementName(decls[0].getElementName()));
		}
		if (!parentPack.isDefaultPackage() && decls.length == 0) {
			return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_add_description,  JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
		}
	} catch(JavaModelException e) {
		JavaPlugin.log(e);
	}
	return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_change_description, JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
}
 
Example 2
Source File: NLSHintHelper.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static IStorage getResourceBundle(IPackageFragmentRoot root, String packageName, String resourceName) throws JavaModelException {
	IPackageFragment packageFragment= root.getPackageFragment(packageName);
	if (packageFragment.exists()) {
		Object[] resources= packageFragment.isDefaultPackage() ? root.getNonJavaResources() : packageFragment.getNonJavaResources();
		for (int j= 0; j < resources.length; j++) {
			Object object= resources[j];
			if (JavaModelUtil.isOpenableStorage(object)) {
				IStorage storage= (IStorage)object;
				if (storage.getName().equals(resourceName)) {
					return storage;
				}
			}
		}
	}
	return null;
}
 
Example 3
Source File: JavaElementUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param pack a package fragment
 * @return an array containing the given package and all subpackages
 * @throws JavaModelException if getting the all sibling packages fails
 */
public static IPackageFragment[] getPackageAndSubpackages(IPackageFragment pack) throws JavaModelException {
	if (pack.isDefaultPackage()) {
		return new IPackageFragment[] { pack };
	}

	IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent();
	IJavaElement[] allPackages= root.getChildren();
	ArrayList<IPackageFragment> subpackages= new ArrayList<>();
	subpackages.add(pack);
	String prefix= pack.getElementName() + '.';
	for (int i= 0; i < allPackages.length; i++) {
		IPackageFragment currentPackage= (IPackageFragment) allPackages[i];
		if (currentPackage.getElementName().startsWith(prefix)) {
			subpackages.add(currentPackage);
		}
	}
	return subpackages.toArray(new IPackageFragment[subpackages.size()]);
}
 
Example 4
Source File: AbstractNewXtendElementWizardPage.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected IStatus typeNameChanged() {
	IPackageFragment packageFragment = getPackageFragment();
	if (packageFragment != null) {
		IResource resource = packageFragment.getResource();
		if (resource instanceof IFolder) {
			IFolder folder = (IFolder) resource;
			if (folder.getFile(getTypeName() + ".xtend").exists()) { //$NON-NLS-1$
				String packageName = ""; //$NON-NLS-1$
				if (!packageFragment.isDefaultPackage()) {
					packageName = packageFragment.getElementName() + "."; //$NON-NLS-1$
				}
				return new StatusInfo(IStatus.ERROR, Messages.TYPE_EXISTS_0 + packageName + getTypeName()
						+ Messages.TYPE_EXISTS_1);
			}
		}
	}
	return super.typeNameChanged();
}
 
Example 5
Source File: RenameModifications.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private IPackageFragment[] getSubpackages(IPackageFragment pack) throws CoreException {
	IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent();
	IJavaElement[] allPackages= root.getChildren();
	if (pack.isDefaultPackage()) {
		return new IPackageFragment[0];
	}
	ArrayList<IPackageFragment> result= new ArrayList<>();
	String prefix= pack.getElementName() + '.';
	for (int i= 0; i < allPackages.length; i++) {
		IPackageFragment currentPackage= (IPackageFragment) allPackages[i];
		if (currentPackage.getElementName().startsWith(prefix)) {
			result.add(currentPackage);
		}
	}
	return result.toArray(new IPackageFragment[result.size()]);
}
 
Example 6
Source File: JavaElementUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @param pack a package fragment
 * @return an array containing the given package and all subpackages
 * @throws JavaModelException if getting the all sibling packages fails
 */
public static IPackageFragment[] getPackageAndSubpackages(IPackageFragment pack) throws JavaModelException {
	if (pack.isDefaultPackage())
		return new IPackageFragment[] { pack };

	IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent();
	IJavaElement[] allPackages= root.getChildren();
	ArrayList<IPackageFragment> subpackages= new ArrayList<IPackageFragment>();
	subpackages.add(pack);
	String prefix= pack.getElementName() + '.';
	for (int i= 0; i < allPackages.length; i++) {
		IPackageFragment currentPackage= (IPackageFragment) allPackages[i];
		if (currentPackage.getElementName().startsWith(prefix))
			subpackages.add(currentPackage);
	}
	return subpackages.toArray(new IPackageFragment[subpackages.size()]);
}
 
Example 7
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Appends the label for a compilation unit. Considers the CU_* flags.
 *
 * @param cu the element to render
 * @param flags the rendering flags. Flags with names starting with 'CU_' are considered.
 */
public void appendCompilationUnitLabel(ICompilationUnit cu, long flags) {
	if (getFlag(flags, JavaElementLabels.CU_QUALIFIED)) {
		IPackageFragment pack= (IPackageFragment) cu.getParent();
		if (!pack.isDefaultPackage()) {
			appendPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS));
			fBuffer.append('.');
		}
	}
	fBuffer.append(cu.getElementName());

	if (getFlag(flags, JavaElementLabels.CU_POST_QUALIFIED)) {
		int offset= fBuffer.length();
		fBuffer.append(JavaElementLabels.CONCAT_STRING);
		appendPackageFragmentLabel((IPackageFragment) cu.getParent(), flags & QUALIFIER_FLAGS);
		if (getFlag(flags, JavaElementLabels.COLORIZE)) {
			fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
		}
	}
}
 
Example 8
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @return <code>true</code> if the user could expect that we update references, but we don't
 * @since 3.5
 */
private boolean cannotUpdateReferencesForDestination() {
	if (getCus().length == 0) {
		return false;
	}
	IPackageFragment pack= getDestinationAsPackageFragment();
	if (pack == null || pack.isDefaultPackage()) {
		return true;
	}

	IJavaElement destination= getJavaElementDestination();
	if (destination instanceof IPackageFragmentRoot && getCus().length > 0) {
		return false;
	}

	Object commonParent= getCommonParent();
	return JavaElementUtil.isDefaultPackage(commonParent);
}
 
Example 9
Source File: NewTypeWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Uses the New Java file template from the code template page to generate a
 * compilation unit with the given type content.
 * 
 * @param cu The new created compilation unit
 * @param typeContent The content of the type, including signature and type
 * body.
 * @param lineDelimiter The line delimiter to be used.
 * @return String Returns the result of evaluating the new file template
 * with the given type content.
 * @throws CoreException when fetching the file comment fails or fetching the content for the
 *             new compilation unit fails
 * @since 2.1
 */
protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
	String fileComment= getFileComment(cu, lineDelimiter);
	String typeComment= getTypeComment(cu, lineDelimiter);
	IPackageFragment pack= (IPackageFragment) cu.getParent();
	String content= CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter);
	if (content != null) {
		ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
		parser.setProject(cu.getJavaProject());
		parser.setSource(content.toCharArray());
		CompilationUnit unit= (CompilationUnit) parser.createAST(null);
		if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
			return content;
		}
	}
	StringBuffer buf= new StringBuffer();
	if (!pack.isDefaultPackage()) {
		buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$
	}
	buf.append(lineDelimiter).append(lineDelimiter);
	if (typeComment != null) {
		buf.append(typeComment).append(lineDelimiter);
	}
	buf.append(typeContent);
	return buf.toString();
}
 
Example 10
Source File: CorrectPackageDeclarationProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
	super.addEdits(doc, root);

	ICompilationUnit cu= getCompilationUnit();

	IPackageFragment parentPack= (IPackageFragment) cu.getParent();
	IPackageDeclaration[] decls= cu.getPackageDeclarations();

	if (parentPack.isDefaultPackage() && decls.length > 0) {
		for (int i= 0; i < decls.length; i++) {
			ISourceRange range= decls[i].getSourceRange();
			root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
		}
		return;
	}
	if (!parentPack.isDefaultPackage() && decls.length == 0) {
		String lineDelim = "\n";
		String str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; //$NON-NLS-1$
		root.addChild(new InsertEdit(0, str));
		return;
	}

	root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
 
Example 11
Source File: ReorgCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
	ICompilationUnit cu= context.getCompilationUnit();
	boolean isLinked= cu.getResource().isLinked();

	// correct package declaration
	int relevance= cu.getPackageDeclarations().length == 0 ? IProposalRelevance.MISSING_PACKAGE_DECLARATION : IProposalRelevance.CORRECT_PACKAGE_DECLARATION; // bug 38357
	proposals.add(new CorrectPackageDeclarationProposal(cu, problem, relevance));

	// move to package
	IPackageDeclaration[] packDecls= cu.getPackageDeclarations();
	String newPackName= packDecls.length > 0 ? packDecls[0].getElementName() : ""; //$NON-NLS-1$

	IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(cu);
	IPackageFragment newPack= root.getPackageFragment(newPackName);

	ICompilationUnit newCU= newPack.getCompilationUnit(cu.getElementName());
	if (!newCU.exists() && !isLinked) {
		String label;
		if (newPack.isDefaultPackage()) {
			label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_default_description, BasicElementLabels.getFileName(cu));
		} else {
			String packageLabel= JavaElementLabels.getElementLabel(newPack, JavaElementLabels.ALL_DEFAULT);
			label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_description, new Object[] { BasicElementLabels.getFileName(cu), packageLabel });
		}
		CompositeChange composite= new CompositeChange(label);
		composite.add(new CreatePackageChange(newPack));
		composite.add(new MoveCompilationUnitChange(cu, newPack));

		proposals.add(new ChangeCorrectionProposal(label, composite, IProposalRelevance.MOVE_CU_TO_PACKAGE, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_MOVE)));
	}
}
 
Example 12
Source File: ExtractInterfaceProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks whether the supertype clashes with existing types.
 *
 * @return the status of the condition checking
 * @throws JavaModelException
 *             if an error occurs
 */
protected final RefactoringStatus checkSuperType() throws JavaModelException {
	final IPackageFragment fragment= fSubType.getPackageFragment();
	final IType type= Checks.findTypeInPackage(fragment, fSuperName);
	if (type != null && type.exists()) {
		if (fragment.isDefaultPackage())
			return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.ExtractInterfaceProcessor_existing_default_type, BasicElementLabels.getJavaElementName(fSuperName)));
		else {
			String packageLabel= JavaElementLabels.getElementLabel(fragment, JavaElementLabels.ALL_DEFAULT);
			return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.ExtractInterfaceProcessor_existing_type, new String[] { BasicElementLabels.getJavaElementName(fSuperName), packageLabel }));
		}
	}
	return new RefactoringStatus();
}
 
Example 13
Source File: TypeCreator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private String createCuContent(ICompilationUnit cu, String typeContent) throws CoreException {
  String fileComment = getFileComment(cu);
  String typeComment = getTypeComment(cu);
  IPackageFragment cuPckg = (IPackageFragment) cu.getParent();

  // Use the 'New Java File' code template specified by workspace preferences
  String content = CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter);
  if (content != null) {
    // Parse the generated source to make sure it's error-free
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setProject(cu.getJavaProject());
    parser.setSource(content.toCharArray());
    CompilationUnit unit = (CompilationUnit) parser.createAST(null);
    if ((cuPckg.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
      return content;
    }
  }

  // If we didn't have a template to use, just generate the source by hand
  StringBuffer buf = new StringBuffer();
  if (!cuPckg.isDefaultPackage()) {
    buf.append("package ").append(cuPckg.getElementName()).append(';');
  }
  buf.append(lineDelimiter).append(lineDelimiter);
  if (typeComment != null) {
    buf.append(typeComment).append(lineDelimiter);
  }
  buf.append(typeContent);
  return buf.toString();
}
 
Example 14
Source File: DialogPackageExplorer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
          try {
              if (element instanceof IFile) {
                  IFile file= (IFile) element;
                  if (file.getName().equals(".classpath") || file.getName().equals(".project")) //$NON-NLS-1$//$NON-NLS-2$
                      return false;
              } else if (element instanceof IPackageFragmentRoot) {
                  IClasspathEntry cpe= ((IPackageFragmentRoot)element).getRawClasspathEntry();
                  if (cpe == null || cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER || cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY || cpe.getEntryKind() == IClasspathEntry.CPE_VARIABLE)
                      return false;
              } else if (element instanceof PackageFragmentRootContainer) {
              	return false;
              } else if (element instanceof IPackageFragment) {
			IPackageFragment fragment= (IPackageFragment)element;
              	if (fragment.isDefaultPackage() && !fragment.hasChildren())
              		return false;
              } else if (element instanceof IFolder) {
              	IFolder folder= (IFolder)element;
              	if (folder.getName().startsWith(".")) //$NON-NLS-1$
              		return false;
              }
          } catch (JavaModelException e) {
              JavaPlugin.log(e);
          }
          /*if (element instanceof IPackageFragmentRoot) {
              IPackageFragmentRoot root= (IPackageFragmentRoot)element;
              if (root.getElementName().endsWith(".jar") || root.getElementName().endsWith(".zip")) //$NON-NLS-1$ //$NON-NLS-2$
                  return false;
          }*/
          return /*super.select(viewer, parentElement, element) &&*/ fOutputFolderFilter.select(viewer, parentElement, element);
      }
 
Example 15
Source File: PackagesViewHierarchicalContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean hasChildren(Object element) {

		if (element instanceof IPackageFragment) {
			IPackageFragment fragment= (IPackageFragment) element;
			if(fragment.isDefaultPackage() || !fragment.exists())
				return false;
		}
		return getChildren(element).length > 0;
	}
 
Example 16
Source File: EmptyInnerPackageFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean select(Viewer viewer, Object parent, Object element) {
	if (element instanceof IPackageFragment) {
		IPackageFragment pkg= (IPackageFragment)element;
		try {
			if (pkg.isDefaultPackage())
				return pkg.hasChildren();
			return !pkg.hasSubpackages() || pkg.hasChildren() || EmptyPackageFilter.hasUnfilteredResources(viewer, pkg);
		} catch (JavaModelException e) {
			return false;
		}
	}

	return true;
}
 
Example 17
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isRenameAvailable(final IPackageFragment fragment) throws JavaModelException {
	if (fragment == null) {
		return false;
	}
	if (!Checks.isAvailable(fragment)) {
		return false;
	}
	if (fragment.isDefaultPackage()) {
		return false;
	}
	return true;
}
 
Example 18
Source File: RenameModifications.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IPackageFragment[] getSubpackages(IPackageFragment pack) throws CoreException {
	IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent();
	IJavaElement[] allPackages= root.getChildren();
	if (pack.isDefaultPackage())
		return new IPackageFragment[0];
	ArrayList<IPackageFragment> result= new ArrayList<IPackageFragment>();
	String prefix= pack.getElementName() + '.';
	for (int i= 0; i < allPackages.length; i++) {
		IPackageFragment currentPackage= (IPackageFragment) allPackages[i];
		if (currentPackage.getElementName().startsWith(prefix))
			result.add(currentPackage);
	}
	return result.toArray(new IPackageFragment[result.size()]);
}
 
Example 19
Source File: JdtClasspathUriResolver.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private Object[] getNonJavaResources(IPackageFragmentRoot packageFragmentRoot, IPackageFragment packageFragment)
		throws JavaModelException {
	Object[] nonJavaResources = null;
	if (packageFragment.isDefaultPackage()) {
		nonJavaResources = packageFragmentRoot.getNonJavaResources();
	} else {
		nonJavaResources = packageFragment.getNonJavaResources();
	}
	return nonJavaResources;
}
 
Example 20
Source File: TreeHierarchyLayoutProblemsDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected int computePackageAdornmentFlags(IPackageFragment fragment) {
	if (!fIsFlatLayout && !fragment.isDefaultPackage()) {
		return super.computeAdornmentFlags(fragment.getResource());
	}
	return super.computeAdornmentFlags(fragment);
}