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.eclipse.ajdt.core.tests.AJDTCoreTestCase.java

License:Open Source License

private IPackageFragmentRoot createDefaultSourceFolder(IJavaProject javaProject) throws CoreException {
    IProject project = javaProject.getProject();
    IFolder folder = project.getFolder("src");
    if (!folder.exists())
        ensureExists(folder);/*from  w  ww. j  a  v a 2 s  .  co m*/

    // if already exists, do nothing
    final IClasspathEntry[] entries = javaProject.getResolvedClasspath(false);
    final IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(folder);
    for (int i = 0; i < entries.length; i++) {
        final IClasspathEntry entry = entries[i];
        if (entry.getPath().equals(folder.getFullPath())) {
            return root;
        }
    }

    // else, remove old source folders and add this new one
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    List<IClasspathEntry> oldEntriesList = new ArrayList<IClasspathEntry>();
    oldEntriesList.add(JavaCore.newSourceEntry(root.getPath()));
    for (IClasspathEntry entry : oldEntries) {
        if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            oldEntriesList.add(entry);
        }
    }

    IClasspathEntry[] newEntries = oldEntriesList.toArray(new IClasspathEntry[0]);
    javaProject.setRawClasspath(newEntries, null);
    return root;
}

From source file:org.eclipse.ajdt.core.tests.builder.AJBuilderTest2.java

License:Open Source License

/**
 * Part of Bug 91420 - if a library has been added to the classpath then we
 * need to do a rebuild./*w  w  w.  j a v  a 2s. c om*/
 */
public void testChangeInRequiredLibsCausesReBuild() throws Exception {
    IProject project = createPredefinedProject("bug91420"); //$NON-NLS-1$
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] origClasspath = javaProject.getRawClasspath();

    TestLogger testLog = new TestLogger();
    AspectJPlugin.getDefault().setAJLogger(testLog);
    // add a library to the classpath
    addLibraryToClasspath(project, "testJar.jar"); //$NON-NLS-1$
    joinBackgroudActivities();

    // check it's been added to the classpath
    assertTrue("library should have been added to classpath", //$NON-NLS-1$
            projectHasLibraryOnClasspath(javaProject, "testJar.jar")); //$NON-NLS-1$

    // check that a build has in fact occured
    List log = testLog.getMostRecentEntries(3);
    // print log to the screen for test case development purposes
    testLog.printLog();

    assertTrue("classpath has changed (new required library) so should have spent time in AJDE", //$NON-NLS-1$
            testLog.containsMessage("Total time spent in AJDE")); //$NON-NLS-1$
    assertTrue("classpath has changed (new required library) so should have spent time in AJBuilder.build()", //$NON-NLS-1$
            testLog.containsMessage("Total time spent in AJBuilder.build()")); //$NON-NLS-1$

    // reset the changes
    javaProject.setRawClasspath(origClasspath, null);
    joinBackgroudActivities();

    assertFalse("library should no longer be on the classpath", //$NON-NLS-1$
            projectHasLibraryOnClasspath(javaProject, "testJar.jar")); //$NON-NLS-1$
}

From source file:org.eclipse.ajdt.core.tests.builder.AJBuilderTest2.java

License:Open Source License

/**
 * Part of Bug 91420 - if there has been a change in required projects then
 * need to to a rebuild// w  w  w  . j  ava 2s  . c o  m
 */
public void testChangeInRequiredProjectsCausesReBuild() throws Exception {
    IProject project = createPredefinedProject("bug91420"); //$NON-NLS-1$
    IProject project2 = createPredefinedProject("bug101481"); //$NON-NLS-1$
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] origClasspath = javaProject.getRawClasspath();

    TestLogger testLog = new TestLogger();
    AspectJPlugin.getDefault().setAJLogger(testLog);
    addProjectDependency(project, project2);
    joinBackgroudActivities();

    // check the dependency is there
    assertTrue("project bug91420 should have a project dependency on project bug101481", //$NON-NLS-1$
            projectHasProjectDependency(javaProject, project2));

    List log = testLog.getMostRecentEntries(3);
    // print log to the screen for test case development purposes
    testLog.printLog();

    assertTrue("classpath has changed (new project dependency) so should have spent time in AJDE", //$NON-NLS-1$
            testLog.containsMessage("Total time spent in AJDE")); //$NON-NLS-1$  
    assertTrue("classpath has changed (new project dependency) so should have spent time in AJBuilder.build()", //$NON-NLS-1$
            testLog.containsMessage("Total time spent in AJBuilder.build()")); //$NON-NLS-1$ 

    // reset the changes
    javaProject.setRawClasspath(origClasspath, null);
    joinBackgroudActivities();
    assertFalse("project dependencies should have been removed", //$NON-NLS-1$
            projectHasProjectDependency(javaProject, project2));
}

From source file:org.eclipse.ajdt.core.tests.builder.AJBuilderTest2.java

License:Open Source License

private void addLibraryToClasspath(IProject project, String libName) throws JavaModelException {
    IJavaProject javaProject = JavaCore.create(project);

    IClasspathEntry[] originalCP = javaProject.getRawClasspath();
    IClasspathEntry ajrtLIB = JavaCore.newLibraryEntry(project.getLocation().append(libName).makeAbsolute(), // library
            // location
            null, // no source
            null // no source
    );//from w ww. j  a  va 2 s . c o m
    int originalCPLength = originalCP.length;
    IClasspathEntry[] newCP = new IClasspathEntry[originalCPLength + 1];
    System.arraycopy(originalCP, 0, newCP, 0, originalCPLength);
    newCP[originalCPLength] = ajrtLIB;
    javaProject.setRawClasspath(newCP, null);
}

From source file:org.eclipse.ajdt.core.tests.builder.AJBuilderTest2.java

License:Open Source License

private void addProjectDependency(IProject project, IProject projectDependedOn) throws JavaModelException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] originalCP = javaProject.getRawClasspath();
    IClasspathEntry newEntry = JavaCore.newProjectEntry(projectDependedOn.getFullPath());
    int originalCPLength = originalCP.length;
    IClasspathEntry[] newCP = new IClasspathEntry[originalCPLength + 1];
    System.arraycopy(originalCP, 0, newCP, 0, originalCPLength);
    newCP[originalCPLength] = newEntry;//from w  ww  .java2s .  c  o  m
    javaProject.setRawClasspath(newCP, null);
}

From source file:org.eclipse.ajdt.core.tests.builder.Bug99133Test.java

License:Open Source License

/**
 * A depends on B and in particular calls a method in B.
 * The signature of an unreferenced method in B changes. Then an
 * incremental build of project B and a full build of project A
 * should occur. (both A and B are AJ projects)
 *//*from  w  ww  .j  a  v a2s.c om*/
//   public void testBug99133d() throws Exception {
//
//      //change the return type of the method m1() in bug99133b\src\p\C1.java
//      IFile c1 = getFile(pB,"p","C1.java"); //$NON-NLS-1$ //$NON-NLS-2$
//      BufferedReader br1 = new BufferedReader(new InputStreamReader(c1.getContents()));
//
//      StringBuffer sb1 = new StringBuffer();
//      int lineNumber1 = 1;
//      String line1 = br1.readLine();
//      while (line1 != null) {
//         if (lineNumber1 == 8) {
//            sb1.append("public String m2() {"); //$NON-NLS-1$
//         } else if (lineNumber1 == 9) {
//            sb1.append("return \"Hello\";"); //$NON-NLS-1$
//         } else {
//            sb1.append(line1);
//         }
//         sb1.append(System.getProperty("line.separator")); //$NON-NLS-1$
//         lineNumber1++;
//         line1 = br1.readLine();
//      }
//      br1.close();
//      StringReader reader1 = new StringReader(sb1.toString());
//      c1.setContents(new ReaderInputStream(reader1), true, true, null);
//      waitForAutoBuild();
//      waitForAutoBuild();
//
//      assertEquals("two more builds should have occured", //$NON-NLS-1$
//            numberOfBuilds + 2,
//            testLog.getNumberOfBuildsRun());
//      
//      List buildLogB = testLog.getPreviousBuildEntry(2);
//      boolean incB = listContainsString(buildLogB,
//            "AspectJ reports build successful, build was: INCREMENTAL"); //$NON-NLS-1$
//      boolean fullB = listContainsString(buildLogB,
//            "AspectJ reports build successful, build was: FULL"); //$NON-NLS-1$
//      // if not an incremental build of project bug99133b build then fail
//      // printing out whether did a full build instead
//      if (!incB) {
//         fail("Changing the signature of a method in project bug99133b "  //$NON-NLS-1$
//            + "should cause an incremental build of project bug99133b " //$NON-NLS-1$
//            + ": (did a full build instead:" + fullB+")"); //$NON-NLS-1$ //$NON-NLS-2$
//      }
//      
//      List buildLogA = testLog.getPreviousBuildEntry(1);
//      boolean incA = listContainsString(buildLogA,
//            "AspectJ reports build successful, build was: INCREMENTAL"); //$NON-NLS-1$
//      boolean fullA = listContainsString(buildLogA,
//            "AspectJ reports build successful, build was: FULL"); //$NON-NLS-1$
//      // if not a full build of project bug99133a build then fail
//      // printing out whether did an incremental build instead
//      if (!fullA) {
//         fail("Changing the signature of an unreferenced method in"  //$NON-NLS-1$
//               + " project bug99133b should cause a full build of " //$NON-NLS-1$
//               + " dependent project bug99133a " //$NON-NLS-1$
//               + ": (did an incremental build instead:" + incA + ")"); //$NON-NLS-1$ //$NON-NLS-2$
//      }
//   }
//
//   /**
//    * A depends on B and in particular calls a method in B.
//    * A new method is added to a class in B. 
//    * Then an incremental build of project B and a full build
//    * of project A should occurr. (both A and B are AJ projects)
//    */
//   public void testBug99133e() throws Exception {
//
//      //change the return type of the method m1() in bug99133b\src\p\C1.java
//      IFile c1 = getFile(pB,"p","C1.java"); //$NON-NLS-1$ //$NON-NLS-2$
//      BufferedReader br1 = new BufferedReader(new InputStreamReader(c1.getContents()));
//
//      StringBuffer sb1 = new StringBuffer();
//      int lineNumber1 = 1;
//      String line1 = br1.readLine();
//      while (line1 != null) {
//         if (lineNumber1 == 10) {
//            sb1.append("}"); //$NON-NLS-1$
//            sb1.append(System.getProperty("line.separator")); //$NON-NLS-1$
//            sb1.append("public void m3() {}"); //$NON-NLS-1$
//         } else {
//            sb1.append(line1);
//         }
//         sb1.append(System.getProperty("line.separator")); //$NON-NLS-1$
//         lineNumber1++;
//         line1 = br1.readLine();
//      }
//      br1.close();
//      StringReader reader1 = new StringReader(sb1.toString());
//      c1.setContents(new ReaderInputStream(reader1), true, true, null);
//      waitForAutoBuild();
//      waitForAutoBuild();
//
//      assertEquals("two more builds should have occured", //$NON-NLS-1$
//            numberOfBuilds + 2,
//            testLog.getNumberOfBuildsRun());
//      
//      List buildLogB = testLog.getPreviousBuildEntry(2);
//      boolean incB = listContainsString(buildLogB,
//            "AspectJ reports build successful, build was: INCREMENTAL"); //$NON-NLS-1$
//      boolean fullB = listContainsString(buildLogB,
//            "AspectJ reports build successful, build was: FULL"); //$NON-NLS-1$
//      // if not an incremental build of project bug99133b build then fail
//      // printing out whether did a full build instead
//      if (!incB) {
//         fail("Adding a method in project bug99133b "  //$NON-NLS-1$
//            + "should cause an incremental build of project bug99133b " //$NON-NLS-1$
//            + ": (did a full build instead:" + fullB+")"); //$NON-NLS-1$ //$NON-NLS-2$
//      }
//      
//      List buildLogA = testLog.getPreviousBuildEntry(1);
//      boolean incA = listContainsString(buildLogA,
//            "AspectJ reports build successful, build was: INCREMENTAL"); //$NON-NLS-1$
//      boolean fullA = listContainsString(buildLogA,
//            "AspectJ reports build successful, build was: FULL"); //$NON-NLS-1$
//      // if not a full build of project bug99133a build then fail
//      // printing out whether did an incremental build instead
//      if (!fullA) {
//         fail("Adding a method in project bug99133b " //$NON-NLS-1$
//               + "should cause an full build of dependent project bug99133b " //$NON-NLS-1$
//               + ": (did an incremental build instead:" + incA + ")"); //$NON-NLS-1$ //$NON-NLS-2$
//      }
//
//   }

private void addProjectDependency(IProject project, IProject projectDependedOn) throws JavaModelException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] originalCP = javaProject.getRawClasspath();
    IClasspathEntry newEntry = JavaCore.newProjectEntry(projectDependedOn.getFullPath());
    int originalCPLength = originalCP.length;
    IClasspathEntry[] newCP = new IClasspathEntry[originalCPLength + 1];
    System.arraycopy(originalCP, 0, newCP, 0, originalCPLength);
    newCP[originalCPLength] = newEntry;
    javaProject.setRawClasspath(newCP, null);
    waitForAutoBuild();
}

From source file:org.eclipse.ajdt.core.tests.builder.Bug99133Test.java

License:Open Source License

private void removeProjectDependency(IProject project, IProject projectDependedOn) throws JavaModelException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] cpEntry = javaProject.getRawClasspath();
    List newEntries = new ArrayList();

    for (int j = 0; j < cpEntry.length; j++) {
        IClasspathEntry entry = cpEntry[j];
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            if (!entry.getPath().equals(projectDependedOn.getFullPath())
                    && !entry.getPath().equals(projectDependedOn.getFullPath().makeAbsolute())) {
                newEntries.add(entry);//w  ww. j  a v a2 s  . c  o m
            }
        } else {
            newEntries.add(entry);
        }
    }
    IClasspathEntry[] newCP = (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);
    javaProject.setRawClasspath(newCP, null);
}

From source file:org.eclipse.ajdt.internal.builder.UIBuildListener.java

License:Open Source License

private void removeOutjarFromDependingProjects(IProject project, IClasspathEntry unwantedEntry) {
    IProject[] dependingProjects = getDependingProjects(project);

    for (int i = 0; i < dependingProjects.length; i++) {
        IJavaProject javaProject = JavaCore.create(dependingProjects[i]);
        if (javaProject == null)
            continue;
        try {/*from w  w w  . ja v a  2 s.c  o  m*/
            IClasspathEntry[] cpEntry = javaProject.getRawClasspath();
            List newEntries = new ArrayList();
            for (int j = 0; j < cpEntry.length; j++) {
                if (!cpEntry[j].equals(unwantedEntry)) {
                    newEntries.add(cpEntry[j]);
                }
            }
            IClasspathEntry[] newCP = (IClasspathEntry[]) newEntries
                    .toArray(new IClasspathEntry[newEntries.size()]);
            javaProject.setRawClasspath(newCP, new NullProgressMonitor());
        } catch (CoreException e) {
        }
    }
}

From source file:org.eclipse.ajdt.internal.builder.UIBuildListener.java

License:Open Source License

private void updateDependingProjectsWithJar(IProject project, IClasspathEntry newEntry) {
    IProject[] dependingProjects = getDependingProjects(project);

    goThroughProjects: for (int i = 0; i < dependingProjects.length; i++) {
        IJavaProject javaProject = JavaCore.create(dependingProjects[i]);
        if (javaProject == null)
            continue;
        try {/*from w w w  .  j a v  a2 s.  c o m*/
            IClasspathEntry[] cpEntry = javaProject.getRawClasspath();
            List newEntries = new ArrayList();
            for (int j = 0; j < cpEntry.length; j++) {
                if (cpEntry[j].equals(newEntry)) {
                    continue goThroughProjects;
                } else {
                    newEntries.add(cpEntry[j]);
                }
            }
            newEntries.add(newEntry);
            IClasspathEntry[] newCP = (IClasspathEntry[]) newEntries
                    .toArray(new IClasspathEntry[newEntries.size()]);
            javaProject.setRawClasspath(newCP, new NullProgressMonitor());
        } catch (CoreException e) {
        }
    }
}

From source file:org.eclipse.ajdt.internal.utils.AJDTUtils.java

License:Open Source License

private static void includeAJfiles(IProject project, boolean prompt) {
    IJavaProject jp = JavaCore.create(project);
    try {//from ww  w  .  j a v  a2  s .c  o m
        boolean changed = false;
        IClasspathEntry[] cpEntry = jp.getRawClasspath();
        for (int i = 0; i < cpEntry.length; i++) {
            IClasspathEntry entry = cpEntry[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath[] exc = entry.getExclusionPatterns();
                if (exc != null) {
                    List<IPath> removeList = new ArrayList<IPath>();
                    for (int j = 0; j < exc.length; j++) {
                        String ext = exc[j].getFileExtension();
                        if ((ext != null) && ext.equals("aj")) { //$NON-NLS-1$
                            removeList.add(exc[j]);
                        }
                    }
                    if (removeList.size() > 0) {
                        IPath[] exc2 = new IPath[exc.length - removeList.size()];
                        int ind = 0;
                        for (int j = 0; j < exc.length; j++) {
                            if (!removeList.contains(exc[j])) {
                                exc2[ind++] = exc[j];
                            }
                        }
                        IClasspathEntry classpathEntry = JavaCore.newSourceEntry(entry.getPath(), exc2);
                        cpEntry[i] = classpathEntry;
                        changed = true;
                    }
                }
            }
        }
        if (changed) {
            boolean restore = true;
            if (prompt) {
                IWorkbenchWindow window = AspectJUIPlugin.getDefault().getWorkbench()
                        .getActiveWorkbenchWindow();
                restore = MessageDialog.openQuestion(window.getShell(), UIMessages.ExcludedAJ_title,
                        UIMessages.ExcludedAJ_message);
            }
            if (restore) {
                jp.setRawClasspath(cpEntry, null);
            }
        }
    } catch (JavaModelException e) {
    }
}