1 19 20 package org.netbeans.modules.java.hints; 21 import java.lang.ref.Reference ; 22 import java.lang.ref.WeakReference ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.WeakHashMap ; 29 import org.netbeans.api.java.source.CancellableTask; 30 import org.netbeans.api.java.source.CompilationInfo; 31 import org.netbeans.api.java.source.JavaSource; 32 import org.netbeans.api.java.source.JavaSource.Phase; 33 import org.netbeans.api.java.source.JavaSource.Priority; 34 import org.netbeans.api.java.source.support.EditorAwareJavaSourceTaskFactory; 35 import org.openide.filesystems.FileObject; 36 import org.openide.util.Lookup; 37 38 42 public class LazyHintComputationFactory extends EditorAwareJavaSourceTaskFactory { 43 44 private static Map <FileObject, List <Reference <CreatorBasedLazyFixList>>> file2Creators = new WeakHashMap <FileObject, List <Reference <CreatorBasedLazyFixList>>>(); 45 46 47 public LazyHintComputationFactory() { 48 super(Phase.RESOLVED, Priority.LOW); 49 } 50 51 public CancellableTask<CompilationInfo> createTask(FileObject file) { 52 return new LazyHintComputation(file); 53 } 54 55 private static void rescheduleImpl(FileObject file) { 56 LazyHintComputationFactory f = Lookup.getDefault().lookup(LazyHintComputationFactory.class); 57 58 if (f != null) { 59 f.reschedule(file); 60 } 61 } 62 63 public static void addToCompute(FileObject file, CreatorBasedLazyFixList list) { 64 synchronized (LazyHintComputationFactory.class) { 65 List <Reference <CreatorBasedLazyFixList>> references = file2Creators.get(file); 66 67 if (references == null) { 68 file2Creators.put(file, references = new ArrayList <Reference <CreatorBasedLazyFixList>>()); 69 } 70 71 references.add(new WeakReference (list)); 72 } 73 74 rescheduleImpl(file); 75 } 76 77 public static synchronized List <CreatorBasedLazyFixList> getAndClearToCompute(FileObject file) { 78 List <Reference <CreatorBasedLazyFixList>> references = file2Creators.get(file); 79 80 if (references == null) { 81 return Collections.emptyList(); 82 } 83 84 List <CreatorBasedLazyFixList> result = new ArrayList <CreatorBasedLazyFixList>(); 85 86 for (Reference <CreatorBasedLazyFixList> r : references) { 87 CreatorBasedLazyFixList c = r.get(); 88 89 if (c != null) { 90 result.add(c); 91 } 92 } 93 94 references.clear(); 95 96 return result; 97 } 98 } 99 | Popular Tags |