Taking advantage of 8f439a91333f (#211273) to simplify rewrite.
1.1 --- a/javahints/src/org/netbeans/modules/javahints/jdk5/IteratorToFor.java Mon Apr 16 15:13:19 2012 -0400
1.2 +++ b/javahints/src/org/netbeans/modules/javahints/jdk5/IteratorToFor.java Thu Apr 19 12:20:09 2012 -0400
1.3 @@ -42,28 +42,16 @@
1.4
1.5 package org.netbeans.modules.javahints.jdk5;
1.6
1.7 -import com.sun.source.tree.BlockTree;
1.8 -import com.sun.source.tree.ExpressionTree;
1.9 -import com.sun.source.tree.StatementTree;
1.10 import com.sun.source.tree.Tree;
1.11 -import com.sun.source.tree.VariableTree;
1.12 import com.sun.source.util.TreePath;
1.13 import com.sun.source.util.TreePathScanner;
1.14 -import java.util.ArrayList;
1.15 import java.util.Collection;
1.16 -import java.util.Collections;
1.17 -import java.util.List;
1.18 -import java.util.Map;
1.19 import javax.lang.model.element.Element;
1.20 -import javax.lang.model.element.Modifier;
1.21 -import org.netbeans.api.java.source.TreeMaker;
1.22 -import org.netbeans.api.java.source.WorkingCopy;
1.23 import org.netbeans.spi.editor.hints.ErrorDescription;
1.24 import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
1.25 import org.netbeans.spi.java.hints.Hint;
1.26 import org.netbeans.spi.java.hints.HintContext;
1.27 -import org.netbeans.spi.java.hints.JavaFix;
1.28 -import org.netbeans.spi.java.hints.JavaFix.TransformationContext;
1.29 +import org.netbeans.spi.java.hints.JavaFixUtilities;
1.30 import org.netbeans.spi.java.hints.TriggerPattern;
1.31 import org.openide.util.NbBundle.Messages;
1.32
1.33 @@ -81,47 +69,8 @@
1.34 if (uses(ctx, ctx.getMultiVariables().get("$rest$"), ctx.getVariables().get("$it"))) {
1.35 return null;
1.36 }
1.37 - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_IteratorToFor(), new WhileFix(ctx).toEditorFix());
1.38 - }
1.39 -
1.40 - // like JavaFixUtilities.rewriteFix(..., "for ($type $elem : $coll) {$rest$;}"))
1.41 - // but does not mess up interior comments
1.42 - private static final class WhileFix extends JavaFix {
1.43 -
1.44 - private final HintContext hctx;
1.45 -
1.46 - WhileFix(HintContext hctx) {
1.47 - super(hctx.getInfo(), hctx.getPath());
1.48 - // XXX #211273 comment #3: should not be keeping hctx here, ought to rewrite
1.49 - this.hctx = hctx;
1.50 - }
1.51 -
1.52 - @Override protected String getText() {
1.53 - return Bundle.FIX_IteratorToFor();
1.54 - }
1.55 -
1.56 - @Override protected void performRewrite(TransformationContext ctx) {
1.57 - WorkingCopy wc = ctx.getWorkingCopy();
1.58 - TreeMaker tm = wc.getTreeMaker();
1.59 - Map<String,TreePath> vars = hctx.getVariables();
1.60 - Map<String,Collection<? extends TreePath>> multivars = hctx.getMultiVariables();
1.61 - BlockTree block = (BlockTree) vars.get("$_").getLeaf();
1.62 - List<StatementTree> stmts = new ArrayList<StatementTree>(block.getStatements());
1.63 - boolean deleted = stmts.remove((StatementTree) vars.get("$it").getLeaf());
1.64 - assert deleted;
1.65 - int idx = stmts.indexOf((StatementTree) vars.get("$elem").getParentPath().getParentPath().getLeaf());
1.66 - assert idx != -1;
1.67 - VariableTree decl = tm.Variable(tm.Modifiers(Collections.<Modifier>emptySet()), hctx.getVariableNames().get("$elem"), vars.get("$type").getLeaf(), null);
1.68 - ExpressionTree expr = (ExpressionTree) vars.get("$coll").getLeaf();
1.69 - List<StatementTree> rest = new ArrayList<StatementTree>();
1.70 - for (TreePath p : multivars.get("$rest$")) {
1.71 - rest.add((StatementTree) p.getLeaf());
1.72 - }
1.73 - StatementTree body = tm.Block(rest, false);
1.74 - stmts.set(idx, tm.EnhancedForLoop(decl, expr, body));
1.75 - wc.rewrite(block, tm.Block(stmts, false));
1.76 - }
1.77 -
1.78 + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_IteratorToFor(),
1.79 + JavaFixUtilities.rewriteFix(ctx, Bundle.FIX_IteratorToFor(), ctx.getPath(), "for ($type $elem : $coll) {$rest$;}"));
1.80 }
1.81
1.82 @TriggerPattern("for (java.util.Iterator $it = $coll.iterator(); $it.hasNext(); ) {$type $elem = ($type) $it.next(); $rest$;}")
1.83 @@ -129,37 +78,8 @@
1.84 if (uses(ctx, ctx.getMultiVariables().get("$rest$"), ctx.getVariables().get("$it"))) {
1.85 return null;
1.86 }
1.87 - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_IteratorToFor(), new ForFix(ctx).toEditorFix());
1.88 - }
1.89 -
1.90 - private static final class ForFix extends JavaFix {
1.91 -
1.92 - private final HintContext hctx;
1.93 -
1.94 - ForFix(HintContext hctx) {
1.95 - super(hctx.getInfo(), hctx.getPath());
1.96 - this.hctx = hctx;
1.97 - }
1.98 -
1.99 - @Override protected String getText() {
1.100 - return Bundle.FIX_IteratorToFor();
1.101 - }
1.102 -
1.103 - @Override protected void performRewrite(TransformationContext ctx) {
1.104 - WorkingCopy wc = ctx.getWorkingCopy();
1.105 - TreeMaker tm = wc.getTreeMaker();
1.106 - Map<String,TreePath> vars = hctx.getVariables();
1.107 - Map<String,Collection<? extends TreePath>> multivars = hctx.getMultiVariables();
1.108 - VariableTree decl = tm.Variable(tm.Modifiers(Collections.<Modifier>emptySet()), hctx.getVariableNames().get("$elem"), vars.get("$type").getLeaf(), null);
1.109 - ExpressionTree expr = (ExpressionTree) vars.get("$coll").getLeaf();
1.110 - List<StatementTree> rest = new ArrayList<StatementTree>();
1.111 - for (TreePath p : multivars.get("$rest$")) {
1.112 - rest.add((StatementTree) p.getLeaf());
1.113 - }
1.114 - StatementTree body = tm.Block(rest, false);
1.115 - wc.rewrite(ctx.getPath().getLeaf(), tm.EnhancedForLoop(decl, expr, body));
1.116 - }
1.117 -
1.118 + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_IteratorToFor(),
1.119 + JavaFixUtilities.rewriteFix(ctx, Bundle.FIX_IteratorToFor(), ctx.getPath(), "for ($type $elem : $coll) {$rest$;}"));
1.120 }
1.121
1.122 // adapted from org.netbeans.modules.java.hints.declarative.conditionapi.Matcher.referencedIn