package com.onpositive.gae.profiler;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceStatus;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import com.onpositive.instrumentation.tasks.CallDictionary;
import com.onpositive.instrumentation.tasks.Instrumentor;
public class ProfilerProjectBuilder extends IncrementalProjectBuilder {
public ProfilerProjectBuilder() {
}
@SuppressWarnings("unchecked")
protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
throws CoreException {
IProject project = getProject();
IJavaProject create = JavaCore.create(project);
if (kind == FULL_BUILD) {
boolean deleteCallDictinary = DictionaryManager
.deleteCallDictinary(project);
if (!deleteCallDictinary) {
throw new CoreException(new Status(IStatus.ERROR,
Activator.PLUGIN_ID, IResourceStatus.BUILD_FAILED,
"Unable to cleanup call dictionary", null));
}
}
final IPath outputLocation = create.getOutputLocation();
IResourceDelta delta = getDelta(project);
final HashSet<IFile> toInstrument = new HashSet<IFile>();
if (delta != null) {
delta.accept(new IResourceDeltaVisitor() {
public boolean visit(IResourceDelta delta) throws CoreException {
IResource res = delta.getResource();
if (res instanceof IFile) {
if (delta.getKind() != IResourceDelta.REMOVED) {
IFile fl = (IFile) res;
if (outputLocation.isPrefixOf(fl.getFullPath())) {
if (fl.getName().endsWith(".class")) {
toInstrument.add(fl);
}
}
}
else{
IFile fl = (IFile) res;
if (outputLocation.isPrefixOf(fl.getFullPath())) {
if (fl.getName().endsWith(".class")) {
toInstrument.add(fl);
}
}
}
}
return true;
}
});
} else {
ResourcesPlugin.getWorkspace().getRoot().getFolder(outputLocation)
.accept(new IResourceVisitor() {
public boolean visit(IResource resource)
throws CoreException {
if (resource instanceof IFile) {
IFile fl = (IFile) resource;
if (outputLocation.isPrefixOf(fl.getFullPath())) {
if (fl.getName().endsWith(".class")) {
toInstrument.add(fl);
}
}
}
return true;
}
});
}
// if (true){
// return null;
// }
if (!toInstrument.isEmpty()) {
try {
CallDictionary ds = DictionaryManager
.getCallDictionary(project);
Instrumentor t = new Instrumentor(ds, create);
for (IFile f : toInstrument) {
try {
t.instrumentClass1(f);
} catch (IOException e) {
Activator.getDefault().log(e);
throw new CoreException(
new Status(IStatus.ERROR, Activator.PLUGIN_ID,
IResourceStatus.BUILD_FAILED, e
.getMessage(), e));
}
}
ds.commit();
} catch (IOException e) {
StartProfilingAction.rebuild(create);
throw new CoreException(new Status(IStatus.ERROR,
Activator.PLUGIN_ID, IResourceStatus.BUILD_FAILED, e
.getMessage(), e));
}
}
return null;
}
}