Example usage for org.eclipse.jdt.core IClasspathEntry isExported

List of usage examples for org.eclipse.jdt.core IClasspathEntry isExported

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry isExported.

Prototype

boolean isExported();

Source Link

Document

Returns whether this entry is exported to dependent projects.

Usage

From source file:org.eclipse.jst.jsp.core.tests.taglibindex.TestIndex.java

License:Open Source License

public void testAvailableFromExportedOnBuildpathFromAnotherProject() throws Exception {
    TaglibIndex.shutdown();//from   w  w  w  .  j ava2s.co  m

    // Create project 1
    IProject project = BundleResourceUtil.createSimpleProject("testavailable1", null, null);
    assertTrue(project.isAccessible());
    BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/testavailable1", "/testavailable1");

    // Create project 2
    IProject project2 = BundleResourceUtil.createSimpleProject("testavailable2", null, null);
    assertTrue(project2.isAccessible());
    BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/testavailable2", "/testavailable2");
    BundleResourceUtil.copyBundleEntryIntoWorkspace("/testfiles/bug_118251-sample/sample_tld.jar",
            "/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar");

    TaglibIndex.startup();

    // make sure project 1 sees no taglibs
    ITaglibRecord[] records = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable1/WebContent"));
    assertEquals("ITaglibRecords were found", 0, records.length);
    // make sure project 2 sees two taglibs
    ITaglibRecord[] records2 = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable2/WebContent"));
    if (records2.length != 2) {
        for (int i = 0; i < records2.length; i++) {
            System.err.println(records2[i]);
        }
    }
    assertEquals("total ITaglibRecord count doesn't match", 2, records2.length);

    TaglibIndex.shutdown();
    TaglibIndex.startup();

    records2 = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable2/WebContent"));
    assertEquals("total ITaglibRecord count doesn't match after restart", 2, records2.length);

    IJavaProject created = JavaCore.create(project2);
    assertTrue("/availabletest2 not a Java project", created.exists());

    // export the jar from project 2
    IClasspathEntry[] entries = created.getRawClasspath();
    boolean found = false;
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getPath().equals(new Path("/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar"))) {
            found = true;
            assertFalse("was exported", entry.isExported());
            ((ClasspathEntry) entry).isExported = true;
        }
    }
    assertTrue("/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar was not found in build path", found);
    IClasspathEntry[] entries2 = new IClasspathEntry[entries.length];
    System.arraycopy(entries, 1, entries2, 0, entries.length - 1);
    entries2[entries.length - 1] = entries[0];
    created.setRawClasspath(entries2, new NullProgressMonitor());

    entries = created.getRawClasspath();
    found = false;
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getPath().equals(new Path("/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar"))) {
            found = true;
            assertTrue("/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar was not exported",
                    ((ClasspathEntry) entry).isExported);
        }
    }
    assertTrue(
            "/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar was not found (and exported) in build path",
            found);

    // project 2 should still have just two taglibs
    records = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable2/WebContent"));
    assertEquals("total ITaglibRecord count doesn't match (after exporting jar)", 2, records.length);

    // now one taglib should be visible from project 1
    records = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable1/WebContent"));
    assertEquals("total ITaglibRecord count doesn't match (after exporting jar), classpath provider problem?",
            1, records.length);

    TaglibIndex.shutdown();
    TaglibIndex.startup();

    // project 2 should still have just two taglibs
    records = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable2/WebContent"));
    assertEquals("total ITaglibRecord count doesn't match (after exporting jar and restarting)", 2,
            records.length);

    // and one taglib should still be visible from project 1
    records = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable1/WebContent"));
    assertEquals("total ITaglibRecord count doesn't match (after exporting jar and restarting)", 1,
            records.length);
}

From source file:org.eclipse.m2e.jdt.internal.BuildPathManager.java

License:Open Source License

void attachSourcesAndJavadoc(IPackageFragmentRoot fragment, File sources, File javadoc,
        IProgressMonitor monitor) {/*w  w  w . j  av  a 2 s  . c  o m*/
    IJavaProject javaProject = fragment.getJavaProject();

    IPath srcPath = sources != null ? Path.fromOSString(sources.getAbsolutePath()) : null;
    String javaDocUrl = getJavaDocUrl(javadoc);

    try {
        IClasspathEntry[] cp = javaProject.getRawClasspath();
        for (int i = 0; i < cp.length; i++) {
            IClasspathEntry entry = cp[i];
            if (IClasspathEntry.CPE_LIBRARY == entry.getEntryKind()
                    && entry.equals(fragment.getRawClasspathEntry())) {
                List<IClasspathAttribute> attributes = new ArrayList<IClasspathAttribute>(
                        Arrays.asList(entry.getExtraAttributes()));

                if (srcPath == null) {
                    // configure javadocs if available
                    if (javaDocUrl != null) {
                        attributes.add(JavaCore.newClasspathAttribute(
                                IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javaDocUrl));
                    }
                }

                cp[i] = JavaCore.newLibraryEntry(entry.getPath(), srcPath, null, entry.getAccessRules(), //
                        attributes.toArray(new IClasspathAttribute[attributes.size()]), // 
                        entry.isExported());

                break;
            }
        }

        javaProject.setRawClasspath(cp, monitor);
    } catch (CoreException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:org.eclipse.m2e.jdt.internal.ClasspathEntryDescriptor.java

License:Open Source License

private void setClasspathEntry(IClasspathEntry entry) {
    this.entryKind = entry.getEntryKind();
    this.path = entry.getPath();
    this.exported = entry.isExported();
    this.outputLocation = entry.getOutputLocation();

    this.accessRules = new ArrayList<IAccessRule>();
    for (IAccessRule rule : entry.getAccessRules()) {
        this.accessRules.add(rule);
    }/* w w w.  ja v a 2 s .c  om*/

    this.attributes = new LinkedHashMap<String, String>();
    for (IClasspathAttribute attribute : entry.getExtraAttributes()) {
        attributes.put(attribute.getName(), attribute.getValue());
    }

    this.sourceAttachmentPath = entry.getSourceAttachmentPath();
    this.sourceAttachmentRootPath = entry.getSourceAttachmentRootPath();
    setInclusionPatterns(entry.getInclusionPatterns());
    setExclusionPatterns(entry.getExclusionPatterns());
    this.combineAccessRules = entry.combineAccessRules();
}

From source file:org.eclipse.m2e.tests.jdt.internal.MavenClasspathContainerSaveHelperTest.java

License:Open Source License

public void testClasspathContainerSace() throws Exception {
    IClasspathEntry[] entries = new IClasspathEntry[2];

    {//ww w. ja va  2s  .c  o  m
        IAccessRule[] accessRules = new IAccessRule[1];
        accessRules[0] = JavaCore.newAccessRule(new Path("aa/**"), IAccessRule.K_ACCESSIBLE);

        IClasspathAttribute[] attributes = new IClasspathAttribute[2];
        attributes[0] = JavaCore.newClasspathAttribute("foo", "11");
        attributes[1] = JavaCore.newClasspathAttribute("moo", "22");

        entries[0] = JavaCore.newProjectEntry(new Path("/foo"), accessRules, true, attributes, false);
    }

    {
        IAccessRule[] accessRules = new IAccessRule[1];
        accessRules[0] = JavaCore.newAccessRule(new Path("bb/**"), IAccessRule.K_DISCOURAGED);

        IClasspathAttribute[] attributes = new IClasspathAttribute[1];
        attributes[0] = JavaCore.newClasspathAttribute("foo", "aa");

        entries[1] = JavaCore.newLibraryEntry(new Path("/foo/moo.jar"), new Path("/foo/moo-sources.jar"),
                new Path("/foo/moo-javadoc.jar"), accessRules, attributes, false);
    }

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    helper.writeContainer(new MavenClasspathContainer(new Path(IClasspathManager.CONTAINER_ID), entries), os);
    os.close();

    IClasspathContainer container = helper.readContainer(new ByteArrayInputStream(os.toByteArray()));

    assertEquals(IClasspathManager.CONTAINER_ID, container.getPath().toString());

    IClasspathEntry[] classpathEntries = container.getClasspathEntries();
    assertEquals(2, classpathEntries.length);

    {
        IClasspathEntry entry = classpathEntries[0];
        assertEquals(IClasspathEntry.CPE_PROJECT, entry.getEntryKind());
        assertEquals("/foo", entry.getPath().toString());
        assertEquals(false, entry.isExported());
        assertEquals(true, entry.combineAccessRules());

        IAccessRule[] accessRules = entry.getAccessRules();
        assertEquals(1, accessRules.length);
        assertEquals(IAccessRule.K_ACCESSIBLE, accessRules[0].getKind());
        assertEquals("aa/**", accessRules[0].getPattern().toString());

        IClasspathAttribute[] attributes = entry.getExtraAttributes();
        assertEquals(2, attributes.length);
        assertEquals("foo", attributes[0].getName());
        assertEquals("11", attributes[0].getValue());
        assertEquals("moo", attributes[1].getName());
        assertEquals("22", attributes[1].getValue());
    }

    {
        IClasspathEntry entry = classpathEntries[1];
        assertEquals(IClasspathEntry.CPE_LIBRARY, entry.getEntryKind());
        assertEquals("/foo/moo.jar", entry.getPath().toString());
        assertEquals(false, entry.isExported());

        IAccessRule[] accessRules = entry.getAccessRules();
        assertEquals(1, accessRules.length);
        assertEquals(IAccessRule.K_DISCOURAGED, accessRules[0].getKind());
        assertEquals("bb/**", accessRules[0].getPattern().toString());

        IClasspathAttribute[] attributes = entry.getExtraAttributes();
        assertEquals(1, attributes.length);
        assertEquals("foo", attributes[0].getName());
        assertEquals("aa", attributes[0].getValue());
    }
}

From source file:org.eclipse.objectteams.otdt.tests.AbstractJavaModelTests.java

License:Open Source License

protected void setUpProjectCompliance(IJavaProject javaProject, String compliance)
        throws JavaModelException, IOException {
    // Look for version to set and return if that's already done
    String version = compliance; // assume that the values of CompilerOptions.VERSION_* are used
    if (version.equals(javaProject.getOption(CompilerOptions.OPTION_Compliance, false))) {
        return;/*ww w .jav  a 2  s .  co m*/
    }
    String jclLibString;
    String newJclLibString;
    String newJclSrcString;
    if (compliance.charAt(2) > '4') {
        jclLibString = "JCL_LIB";
        newJclLibString = "JCL15_LIB";
        newJclSrcString = "JCL15_SRC";
    } else {
        jclLibString = "JCL15_LIB";
        newJclLibString = "JCL_LIB";
        newJclSrcString = "JCL_SRC";
    }

    // ensure variables are set
    setUpJCLClasspathVariables(compliance);

    // set options
    Map options = new HashMap();
    options.put(CompilerOptions.OPTION_Compliance, version);
    options.put(CompilerOptions.OPTION_Source, version);
    options.put(CompilerOptions.OPTION_TargetPlatform, version);
    javaProject.setOptions(options);

    // replace JCL_LIB with JCL15_LIB, and JCL_SRC with JCL15_SRC
    IClasspathEntry[] classpath = javaProject.getRawClasspath();
    IPath jclLib = new Path(jclLibString);
    for (int i = 0, length = classpath.length; i < length; i++) {
        IClasspathEntry entry = classpath[i];
        if (entry.getPath().equals(jclLib)) {
            classpath[i] = JavaCore.newVariableEntry(new Path(newJclLibString), new Path(newJclSrcString),
                    entry.getSourceAttachmentRootPath(), entry.getAccessRules(), new IClasspathAttribute[0],
                    entry.isExported());
            break;
        }
    }
    javaProject.setRawClasspath(classpath, null);
}

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

License:Open Source License

private static void updateRequiredPlugins(IJavaProject javaProject, IProgressMonitor monitor,
        IPluginModelBase model) throws CoreException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    List<IClasspathEntry> classpath = new ArrayList<IClasspathEntry>();
    List<IClasspathEntry> requiredProjects = new ArrayList<IClasspathEntry>();
    for (int i = 0; i < entries.length; i++) {
        if (isPluginProjectEntry(entries[i])) {
            requiredProjects.add(entries[i]);
        } else {//www.  j  ava  2  s. c o m
            classpath.add(entries[i]);
        }
    }
    if (requiredProjects.size() <= 0)
        return;
    IFile file = PDEProject.getManifest(javaProject.getProject());
    try {
        // TODO format manifest
        Manifest manifest = new Manifest(file.getContents());
        String value = manifest.getMainAttributes().getValue(Constants.REQUIRE_BUNDLE);
        StringBuffer sb = value != null ? new StringBuffer(value) : new StringBuffer();
        if (sb.length() > 0)
            sb.append(","); //$NON-NLS-1$
        for (int i = 0; i < requiredProjects.size(); i++) {
            IClasspathEntry entry = requiredProjects.get(i);
            if (i > 0)
                sb.append(","); //$NON-NLS-1$
            sb.append(entry.getPath().segment(0));
            if (entry.isExported())
                sb.append(";visibility:=reexport"); // TODO is there a //$NON-NLS-1$
            // constant?
        }
        manifest.getMainAttributes().putValue(Constants.REQUIRE_BUNDLE, sb.toString());
        ByteArrayOutputStream content = new ByteArrayOutputStream();
        manifest.write(content);
        file.setContents(new ByteArrayInputStream(content.toByteArray()), true, false, monitor);
        // now update .classpath
        javaProject.setRawClasspath(classpath.toArray(new IClasspathEntry[classpath.size()]), monitor);
        //         ClasspathComputer.setClasspath(javaProject.getProject(), model);
    } catch (IOException e) {
    } catch (CoreException e) {
    }
}

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

License:Open Source License

/**
 * @return updated classpath or null if there were no changes
 *//*from   w  ww.ja v a 2  s . c o m*/
private IClasspathEntry[] getUpdatedClasspath(IClasspathEntry[] cp, IJavaProject currentProject) {
    boolean exposed = false;
    int refIndex = -1;
    List<IClasspathEntry> result = new ArrayList<IClasspathEntry>();
    Set<Manifest> manifests = new HashSet<Manifest>();
    for (int i = 0; i < fData.getLibraryPaths().length; ++i) {
        try {
            manifests.add(new JarFile(fData.getLibraryPaths()[i]).getManifest());
        } catch (IOException e) {
            PDEPlugin.log(e);
        }
    }
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for (int i = 0; i < cp.length; ++i) {
        IClasspathEntry cpe = cp[i];
        switch (cpe.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            String path = null;
            IPath location = root.getFile(cpe.getPath()).getLocation();
            if (location != null) {
                path = location.toString();
            }
            //try maybe path is absolute
            if (path == null) {
                path = cpe.getPath().toString();
            }
            JarFile jarFile = null;
            try {
                jarFile = new JarFile(path);
                if (manifests.contains(jarFile.getManifest())) {
                    if (refIndex < 0) {
                        // allocate slot
                        refIndex = result.size();
                        result.add(null);
                    }
                    exposed |= cpe.isExported();
                } else {
                    result.add(cpe);
                }
            } catch (IOException e) {
                PDEPlugin.log(e);
            } finally {
                if (jarFile != null) {
                    try {
                        jarFile.close();
                    } catch (IOException e) {
                        PDEPlugin.log(e);
                    }
                }
            }
            break;
        default:
            result.add(cpe);
            break;
        }
    }
    if (refIndex >= 0) {
        result.set(refIndex, JavaCore.newProjectEntry(currentProject.getPath(), exposed));
        return result.toArray(new IClasspathEntry[result.size()]);
    }
    return null;
}

From source file:org.eclipse.xtext.common.types.access.jdt.JdtTypeProvider.java

License:Open Source License

/**
 * @see JavaProject#computePackageFragmentRoots(IClasspathEntry, ObjectVector, HashSet, IClasspathEntry, boolean, java.util.Map)
 *///from  w  ww.ja  v  a 2s . com
private void collectSourcePackageFragmentRoots(JavaProject javaProject, HashSet<String> rootIDs,
        IClasspathEntry referringEntry, ObjectVector result) throws JavaModelException {
    if (referringEntry == null) {
        rootIDs.add(javaProject.rootID());
    } else if (rootIDs.contains(javaProject.rootID())) {
        return;
    }
    IWorkspaceRoot workspaceRoot = javaProject.getProject().getWorkspace().getRoot();
    for (IClasspathEntry entry : javaProject.getResolvedClasspath()) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_PROJECT:
            if (referringEntry != null && !entry.isExported())
                return;

            IPath pathToProject = entry.getPath();
            IResource referencedProject = workspaceRoot.findMember(pathToProject);
            if (referencedProject != null && referencedProject.getType() == IResource.PROJECT) {
                IProject casted = (IProject) referencedProject;
                if (JavaProject.hasJavaNature(casted)) {
                    rootIDs.add(javaProject.rootID());
                    JavaProject referencedJavaProject = (JavaProject) JavaCore.create(casted);
                    collectSourcePackageFragmentRoots(referencedJavaProject, rootIDs, entry, result);
                }
            }
            break;
        case IClasspathEntry.CPE_SOURCE:
            javaProject.computePackageFragmentRoots(entry, result, rootIDs, referringEntry, true, null);
            break;
        }
    }
}

From source file:org.eclipse.xtext.ui.util.JdtClasspathUriResolver.java

License:Open Source License

private URI findResourceInProjectRoot(IJavaProject javaProject, String path, Set<String> visited)
        throws CoreException {
    boolean includeAll = visited.isEmpty();
    if (visited.add(javaProject.getElementName())) {
        IProject project = javaProject.getProject();
        IResource resourceFromProjectRoot = project.findMember(path);
        if (resourceFromProjectRoot != null && resourceFromProjectRoot.exists()) {
            return createPlatformResourceURI(resourceFromProjectRoot);
        }//from   ww  w  . j  av a  2 s  .co  m
        for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                if (includeAll || entry.isExported()) {
                    IResource referencedProject = project.getWorkspace().getRoot().findMember(entry.getPath());
                    if (referencedProject != null && referencedProject.getType() == IResource.PROJECT) {
                        IJavaProject referencedJavaProject = JavaCore.create((IProject) referencedProject);
                        if (referencedJavaProject.exists()) {
                            URI result = findResourceInProjectRoot(referencedJavaProject, path, visited);
                            if (result != null) {
                                return result;
                            }
                        }
                    }
                    break;
                }
            }
        }
    }
    return null;
}

From source file:org.evosuite.eclipse.popup.actions.TestGenerationJob.java

License:Open Source License

private String buildProjectCP() throws JavaModelException {
    IJavaProject jProject = JavaCore.create(target.getProject());
    IClasspathEntry[] oldEntries = jProject.getRawClasspath();
    String classPath = "";
    boolean first = true;

    for (int i = 0; i < oldEntries.length; i++) {
        IClasspathEntry curr = oldEntries[i];
        System.out.println("Current entry: " + curr.getPath());

        if (curr.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            IPath path = curr.getPath();
            if (path.toFile().getName().startsWith("evosuite")) {
                System.out.println("Skipping evosuite.jar");
                continue;
            }/* w w w.j  a va 2s  .co  m*/
            if (!first)
                classPath += File.pathSeparator;
            else
                first = false;

            if (path.toFile().exists()) {
                classPath += path.toOSString();
                System.out.println("Adding CPE_LIBRARY to classpath: " + path.toOSString());
            } else {
                classPath += target.getWorkspace().getRoot().getLocation().toOSString() + path.toOSString();
                System.out.println("Adding CPE_LIBRARY to classpath: "
                        + target.getWorkspace().getRoot().getLocation().toOSString() + path.toOSString());
            }
        } else if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            if (curr.isExported()) {
                if (curr.toString().equals("org.eclipse.jdt.launching.JRE_CONTAINER")) {
                    System.out.println("Found JRE container");
                } else if (curr.toString().startsWith("org.eclipse.jdt.junit.JUNIT_CONTAINER")) {
                    System.out.println("Found JUnit container");
                } else {
                    System.out.println("Found unknown container: " + curr);
                }
            } else {
                System.out.println("Container not exported: " + curr);
            }
        } else if (curr.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            // Add binary dirs of this project to classpath
            System.out.println("Don't handle CPE_PROJECT yet");
        } else if (curr.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            System.out.println("Path: " + curr.getPath());
            System.out.println("Resolved Path: " + JavaCore.getResolvedVariablePath(curr.getPath()));
            if (!first)
                classPath += File.pathSeparator;
            else
                first = false;

            classPath += JavaCore.getResolvedVariablePath(curr.getPath());
        } else if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            System.out.println("Don't handle CPE_SOURCE yet");
        } else {
            System.out.println("CP type: " + curr.getEntryKind());
        }
    }
    ResourceList.resetAllCaches();
    if (!first)
        classPath += File.pathSeparator;

    classPath += target.getWorkspace().getRoot().findMember(jProject.getOutputLocation()).getLocation()
            .toOSString();
    return classPath;
}