From 183fc23883289ae42854ce9afcf2d3b0d29b7599 Mon Sep 17 00:00:00 2001 From: aclement Date: Thu, 18 Mar 2004 13:00:01 +0000 Subject: Fix for Bugzilla Bug 40192 build cancel during weaving --- .../ajdt/internal/compiler/AjCompilerAdapter.java | 7 +- .../ajdt/internal/compiler/WeaverAdapter.java | 88 ++++++++++++++++++++-- .../ajdt/internal/core/builder/AjBuildManager.java | 20 ++++- 3 files changed, 102 insertions(+), 13 deletions(-) (limited to 'org.aspectj.ajdt.core') diff --git a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/AjCompilerAdapter.java b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/AjCompilerAdapter.java index d679d12b9..a6eacc40a 100644 --- a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/AjCompilerAdapter.java +++ b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/AjCompilerAdapter.java @@ -21,6 +21,7 @@ import java.util.Map; import org.aspectj.ajdt.internal.compiler.lookup.EclipseFactory; import org.aspectj.bridge.IMessage; import org.aspectj.bridge.IMessageHandler; +import org.aspectj.bridge.IProgressListener; import org.aspectj.weaver.bcel.BcelWeaver; import org.aspectj.weaver.bcel.BcelWorld; import org.eclipse.jdt.internal.compiler.CompilationResult; @@ -44,6 +45,7 @@ public class AjCompilerAdapter implements ICompilerAdapter { private boolean reportedErrors; private boolean isXNoWeave; private IIntermediateResultsRequestor intermediateResultsRequestor; + private IProgressListener progressListener; private IOutputClassFileNameProvider outputFileNameProvider; private WeaverMessageHandler weaverMessageHandler; private List /* InterimCompilationResult */ binarySources = new ArrayList(); @@ -73,6 +75,7 @@ public class AjCompilerAdapter implements ICompilerAdapter { BcelWeaver weaver, EclipseFactory eFactory, IIntermediateResultsRequestor intRequestor, + IProgressListener progressListener, IOutputClassFileNameProvider outputFileNameProvider, Map binarySourceEntries, /* fileName |-> List */ Collection /* InterimCompilationResult */ resultSetForFullWeave, @@ -81,6 +84,7 @@ public class AjCompilerAdapter implements ICompilerAdapter { this.isBatchCompile = isBatchCompile; this.weaver = weaver; this.intermediateResultsRequestor = intRequestor; + this.progressListener = progressListener; this.outputFileNameProvider = outputFileNameProvider; this.isXNoWeave = isXNoWeave; this.resultSetForFullWeave = resultSetForFullWeave; @@ -130,6 +134,7 @@ public class AjCompilerAdapter implements ICompilerAdapter { } else { resultsPendingWeave.add(intRes); } + } public void beforeResolving(CompilationUnitDeclaration unit, ICompilationUnit sourceUnit, boolean verifyMethods, boolean analyzeCode, boolean generateCode) { @@ -212,7 +217,7 @@ public class AjCompilerAdapter implements ICompilerAdapter { addAllKnownClassesToWeaveList(); } - weaver.weave(new WeaverAdapter(this,weaverMessageHandler)); + weaver.weave(new WeaverAdapter(this,weaverMessageHandler,progressListener)); } private void addAllKnownClassesToWeaveList() { diff --git a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/WeaverAdapter.java b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/WeaverAdapter.java index 6eead0082..345b928ad 100644 --- a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/WeaverAdapter.java +++ b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/WeaverAdapter.java @@ -14,9 +14,12 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; +import org.aspectj.bridge.IProgressListener; import org.aspectj.weaver.IClassFileProvider; import org.aspectj.weaver.IWeaveRequestor; import org.aspectj.weaver.bcel.UnwovenClassFile; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.jdt.internal.compiler.problem.AbortCompilation; /** * @author colyer @@ -32,13 +35,24 @@ public class WeaverAdapter implements IClassFileProvider, IWeaveRequestor, Itera private InterimCompilationResult nowProcessing; private InterimCompilationResult lastReturnedResult; private WeaverMessageHandler weaverMessageHandler; + private IProgressListener progressListener; private boolean finalPhase = false; + private int localIteratorCounter; + // Fields related to progress monitoring + private int progressMaxTypes; + private String progressPhasePrefix; + private double fromPercent; + private double toPercent = 100.0; + private int progressCompletionCount; + public WeaverAdapter(AjCompilerAdapter forCompiler, - WeaverMessageHandler weaverMessageHandler) { - this.compilerAdapter = forCompiler; + WeaverMessageHandler weaverMessageHandler, + IProgressListener progressListener) { + this.compilerAdapter = forCompiler; this.weaverMessageHandler = weaverMessageHandler; + this.progressListener = progressListener; } /* (non-Javadoc) @@ -46,6 +60,7 @@ public class WeaverAdapter implements IClassFileProvider, IWeaveRequestor, Itera */ public Iterator getClassFileIterator() { classFileIndex = 0; + localIteratorCounter = 0; nowProcessing = null; lastReturnedResult = null; resultIterator = compilerAdapter.resultsPendingWeave.iterator(); @@ -98,8 +113,10 @@ public class WeaverAdapter implements IClassFileProvider, IWeaveRequestor, Itera finishedWith(lastReturnedResult); } } + localIteratorCounter++; lastReturnedResult = nowProcessing; weaverMessageHandler.setCurrentResult(nowProcessing.result()); + // weaverMessageHandler.handleMessage(new Message("weaving " + nowProcessing.fileName(),IMessage.INFO, null, null)); return nowProcessing.unwovenClassFiles()[classFileIndex++]; } /* (non-Javadoc) @@ -108,22 +125,50 @@ public class WeaverAdapter implements IClassFileProvider, IWeaveRequestor, Itera public void remove() { throw new UnsupportedOperationException(); } - + // IWeaveRequestor // ===================================================================================== - + // weave phases as indicated by bcelWeaver... - public void processingReweavableState() {} - public void addingTypeMungers() {} - public void weavingAspects() {} - public void weavingClasses() {finalPhase = true;} + public void processingReweavableState() { + + // progress reporting logic + fromPercent = 50.0; // Assume weaving takes 50% of the progress bar... + recordProgress("processing reweavable state"); + } + + public void addingTypeMungers() { + + // progress reporting logic + // At this point we have completed one iteration through all the classes/aspects + // we'll be dealing with, so let us remember this max value for localIteratorCounter + // (for accurate progress reporting) + recordProgress("adding type mungers"); + progressMaxTypes = localIteratorCounter; + } + + public void weavingAspects() { + + // progress reporting logic + progressPhasePrefix="woven aspect "; + progressCompletionCount=0; // Start counting from *now* + } + + public void weavingClasses() { + finalPhase = true; + + // progress reporting logic + progressPhasePrefix="woven class "; + } public void weaveCompleted() { if ((lastReturnedResult != null) && (!lastReturnedResult.result().hasBeenAccepted)) { finishedWith(lastReturnedResult); } } + + /* (non-Javadoc) * @see org.aspectj.weaver.IWeaveRequestor#acceptResult(org.aspectj.weaver.bcel.UnwovenClassFile) @@ -135,6 +180,20 @@ public class WeaverAdapter implements IClassFileProvider, IWeaveRequestor, Itera AjClassFile ajcf = new AjClassFile(className.toCharArray(), result.getBytes()); lastReturnedResult.result().record(ajcf.fileName(),ajcf); + + if (progressListener != null) { + progressCompletionCount++; + + // Smoothly take progress from 'fromPercent' to 'toPercent' + recordProgress( + fromPercent + +((progressCompletionCount/(double)progressMaxTypes)*(toPercent-fromPercent)), + progressPhasePrefix+result.getClassName()+" (from "+nowProcessing.fileName()+")"); + + if (progressListener.isCancelledRequested()) { + throw new AbortCompilation(true,new OperationCanceledException("Weaving cancelled as requested")); + } + } } // helpers... @@ -160,4 +219,17 @@ public class WeaverAdapter implements IClassFileProvider, IWeaveRequestor, Itera table.remove(victim); } } + + private void recordProgress(String message) { + if (progressListener!=null) { + progressListener.setText(message); + } + } + + private void recordProgress(double percentage,String message) { + if (progressListener!=null) { + progressListener.setProgress(percentage/100); + progressListener.setText(message); + } + } } \ No newline at end of file diff --git a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildManager.java b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildManager.java index f49ea9e3c..dc1eef8f5 100644 --- a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildManager.java +++ b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildManager.java @@ -31,6 +31,7 @@ import org.aspectj.bridge.*; import org.aspectj.util.FileUtil; import org.aspectj.weaver.World; import org.aspectj.weaver.bcel.*; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.compiler.*; import org.eclipse.jdt.internal.compiler.*; import org.eclipse.jdt.internal.compiler.batch.*; @@ -38,6 +39,7 @@ import org.eclipse.jdt.internal.compiler.batch.FileSystem; import org.eclipse.jdt.internal.compiler.env.*; import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.compiler.parser.Parser; +import org.eclipse.jdt.internal.compiler.problem.AbortCompilation; import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; //import org.eclipse.jdt.internal.compiler.util.HashtableOfObject; @@ -158,7 +160,7 @@ public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAda for (int i = 0; (i < 5) && !files.isEmpty(); i++) { // System.err.println("XXXX inc: " + files); performCompilation(files); - if (handler.hasErrors()) { + if (handler.hasErrors() || (progressListener!=null && progressListener.isCancelledRequested())) { return false; } files = state.getFilesToCompile(false); @@ -572,8 +574,11 @@ public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAda options.produceReferenceInfo(true); //TODO turn off when not needed - compiler.compile(getCompilationUnits(filenames, encodings)); - + try { + compiler.compile(getCompilationUnits(filenames, encodings)); + } catch (OperationCanceledException oce) { + handler.handleMessage(new Message("build cancelled:"+oce.getMessage(),IMessage.WARNING,null,null)); + } // cleanup environment.cleanup(); environment = null; @@ -592,6 +597,11 @@ public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAda progressListener.setText("compiled: " + result.fileName()); } state.noteResult(result); + + if (progressListener!=null && progressListener.isCancelledRequested()) { + throw new AbortCompilation(true, + new OperationCanceledException("Compilation cancelled as requested")); + } } }; } @@ -853,7 +863,9 @@ public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAda return new AjCompilerAdapter(forCompiler,batchCompile,bcelWorld,bcelWeaver, factory, - getInterimResultRequestor(),this, + getInterimResultRequestor(), + progressListener, + this, state.binarySourceFiles, state.resultsFromFile.values(), buildConfig.isNoWeave()); -- cgit v1.2.3