KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > ParameterInfo


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jdt.core.dom.ITypeBinding;
16 import org.eclipse.jdt.core.dom.IVariableBinding;
17
18
19 public class ParameterInfo {
20     
21     public static final int INDEX_FOR_ADDED= -1;
22     public static final String JavaDoc ELLIPSIS= "..."; //$NON-NLS-1$
23

24     private IVariableBinding fOldBinding;
25     private ITypeBinding fOldTypeBinding;
26     private final String JavaDoc fOldName;
27     private final String JavaDoc fOldTypeName;
28     private final int fOldIndex;
29
30     private String JavaDoc fNewTypeName;
31     private ITypeBinding fNewTypeBinding;
32     private String JavaDoc fDefaultValue;
33     private String JavaDoc fNewName;
34     private boolean fIsDeleted;
35     private boolean fCreateField=true;
36     private boolean fInlined;
37     private boolean fResolve= true;
38     
39     public ParameterInfo(String JavaDoc type, String JavaDoc name, int index) {
40         this(null, null, type, name, index);
41     }
42
43     public ParameterInfo(IVariableBinding binding, String JavaDoc type, String JavaDoc name, int index) {
44         this(binding, null, type, name, index);
45     }
46     
47     private ParameterInfo(IVariableBinding binding, ITypeBinding typeBinding, String JavaDoc type, String JavaDoc name, int index) {
48         fOldBinding= binding;
49         fOldTypeBinding= typeBinding;
50         fNewTypeBinding= typeBinding;
51         fOldTypeName= type;
52         fNewTypeName= type;
53         fOldName= name;
54         fNewName= name;
55         fOldIndex= index;
56         fDefaultValue= ""; //$NON-NLS-1$
57
fIsDeleted= false;
58     }
59
60     
61     /**
62      * Creates a new ParameterInfo. Parameter is marked as added and not resolvable
63      * @param type the fullyqualified type
64      * @param name the name
65      * @return the parameter info object
66      */

67     public static ParameterInfo createInfoForAddedParameter(String JavaDoc type, String JavaDoc name) {
68         ParameterInfo info= new ParameterInfo("", "", INDEX_FOR_ADDED); //$NON-NLS-1$ //$NON-NLS-2$
69
info.setNewTypeName(type);
70         info.setNewName(name);
71         info.setResolve(false);
72         return info;
73     }
74     
75     private void setResolve(boolean resolve) {
76         fResolve= resolve;
77     }
78
79     public static ParameterInfo createInfoForAddedParameter(String JavaDoc type, String JavaDoc name, String JavaDoc defaultValue) {
80         ParameterInfo info= new ParameterInfo("", "", INDEX_FOR_ADDED); //$NON-NLS-1$ //$NON-NLS-2$
81
info.setNewTypeName(type);
82         info.setNewName(name);
83         info.setDefaultValue(defaultValue);
84         return info;
85     }
86     
87     public static ParameterInfo createInfoForAddedParameter(ITypeBinding typeBinding, String JavaDoc type, String JavaDoc name, String JavaDoc defaultValue) {
88         ParameterInfo info= new ParameterInfo(null, typeBinding, "", "", INDEX_FOR_ADDED); //$NON-NLS-1$ //$NON-NLS-2$
89
info.setNewTypeName(type);
90         info.setNewName(name);
91         info.setDefaultValue(defaultValue);
92         return info;
93     }
94     
95     public int getOldIndex() {
96         return fOldIndex;
97     }
98     
99     public boolean isDeleted(){
100         return fIsDeleted;
101     }
102     
103     public void markAsDeleted(){
104         Assert.isTrue(! isAdded());//added param infos should be simply removed from the list
105
fIsDeleted= true;
106     }
107     
108     public boolean isAdded(){
109         return fOldIndex == INDEX_FOR_ADDED;
110     }
111     
112     public boolean isTypeNameChanged() {
113         return !fOldTypeName.equals(fNewTypeName);
114     }
115     
116     public boolean isRenamed() {
117         return !fOldName.equals(fNewName);
118     }
119     
120     public boolean isVarargChanged() {
121         return isOldVarargs() != isNewVarargs();
122     }
123     
124     public IVariableBinding getOldBinding() {
125         return fOldBinding;
126     }
127
128     public String JavaDoc getOldTypeName() {
129         return fOldTypeName;
130     }
131     
132     public String JavaDoc getNewTypeName() {
133         return fNewTypeName;
134     }
135     
136     public void setNewTypeName(String JavaDoc type){
137         Assert.isNotNull(type);
138         fNewTypeName= type;
139     }
140
141     public ITypeBinding getNewTypeBinding() {
142         return fNewTypeBinding;
143     }
144     
145     public void setNewTypeBinding(ITypeBinding typeBinding){
146         //TODO: TypeContextChecker should not resolve such a parameter at all.
147
// That would also make IProblemVerifier obsolete.
148
if (fResolve)
149             fNewTypeBinding= typeBinding;
150     }
151
152     public boolean isOldVarargs() {
153         return isVarargs(fOldTypeName);
154     }
155     
156     public boolean isNewVarargs() {
157         return isVarargs(fNewTypeName);
158     }
159     
160     public String JavaDoc getOldName() {
161         return fOldName;
162     }
163
164     public String JavaDoc getNewName() {
165         return fNewName;
166     }
167     
168     public void setNewName(String JavaDoc newName) {
169         Assert.isNotNull(newName);
170         fNewName= newName;
171     }
172
173     public String JavaDoc getDefaultValue(){
174         return fDefaultValue;
175     }
176     
177     public void setDefaultValue(String JavaDoc value){
178         Assert.isNotNull(value);
179         fDefaultValue= value;
180     }
181
182     public String JavaDoc toString() {
183         return fOldTypeName + " " + fOldName + " @" + fOldIndex + " -> " //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
184
+ fNewTypeName + " " + fNewName + ": " + fDefaultValue //$NON-NLS-1$//$NON-NLS-2$
185
+ (fIsDeleted ? " (deleted)" : " (stays)"); //$NON-NLS-1$//$NON-NLS-2$
186
}
187     
188     public static String JavaDoc stripEllipsis(String JavaDoc typeName) {
189         if (isVarargs(typeName))
190             return typeName.substring(0, typeName.length() - 3);
191         else
192             return typeName;
193     }
194     
195     public static boolean isVarargs(String JavaDoc typeName) {
196         return typeName.endsWith("..."); //$NON-NLS-1$
197
}
198
199     public ITypeBinding getOldTypeBinding() {
200         return fOldTypeBinding;
201     }
202
203     public boolean isCreateField() {
204         return fCreateField;
205     }
206
207     public void setCreateField(boolean createField) {
208         fIsDeleted= createField;
209         fCreateField= createField;
210     }
211
212     public void setOldBinding(IVariableBinding binding) {
213         //TODO: remove setter again ( https://bugs.eclipse.org/bugs/show_bug.cgi?id=102287 )
214
fOldBinding=binding;
215         fOldTypeBinding=binding.getType();
216         fNewTypeBinding=binding.getType();
217     }
218
219     public void setInlined(boolean inlined) {
220         fInlined=inlined;
221     }
222     
223     public boolean isInlined() {
224         return fInlined;
225     }
226
227 }
228
Popular Tags