This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 176929
Collapse All | Expand All

(-)a/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/RenameTransformer.java (-21 / +110 lines)
Lines 41-61 Link Here
41
41
42
package org.netbeans.modules.refactoring.java.plugins;
42
package org.netbeans.modules.refactoring.java.plugins;
43
43
44
import com.sun.source.tree.ClassTree;
45
import com.sun.source.tree.CompilationUnitTree;
46
import com.sun.source.tree.IdentifierTree;
47
import com.sun.source.tree.ImportTree;
48
import com.sun.source.tree.MemberSelectTree;
49
import com.sun.source.tree.MethodTree;
50
import com.sun.source.tree.Scope;
51
import com.sun.source.tree.Tree;
52
import com.sun.source.tree.TypeParameterTree;
53
import com.sun.source.tree.VariableTree;
54
import com.sun.source.util.TreePath;
44
import com.sun.source.util.Trees;
55
import com.sun.source.util.Trees;
45
import org.netbeans.modules.refactoring.java.spi.RefactoringVisitor;
46
import com.sun.source.tree.*;
47
import com.sun.source.util.TreePath;
48
import java.util.Iterator;
56
import java.util.Iterator;
49
import java.util.Set;
57
import java.util.Set;
50
import java.util.logging.Logger;
58
import java.util.logging.Logger;
51
import javax.lang.model.element.*;
59
import javax.lang.model.element.Element;
60
import javax.lang.model.element.ElementKind;
61
import javax.lang.model.element.ExecutableElement;
62
import javax.lang.model.element.Name;
52
import javax.lang.model.type.TypeMirror;
63
import javax.lang.model.type.TypeMirror;
53
import org.netbeans.api.java.lexer.JavaTokenId;
64
import org.netbeans.api.java.lexer.JavaTokenId;
54
import org.netbeans.api.java.source.ElementHandle;
65
import org.netbeans.api.java.source.ElementHandle;
55
import org.netbeans.api.java.source.ElementUtilities;
66
import org.netbeans.api.java.source.ElementUtilities;
56
import org.netbeans.api.java.source.SourceUtils;
57
import org.netbeans.api.lexer.Token;
67
import org.netbeans.api.lexer.Token;
58
import org.netbeans.api.lexer.TokenSequence;
68
import org.netbeans.api.lexer.TokenSequence;
69
import org.netbeans.modules.refactoring.java.spi.RefactoringVisitor;
70
71
import static org.netbeans.api.java.lexer.JavaTokenId.LINE_COMMENT;
72
import static org.netbeans.api.java.lexer.JavaTokenId.BLOCK_COMMENT;
73
import static org.netbeans.api.java.lexer.JavaTokenId.JAVADOC_COMMENT;
74
import static org.netbeans.api.java.lexer.JavaTokenId.WHITESPACE;
59
75
60
/**
76
/**
61
 *
77
 *
Lines 75-101 Link Here
75
91
76
    @Override
92
    @Override
77
    public Tree visitCompilationUnit(CompilationUnitTree node, Element p) {
93
    public Tree visitCompilationUnit(CompilationUnitTree node, Element p) {
78
        if (renameInComments) {
94
        if (!renameInComments) {
95
            return super.visitCompilationUnit(node, p);
96
        }
97
98
        if (p.getKind() == ElementKind.PARAMETER) {
99
            renameParameterInMethodComments(p);
100
101
        } else {
79
            String originalName = getOldSimpleName(p);
102
            String originalName = getOldSimpleName(p);
80
            if (originalName!=null) {
103
            if (originalName!=null) {
81
                TokenSequence<JavaTokenId> ts = workingCopy.getTokenHierarchy().tokenSequence(JavaTokenId.language());
104
                TokenSequence<JavaTokenId> ts = workingCopy.getTokenHierarchy().tokenSequence(JavaTokenId.language());
82
105
83
                while (ts.moveNext()) {
106
                while (ts.moveNext()) {
84
                    Token t = ts.token();
107
                    Token<JavaTokenId> t = ts.token();
85
108
                    
86
                    if (t.id() == JavaTokenId.BLOCK_COMMENT || t.id() == JavaTokenId.LINE_COMMENT || t.id() == JavaTokenId.JAVADOC_COMMENT) {
109
                    if (isComment(t)) {
87
                        int index = -1;
110
                        rewriteAllInComment(t.text().toString(), ts.offset(), originalName);
88
                        String text = t.text().toString();
89
90
                        while ((index = text.indexOf(originalName, index + 1)) != (-1)) {
91
                            if (index > 0 && Character.isJavaIdentifierPart(text.charAt(index - 1))) {
92
                                continue;
93
                            }
94
                            if ((index + originalName.length() < text.length()) && Character.isJavaIdentifierPart(text.charAt(index + originalName.length()))) {
95
                                continue;
96
                            }
97
                            workingCopy.rewriteInComment(ts.offset() + index, originalName.length(), newName);
98
                        }
99
                    }
111
                    }
100
                }
112
                }
101
            }
113
            }
Lines 157-162 Link Here
157
                    return;
169
                    return;
158
                }
170
                }
159
                Iterator iter = workingCopy.getElementUtilities().getMembers(el.asType(),new ElementUtilities.ElementAcceptor() {
171
                Iterator iter = workingCopy.getElementUtilities().getMembers(el.asType(),new ElementUtilities.ElementAcceptor() {
172
                    @Override
160
                    public boolean accept(Element e, TypeMirror type) {
173
                    public boolean accept(Element e, TypeMirror type) {
161
                        return id.equals(e.getSimpleName());
174
                        return id.equals(e.getSimpleName());
162
                    }
175
                    }
Lines 257-260 Link Here
257
        }
270
        }
258
        return false;
271
        return false;
259
    }
272
    }
273
274
    /**
275
     * Renames the method (or constructor) parameter in comments. This method
276
     * considers comments before and inside the method declaration, and within
277
     * the method body.
278
     *
279
     * @param parameter the method or constructor parameter {@link Element}
280
     */
281
    private void renameParameterInMethodComments(final Element parameter) {
282
        final Tree method = workingCopy.getTrees().getPath(parameter).getParentPath().getLeaf();
283
284
        final String originalName = getOldSimpleName(parameter);
285
        final int methodStart = (int) workingCopy.getTrees().getSourcePositions()
286
                .getStartPosition(workingCopy.getCompilationUnit(), method);
287
        final TokenSequence<JavaTokenId> tokenSequence = workingCopy.getTokenHierarchy().tokenSequence(JavaTokenId.language());
288
289
        //renaming in comments before the method/constructor
290
        tokenSequence.move(methodStart);
291
        while (tokenSequence.movePrevious()) {
292
            final Token<JavaTokenId> token = tokenSequence.token();
293
            if (isComment(token)) {
294
                rewriteAllInComment(token.text().toString(), tokenSequence.offset(), originalName);
295
            } else if (token.id() != WHITESPACE) {
296
                break;
297
            }            
298
        }
299
300
        //renaming in comments within the method/constructor declaration and body
301
        final int methodEnd = (int) workingCopy.getTrees().getSourcePositions()
302
                .getEndPosition(workingCopy.getCompilationUnit(), method);
303
304
        tokenSequence.move(methodStart);
305
        while (tokenSequence.moveNext() && tokenSequence.offset() < methodEnd) {
306
            final Token<JavaTokenId> token = tokenSequence.token();
307
            if (isComment(token)) {
308
                rewriteAllInComment(token.text().toString(), tokenSequence.offset(), originalName);
309
            }
310
        }
311
    }
312
313
    /**
314
     * Checks if {@code token} represents a comment.
315
     * 
316
     * @param token the {@link Token} to check
317
     * @return {@code true} if {@code token} represents a line comment, block
318
     *   comment or javadoc; {@code false} otherwise.
319
     */
320
    private boolean isComment(final Token<JavaTokenId> token) {
321
        switch (token.id()) {
322
            case LINE_COMMENT:
323
            case BLOCK_COMMENT:
324
            case JAVADOC_COMMENT:
325
                return true;
326
            default:
327
                return false;
328
        }
329
    }
330
331
    /**
332
     * Changes all occurrences of {@code originalName} to the new name in the comment {@code text}.
333
     *
334
     * @param text the text of the comment token
335
     * @param offset the offset of the comment token
336
     * @param originalName the old name to change
337
     */
338
    private void rewriteAllInComment(final String text, final int offset, final String originalName) {
339
        for (int index = text.indexOf(originalName); index != -1; index = text.indexOf(originalName, index + 1)) {
340
            if (index > 0 && Character.isJavaIdentifierPart(text.charAt(index - 1))) {
341
                continue;
342
            }
343
            if ((index + originalName.length() < text.length()) && Character.isJavaIdentifierPart(text.charAt(index + originalName.length()))) {
344
                continue;
345
            }
346
            workingCopy.rewriteInComment(offset + index, originalName.length(), newName);
347
        }
348
    }
260
}
349
}

Return to bug 176929