1 19 package org.netbeans.modules.java.hints.infrastructure; 20 21 import com.sun.source.tree.CompilationUnitTree; 22 import com.sun.source.tree.Tree; 23 import com.sun.source.tree.Tree.Kind; 24 import com.sun.source.util.TreePath; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import java.util.Map ; 28 import org.netbeans.api.java.source.CompilationInfo; 29 import org.netbeans.api.java.source.support.CancellableTreePathScanner; 30 import org.netbeans.modules.java.editor.semantic.ScanningCancellableTask; 31 import org.netbeans.modules.java.hints.spi.TreeRule; 32 import org.netbeans.spi.editor.hints.ErrorDescription; 33 import org.netbeans.spi.editor.hints.HintsController; 34 35 39 public class HintsTask extends ScanningCancellableTask<CompilationInfo> { 40 41 public HintsTask() { 42 } 43 44 public void run(CompilationInfo info) throws Exception { 45 Map <Kind, List <TreeRule>> hints = RulesManager.getInstance().getHints(); 46 47 if (hints.isEmpty()) { 48 return ; 49 } 50 51 List <ErrorDescription> result = new ArrayList <ErrorDescription>(); 52 53 scan(new ScannerImpl(info, hints), info.getCompilationUnit(), result); 54 55 if (isCancelled()) 56 return ; 57 58 HintsController.setErrors(info.getFileObject(), HintsTask.class.getName(), result); 59 } 60 61 private static final class ScannerImpl extends CancellableTreePathScanner<Void , List <ErrorDescription>> { 62 63 private CompilationInfo info; 64 private Map <Kind, List <TreeRule>> hints; 65 66 public ScannerImpl(CompilationInfo info, Map <Kind, List <TreeRule>> hints) { 67 this.info = info; 68 this.hints = hints; 69 } 70 71 private void runAndAdd(TreePath path, List <TreeRule> rules, List <ErrorDescription> d) { 72 if (rules != null) { 73 for (TreeRule tr : rules) { 74 if (isCanceled()) { 75 return ; 76 } 77 List <ErrorDescription> errors = tr.run(info, path); 78 79 if (errors != null) { 80 d.addAll(errors); 81 } 82 } 83 } 84 } 85 86 @Override 87 public Void scan(Tree tree, List <ErrorDescription> p) { 88 if (tree == null) 89 return null; 90 91 TreePath tp = new TreePath(getCurrentPath(), tree); 92 Kind k = tree.getKind(); 93 94 runAndAdd(tp, hints.get(k), p); 95 96 if (isCanceled()) { 97 return null; 98 } 99 100 return super.scan(tree, p); 101 } 102 103 @Override 104 public Void scan(TreePath path, List <ErrorDescription> p) { 105 Kind k = path.getLeaf().getKind(); 106 runAndAdd(path, hints.get(k), p); 107 108 if (isCanceled()) { 109 return null; 110 } 111 112 return super.scan(path, p); 113 } 114 115 } 116 117 } 118 | Popular Tags |