Example usage for org.eclipse.jdt.core IJavaProject getRawClasspath

List of usage examples for org.eclipse.jdt.core IJavaProject getRawClasspath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getRawClasspath.

Prototype

IClasspathEntry[] getRawClasspath() throws JavaModelException;

Source Link

Document

Returns the raw classpath for the project, as a list of classpath entries.

Usage

From source file:net.sf.jasperreports.samples.wizards.SampleNewWizard.java

License:Open Source License

private static void addSourceFolders(Set<String> cpaths, IJavaProject project, IProgressMonitor monitor) {
    if (cpaths.isEmpty())
        return;//  www . ja va  2 s . c  om
    try {
        IClasspathEntry[] cpentries = project.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[cpentries.length + cpaths.size()];
        System.arraycopy(cpentries, 0, newEntries, 0, cpentries.length);
        int i = cpentries.length;
        for (String cp : cpaths) {
            newEntries[i] = JavaCore.newSourceEntry(project.getProject().getFile(cp).getFullPath());
            i++;
            if (monitor.isCanceled())
                return;
        }
        project.setRawClasspath(newEntries, monitor);
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

}

From source file:net.sourceforge.floggy.eclipse.ToggleNatureAction.java

License:Open Source License

private void updateClasspath(IProject project) throws Exception {
    IJavaProject javaProject = JavaCore.create(project);
    List rawClasspath = new LinkedList(Arrays.asList(javaProject.getRawClasspath()));

    boolean contains = false;

    for (int i = 0; i < rawClasspath.size(); i++) {
        IClasspathEntry classpath = (IClasspathEntry) rawClasspath.get(i);

        if (classpath.getPath().toOSString().indexOf("floggy-persistence-framework") != -1) {
            contains = true;//from www.  ja  va  2s .co  m

            break;
        }
    }

    if (!contains) {
        Enumeration e = Activator.findEntries("/", "*.jar", true);

        while (e.hasMoreElements()) {
            URL url = (URL) e.nextElement();
            String path = FileLocator.toFileURL(url).getPath();

            if (path.indexOf("floggy-persistence-framework") != -1) {
                String javadocURL = "http://floggy.sourceforge.net/modules/floggy-persistence-framework/apidocs/";

                String version = getVersion(path);

                if (version != null) {
                    javadocURL = "http://floggy.sourceforge.net/modules/floggy-persistence-framework/" + version
                            + "/floggy-persistence-framework/apidocs/";
                }

                IClasspathAttribute attribute = JavaCore.newClasspathAttribute("javadoc_location", javadocURL);
                IClasspathEntry varEntry = JavaCore.newLibraryEntry(new Path(path), null, null, null,
                        new IClasspathAttribute[] { attribute }, true);
                rawClasspath.add(varEntry);
                javaProject.setRawClasspath(
                        (IClasspathEntry[]) rawClasspath.toArray(new IClasspathEntry[rawClasspath.size()]),
                        null);

                break;
            }
        }

        if (Activator.isMTJAvailble()) {
            IFile implJar = project.getFile("floggy-persistence-framework-impl.jar");

            if (!implJar.exists()) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                JarOutputStream out = new JarOutputStream(baos, new Manifest());

                out.close();

                implJar.create(new ByteArrayInputStream(baos.toByteArray()), IResource.DERIVED, null);
            }

            IPath filePatternImpl = new Path("net/sourceforge/floggy/persistence/impl/*");
            IPath filePatternImplMigration = new Path("net/sourceforge/floggy/persistence/impl/migration/*");

            IAccessRule[] accessRules = new IAccessRule[2];

            accessRules[0] = JavaCore.newAccessRule(filePatternImpl, IAccessRule.K_NON_ACCESSIBLE);
            accessRules[1] = JavaCore.newAccessRule(filePatternImplMigration, IAccessRule.K_NON_ACCESSIBLE);

            IClasspathEntry entry = JavaCore.newLibraryEntry(implJar.getLocation(), null, null, accessRules,
                    null, true);

            rawClasspath.add(entry);
            javaProject.setRawClasspath(
                    (IClasspathEntry[]) rawClasspath.toArray(new IClasspathEntry[rawClasspath.size()]), null);
        }
    }
}

From source file:net.ssehub.easy.producer.ui.project_management.EASyJavaConfigurator.java

License:Apache License

@Override
public void configure(IProject project, IProject parentProject) {
    JavaCapabilityConfigurationPage jcpage = new JavaCapabilityConfigurationPage();
    IJavaProject javaProject = JavaCore.create(project);
    IJavaProject javaParentProject = JavaCore.create(parentProject);

    jcpage.init(javaProject, null, null, false);
    try {//from  ww w  .  ja  v  a2  s  . co m
        jcpage.configureJavaProject(null);
    } catch (CoreException e1) {
        EASyLoggerFactory.INSTANCE.getLogger(EASyJavaConfigurator.class, Activator.PLUGIN_ID).exception(e1);
    } catch (InterruptedException e1) {
        EASyLoggerFactory.INSTANCE.getLogger(EASyJavaConfigurator.class, Activator.PLUGIN_ID).exception(e1);
    }

    // Try to copy settings from parent
    try {
        IClasspathEntry[] parrentEntries = javaParentProject.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[parrentEntries.length];

        // Copy Classpath Entries
        for (int i = 0; i < newEntries.length; i++) {
            IClasspathEntry parentEntry = parrentEntries[i];
            newEntries[i] = javaProject
                    .decodeClasspathEntry(javaParentProject.encodeClasspathEntry(parentEntry));

            try {
                IPath entryPath = newEntries[i].getPath().makeAbsolute();
                if (project.getFullPath().matchingFirstSegments(entryPath) > 0) {
                    File entryFile = entryPath.toFile();
                    IFolder folder = project.getFolder(entryFile.getName());
                    if (!folder.exists()) {
                        folder.create(false, true, null);
                    }
                }
            } catch (CoreException e) {
                EASyLoggerFactory.INSTANCE.getLogger(EASyJavaConfigurator.class, Activator.PLUGIN_ID)
                        .exception(e);
            }
        }
        javaProject.setRawClasspath(newEntries, null);
    } catch (CoreException e) {
        EASyLoggerFactory.INSTANCE.getLogger(EASyJavaConfigurator.class, Activator.PLUGIN_ID).exception(e);
    }

}

From source file:nl.mwensveen.m2e.extras.cxf.tests.CXFGenerationTest.java

License:Open Source License

public void test_p001_simple() throws Exception {
    ResolverConfiguration configuration = new ResolverConfiguration();
    IProject project1 = importProject("projects/cxf/pom.xml", configuration);
    waitForJobsToComplete();//from  w  ww  . j a v a2 s  .c  om

    project1.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
    project1.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
    waitForJobsToComplete();

    assertNoErrors(project1);

    IJavaProject javaProject1 = JavaCore.create(project1);
    IClasspathEntry[] cp1 = javaProject1.getRawClasspath();
    boolean found = false;

    for (IClasspathEntry iClasspathEntry : cp1) {
        if (iClasspathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            if (iClasspathEntry.getPath().equals(new Path("/cxf-test-project/target/generated-sources/cxf"))) {
                found = true;
            }
        }
    }
    assertTrue(found);

    assertTrue(project1.getFile("target/generated-sources/cxf/com/example/xsd/ComplexType.java")
            .isSynchronized(IResource.DEPTH_ZERO));
    assertTrue(
            project1.getFile("target/generated-sources/cxf/com/example/xsd/ComplexType.java").isAccessible());
}

From source file:nl.mwensveen.m2e.extras.xml.tests.XMLTransformation2Test.java

License:Open Source License

public void test_p001_simple() throws Exception {
    ResolverConfiguration configuration = new ResolverConfiguration();
    IProject project1 = importProject("projects/xml2/pom.xml", configuration);
    waitForJobsToComplete();//from w  w w .j  a v  a  2  s.c o m

    project1.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
    project1.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
    waitForJobsToComplete();

    assertNoErrors(project1);

    IJavaProject javaProject1 = JavaCore.create(project1);
    IClasspathEntry[] cp1 = javaProject1.getRawClasspath();
    boolean found = false;
    for (IClasspathEntry iClasspathEntry : cp1) {
        System.out.println("classpath entry: " + iClasspathEntry.getPath().toString());
        if ("/xml-test-project/target/temp".equals(iClasspathEntry.getPath().toString())) {
            found = true;
        }
    }
    assertTrue(!found);

    assertTrue(project1.getFile("target/temp/example.xsd").isSynchronized(IResource.DEPTH_ZERO));
    assertTrue(project1.getFile("target/temp/example.xsd").isAccessible());
}

From source file:nl.mwensveen.m2e.extras.xml.tests.XMLTransformationTest.java

License:Open Source License

public void test_p001_simple() throws Exception {
    ResolverConfiguration configuration = new ResolverConfiguration();
    IProject project1 = importProject("projects/xml/pom.xml", configuration);
    waitForJobsToComplete();//from  w w w.j  a v a2s .com

    project1.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
    project1.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
    waitForJobsToComplete();

    assertNoErrors(project1);

    IJavaProject javaProject1 = JavaCore.create(project1);
    IClasspathEntry[] cp1 = javaProject1.getRawClasspath();
    boolean found = false;
    for (IClasspathEntry iClasspathEntry : cp1) {
        System.out.println("classpath entry: " + iClasspathEntry.getPath().toString());
        if ("/xml-test-project/target/temp".equals(iClasspathEntry.getPath().toString())) {
            found = true;
        }
    }
    assertTrue(found);

    assertTrue(project1.getFile("target/temp/example.xsd").isSynchronized(IResource.DEPTH_ZERO));
    assertTrue(project1.getFile("target/temp/example.xsd").isAccessible());
}

From source file:nz.ac.auckland.ptjava.builder.PTJavaClasspath.java

License:Open Source License

/**
 * Add the PTRuntime.jar to the classpath of javaProject
 * /*from w w  w  .  jav  a2 s  . c  o  m*/
 * @param javaProject
 * @param monitor
 */
public static void addPTRuntimeToClasspath(IJavaProject javaProject, IProgressMonitor monitor) {
    try {
        IClasspathEntry entries[] = javaProject.getRawClasspath();
        List<IClasspathEntry> list = new ArrayList<IClasspathEntry>(entries.length + 1);
        for (int i = 0; i < entries.length; i++) {
            list.add(entries[i]);
        }

        // add the java runtime library
        IPath path = null;
        IPreferenceStore prefs = PTJavaPlugin.getDefault().getPreferenceStore();
        if (prefs.getBoolean(PreferenceConstants.PTJAVA_USE_CUSTOM_RUNTIME)) {
            path = new Path(prefs.getString(PreferenceConstants.PTJAVA_RUNTIME_PATH));
        } else {
            path = PTJavaPreferencePage.getDefaultRuntimeJarPath();
        }
        list.add(JavaCore.newLibraryEntry(path, null, null));

        IClasspathEntry updatedEntries[] = new IClasspathEntry[list.size()];
        list.toArray(updatedEntries);

        javaProject.setRawClasspath(updatedEntries, monitor);
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

From source file:nz.ac.auckland.ptjava.newclasswizard.WizardNewPTJavaFileCreationPage.java

License:Open Source License

/**
 * Creates the initial contents to be placed inside the newly created PTJava class file.
 * Current version will generate package declaration and basic class declaration if
 * those options are checked on the wizard page.
 *//*from   www .j  av  a  2s.com*/
@Override
protected InputStream getInitialContents() {
    //  String buffer for appending to file
    StringBuffer sb = new StringBuffer();
    if (fCodeCreationGroup.getGeneratePackage()) {
        IPath containerFullPath = getContainerFullPath();

        //  the original string to the container path
        String pathString = containerFullPath.toString();
        //System.out.println(pathString);

        int slashIndex = pathString.indexOf('/', 1);
        String truncatedPath = null;

        if (slashIndex > 0)
            truncatedPath = pathString.substring(0, slashIndex);
        else
            truncatedPath = pathString;

        //  find the project src folder path
        IProject project = JavaPlugin.getWorkspace().getRoot().getProject(truncatedPath);
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject != null) {
            try {
                String srcPath = null;
                IClasspathEntry[] cp = javaProject.getRawClasspath();
                for (IClasspathEntry i : cp) {
                    //System.out.println("i:"+i);
                    if (i.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                        //  src entry found!
                        srcPath = i.getPath().toString();
                        //System.out.println("srcPath: " + srcPath);
                        break;
                    }
                }
                if (srcPath != null) {
                    if (pathString.equals(srcPath)) {
                        //  do nothing
                    } else {
                        String s = null;
                        //  omit src path if part of path string
                        if (pathString.startsWith(srcPath))
                            s = pathString.substring(srcPath.length() + 1);
                        //  the +1 is to remove the first "/" after the src path
                        else
                            s = pathString;

                        //  currently looks like "some/path/to/file", we want "some.path.to.file"
                        s = s.replace('/', '.');

                        //  append it
                        //  should be something like "package some.path;"
                        sb.append("package ");
                        sb.append(s);
                        sb.append(";");
                        sb.append("\n\n");
                        //System.out.println("s: " + s);
                    }
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        }
    }

    if (fCodeCreationGroup.getGenerateClass()) {
        sb.append("public class ");
        String filename = getFileName();
        if (filename.contains("."))
            filename = filename.substring(0, filename.indexOf('.'));
        sb.append(filename);
        sb.append(" {\n\t\n}");
    }
    if (sb.length() == 0)
        return null;
    return new ByteArrayInputStream(sb.toString().getBytes());
}

From source file:nz.ac.auckland.ptjava.preferences.PTJavaPreferencePage.java

License:Open Source License

@Override
public boolean performOk() {
    boolean ret = super.performOk();
    Pattern pattern = Pattern.compile(".*/" + fgRuntimeExtension.replace(".", "\\."));

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject[] projects = root.getProjects();
    for (IProject project : projects) {
        try {/*from  ww  w  . j av a 2  s .  c  o m*/
            if (project.hasNature(JavaCore.NATURE_ID) && project.hasNature(PTJavaFileBuilderNature.NATURE_ID)) {
                IJavaProject jProject = JavaCore.create(project);
                IClasspathEntry[] entries = jProject.getRawClasspath();
                List<IClasspathEntry> newEntryList = new ArrayList<IClasspathEntry>();

                //  remove PTRuntime.jar from Java project class path
                for (int i = 0; i < entries.length; ++i) {
                    IPath path = entries[i].getPath();
                    Matcher matcher = pattern.matcher(path.toString());
                    if (!matcher.matches()) {
                        newEntryList.add(entries[i]);
                    }
                }

                //  add the new runtime to class path
                if (getPreferenceStore().getBoolean(PreferenceConstants.PTJAVA_USE_CUSTOM_RUNTIME)) {
                    String sPath = fPTJavaRuntimePathEditor.getStringValue();
                    if (sPath.endsWith(fgRuntimeExtension) && Path.EMPTY.isValidPath(sPath)) {
                        IPath runtimePath = new Path(sPath);
                        newEntryList.add(JavaCore.newLibraryEntry(runtimePath, null, null));
                    }
                } else {
                    IPath runtimePath = getDefaultRuntimeJarPath();
                    newEntryList.add(JavaCore.newLibraryEntry(runtimePath, null, null));
                }
                IClasspathEntry[] updatedEntries = new IClasspathEntry[newEntryList.size()];
                newEntryList.toArray(updatedEntries);

                jProject.setRawClasspath(updatedEntries, null);

            }
        } catch (CoreException e) {
            e.printStackTrace();
        }
    }
    return ret;
}

From source file:org.antlr.eclipse.core.AntlrNature.java

License:Open Source License

/**
 * Add the ANTLR nature to a project// ww w. ja  v  a2  s .com
 * @param aProject The project to add it to
 * @param aMonitor A progess monitor
 */
public static void addNature(final IProject aProject, final IProgressMonitor aMonitor) {
    if (aProject != null) {
        if (DEBUG) {
            System.out.println("adding ANTLR nature to project '" + aProject.getName() + "'");
        }
        try {
            if (!aProject.hasNature(ID)) {
                IProjectDescription desc = aProject.getDescription();
                ArrayList<String> natures = new ArrayList<String>(Arrays.asList(desc.getNatureIds()));
                natures.add(0, ID);
                desc.setNatureIds(natures.toArray(new String[natures.size()]));
                aProject.setDescription(desc, aMonitor);

                // if it's a Java project, set up classpath variable for:
                //    ANTLR_HOME
                // these will point to the ANTLR plugins' 
                //   jars, respectively
                if (aProject.hasNature(JavaCore.NATURE_ID)) {
                    IJavaProject javaProject = JavaCore.create(aProject);
                    ArrayList<IClasspathEntry> rawClasspath = new ArrayList<IClasspathEntry>(
                            Arrays.asList(javaProject.getRawClasspath()));
                    boolean hasAntlrJar = false;
                    for (Iterator<IClasspathEntry> i = rawClasspath.iterator(); i.hasNext();) {
                        IClasspathEntry entry = i.next();
                        if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                            String segment0 = entry.getPath().segment(0);
                            if (AntlrCorePlugin.ANTLR_HOME.equals(segment0)) {
                                hasAntlrJar = true;
                                break;
                            }
                        }
                    }
                    if (!hasAntlrJar)
                        rawClasspath.add(
                                JavaCore.newVariableEntry(new Path(AntlrCorePlugin.ANTLR_HOME + "/antlr.jar"),
                                        new Path(AntlrCorePlugin.ANTLR_HOME + "/antlrsrc.zip"), null));

                    if (!hasAntlrJar)
                        javaProject.setRawClasspath(
                                rawClasspath.toArray(new IClasspathEntry[rawClasspath.size()]), null);
                }
            }
        } catch (CoreException e) {
            AntlrCorePlugin.log(e);
        }
    }
}