Skip to content

Commit

Permalink
Added more comments, cleaned up ScalaJavaDepTest
Browse files Browse the repository at this point in the history
Removed unnecessary logging
(cherry picked from commit 2dfffd3)
  • Loading branch information
Luc Bourlier committed Dec 8, 2011
1 parent 1b1cdc3 commit 8ef1361
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ import org.eclipse.core.internal.events.BuildManager;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceDelta;

/**
* Weaving on the BuildManager, to be able to replace and update
* deltas.
*/
@SuppressWarnings("restriction")
public aspect BuildManagerAspect {

/**
* Point cut on {@link BuildManager#getDelta(IProject)}
*/
pointcut getDelta(BuildManager bm, IProject project):
execution(IResourceDelta BuildManager+.getDelta(IProject)) &&
args(project) &&
this(bm);


/**
* Around {@link BuildManager#getDelta(IProject)}
*/
IResourceDelta around(BuildManager bm, IProject project):
getDelta(bm, project) {
System.out.println("[luc] getDelta() for " + project.getName());
return BuildManagerStore.INSTANCE.appendJavaSourceFilesToCompile(proceed(bm, project), project);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,37 @@
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;

/**
* Class used to store java files to be compile per projects
*/
public class BuildManagerStore {

/**
* Singleton
*/
public static final BuildManagerStore INSTANCE= new BuildManagerStore();

/**
* Project to java files to compile
*/
private Map<IProject, File[]> projectToJavaSourceFiles= new HashMap<IProject, File[]>();

private BuildManagerStore() {
}

/**
* Return a resource delta containing the same changes as the given delta, plus the files set to be compiled for
* the given project.<br>
* If delta is <code>null</code>, or there are no files to compile for the given project, return the given resource delta.
*/
public IResourceDelta appendJavaSourceFilesToCompile(IResourceDelta delta, IProject project) {
if (delta == null) {
return delta;
}

// no need to create a new resource delta if no files have to be added.
File[] files= projectToJavaSourceFiles.get(project);
if (files == null) {
if (files == null || files.length == 0) {
return delta;
}

Expand All @@ -46,6 +61,10 @@ public IResourceDelta appendJavaSourceFilesToCompile(IResourceDelta delta, IProj
return newDelta;
}

/**
* Set the java files to compile for the given project.<br>
* Use <code>null</code> to reset the data for a project.
*/
public void setJavaSourceFilesToCompile(File[] files, IProject project) {
if (files == null) {
projectToJavaSourceFiles.remove(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,51 @@ public class ExpandableResourceDelta extends PlatformObject implements IResource
private IResource resource;

/**
*
* @param resourceDelta
* Create a node wrapping the give resource delta.
*/
private ExpandableResourceDelta(IResourceDelta resourceDelta) {
wrapped= resourceDelta;
}


/**
* Create a node for the give resource, with a status CONTENT and CHANGE.
*/
private ExpandableResourceDelta(IResource resource) {
this.resource= resource;
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#accept(org.eclipse.core.resources.IResourceDeltaVisitor)
*/
public void accept(IResourceDeltaVisitor visitor) throws CoreException {
accept(visitor, IResource.NONE);
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#accept(org.eclipse.core.resources.IResourceDeltaVisitor, boolean)
*/
public void accept(IResourceDeltaVisitor visitor, boolean includePhantoms)
throws CoreException {
accept(visitor, includePhantoms ? IContainer.INCLUDE_PHANTOMS : IResource.NONE);
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#accept(org.eclipse.core.resources.IResourceDeltaVisitor, int)
*/
public void accept(IResourceDeltaVisitor visitor, int memberFlags)
throws CoreException {
for (IResourceDelta child: getAffectedChildren(ADDED | REMOVED | CHANGED, memberFlags)) {
visitor.visit(child);
}
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#findMember(org.eclipse.core.runtime.IPath)
*/
public IResourceDelta findMember(IPath path) {
if (path.segmentCount() == 0)
return this;
Expand All @@ -84,14 +102,26 @@ public IResourceDelta findMember(IPath path) {
return null;
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#getAffectedChildren()
*/
public IResourceDelta[] getAffectedChildren() {
return getAffectedChildren(ADDED | REMOVED | CHANGED, IResource.NONE);
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#getAffectedChildren(int)
*/
public IResourceDelta[] getAffectedChildren(int kindMask) {
return getAffectedChildren(kindMask, IResource.NONE);
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#getAffectedChildren(int, int)
*/
public IResourceDelta[] getAffectedChildren(int kindMask, int memberFlags) {
List<IResourceDelta> affectedChildren= new ArrayList<IResourceDelta>();
if ((memberFlags & IContainer.INCLUDE_PHANTOMS) != 0) {
Expand All @@ -109,59 +139,100 @@ public IResourceDelta[] getAffectedChildren(int kindMask, int memberFlags) {
return affectedChildren.toArray(new IResourceDelta[affectedChildren.size()]);
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#getFlags()
*/
public int getFlags() {
if (wrapped != null)
return wrapped.getFlags();
return IResourceDelta.CONTENT;
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#getFullPath()
*/
public IPath getFullPath() {
if (wrapped != null)
return wrapped.getFullPath();
return resource.getFullPath();
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#getKind()
*/
public int getKind() {
if (wrapped != null)
return wrapped.getKind();
return IResourceDelta.CHANGED;
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#getMarkerDeltas()
*/
public IMarkerDelta[] getMarkerDeltas() {
if (wrapped != null)
return wrapped.getMarkerDeltas();
return new IMarkerDelta[0];
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#getMovedFromPath()
*/
public IPath getMovedFromPath() {
if (wrapped != null)
return wrapped.getMovedFromPath();
return null;
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#getMovedToPath()
*/
public IPath getMovedToPath() {
if (wrapped != null)
return wrapped.getMovedToPath();
return null;
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#getProjectRelativePath()
*/
public IPath getProjectRelativePath() {
if (wrapped != null)
return wrapped.getProjectRelativePath();
return resource.getFullPath().makeRelativeTo(resource.getProject().getFullPath());
}

/*
* (non-Javadoc)
* @see org.eclipse.core.resources.IResourceDelta#getResource()
*/
public IResource getResource() {
if (wrapped != null)
return wrapped.getResource();
return resource;
}

/**
* Add the given resource in this resource delta, as a content change node.<br>
* Creates the parent nodes if required.
*/
public void addChangedResource(IResource changedResource) {
getOrAddResourceForChange(changedResource);
}

/**
* Return the node for a given resource. If it doesn't exist yet, create it, and its parent if required.<br>
* Make sure that it is "reachable": it doesn't have NO_CHANGE parents.
*/
private ExpandableResourceDelta getOrAddResourceForChange(IResource newResource) {
// if this is the node for the given resource, we found it
if (getResource().equals(newResource)) {

// if the existing node is marked as NO_CHANGE, we need to unwrap it (thus making it CHANGE),
Expand All @@ -172,17 +243,26 @@ private ExpandableResourceDelta getOrAddResourceForChange(IResource newResource)
}
return this;
}

// otherwise, look for its parent
ExpandableResourceDelta parentResourceDelta= getOrAddResourceForChange(newResource.getParent());

// check if one of the existing siblings is actually the node for the given resource
for (ExpandableResourceDelta childResourceDelta: parentResourceDelta.children) {
if (childResourceDelta.getResource().equals(newResource)) {
return childResourceDelta;
}
}

// if it doesn't exist, create the node and adds it to its parent
ExpandableResourceDelta newResourceDelta= new ExpandableResourceDelta(newResource);
parentResourceDelta.children.add(newResourceDelta);
return newResourceDelta;
}

/**
* Duplicate a resource delta by wrapping the root node and duplicating its children.
*/
public static ExpandableResourceDelta duplicate(IResourceDelta original) {
ExpandableResourceDelta newDelta = new ExpandableResourceDelta(original);
for (IResourceDelta child: original.getAffectedChildren(ALL_WITH_PHANTOMS, IContainer.INCLUDE_HIDDEN | IContainer.INCLUDE_PHANTOMS | IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS)) {
Expand All @@ -192,6 +272,10 @@ public static ExpandableResourceDelta duplicate(IResourceDelta original) {
}


/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "ExtendedResourceDelta(" + getFullPath() + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,26 @@ import org.eclipse.jdt.core.IPackageFragmentRoot
import scala.tools.eclipse.testsetup.SDTTestUtils
import org.eclipse.core.resources.IFile

/**
* Test for test cases requiring nested projects (one project root is a subfolder of an other project)
*/
object NestedProjectsTest extends TestProjectSetup("nested-parent") {

final val scalaProjectName= "nested-scala"

/**
* The nested scala project
*/
lazy val scalaProject: ScalaProject = {
val workspace = ResourcesPlugin.getWorkspace()
EclipseUtils.workspaceRunnableIn(workspace) { monitor =>
val project= workspace.getRoot().getProject("nested-scala")
val projectDescription= workspace.newProjectDescription("nested-scala")
projectDescription.setLocation(workspace.getRoot().getLocation().append("nested-parent/nested-scala"))
project.create(projectDescription, null)
project.open(null)
JavaCore.create(project)
// create the project
val newProject= workspace.getRoot().getProject(scalaProjectName)
val projectDescription= workspace.newProjectDescription(scalaProjectName)
projectDescription.setLocation(project.underlying.getLocation().append(scalaProjectName))
newProject.create(projectDescription, null)
newProject.open(null)
JavaCore.create(newProject)
}
ScalaPlugin.plugin.getScalaProject(workspace.getRoot.getProject(scalaProjectName))
}
Expand All @@ -41,10 +48,11 @@ class NestedProjectsTest {

/**
* At first, the ExpandableResourceDelta was crashing when used for nested projects. This test checks that it is not
* happening.
* happening any more.
*/
@Test
def checkJavaCompilesInNestedProject() {
// clean the nested project
scalaProject.underlying.build(IncrementalProjectBuilder.CLEAN_BUILD, new NullProgressMonitor)
scalaProject.underlying.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor)

Expand Down
Loading

0 comments on commit 8ef1361

Please sign in to comment.