1 19 20 package org.netbeans.modules.refactoring.java.plugins; 21 22 import com.sun.source.tree.CompilationUnitTree; 23 import com.sun.source.tree.Tree; 24 import com.sun.source.util.TreePath; 25 import java.io.IOException ; 26 import java.net.MalformedURLException ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collection ; 30 import java.util.Collections ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.Set ; 34 import javax.lang.model.element.Element; 35 import javax.swing.Action ; 36 import org.netbeans.api.java.source.CancellableTask; 37 import org.netbeans.api.java.source.ClasspathInfo; 38 import org.netbeans.api.java.source.CompilationController; 39 import org.netbeans.api.java.source.CompilationInfo; 40 import org.netbeans.api.java.source.JavaSource; 41 import org.netbeans.api.java.source.JavaSource.Phase; 42 import org.netbeans.api.java.source.ModificationResult; 43 import org.netbeans.api.java.source.TreePathHandle; 44 import org.netbeans.api.java.source.WorkingCopy; 45 import org.netbeans.modules.refactoring.java.DiffElement; 46 import org.netbeans.modules.refactoring.api.AbstractRefactoring; 47 import org.netbeans.modules.refactoring.api.Problem; 48 import org.netbeans.modules.refactoring.api.RefactoringElement; 49 import org.netbeans.modules.refactoring.api.RefactoringSession; 50 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring; 51 import org.netbeans.modules.refactoring.api.WhereUsedQuery; 52 import org.netbeans.modules.refactoring.spi.ui.UI; 53 import org.netbeans.modules.refactoring.java.api.WhereUsedQueryConstants; 54 import org.netbeans.modules.refactoring.spi.ProblemDetailsFactory; 55 import org.netbeans.modules.refactoring.spi.ProblemDetailsImplementation; 56 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 57 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 58 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl; 59 import org.netbeans.modules.refactoring.spi.ui.RefactoringUI; 60 import org.netbeans.modules.refactoring.java.ui.SafeDeleteUI; 61 import org.netbeans.modules.refactoring.java.ui.WhereUsedQueryUI; 62 import org.netbeans.modules.refactoring.java.ui.tree.ElementGrip; 63 import org.netbeans.modules.refactoring.java.ui.tree.ElementGripFactory; 64 import org.openide.ErrorManager; 65 import org.openide.filesystems.FileObject; 66 import org.openide.text.PositionBounds; 67 import org.openide.util.Cancellable; 68 import org.openide.util.NbBundle; 69 import org.openide.util.lookup.Lookups; 70 71 72 76 public class SafeDeleteRefactoringPlugin extends JavaRefactoringPlugin { 77 private SafeDeleteRefactoring refactoring; 78 private WhereUsedQuery[] whereUsedQueries; 79 80 85 public SafeDeleteRefactoringPlugin(SafeDeleteRefactoring refactoring) { 86 this.refactoring = refactoring; 87 } 88 89 98 public Problem prepare(RefactoringElementsBag refactoringElements) { 99 RefactoringSession inner = RefactoringSession.create("delete"); Set refactoredObjects = new HashSet (); 101 fireProgressListenerStart(AbstractRefactoring.PARAMETERS_CHECK, whereUsedQueries.length + 1); 102 for(int i = 0;i < whereUsedQueries.length; ++i) { 103 Object refactoredObject = whereUsedQueries[i].getRefactoringSource().lookup(Object .class); 104 refactoredObjects.add(refactoredObject); 105 106 whereUsedQueries[i].prepare(inner); 107 108 JavaSource javaSource = JavaSource.forFileObject(grips.get(i).getFileObject()); 109 try { 110 final ModificationResult result = javaSource.runModificationTask(new FindTask(refactoringElements, grips.get(i))); 111 refactoringElements.registerTransaction(new RetoucheCommit(Collections.singleton(result))); 112 for (FileObject jfo : result.getModifiedFileObjects()) { 113 for (ModificationResult.Difference dif: result.getDifferences(jfo)) { 114 refactoringElements.add(refactoring,DiffElement.create(dif, jfo, result)); 115 } 116 } 117 } catch (MalformedURLException ex) { 118 ex.printStackTrace(); 119 } catch (IOException ex) { 120 ex.printStackTrace(); 121 } 122 123 fireProgressListenerStep(); 124 } 125 126 Collection importStmts = new ArrayList (); 127 128 for (Iterator iter = inner.getRefactoringElements().iterator(); iter.hasNext(); ) { 129 ElementGrip elem = (ElementGrip) ((RefactoringElement) iter.next()).getComposite(); 130 ElementGrip comp = elem; 131 boolean isOuterRef = true; 132 if (isOuterRef) { 166 fireProgressListenerStop(); 167 return new Problem(false, getString("ERR_ReferencesFound"), ProblemDetailsFactory.createProblemDetails(new ProblemDetailsImplemen(new WhereUsedQueryUI(elem.getHandle(), "!!!TODO!!!", refactoring), inner))); 168 } 169 } 170 171 if(importStmts.size() > 0){ 173 for (Iterator it = importStmts.iterator(); it.hasNext();) { 174 RefactoringElementImplementation refacElem = 175 (RefactoringElementImplementation) it.next(); 176 refactoringElements.add(refactoring, refacElem); 177 } 178 } 179 180 fireProgressListenerStop(); 181 return null; 182 } 183 184 private class ProblemDetailsImplemen implements ProblemDetailsImplementation { 185 186 private RefactoringUI ui; 187 private RefactoringSession rs; 188 189 public ProblemDetailsImplemen(RefactoringUI ui, RefactoringSession rs) { 190 this.ui = ui; 191 this.rs = rs; 192 } 193 194 public void showDetails(Action callback, Cancellable parent) { 195 parent.cancel(); 196 UI.openRefactoringUI(ui, rs, callback); 197 } 198 199 public String getDetailsHint() { 200 return getString("LBL_ShowUsages"); 201 } 202 203 } 204 205 209 public Problem preCheck() { 210 return null; 227 } 228 229 232 public Problem fastCheckParameters() { 233 return null; 235 } 236 237 private ArrayList <TreePathHandle> grips = new ArrayList (); 238 243 public Problem checkParameters() { 244 grips.clear(); 247 final ArrayList <ClasspathInfo> controllers = new ArrayList (); 248 for (final FileObject f:refactoring.getRefactoringSource().lookupAll(FileObject.class)) { 249 JavaSource source = JavaSource.forFileObject(f); 250 try { 251 source.runUserActionTask(new CancellableTask<CompilationController>() { 252 public void cancel() { 253 254 } 255 public void run(CompilationController co) throws Exception { 256 co.toPhase(Phase.ELEMENTS_RESOLVED); 257 CompilationUnitTree cut = co.getCompilationUnit(); 258 for (Tree t: cut.getTypeDecls()) { 259 grips.add(TreePathHandle.create(TreePath.getPath(cut, t), co)); 260 } 261 controllers.add(co.getClasspathInfo()); 262 } 263 }, true); 264 } catch (IllegalArgumentException ex) { 265 ex.printStackTrace(); 266 } catch (IOException ex) { 267 ex.printStackTrace(); 268 } 269 } 270 271 grips.addAll(refactoring.getRefactoringSource().lookupAll(TreePathHandle.class)); 272 273 whereUsedQueries = new WhereUsedQuery[grips.size()]; 274 for(int i = 0;i < whereUsedQueries.length; ++i) { 275 if (!controllers.isEmpty()) { 276 refactoring.getContext().add(controllers.get(i)); 277 } 278 whereUsedQueries[i] = createQuery(grips.get(i)); 279 280 whereUsedQueries[i].putValue(WhereUsedQuery.SEARCH_IN_COMMENTS, refactoring.isCheckInComments()); 281 if(Tree.Kind.METHOD.equals(grips.get(i).getKind())){ 282 whereUsedQueries[i].putValue(WhereUsedQueryConstants.FIND_OVERRIDING_METHODS,true); 283 } 284 } 285 286 Problem problemFromUsage = null; 287 for(int i = 0;i < whereUsedQueries.length; ++i) { 288 if((problemFromUsage = whereUsedQueries[i].checkParameters()) != null) 292 return problemFromUsage; 293 } 294 return null; 295 } 296 297 private WhereUsedQuery createQuery(TreePathHandle tph) { 298 WhereUsedQuery q = new WhereUsedQuery(Lookups.singleton(tph)); 299 for (Object o:refactoring.getContext().lookupAll(Object .class)) { 300 q.getContext().add(o); 301 } 302 return q; 303 } 304 305 306 private static String getString(String key) { 308 return NbBundle.getMessage(SafeDeleteRefactoringPlugin.class, key); 309 } 310 311 315 private Problem getProblemMessage(Object refactoredObject) { 316 String errorMsg = NbBundle.getMessage(SafeDeleteUI.class, 317 "DSC_SafeDelProblem", refactoredObject); return new Problem(true,errorMsg); 319 } 320 321 private static class ImportRefDeleteElement extends SimpleRefactoringElementImpl{ 322 323 private final String text = "todo"; 325 326 332 public String getText() { 333 return text; 334 } 335 336 public String getDisplayText() { 337 return getText(); 338 } 339 340 public void performChange() { 341 } 343 344 public Element getComposite() { 345 return null; 347 } 348 349 public FileObject getParentFile() { 350 return null; 352 } 353 354 public PositionBounds getPosition() { 355 return null; 357 } 358 } 359 360 private class FindTask implements CancellableTask<WorkingCopy> { 361 362 private RefactoringElementsBag elements; 363 private TreePathHandle jmiObject; 364 365 public FindTask(RefactoringElementsBag elements, TreePathHandle element) { 366 super(); 367 this.elements = elements; 368 this.jmiObject = element; 369 } 370 371 public void cancel() { 372 } 373 374 public void run(WorkingCopy compiler) throws IOException { 375 compiler.toPhase(Phase.RESOLVED); 376 CompilationUnitTree cu = compiler.getCompilationUnit(); 377 if (cu == null) { 378 ErrorManager.getDefault().log(ErrorManager.ERROR, "compiler.getCompilationUnit() is null " + compiler); 379 return; 380 } 381 Element el = jmiObject.resolveElement(compiler); 382 assert el != null; 383 384 DeleteTransformer findVisitor = new DeleteTransformer(compiler); 385 findVisitor.scan(compiler.getCompilationUnit(), el); 386 387 for (TreePath tree : findVisitor.getUsages()) { 388 ElementGripFactory.getDefault().put(compiler.getFileObject(), tree, compiler); 389 } 390 } 391 } 392 } 393 | Popular Tags |