1 19 package org.netbeans.modules.refactoring.java.plugins; 20 21 import java.io.IOException ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import javax.lang.model.element.Element; 30 import javax.lang.model.type.TypeKind; 31 import org.netbeans.api.java.classpath.ClassPath; 32 import org.netbeans.api.java.source.CancellableTask; 33 import org.netbeans.api.java.source.ClasspathInfo; 34 import org.netbeans.api.java.source.CompilationInfo; 35 import org.netbeans.api.java.source.JavaSource; 36 import org.netbeans.api.java.source.ModificationResult; 37 import org.netbeans.api.java.source.TreePathHandle; 38 import org.netbeans.api.java.source.WorkingCopy; 39 import org.netbeans.modules.refactoring.spi.*; 40 import org.netbeans.modules.refactoring.api.*; 41 import org.openide.filesystems.FileObject; 42 import org.openide.util.Cancellable; 43 import org.openide.util.NbBundle; 44 45 49 public abstract class JavaRefactoringPlugin extends ProgressProviderAdapter implements RefactoringPlugin { 50 51 protected volatile boolean cancelRequest = false; 52 private volatile CancellableTask currentTask; 53 54 public void cancelRequest() { 55 cancelRequest = true; 56 if (currentTask!=null) { 57 currentTask.cancel(); 58 } 59 } 60 61 protected static final Problem createProblem(Problem result, boolean isFatal, String message) { 62 Problem problem = new Problem(isFatal, message); 63 if (result == null) { 64 return problem; 65 } else if (isFatal) { 66 problem.setNext(result); 67 return problem; 68 } else { 69 72 Problem p = result; 74 while (p.getNext() != null) 75 p = p.getNext(); 76 p.setNext(problem); 77 return result; 78 } 79 } 80 81 91 protected static Problem isElementAvail(TreePathHandle e, CompilationInfo info) { 92 if (e==null) { 93 return new Problem(true, NbBundle.getMessage(JavaRefactoringPlugin.class, "DSC_ElNotAvail")); } else { 96 Element el = e.resolveElement(info); 97 if (el == null || el.asType().getKind() == TypeKind.ERROR) { 98 return new Problem(true, NbBundle.getMessage(JavaRefactoringPlugin.class, "DSC_ElementNotResolved")); 99 } 100 101 return null; 103 } 104 } 105 106 private Iterable <? extends List <FileObject>> groupByRoot (Iterable <? extends FileObject> data) { 107 Map <FileObject,List <FileObject>> result = new HashMap <FileObject,List <FileObject>> (); 108 for (FileObject file : data) { 109 ClassPath cp = ClassPath.getClassPath(file, ClassPath.SOURCE); 110 if (cp != null) { 111 FileObject root = cp.findOwnerRoot(file); 112 if (root != null) { 113 List <FileObject> subr = result.get (root); 114 if (subr == null) { 115 subr = new LinkedList <FileObject>(); 116 result.put (root,subr); 117 } 118 subr.add (file); 119 } 120 } 121 } 122 return result.values(); 123 } 124 125 protected final Collection <ModificationResult> processFiles(Set <FileObject> files, CancellableTask<WorkingCopy> task) { 126 currentTask = task; 127 Iterable <? extends List <FileObject>> work = groupByRoot (files); 128 Collection <ModificationResult> results = new LinkedList (); 129 for (List <FileObject> fos : work) { 130 final JavaSource javaSource = JavaSource.create(ClasspathInfo.create(fos.get(0)), fos); 131 try { 132 results.add(javaSource.runModificationTask(task)); 133 } catch (IOException ex) { 134 throw (RuntimeException ) new RuntimeException ().initCause(ex); 135 } 136 } 137 return results; 138 } 139 } 140 | Popular Tags |