Java Code Examples for org.eclipse.jdt.core.IPackageFragmentRoot#getPackageFragment()
The following examples show how to use
org.eclipse.jdt.core.IPackageFragmentRoot#getPackageFragment() .
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: JavaClassPathResourceForIEditorInputFactoryTest.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
@Test public void testBug463258_02() throws Exception { IJavaProject project = createJavaProject("foo"); IFile file = project.getProject().getFile("foo.jar"); file.create(jarInputStream(new TextFile("do/not", "care")), true, monitor()); addJarToClasspath(project, file); IPackageFragmentRoot root = project.getPackageFragmentRoot(file); IPackageFragment foo = root.getPackageFragment("unknown"); JarEntryFile fileInJar = new JarEntryFile("doesNotExist.testlanguage"); fileInJar.setParent(foo); XtextReadonlyEditorInput editorInput = new XtextReadonlyEditorInput(fileInJar); Resource resource = factory.createResource(editorInput); assertNotNull(resource); try { resource.load(null); } catch(IOException e) { // expected } }
Example 2
Source File: JavaClassPathResourceForIEditorInputFactoryTest.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
@Test public void testBug463258_01() throws Exception { IJavaProject project = createJavaProject("foo"); IFile file = project.getProject().getFile("foo.jar"); file.create(jarInputStream(new TextFile("foo/A.testlanguage", "//empty")), true, monitor()); addJarToClasspath(project, file); IPackageFragmentRoot root = project.getPackageFragmentRoot(file); IPackageFragment foo = root.getPackageFragment("foo"); JarEntryFile fileInJar = new JarEntryFile("A.testlanguage"); fileInJar.setParent(foo); XtextReadonlyEditorInput editorInput = new XtextReadonlyEditorInput(fileInJar); Resource resource = factory.createResource(editorInput); assertNotNull(resource); resource.load(null); }
Example 3
Source File: JavaElementLinks.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private String getPackageFragmentElementName(IJavaElement javaElement) { IPackageFragmentRoot root= (IPackageFragmentRoot) javaElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); String javaElementName= javaElement.getElementName(); String[] individualSegmentNames= javaElementName.split("\\."); //$NON-NLS-1$ String packageName= null; StringBuffer strBuffer= new StringBuffer(); for (int i= 0; i < individualSegmentNames.length; i++) { String lastSegmentName= individualSegmentNames[i]; if (packageName != null) { strBuffer.append('.'); packageName= packageName + '.' + lastSegmentName; } else { packageName= lastSegmentName; } IPackageFragment subFragment= root.getPackageFragment(packageName); strBuffer.append(getElementName(subFragment, lastSegmentName)); } return strBuffer.toString(); }
Example 4
Source File: JavaClassPathResourceForIEditorInputFactoryTest.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
@Test(expected=CoreException.class) public void testBug463258_04() throws Throwable { IFolder externalFolder = createExternalFolder("externalFolder"); IJavaProject project = createJavaProject("foo"); addExternalFolderToClasspath(project, externalFolder); IPackageFragmentRoot root = project.getPackageFragmentRoot(externalFolder); IPackageFragment foo = root.getPackageFragment("foo"); NonJavaResource fileInFolder = new NonJavaResource(foo, externalFolder.getFile("foo/doesNotExist.testlanguage")); externalFolder.delete(true, null); XtextReadonlyEditorInput editorInput = new XtextReadonlyEditorInput(fileInFolder); try { factory.createResource(editorInput); } catch(WrappedException e) { throw e.getCause(); } }
Example 5
Source File: ReorgPolicyFactory.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private IPackageFragment getJavaDestinationAsPackageFragment(IJavaElement javaDest) { if (javaDest == null || fCheckDestination && !javaDest.exists()) return null; if (javaDest instanceof IPackageFragment) return (IPackageFragment) javaDest; if (javaDest instanceof IPackageFragmentRoot) return ((IPackageFragmentRoot) javaDest).getPackageFragment(""); //$NON-NLS-1$ if (javaDest instanceof IJavaProject) { try { IPackageFragmentRoot root= ReorgUtils.getCorrespondingPackageFragmentRoot((IJavaProject) javaDest); if (root != null) return root.getPackageFragment(""); //$NON-NLS-1$ } catch (JavaModelException e) { // fall through } } return (IPackageFragment) javaDest.getAncestor(IJavaElement.PACKAGE_FRAGMENT); }
Example 6
Source File: Storage2UriMapperJdtImplTest.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
@Test public void testBug463258_01() throws Exception { IJavaProject project = createJavaProject("foo"); IFile file = project.getProject().getFile("foo.jar"); file.create(jarInputStream(new TextFile("foo/bar.notindexed", "//empty")), true, monitor()); addJarToClasspath(project, file); Storage2UriMapperJavaImpl impl = getStorage2UriMapper(); IPackageFragmentRoot root = project.getPackageFragmentRoot(file); IPackageFragment foo = root.getPackageFragment("foo"); JarEntryFile fileInJar = new JarEntryFile("bar.notindexed"); fileInJar.setParent(foo); URI uri = impl.getUri(fileInJar); assertEquals("archive:platform:/resource/foo/foo.jar!/foo/bar.notindexed", uri.toString()); try (InputStream stream = new ResourceSetImpl().getURIConverter().createInputStream(uri)) { byte[] bytes = ByteStreams.toByteArray(stream); assertEquals("//empty", new String(bytes)); } }
Example 7
Source File: JavaElementUtil.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
/** * @param pack the package fragment; may not be null * @return the parent package fragment, or null if the given package fragment is the default package or a top level package */ public static IPackageFragment getParentSubpackage(IPackageFragment pack) { if (pack.isDefaultPackage()) { return null; } final int index= pack.getElementName().lastIndexOf('.'); if (index == -1) { return null; } final IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent(); final String newPackageName= pack.getElementName().substring(0, index); final IPackageFragment parent= root.getPackageFragment(newPackageName); if (parent.exists()) { return parent; } else { return null; } }
Example 8
Source File: Storage2UriMapperJdtImplTest.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
@Test public void testBug463258_03a() throws Exception { IJavaProject project = createJavaProject("foo"); IFile file = project.getProject().getFile("foo.jar"); file.create(jarInputStream(new TextFile("foo/bar.notindexed", "//empty")), true, monitor()); addJarToClasspath(project, file); Storage2UriMapperJavaImpl impl = getStorage2UriMapper(); IPackageFragmentRoot root = project.getPackageFragmentRoot(file); IPackageFragment foo = root.getPackageFragment("foo"); JarEntryFile fileInJar = new JarEntryFile("bar.notindexed"); fileInJar.setParent(foo); File jarFile = file.getRawLocation().toFile(); assertTrue("exists", jarFile.exists()); assertTrue("delete", jarFile.delete()); URI uri = impl.getUri(fileInJar); assertEquals("archive:platform:/resource/foo/foo.jar!/foo/bar.notindexed", uri.toString()); }
Example 9
Source File: NewPackageWizardPage.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Returns the resource handle that corresponds to the element that was created or * will be created. * @return A resource or null if the page contains illegal values. * @since 3.0 */ public IResource getModifiedResource() { IPackageFragmentRoot root= getPackageFragmentRoot(); if (root != null) { IPackageFragment pack= root.getPackageFragment(getPackageText()); IResource packRes= pack.getResource(); if (isCreatePackageDocumentation()) { if (JavaModelUtil.is50OrHigher(getJavaProject())) { return pack.getCompilationUnit(PACKAGE_INFO_JAVA_FILENAME).getResource(); } else if (packRes instanceof IFolder){ return ((IFolder) packRes).getFile(PACKAGE_HTML_FILENAME); } } return packRes; } return null; }
Example 10
Source File: Storage2UriMapperJdtImplTest.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
@Test public void testBug463258_03c() throws Exception { IJavaProject project = createJavaProject("foo"); IFile file = project.getProject().getFile("foo.jar"); file.create(jarInputStream(new TextFile("foo/bar.notindexed", "//empty")), true, monitor()); addJarToClasspath(project, file); Storage2UriMapperJavaImpl impl = getStorage2UriMapper(); IPackageFragmentRoot root = JarPackageFragmentRootTestUtil.getJarPackageFragmentRoot(file, (JavaProject) project); IPackageFragment foo = root.getPackageFragment("foo"); JarEntryFile fileInJar = new JarEntryFile("bar.notindexed"); fileInJar.setParent(foo); File jarFile = file.getLocation().toFile(); assertTrue("exists", jarFile.exists()); assertTrue("delete", jarFile.delete()); // simulate an automated refresh file.refreshLocal(IResource.DEPTH_ONE, null); URI uri = impl.getUri(fileInJar); assertNull(uri); }
Example 11
Source File: NLSHintHelper.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
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 12
Source File: ProjectAwareUniqueClassNameValidator.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
private SourceTraversal doCheckUniqueInProjectSource(String packageName, String typeName, JvmDeclaredType type, List<IPackageFragmentRoot> sourceFolders) throws JavaModelException { IndexManager indexManager = JavaModelManager.getIndexManager(); for (IPackageFragmentRoot sourceFolder : sourceFolders) { if (indexManager.awaitingJobsCount() > 0) { if (!isDerived(sourceFolder.getResource())) { IPackageFragment packageFragment = sourceFolder.getPackageFragment(packageName); if (packageFragment.exists()) { for (ICompilationUnit unit : packageFragment.getCompilationUnits()) { if (!isDerived(unit.getResource())) { IType javaType = unit.getType(typeName); if (javaType.exists()) { addIssue(type, unit.getElementName()); return SourceTraversal.DUPLICATE; } } } } } } else { return SourceTraversal.INTERRUPT; } } return SourceTraversal.UNIQUE; }
Example 13
Source File: ReorgCorrectionsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
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 14
Source File: RenamePackageProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static boolean isPackageNameOkInRoot(String newName, IPackageFragmentRoot root) throws CoreException { IPackageFragment pack= root.getPackageFragment(newName); if (! pack.exists()) { return true; } else if (pack.containsJavaResources()) { return false; } else if (pack.getNonJavaResources().length != 0) { return false; } else { return true; } }
Example 15
Source File: RenamePackageProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static boolean isPackageNameOkInRoot(String newName, IPackageFragmentRoot root) throws CoreException { IPackageFragment pack= root.getPackageFragment(newName); if (! pack.exists()) return true; else if (pack.containsJavaResources()) return false; else if (pack.getNonJavaResources().length != 0) return false; else return true; }
Example 16
Source File: RenamePackageProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private RefactoringStatus checkTypeNameConflicts(IPackageFragmentRoot root, String newName, Set<String> topLevelTypeNames) throws CoreException { IPackageFragment otherPack= root.getPackageFragment(newName); if (fPackage.equals(otherPack)) { return null; } ICompilationUnit[] cus= otherPack.getCompilationUnits(); RefactoringStatus result= new RefactoringStatus(); for (int i= 0; i < cus.length; i++) { result.merge(checkTypeNameConflicts(cus[i], topLevelTypeNames)); } return result; }
Example 17
Source File: ReorgCorrectionsSubProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) throws CoreException { ICompilationUnit cu= context.getCompilationUnit(); // 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()); boolean isLinked= cu.getResource().isLinked(); 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 }); } proposals.add(new ChangeCorrectionProposal(label, CodeActionKind.QuickFix, new MoveCompilationUnitChange(cu, newPack), IProposalRelevance.MOVE_CU_TO_PACKAGE)); } }
Example 18
Source File: RenamePackageProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private RefactoringStatus checkTypeNameConflicts(IPackageFragmentRoot root, String newName, Set<String> topLevelTypeNames) throws CoreException { IPackageFragment otherPack= root.getPackageFragment(newName); if (fPackage.equals(otherPack)) return null; ICompilationUnit[] cus= otherPack.getCompilationUnits(); RefactoringStatus result= new RefactoringStatus(); for (int i= 0; i < cus.length; i++) { result.merge(checkTypeNameConflicts(cus[i], topLevelTypeNames)); } return result; }
Example 19
Source File: ElementTypeTeller.java From txtUML with Eclipse Public License 1.0 | 5 votes |
public static boolean isModelPackage(IPackageFragmentRoot pfRoot, String packageName) throws JavaModelException { IPackageFragment pack; while (!packageName.isEmpty()) { pack = pfRoot.getPackageFragment(packageName); if (pack.exists() && isModelRootPackage(pack)) { return true; } int lastDot = packageName.lastIndexOf("."); if (lastDot == -1) { lastDot = 0; } packageName = packageName.substring(0, lastDot); } return false; }
Example 20
Source File: NewTypeWizardPage.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Checks if the package field has to be pre-filled in this page and returns the package * fragment to be used for that. The package fragment has the name of the project if the source * folder does not contain any package and if the project name is a valid package name. If the * source folder contains exactly one package then the name of that package is used as the * package fragment's name. <code>null</code> is returned if none of the above is applicable. * * @param javaProject the containing Java project of the selection used to initialize this page * * @return the package fragment to be pre-filled in this page or <code>null</code> if no * suitable package can be suggested for the given project * * @since 3.9 */ private IPackageFragment getPackage(IJavaProject javaProject) { String packName= null; final IPackageFragmentRoot pkgFragmentRoot= getPackageFragmentRoot(); IJavaElement[] packages= null; try { if (pkgFragmentRoot != null && pkgFragmentRoot.exists()) { packages= pkgFragmentRoot.getChildren(); if (packages.length == 1) { // only default package -> use Project name packName= javaProject.getElementName(); // validate package name IStatus status= validatePackageName(packName, javaProject); if (status.getSeverity() == IStatus.OK) { return pkgFragmentRoot.getPackageFragment(packName); } } else { int noOfPackages= 0; IPackageFragment thePackage= null; for (final IJavaElement pack : packages) { IPackageFragment pkg= (IPackageFragment) pack; // ignoring empty parent packages and default package if ((!pkg.hasSubpackages() || pkg.hasChildren()) && !pkg.isDefaultPackage()) { noOfPackages++; thePackage= pkg; if (noOfPackages > 1) { return null; } } } if (noOfPackages == 1) { // use package name packName= thePackage.getElementName(); return pkgFragmentRoot.getPackageFragment(packName); } } } } catch (JavaModelException e) { // fall through } return null; }