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

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

Introduction

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

Prototype

void setRawClasspath(IClasspathEntry[] entries, IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Sets the classpath of this project using a list of classpath entries.

Usage

From source file:org.apache.hadoop.eclipse.MapReduceNature.java

License:Apache License

/**
 * Configures an Eclipse project as a Map/Reduce project by adding the
 * Hadoop libraries to a project's classpath.
 *///from w w w . j a  v  a  2 s.com
public void configure() throws CoreException {
    String path = project.getPersistentProperty(new QualifiedName(Activator.PLUGIN_ID, "hadoop.runtime.path"));

    File dir = new File(path);
    final ArrayList<File> coreJars = new ArrayList<File>();
    dir.listFiles(new FileFilter() {
        public boolean accept(File pathname) {
            String fileName = pathname.getName();

            // get the hadoop core jar without touching test or examples
            // older version of hadoop don't use the word "core" -- eyhung
            if ((fileName.indexOf("hadoop") != -1) && (fileName.endsWith("jar"))
                    && (fileName.indexOf("test") == -1) && (fileName.indexOf("examples") == -1)) {
                coreJars.add(pathname);
            }

            return false; // we don't care what this returns
        }
    });
    File dir2 = new File(path + File.separatorChar + "lib");
    if (dir2.exists() && dir2.isDirectory()) {
        dir2.listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                if ((!pathname.isDirectory()) && (pathname.getName().endsWith("jar"))) {
                    coreJars.add(pathname);
                }

                return false; // we don't care what this returns
            }
        });
    }

    // Add Hadoop libraries onto classpath
    IJavaProject javaProject = JavaCore.create(getProject());
    // Bundle bundle = Activator.getDefault().getBundle();
    try {
        IClasspathEntry[] currentCp = javaProject.getRawClasspath();
        IClasspathEntry[] newCp = new IClasspathEntry[currentCp.length + coreJars.size()];
        System.arraycopy(currentCp, 0, newCp, 0, currentCp.length);

        final Iterator<File> i = coreJars.iterator();
        int count = 0;
        while (i.hasNext()) {
            // for (int i = 0; i < s_coreJarNames.length; i++) {

            final File f = (File) i.next();
            // URL url = FileLocator.toFileURL(FileLocator.find(bundle, new
            // Path("lib/" + s_coreJarNames[i]), null));
            URL url = f.toURI().toURL();
            log.finer("hadoop library url.getPath() = " + url.getPath());

            newCp[newCp.length - 1 - count] = JavaCore.newLibraryEntry(new Path(url.getPath()), null, null);
            count++;
        }

        javaProject.setRawClasspath(newCp, new NullProgressMonitor());
    } catch (Exception e) {
        log.log(Level.SEVERE, "IOException generated in " + this.getClass().getCanonicalName(), e);
    }
}

From source file:org.apache.hdt.core.natures.MapReduceNature.java

License:Apache License

/**
 * Configures an Eclipse project as a Map/Reduce project by adding the
 * Hadoop libraries to a project's classpath.
 *//*from w  ww . j av a 2s  .  c  om*/
/*
 * TODO Versioning connector needed here
 */
public void configure() throws CoreException {
    String path = project.getPersistentProperty(new QualifiedName(Activator.PLUGIN_ID, "hadoop.runtime.path"));

    File dir = new File(path);
    final ArrayList<File> coreJars = new ArrayList<File>();
    dir.listFiles(new FileFilter() {
        public boolean accept(File pathname) {
            String fileName = pathname.getName();

            // get the hadoop core jar without touching test or examples
            // older version of hadoop don't use the word "core" -- eyhung
            if ((fileName.indexOf("hadoop-core") != -1) && (fileName.endsWith("jar"))
                    && (fileName.indexOf("test") == -1) && (fileName.indexOf("examples") == -1)) {
                coreJars.add(pathname);
            }

            return false; // we don't care what this returns
        }
    });

    // Add Hadoop libraries onto classpath
    IJavaProject javaProject = JavaCore.create(getProject());
    // Bundle bundle = Activator.getDefault().getBundle();
    try {
        IClasspathEntry[] currentCp = javaProject.getRawClasspath();
        IClasspathEntry[] newCp = new IClasspathEntry[currentCp.length + coreJars.size()];
        System.arraycopy(currentCp, 0, newCp, 0, currentCp.length);

        final Iterator<File> i = coreJars.iterator();
        int count = 0;
        while (i.hasNext()) {
            // for (int i = 0; i < s_coreJarNames.length; i++) {

            final File f = (File) i.next();
            // URL url = FileLocator.toFileURL(FileLocator.find(bundle, new
            // Path("lib/" + s_coreJarNames[i]), null));
            URL url = f.toURI().toURL();
            log.finer("hadoop library url.getPath() = " + url.getPath());

            newCp[newCp.length - 1 - count] = JavaCore.newLibraryEntry(new Path(url.getPath()), null, null);
            count++;
        }

        javaProject.setRawClasspath(newCp, new NullProgressMonitor());
    } catch (Exception e) {
        log.log(Level.SEVERE, "IOException generated in " + this.getClass().getCanonicalName(), e);
    }
}

From source file:org.apache.ivyde.internal.eclipse.IvyNature.java

License:Apache License

public void deconfigure() throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);
    if (!javaProject.exists()) {
        return;//from   www.jav  a  2  s.  co m
    }
    IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
    List newEntries = new ArrayList();

    for (int i = 0; i < classpathEntries.length; i++) {
        if (!IvyClasspathContainerHelper.isIvyClasspathContainer(classpathEntries[i].getPath())) {
            newEntries.add(classpathEntries[i]);
        }
    }

    if (newEntries.size() != classpathEntries.length) {
        IClasspathEntry[] newClasspathEntries = (IClasspathEntry[]) newEntries
                .toArray(new IClasspathEntry[newEntries.size()]);
        javaProject.setRawClasspath(newClasspathEntries, null);
    }

    IvyMarkerManager ivyMarkerManager = IvyPlugin.getDefault().getIvyMarkerManager();
    ivyMarkerManager.removeMarkers(project);
}

From source file:org.apache.openjpa.eclipse.EnhancerBuilderTest.java

License:Apache License

protected void setUp() throws Exception {

    IWorkspace workspace = null;/*from  w ww . j a va 2s .  c o  m*/
    try {
        workspace = ResourcesPlugin.getWorkspace();
    } catch (IllegalStateException e) {
        fail("workspace is closed, you are most probably running this as a standalone JUnit Test instead of as an Eclipse PDE Plug-In Test?!");
    }

    // create project
    project = workspace.getRoot().getProject(PROJECT_NAME);
    project.create(null);
    project.open(null);

    // create source and output folders
    IFolder srcFolder = project.getFolder("src");
    srcFolder.create(true, true, null);
    IFolder binFolder = project.getFolder("bin");
    binFolder.create(true, true, null);

    IProjectDescription desc = project.getDescription();

    // Set the Java and OpenJPA natures on the project, 
    // so that the builders is added and initialized
    desc.setNatureIds(new String[] { JavaCore.NATURE_ID, OpenJPANature.NATURE_ID });
    project.setDescription(desc, null);

    // Declare Java source and output folders
    IJavaProject javaProject = JavaCore.create(project);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);
    IClasspathEntry cpEntry = JavaCore.newSourceEntry(srcFolder.getFullPath());
    javaProject.setRawClasspath(new IClasspathEntry[] { cpEntry }, null);

    // create a Java package and a class
    IPackageFragmentRoot pkgFragmentRoot = javaProject.getPackageFragmentRoot(srcFolder);
    IPackageFragment pkgFragment = pkgFragmentRoot.createPackageFragment(TESTCLASS_PACKAGE, true, null);

    // and copy the same classes already used in the PCEnhancerHelperTest
    copyTestClassToPackage(pkgFragment, "TestEntity.java");
    // copyTestClassToPackage(pkgFragment, "NotToEnhance.java");
    javaProject.save(null, true);
}

From source file:org.apache.openjpa.eclipse.ToggleNatureAction.java

License:Apache License

/**
 * Add the captive runtime libraries of the bundle to the classpath of the given project.
 *//*www.ja v a 2s  . c  o m*/
private void addClasspath(IProject project, IClasspathEntry[] libs) throws CoreException {
    if (libs.length == 0)
        return;
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] projectClasspaths = javaProject.getRawClasspath();

    IClasspathEntry[] newClasspaths = new IClasspathEntry[projectClasspaths.length + libs.length];
    System.arraycopy(libs, 0, newClasspaths, 0, libs.length);
    System.arraycopy(projectClasspaths, 0, newClasspaths, libs.length, projectClasspaths.length);
    javaProject.setRawClasspath(newClasspaths, null);

    project.setPersistentProperty(PluginProperty.USING_CAPTIVE_LIBS, "" + true);
}

From source file:org.apache.openjpa.eclipse.ToggleNatureAction.java

License:Apache License

private void removeClasspath(IProject project) throws CoreException {
    if ("false".equalsIgnoreCase(project.getPersistentProperty(PluginProperty.USING_CAPTIVE_LIBS))) {
        return;/*from   w  w w  .j  a v  a  2 s  .  c  o m*/
    }
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] projectClasspaths = javaProject.getRawClasspath();

    PluginLibrary cpc = new PluginLibrary();
    IClasspathEntry[] cpsOpenJPA = cpc.getLibraryClasspaths(project, null, false);
    List<IClasspathEntry> cpsModified = new ArrayList<IClasspathEntry>();
    cpsModified.addAll(Arrays.asList(projectClasspaths));
    cpsModified.removeAll(Arrays.asList(cpsOpenJPA));
    javaProject.setRawClasspath(cpsModified.toArray(new IClasspathEntry[cpsModified.size()]), null);

    project.setPersistentProperty(PluginProperty.USING_CAPTIVE_LIBS, "" + false);
}

From source file:org.apache.openjpa.eclipse.util.ClasspathHelperTest.java

License:Apache License

protected void setUp() throws Exception {

    IWorkspace workspace = null;/*from   w  ww.  j  a  v  a2s  .  c o m*/
    try {
        workspace = ResourcesPlugin.getWorkspace();
    } catch (IllegalStateException e) {
        fail("workspace is closed, you are most probably running this as a standalone JUnit Test instead of as an Eclipse PDE Plug-In Test?!");
    }

    // create project
    project = workspace.getRoot().getProject(PROJECT_NAME);
    project.create(null);
    project.open(null);

    // create source and output folders
    IFolder srcFolder = project.getFolder("src");
    srcFolder.create(true, true, null);
    IFolder binFolder = project.getFolder("bin");
    binFolder.create(true, true, null);

    // Set the Java nature on the project, so that the builder is added and initialized
    IProjectDescription desc = workspace.newProjectDescription(PROJECT_NAME);
    desc.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(desc, null);

    // Declare Java source and output folders
    IJavaProject javaProject = JavaCore.create(project);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);
    IClasspathEntry cpEntry = JavaCore.newSourceEntry(srcFolder.getFullPath());
    javaProject.setRawClasspath(new IClasspathEntry[] { cpEntry }, null);

    // create a Java package and a class
    IPackageFragmentRoot pkgFragmentRoot = javaProject.getPackageFragmentRoot(srcFolder);
    IPackageFragment pkgFragment = pkgFragmentRoot.createPackageFragment(TESTCLASS_PACKAGE, true, null);
    InputStream is = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("/com/odcgroup/classpath/demo/tests/resources/testclasscontent.txt");
    String contents = IOUtils.toString(is);
    pkgFragment.createCompilationUnit(TESTCLASS_NAME + ".java", contents, true, null);
    javaProject.save(null, true);
}

From source file:org.asup.dk.source.jdt.JDTProjectUtil.java

License:Open Source License

public static IJavaProject convertToJavaPluginProject(IProject project) throws CoreException {

    IJavaProject javaProject = JavaCore.create(project);

    IProjectDescription projectDescription = project.getDescription();
    List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
    classpathEntries.addAll(Arrays.asList(javaProject.getRawClasspath()));

    ICommand[] builders = projectDescription.getBuildSpec();
    if (builders == null) {
        builders = new ICommand[0];
    }//from   w  ww  .jav a2 s. c  o m
    boolean hasManifestBuilder = false;
    boolean hasSchemaBuilder = false;
    for (int i = 0; i < builders.length; ++i) {
        if (PDE_MANIFEST_BUILDER.equals(builders[i].getBuilderName())) {
            hasManifestBuilder = true;
        }
        if (PDE_SCHEMA_BUILDER.equals(builders[i].getBuilderName())) {
            hasSchemaBuilder = true;
        }
    }
    if (!hasManifestBuilder) {
        ICommand[] oldBuilders = builders;
        builders = new ICommand[oldBuilders.length + 1];
        System.arraycopy(oldBuilders, 0, builders, 0, oldBuilders.length);
        builders[oldBuilders.length] = projectDescription.newCommand();
        builders[oldBuilders.length].setBuilderName(PDE_MANIFEST_BUILDER);
    }
    if (!hasSchemaBuilder) {
        ICommand[] oldBuilders = builders;
        builders = new ICommand[oldBuilders.length + 1];
        System.arraycopy(oldBuilders, 0, builders, 0, oldBuilders.length);
        builders[oldBuilders.length] = projectDescription.newCommand();
        builders[oldBuilders.length].setBuilderName(PDE_SCHEMA_BUILDER);
    }
    projectDescription.setBuildSpec(builders);

    project.setDescription(projectDescription, null);

    // classpath
    List<IClasspathEntry> classpath = new ArrayList<IClasspathEntry>();
    // jre
    IClasspathEntry jreClasspathEntry = JavaCore.newContainerEntry(new Path(JRE_CONTAINER_ID));
    classpath.add(jreClasspathEntry);
    // pde
    IClasspathEntry pdeClasspathEntry = JavaCore.newContainerEntry(new Path(PDE_CONTAINER_ID));
    classpath.add(pdeClasspathEntry);
    // source folder 
    IClasspathEntry classpathEntry = JavaCore.newSourceEntry(javaProject.getPath().append(JAVA_SRC_DIRECTORY));
    classpath.add(classpathEntry);

    IClasspathEntry[] classpathEntryArray = classpath.toArray(new IClasspathEntry[classpath.size()]);
    javaProject.setRawClasspath(classpathEntryArray, null);
    // compilation output
    javaProject.setOutputLocation(javaProject.getPath().append(JAVA_CLASSES_DIRECTORY), null);

    return javaProject;
}

From source file:org.autorefactor.refactoring.rules.JavaCoreHelper.java

License:Open Source License

public static IJavaProject createJavaProject(String projectName, String binFolderName) throws Exception {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(projectName);
    if (!project.exists()) {
        project.create(null);//from ww w.java  2s .c  om
    } else {
        project.refreshLocal(IResource.DEPTH_INFINITE, null);
    }

    if (!project.isOpen()) {
        project.open(null);
    }

    final IFolder binFolder = project.getFolder(binFolderName);
    createFolder(binFolder);

    addNatureToProject(project, JavaCore.NATURE_ID);

    final IJavaProject javaProject = JavaCore.create(project);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);
    javaProject.setRawClasspath(new IClasspathEntry[0], null);
    return javaProject;
}

From source file:org.autorefactor.refactoring.rules.JavaCoreHelper.java

License:Open Source License

private static void addToClasspath(IJavaProject javaProject, List<IClasspathEntry> classpathEntries)
        throws Exception {
    if (!classpathEntries.isEmpty()) {
        IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
        IClasspathEntry[] newEntries;// w  ww .  j a  va  2 s .  com
        if (oldEntries.length != 0) {
            // remove duplicate entries
            Set<IClasspathEntry> set = new HashSet<IClasspathEntry>(Arrays.asList(oldEntries));
            set.addAll(classpathEntries);
            newEntries = set.toArray(new IClasspathEntry[set.size()]);
        } else {
            newEntries = classpathEntries.toArray(new IClasspathEntry[classpathEntries.size()]);
        }
        javaProject.setRawClasspath(newEntries, null);
    }
}