001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.refactoring.java.plugins;
043:
044: import org.netbeans.modules.refactoring.java.spi.RefactoringVisitor;
045: import com.sun.source.tree.*;
046: import com.sun.source.util.SourcePositions;
047: import com.sun.source.util.TreePath;
048: import java.util.ArrayList;
049: import java.util.Collections;
050: import java.util.HashSet;
051: import java.util.List;
052: import java.util.Set;
053: import java.util.logging.Logger;
054: import javax.lang.model.element.*;
055: import org.netbeans.api.java.source.ElementHandle;
056: import org.netbeans.api.java.source.SourceUtils;
057: import org.netbeans.modules.refactoring.java.api.ChangeParametersRefactoring;
058: import org.netbeans.modules.refactoring.java.api.ChangeParametersRefactoring.ParameterInfo;
059:
060: /**
061: *
062: * @author Jan Becicka
063: */
064: public class ChangeParamsTransformer extends RefactoringVisitor {
065:
066: private Set<ElementHandle<ExecutableElement>> allMethods;
067:
068: public ChangeParamsTransformer(
069: ChangeParametersRefactoring refactoring,
070: Set<ElementHandle<ExecutableElement>> am) {
071: this .refactoring = refactoring;
072: this .allMethods = am;
073: }
074:
075: @Override
076: public Tree visitNewClass(NewClassTree tree, Element p) {
077: if (!workingCopy.getTreeUtilities().isSynthetic(
078: getCurrentPath())) {
079: Element el = workingCopy.getTrees().getElement(
080: getCurrentPath());
081: if (el != null) {
082: if (isMethodMatch(el)) {
083: List<ExpressionTree> arguments = getNewArguments(tree
084: .getArguments());
085: NewClassTree nju = make.NewClass(tree
086: .getEnclosingExpression(),
087: (List<ExpressionTree>) tree
088: .getTypeArguments(), tree
089: .getIdentifier(), arguments, tree
090: .getClassBody());
091: rewrite(tree, nju);
092: }
093: }
094: }
095: return super .visitNewClass(tree, p);
096: }
097:
098: private List<ExpressionTree> getNewArguments(
099: List<? extends ExpressionTree> currentArguments) {
100: List<ExpressionTree> arguments = new ArrayList();
101: ParameterInfo[] pi = refactoring.getParameterInfo();
102: for (int i = 0; i < pi.length; i++) {
103: int originalIndex = pi[i].getOriginalIndex();
104: ExpressionTree vt;
105: if (originalIndex < 0) {
106: String value = pi[i].getDefaultValue();
107: SourcePositions pos[] = new SourcePositions[1];
108: vt = workingCopy.getTreeUtilities().parseExpression(
109: value, pos);
110: } else {
111: vt = currentArguments.get(pi[i].getOriginalIndex());
112: }
113: arguments.add(vt);
114: }
115: return arguments;
116: }
117:
118: public Tree visitMethodInvocation(MethodInvocationTree tree,
119: Element p) {
120: if (!workingCopy.getTreeUtilities().isSynthetic(
121: getCurrentPath())) {
122: Element el = workingCopy.getTrees().getElement(
123: getCurrentPath());
124: if (el != null) {
125: if (isMethodMatch(el)) {
126: List<ExpressionTree> arguments = getNewArguments(tree
127: .getArguments());
128:
129: MethodInvocationTree nju = make.MethodInvocation(
130: (List<ExpressionTree>) tree
131: .getTypeArguments(), tree
132: .getMethodSelect(), arguments);
133: rewrite(tree, nju);
134: }
135: }
136: }
137: return super .visitMethodInvocation(tree, p);
138: }
139:
140: @Override
141: public Tree visitMethod(MethodTree tree, Element p) {
142: renameDeclIfMatch(getCurrentPath(), tree, p);
143: return super .visitMethod(tree, p);
144: }
145:
146: ChangeParametersRefactoring refactoring;
147:
148: private void renameDeclIfMatch(TreePath path, Tree tree,
149: Element elementToFind) {
150: if (workingCopy.getTreeUtilities().isSynthetic(path))
151: return;
152: MethodTree current = (MethodTree) tree;
153: Element el = workingCopy.getTrees().getElement(path);
154: if (isMethodMatch(el)) {
155:
156: List<? extends VariableTree> currentParameters = current
157: .getParameters();
158: List<VariableTree> newParameters = new ArrayList();
159:
160: ParameterInfo[] p = refactoring.getParameterInfo();
161: for (int i = 0; i < p.length; i++) {
162: int originalIndex = p[i].getOriginalIndex();
163: VariableTree vt;
164: if (originalIndex < 0) {
165: vt = make.Variable(make.Modifiers(Collections
166: .<Modifier> emptySet()), p[i].getName(),
167: make.Identifier(p[i].getType()), null);
168: } else {
169: vt = currentParameters.get(p[i].getOriginalIndex());
170: }
171: newParameters.add(vt);
172: }
173: Set<Modifier> modifiers = new HashSet(refactoring
174: .getModifiers());
175: if (!el.getModifiers().contains(Modifier.ABSTRACT)) {
176: modifiers.remove(Modifier.ABSTRACT);
177: }
178: ClassTree enclosingClass = (ClassTree) workingCopy
179: .getTrees().getTree(el.getEnclosingElement());
180: if (workingCopy.getTreeUtilities().isInterface(
181: enclosingClass))
182: modifiers.remove(Modifier.ABSTRACT);
183: MethodTree nju = make.Method(make.Modifiers(modifiers,
184: current.getModifiers().getAnnotations()), current
185: .getName(), current.getReturnType(), current
186: .getTypeParameters(), newParameters, current
187: .getThrows(), current.getBody(),
188: (ExpressionTree) current.getDefaultValue());
189: rewrite(tree, nju);
190: return;
191: }
192: }
193:
194: private boolean isMethodMatch(Element method) {
195: if ((method.getKind() == ElementKind.METHOD || method.getKind() == ElementKind.CONSTRUCTOR)
196: && allMethods != null) {
197: for (ElementHandle<ExecutableElement> mh : allMethods) {
198: ExecutableElement baseMethod = mh.resolve(workingCopy);
199: if (baseMethod == null) {
200: Logger.getLogger(
201: "org.netbeans.modules.refactoring.java")
202: .info(
203: "ChangeParamsTransformer cannot resolve "
204: + mh);
205: continue;
206: }
207: if (baseMethod.equals(method)
208: || workingCopy
209: .getElements()
210: .overrides(
211: (ExecutableElement) method,
212: baseMethod,
213: SourceUtils
214: .getEnclosingTypeElement(baseMethod))) {
215: return true;
216: }
217: }
218: }
219: return false;
220: }
221: }
|