Example usage for org.eclipse.jdt.core IPackageFragmentRoot getPackageFragment

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getPackageFragment

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getPackageFragment.

Prototype

IPackageFragment getPackageFragment(String packageName);

Source Link

Document

Returns the package fragment with the given package name.

Usage

From source file:org.eclipse.pde.api.tools.util.tests.ApiBaselineManagerTests.java

License:Open Source License

/**
 * Adds the given source to the given package in the given fragment root
 * /*from www .  j av  a  2s  .  c  o m*/
 * @param root the root to add the source to
 * @param packagename the name of the package e.g. a.b.c
 * @param sourcename the name of the source file without an extension e.g.
 *            TestClass1
 */
public void assertTestSource(IPackageFragmentRoot root, String packagename, String sourcename)
        throws InvocationTargetException, IOException {
    IPackageFragment fragment = root.getPackageFragment(packagename);
    FileUtils.importFileFromDirectory(SRC_LOC.append(sourcename + ".java").toFile(), fragment.getPath(), //$NON-NLS-1$
            new NullProgressMonitor());
}

From source file:org.eclipse.pde.api.tools.util.tests.ApiBaselineManagerTests.java

License:Open Source License

/**
 * Tests that making Javadoc changes to the source file TestClass2 cause the
 * workspace baseline to be updated.//from  w ww. ja  v  a  2  s  .co m
 * 
 * This test adds a @noinstantiate tag to the source file TestClass2
 */
public void testWPUpdateSourceTypeChanged() throws Exception {
    IJavaProject project = getTestingProject();
    assertNotNull("The testing project must exist", project); //$NON-NLS-1$
    IPackageFragmentRoot root = project.findPackageFragmentRoot(
            new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute());
    assertNotNull("the 'src' package fragment root must exist", root); //$NON-NLS-1$
    NullProgressMonitor monitor = new NullProgressMonitor();
    IPackageFragment fragment = root.getPackageFragment("a.b.c"); //$NON-NLS-1$
    FileUtils.importFileFromDirectory(SRC_LOC.append("TestClass2.java").toFile(), fragment.getPath(), monitor); //$NON-NLS-1$
    ICompilationUnit element = (ICompilationUnit) project.findElement(new Path("a/b/c/TestClass2.java")); //$NON-NLS-1$
    assertNotNull("TestClass2 must exist in the test project", element); //$NON-NLS-1$
    updateTagInSource(element, "TestClass2", null, "@noinstantiate", false); //$NON-NLS-1$ //$NON-NLS-2$
    IApiDescription desc = getTestProjectApiDescription();
    assertNotNull("the testing project api description must exist", desc); //$NON-NLS-1$
    IApiAnnotations annot = desc.resolveAnnotations(Factory.typeDescriptor("a.b.c.TestClass2")); //$NON-NLS-1$
    assertNotNull("the annotations for a.b.c.TestClass2 cannot be null", annot); //$NON-NLS-1$
    assertTrue("there must be a noinstantiate setting for TestClass2", //$NON-NLS-1$
            (annot.getRestrictions() & RestrictionModifiers.NO_INSTANTIATE) != 0);
    assertTrue("there must be a noextend setting for TestClass2", //$NON-NLS-1$
            (annot.getRestrictions() & RestrictionModifiers.NO_EXTEND) != 0);
}

From source file:org.eclipse.pde.internal.core.util.PDEJavaHelper.java

License:Open Source License

private static IPackageFragment getExternalPackageFragment(String packageName, String pluginID) {
    if (pluginID == null)
        return null;
    IPluginModelBase base = null;//from   w ww . ja  v  a 2 s  .c om
    try {
        IPluginModelBase plugin = PluginRegistry.findModel(pluginID);
        if (plugin == null)
            return null;
        ImportPackageSpecification[] packages = plugin.getBundleDescription().getImportPackages();
        for (int i = 0; i < packages.length; i++)
            if (packages[i].getName().equals(packageName)) {
                ExportPackageDescription desc = (ExportPackageDescription) packages[i].getSupplier();
                if (desc != null)
                    base = PluginRegistry.findModel(desc.getExporter().getSymbolicName());
                break;
            }
        if (base == null)
            return null;
        IResource res = base.getUnderlyingResource();
        if (res != null) {
            IJavaProject jp = JavaCore.create(res.getProject());
            if (jp != null)
                try {
                    IPackageFragmentRoot[] roots = jp.getAllPackageFragmentRoots();
                    for (int i = 0; i < roots.length; i++) {
                        IPackageFragment frag = roots[i].getPackageFragment(packageName);
                        if (frag.exists())
                            return frag;
                    }
                } catch (JavaModelException e) {
                }
        }
        IProject proj = PDECore.getWorkspace().getRoot()
                .getProject(SearchablePluginsManager.PROXY_PROJECT_NAME);
        if (proj == null)
            return searchWorkspaceForPackage(packageName, base);
        IJavaProject jp = JavaCore.create(proj);
        IPath path = new Path(base.getInstallLocation());
        // if model is in jar form
        if (!path.toFile().isDirectory()) {
            IPackageFragmentRoot root = jp.findPackageFragmentRoot(path);
            if (root != null) {
                IPackageFragment frag = root.getPackageFragment(packageName);
                if (frag.exists())
                    return frag;
            }
            // else model is in folder form, try to find model's libraries on filesystem
        } else {
            IPluginLibrary[] libs = base.getPluginBase().getLibraries();
            for (int i = 0; i < libs.length; i++) {
                if (IPluginLibrary.RESOURCE.equals(libs[i].getType()))
                    continue;
                String libName = ClasspathUtilCore.expandLibraryName(libs[i].getName());
                IPackageFragmentRoot root = jp.findPackageFragmentRoot(path.append(libName));
                if (root != null) {
                    IPackageFragment frag = root.getPackageFragment(packageName);
                    if (frag.exists())
                        return frag;
                }
            }
        }
    } catch (JavaModelException e) {
    }
    return searchWorkspaceForPackage(packageName, base);
}

From source file:org.eclipse.pde.internal.core.util.PDEJavaHelper.java

License:Open Source License

private static IPackageFragment searchWorkspaceForPackage(String packageName, IPluginModelBase base) {
    IPluginLibrary[] libs = base.getPluginBase().getLibraries();
    ArrayList<IPath> libPaths = new ArrayList<IPath>();
    IPath path = new Path(base.getInstallLocation());
    if (libs.length == 0) {
        libPaths.add(path);/*from w  w  w. j a  v a  2  s.c  om*/
    }
    for (int i = 0; i < libs.length; i++) {
        if (IPluginLibrary.RESOURCE.equals(libs[i].getType()))
            continue;
        String libName = ClasspathUtilCore.expandLibraryName(libs[i].getName());
        libPaths.add(path.append(libName));
    }
    IProject[] projects = PDECore.getWorkspace().getRoot().getProjects();
    for (int i = 0; i < projects.length; i++) {
        try {
            if (!projects[i].hasNature(JavaCore.NATURE_ID) || !projects[i].isOpen())
                continue;
            IJavaProject jp = JavaCore.create(projects[i]);
            ListIterator<IPath> li = libPaths.listIterator();
            while (li.hasNext()) {
                IPackageFragmentRoot root = jp.findPackageFragmentRoot(li.next());
                if (root != null) {
                    IPackageFragment frag = root.getPackageFragment(packageName);
                    if (frag.exists())
                        return frag;
                }
            }
        } catch (CoreException e) {
        }
    }
    return null;
}

From source file:org.eclipse.pde.internal.ui.wizards.plugin.NewLibraryPluginCreationOperation.java

License:Open Source License

private void addPackagesFromFragRoot(IPackageFragmentRoot root, Collection<String> result, List<?> filter) {
    if (root == null)
        return;/*w ww  . j a  va2s  .c o m*/
    try {
        if (filter != null && !filter.contains("*")) { //$NON-NLS-1$
            ListIterator<?> li = filter.listIterator();
            while (li.hasNext()) {
                String pkgName = li.next().toString();
                if (pkgName.endsWith(".*")) //$NON-NLS-1$
                    pkgName = pkgName.substring(0, pkgName.length() - 2);

                IPackageFragment frag = root.getPackageFragment(pkgName);
                if (frag != null)
                    result.add(pkgName);
            }
            return;
        }
        IJavaElement[] children = root.getChildren();
        for (int j = 0; j < children.length; j++) {
            IPackageFragment fragment = (IPackageFragment) children[j];
            String name = fragment.getElementName();
            if (fragment.hasChildren() && !result.contains(name)) {
                result.add(name);
            }
        }
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.rap.ui.internal.launch.rwt.tests.TestProject.java

License:Open Source License

public ICompilationUnit createJavaClass(String packageName, String className, String content)
        throws CoreException {
    initializeJavaProject();//w  w  w  . ja  v  a2 s  .c om
    IProgressMonitor monitor = newProgressMonitor();
    IFile srcFolder = project.getFile(DEFAULT_SOURCE_FOLDER);
    IPackageFragmentRoot packageRoot = javaProject.findPackageFragmentRoot(srcFolder.getFullPath());
    IPackageFragment pkg = packageRoot.getPackageFragment(packageName);
    if (!pkg.exists()) {
        packageRoot.createPackageFragment(packageName, true, monitor);
    }
    String cuName = className + ".java";
    ICompilationUnit result = pkg.createCompilationUnit(cuName, content, true, monitor);
    waitForAutoBuild();
    return result;
}

From source file:org.eclipse.stardust.modeling.validation.util.TypeRequestor.java

License:Open Source License

private IType getJarIType(IJavaModel model, char[] packageName, String qualifiedName, String path, int index)
        throws JavaModelException {
    String jar = path.substring(0, index);
    String rest = path.substring(index + 1);
    index = rest.lastIndexOf(SEPARATOR);
    if (index != -1) {
        rest = rest.substring(index + 1);
    }/*  w  w  w .  j  a va 2  s  .co  m*/
    index = rest.lastIndexOf(EXTENSION_SEPARATOR);
    if (index != -1) {
        String file = rest.substring(0, index);
        String extension = rest.substring(index + 1);

        IPath[] enclosedPaths = scope.enclosingProjectsAndJars();
        for (int i = 0; i < enclosedPaths.length; i++) {
            IPath curr = enclosedPaths[i];
            if (curr.segmentCount() == 1) {
                IJavaProject project = model.getJavaProject(curr.segment(0));
                IPackageFragmentRoot root = project.getPackageFragmentRoot(jar);
                if (root.exists()) {
                    IPackageFragment fragment = root.getPackageFragment(String.valueOf(packageName));
                    if (fragment.exists()) {
                        if ("class".equals(extension)) //$NON-NLS-1$
                        {
                            IClassFile classFile = fragment.getClassFile(file + ".class"); //$NON-NLS-1$
                            if (classFile.exists()) {
                                return classFile.getType();
                            }

                        } else if ("java".equals(extension)) //$NON-NLS-1$
                        {
                            ICompilationUnit unit = fragment.getCompilationUnit(file + ".java"); //$NON-NLS-1$
                            IType[] types = unit.getAllTypes();
                            for (int j = 0; j < types.length; j++) {
                                if (qualifiedName.equals(types[j].getTypeQualifiedName('.'))) {
                                    return types[j];
                                }
                            }
                        }
                    }
                    break;
                }
            }
        }
    }
    return null;
}

From source file:org.eclipse.viatra.cep.tooling.ui.wizards.NewVeplFileWizardContainerConfigurationPage.java

License:Open Source License

public String getPackageName() {
    IPackageFragmentRoot root = getPackageFragmentRoot();

    IPackageFragment fragment = root.getPackageFragment(getPackageText());

    if (!fragment.exists()) {
        try {/*from  ww  w.j av  a  2 s . c  o m*/
            root.createPackageFragment(getPackageText(), true, new NullProgressMonitor());
        } catch (JavaModelException e) {
            Activator.getDefault().logException("Cannot load packages " + e.getMessage(), e);
        }
    }

    return getPackageText();
}

From source file:org.eclipse.viatra.query.tooling.ui.wizards.NewEiqFileWizardContainerConfigurationPage.java

License:Open Source License

/**
 * Returns the name of the package set in the wizard.
 * //w  w  w. j  av a  2  s  .co  m
 * @return the name of the package
 */
public String getPackageName() {
    IPackageFragmentRoot root = getPackageFragmentRoot();

    IPackageFragment fragment = root.getPackageFragment(getPackageText());

    if (!fragment.exists()) {
        try {
            root.createPackageFragment(getPackageText(), true, new NullProgressMonitor());
        } catch (JavaModelException e) {
            ViatraQueryGUIPlugin.getDefault().logException("Cannot load packages " + e.getMessage(), e);
        }
    }

    return getPackageText();
}

From source file:org.eclipse.wb.tests.designer.core.model.util.FactoryCreateActionTest.java

License:Open Source License

public void test_validate() throws Exception {
    ContainerInfo panel = parseContainer("public class Test extends JPanel {", "  public Test() {",
            "    add(new JButton());", "  }", "}");
    ComponentInfo button = panel.getChildrenComponents().get(0);
    // prepare Java elements
    IJavaProject javaProject = m_testProject.getJavaProject();
    IProject project = m_testProject.getProject();
    IPackageFragmentRoot validRoot = javaProject.getPackageFragmentRoot(project.getFolder("src"));
    IPackageFragment validPackage = validRoot.getPackageFragment("test");
    String validClass = "MyFactory";
    String validMethod = "createComponent";
    // source folder
    {//from  w  ww .ja v  a 2  s  .co m
        // "null" as source folder
        {
            String message = callValidate(button, null, null, validClass, validMethod);
            assertTrue(message.contains("source folder"));
            assertTrue(message.contains("invalid"));
        }
        // not existing source folder
        {
            IPackageFragmentRoot invalidRoot = javaProject.getPackageFragmentRoot(project.getFolder("src2"));
            String message = callValidate(button, invalidRoot, null, validClass, validMethod);
            assertTrue(message.contains("source folder"));
            assertTrue(message.contains("invalid"));
        }
    }
    // package
    {
        // "null" as package
        {
            String message = callValidate(button, validRoot, null, validClass, validMethod);
            assertTrue(message.contains("package"));
            assertTrue(message.contains("invalid"));
        }
        // not existing package
        {
            IPackageFragment invalidPackage = validRoot.getPackageFragment("test2");
            String message = callValidate(button, validRoot, invalidPackage, validClass, validMethod);
            assertTrue(message.contains("package"));
            assertTrue(message.contains("invalid"));
        }
        // default package
        {
            IPackageFragment defaultPackage = validRoot.getPackageFragment("");
            String message = callValidate(button, validRoot, defaultPackage, validClass, validMethod);
            assertTrue(message.contains("package"));
            assertTrue(message.contains("default"));
        }
    }
    // class
    {
        // empty class name
        {
            String message = callValidate(button, validRoot, validPackage, "", validMethod);
            assertTrue(message.contains("class name"));
            assertTrue(message.contains("empty"));
        }
        // "." in class name
        {
            String message = callValidate(button, validRoot, validPackage, "bad.name", validMethod);
            assertTrue(message.contains("class name"));
            assertTrue(message.contains("dot"));
        }
        // bad in class name
        {
            String message = callValidate(button, validRoot, validPackage, "bad name", validMethod);
            assertTrue(message.contains("identifier"));
        }
    }
    // method
    {
        // empty method name
        {
            String message = callValidate(button, validRoot, validPackage, validClass, "");
            assertTrue(message.contains("method name"));
            assertTrue(message.contains("empty"));
        }
        // bad method name
        {
            String message = callValidate(button, validRoot, validPackage, validClass, "bad method name");
            assertTrue(message.contains("identifier"));
        }
    }
    // OK
    {
        String message = callValidate(button, validRoot, validPackage, validClass, validMethod);
        assertNull(message);
    }
}