1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import org.eclipse.core.runtime.ISafeRunnable; 18 import org.eclipse.core.runtime.OperationCanceledException; 19 import org.eclipse.core.runtime.SafeRunner; 20 import org.eclipse.jdt.core.*; 21 import org.eclipse.jdt.core.compiler.CategorizedProblem; 22 import org.eclipse.jdt.core.compiler.CompilationParticipant; 23 import org.eclipse.jdt.core.compiler.ReconcileContext; 24 import org.eclipse.jdt.core.dom.AST; 25 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; 26 import org.eclipse.jdt.internal.core.util.Messages; 27 import org.eclipse.jdt.internal.core.util.Util; 28 29 45 public class ReconcileWorkingCopyOperation extends JavaModelOperation { 46 public static boolean PERF = false; 47 48 public int astLevel; 49 public boolean resolveBindings; 50 public HashMap problems; 51 public int reconcileFlags; 52 WorkingCopyOwner workingCopyOwner; 53 public org.eclipse.jdt.core.dom.CompilationUnit ast; 54 public JavaElementDeltaBuilder deltaBuilder; 55 public boolean requestorIsActive; 56 57 public ReconcileWorkingCopyOperation(IJavaElement workingCopy, int astLevel, int reconcileFlags, WorkingCopyOwner workingCopyOwner) { 58 super(new IJavaElement[] {workingCopy}); 59 this.astLevel = astLevel; 60 this.reconcileFlags = reconcileFlags; 61 this.workingCopyOwner = workingCopyOwner; 62 } 63 64 68 protected void executeOperation() throws JavaModelException { 69 checkCanceled(); 70 try { 71 beginTask(Messages.element_reconciling, 2); 72 73 CompilationUnit workingCopy = getWorkingCopy(); 74 boolean wasConsistent = workingCopy.isConsistent(); 75 76 IProblemRequestor problemRequestor = workingCopy.getPerWorkingCopyInfo(); 78 if (problemRequestor != null) 79 problemRequestor = ((JavaModelManager.PerWorkingCopyInfo)problemRequestor).getProblemRequestor(); 80 boolean defaultRequestorIsActive = problemRequestor != null && problemRequestor.isActive(); 81 IProblemRequestor ownerProblemRequestor = this.workingCopyOwner.getProblemRequestor(workingCopy); 82 boolean ownerRequestorIsActive = ownerProblemRequestor != null && ownerProblemRequestor != problemRequestor && ownerProblemRequestor.isActive(); 83 this.requestorIsActive = defaultRequestorIsActive || ownerRequestorIsActive; 84 85 this.deltaBuilder = new JavaElementDeltaBuilder(workingCopy); 87 88 makeConsistent(workingCopy); 90 91 if (!wasConsistent || ((this.reconcileFlags & ICompilationUnit.FORCE_PROBLEM_DETECTION) != 0)) { 94 notifyParticipants(workingCopy); 95 96 if (this.ast == null) 98 makeConsistent(workingCopy); 99 } 100 101 if (this.problems != null && (((this.reconcileFlags & ICompilationUnit.FORCE_PROBLEM_DETECTION) != 0) || !wasConsistent)) { 103 if (defaultRequestorIsActive) { 104 reportProblems(workingCopy, problemRequestor); 105 } 106 if (ownerRequestorIsActive) { 107 reportProblems(workingCopy, ownerProblemRequestor); 108 } 109 } 110 111 JavaElementDelta delta = this.deltaBuilder.delta; 113 if (delta != null) { 114 addReconcileDelta(workingCopy, delta); 115 } 116 } finally { 117 done(); 118 } 119 } 120 121 127 private void reportProblems(CompilationUnit workingCopy, IProblemRequestor problemRequestor) { 128 try { 129 problemRequestor.beginReporting(); 130 for (Iterator iteraror = this.problems.values().iterator(); iteraror.hasNext();) { 131 CategorizedProblem[] categorizedProblems = (CategorizedProblem[]) iteraror.next(); 132 if (categorizedProblems == null) continue; 133 for (int i = 0, length = categorizedProblems.length; i < length; i++) { 134 CategorizedProblem problem = categorizedProblems[i]; 135 if (JavaModelManager.VERBOSE){ 136 System.out.println("PROBLEM FOUND while reconciling : " + problem.getMessage()); } 138 if (this.progressMonitor != null && this.progressMonitor.isCanceled()) break; 139 problemRequestor.acceptProblem(problem); 140 } 141 } 142 } finally { 143 problemRequestor.endReporting(); 144 } 145 } 146 147 150 protected CompilationUnit getWorkingCopy() { 151 return (CompilationUnit)getElementToProcess(); 152 } 153 154 157 public boolean isReadOnly() { 158 return true; 159 } 160 161 165 public org.eclipse.jdt.core.dom.CompilationUnit makeConsistent(CompilationUnit workingCopy) throws JavaModelException { 166 if (!workingCopy.isConsistent()) { 167 if (this.problems == null) this.problems = new HashMap (); 169 this.resolveBindings = this.requestorIsActive; 170 this.ast = workingCopy.makeConsistent(this.astLevel, this.resolveBindings, reconcileFlags, this.problems, this.progressMonitor); 171 this.deltaBuilder.buildDeltas(); 172 if (this.ast != null && this.deltaBuilder.delta != null) 173 this.deltaBuilder.delta.changedAST(this.ast); 174 return this.ast; 175 } 176 if (this.ast != null) 177 return this.ast; 179 CompilationUnitDeclaration unit = null; 180 char[] contents = null; 181 try { 182 if (JavaProject.hasJavaNature(workingCopy.getJavaProject().getProject()) 184 && (this.reconcileFlags & ICompilationUnit.FORCE_PROBLEM_DETECTION) != 0) { 185 this.resolveBindings = this.requestorIsActive; 186 if (this.problems == null) 187 this.problems = new HashMap (); 188 contents = workingCopy.getContents(); 189 unit = 190 CompilationUnitProblemFinder.process( 191 workingCopy, 192 contents, 193 this.workingCopyOwner, 194 this.problems, 195 this.astLevel != ICompilationUnit.NO_AST, 196 reconcileFlags, 197 this.progressMonitor); 198 if (this.progressMonitor != null) this.progressMonitor.worked(1); 199 } 200 201 if (this.astLevel != ICompilationUnit.NO_AST 203 && unit !=null) { 204 Map options = workingCopy.getJavaProject().getOptions(true); 205 this.ast = 207 AST.convertCompilationUnit( 208 this.astLevel, 209 unit, 210 contents, 211 options, 212 this.resolveBindings, 213 workingCopy, 214 reconcileFlags, 215 this.progressMonitor); 216 if (this.ast != null) { 217 this.deltaBuilder.delta = new JavaElementDelta(workingCopy); 218 this.deltaBuilder.delta.changedAST(this.ast); 219 } 220 if (this.progressMonitor != null) this.progressMonitor.worked(1); 221 } 222 } catch (JavaModelException e) { 223 if (JavaProject.hasJavaNature(workingCopy.getJavaProject().getProject())) 224 throw e; 225 } finally { 228 if (unit != null) { 229 unit.cleanUp(); 230 } 231 } 232 return this.ast; 233 } 234 235 private void notifyParticipants(final CompilationUnit workingCopy) { 236 IJavaProject javaProject = getWorkingCopy().getJavaProject(); 237 CompilationParticipant[] participants = JavaModelManager.getJavaModelManager().compilationParticipants.getCompilationParticipants(javaProject); 238 if (participants == null) return; 239 240 final ReconcileContext context = new ReconcileContext(this, workingCopy); 241 for (int i = 0, length = participants.length; i < length; i++) { 242 final CompilationParticipant participant = participants[i]; 243 SafeRunner.run(new ISafeRunnable() { 244 public void handleException(Throwable exception) { 245 if (exception instanceof Error ) { 246 throw (Error ) exception; } else if (exception instanceof OperationCanceledException) 248 throw (OperationCanceledException) exception; 249 else if (exception instanceof UnsupportedOperationException ) { 250 Util.log(exception, "Reconcile participant attempted to modify the buffer of the working copy being reconciled"); } else 253 Util.log(exception, "Exception occurred in reconcile participant"); } 255 public void run() throws Exception { 256 participant.reconcile(context); 257 } 258 }); 259 } 260 } 261 262 protected IJavaModelStatus verify() { 263 IJavaModelStatus status = super.verify(); 264 if (!status.isOK()) { 265 return status; 266 } 267 CompilationUnit workingCopy = getWorkingCopy(); 268 if (!workingCopy.isWorkingCopy()) { 269 return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, workingCopy); } 271 return status; 272 } 273 274 } 275 | Popular Tags |