KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > typeconstraints > ConstraintVariableFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.dom.Expression;
23 import org.eclipse.jdt.core.dom.IBinding;
24 import org.eclipse.jdt.core.dom.IMethodBinding;
25 import org.eclipse.jdt.core.dom.ITypeBinding;
26 import org.eclipse.jdt.core.dom.IVariableBinding;
27 import org.eclipse.jdt.core.dom.ReturnStatement;
28 import org.eclipse.jdt.core.dom.Type;
29 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
30 import org.eclipse.jdt.internal.corext.dom.Bindings;
31
32 /**
33  * Abstract Factory for the creation of ConstraintVariables. This default factory
34  * ensures that no duplicate ConstraintVariables are created, so that Object.equals()
35  * can be used to compare ConstraintVariables.
36  */

37 public class ConstraintVariableFactory implements IConstraintVariableFactory {
38
39     private Map JavaDoc/*<IBinding,IBinding>*/ fBindingMap= new HashMap JavaDoc();
40
41     private Map JavaDoc/*<IBinding + CompilationUnitRange,ExpressionVariable>*/ fExpressionMap= new Hashtable JavaDoc();
42     private Map JavaDoc/*<Integer,ExpressionVariable>*/ fLiteralMap= new HashMap JavaDoc();
43     private Map JavaDoc/*<CompilationUnitRange,TypeVariable>*/ fTypeVariableMap= new HashMap JavaDoc();
44     private Map JavaDoc/*<String,DeclaringTypeVariable>*/ fDeclaringTypeVariableMap= new HashMap JavaDoc();
45     private Map JavaDoc/*<String,ParameterTypeVariable>*/ fParameterMap= new HashMap JavaDoc();
46     private Map JavaDoc/*<String,RawBindingVariable>*/ fRawBindingMap= new HashMap JavaDoc();
47     private Map JavaDoc/*<String,ReturnTypeVariable>*/ fReturnVariableMap= new HashMap JavaDoc();
48     
49     public static final boolean REPORT= false;
50     protected int nrCreated=0;
51     protected int nrRetrieved=0;
52     
53     public int getNumCreated(){
54         return nrCreated;
55     }
56     
57     /* (non-Javadoc)
58      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.IConstraintVariableFactory#makeExpressionVariable(org.eclipse.jdt.core.dom.Expression, org.eclipse.jdt.internal.corext.refactoring.typeconstraints.IContext)
59      */

60     public ConstraintVariable makeExpressionOrTypeVariable(Expression expression,
61                                                            IContext context) {
62         IBinding binding= ExpressionVariable.resolveBinding(expression);
63         
64         if (binding instanceof ITypeBinding){
65             ICompilationUnit cu= ASTCreator.getCu(expression);
66             Assert.isNotNull(cu);
67             CompilationUnitRange range= new CompilationUnitRange(cu, expression);
68             return makeTypeVariable((ITypeBinding)getKey(binding), expression.toString(), range);
69         }
70         
71         if (ASTNodes.isLiteral(expression)){
72             Integer JavaDoc nodeType= new Integer JavaDoc(expression.getNodeType());
73             if (! fLiteralMap.containsKey(nodeType)){
74                 fLiteralMap.put(nodeType, new ExpressionVariable(expression));
75                 if (REPORT) nrCreated++;
76             } else {
77                 if (REPORT) nrRetrieved++;
78             }
79             if (REPORT) dumpConstraintStats();
80             return (ExpressionVariable) fLiteralMap.get(nodeType);
81         }
82             
83         // For ExpressionVariables, there are two cases. If the expression has a binding
84
// we use that as the key. Otherwise, we use the CompilationUnitRange. See
85
// also ExpressionVariable.equals()
86
ExpressionVariable ev;
87         Object JavaDoc key;
88         if (binding != null){
89             key= getKey(binding);
90         } else {
91             key= new CompilationUnitRange(ASTCreator.getCu(expression), expression);
92         }
93         ev= (ExpressionVariable)fExpressionMap.get(key);
94         
95         if (ev != null){
96             if (REPORT) nrRetrieved++;
97         } else {
98             ev= new ExpressionVariable(expression);
99             fExpressionMap.put(key, ev);
100             if (REPORT) nrCreated++;
101             if (REPORT) dumpConstraintStats();
102         }
103         return ev;
104     }
105     
106     
107     //
108
// The method IBinding.equals() does not have the desired behavior, and Bindings.equals()
109
// must be used to compare bindings. We use an additional layer of Hashing.
110
//
111
private IBinding getKey(IBinding binding) {
112         if (fBindingMap.containsKey(binding)){
113             return (IBinding)fBindingMap.get(binding);
114         } else {
115             for (Iterator JavaDoc it= fBindingMap.keySet().iterator(); it.hasNext(); ){
116                 IBinding b2= (IBinding)it.next();
117                 if (Bindings.equals(binding, b2)){
118                     fBindingMap.put(binding, b2);
119                     return b2;
120                 }
121             }
122             fBindingMap.put(binding, binding);
123             return binding;
124         }
125     }
126
127     /* (non-Javadoc)
128      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.IConstraintVariableFactory#makeDeclaringTypeVariable(org.eclipse.jdt.core.dom.ITypeBinding)
129      */

130     public DeclaringTypeVariable makeDeclaringTypeVariable(ITypeBinding memberTypeBinding) {
131         String JavaDoc key = memberTypeBinding.getKey();
132         if (! fDeclaringTypeVariableMap.containsKey(key)){
133             fDeclaringTypeVariableMap.put(key, new DeclaringTypeVariable(memberTypeBinding));
134             if (REPORT) nrCreated++;
135         } else {
136             if (REPORT) nrRetrieved++;
137         }
138         if (REPORT) dumpConstraintStats();
139         return (DeclaringTypeVariable)fDeclaringTypeVariableMap.get(key);
140     }
141     /* (non-Javadoc)
142      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.IConstraintVariableFactory#makeDeclaringTypeVariable(org.eclipse.jdt.core.dom.IVariableBinding)
143      */

144     public DeclaringTypeVariable makeDeclaringTypeVariable(IVariableBinding fieldBinding) {
145         String JavaDoc key= fieldBinding.getKey();
146         if (! fDeclaringTypeVariableMap.containsKey(key)){
147             fDeclaringTypeVariableMap.put(key, new DeclaringTypeVariable(fieldBinding));
148             if (REPORT) nrCreated++;
149         } else {
150             if (REPORT) nrRetrieved++;
151         }
152         if (REPORT) dumpConstraintStats();
153         return (DeclaringTypeVariable)fDeclaringTypeVariableMap.get(key);
154     }
155
156     /* (non-Javadoc)
157      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.IConstraintVariableFactory#makeDeclaringTypeVariable(org.eclipse.jdt.core.dom.IMethodBinding)
158      */

159     public DeclaringTypeVariable makeDeclaringTypeVariable(IMethodBinding methodBinding) {
160         String JavaDoc key= methodBinding.getKey();
161         if (! fDeclaringTypeVariableMap.containsKey(key)){
162             fDeclaringTypeVariableMap.put(key, new DeclaringTypeVariable(methodBinding));
163             if (REPORT) nrCreated++;
164         } else {
165             if (REPORT) nrRetrieved++;
166         }
167         if (REPORT) dumpConstraintStats();
168         return (DeclaringTypeVariable)fDeclaringTypeVariableMap.get(key);
169     }
170     
171     /* (non-Javadoc)
172      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.IConstraintVariableFactory#makeParameterTypeVariable(org.eclipse.jdt.core.dom.IMethodBinding, int)
173      */

174     public ParameterTypeVariable makeParameterTypeVariable(IMethodBinding methodBinding,
175                                                            int parameterIndex) {
176         String JavaDoc key= methodBinding.getKey() + parameterIndex;
177         if (! fParameterMap.containsKey(key)){
178             fParameterMap.put(key, new ParameterTypeVariable(methodBinding, parameterIndex));
179             if (REPORT) nrCreated++;
180         } else {
181             if (REPORT) nrRetrieved++;
182         }
183         if (REPORT) dumpConstraintStats();
184         return (ParameterTypeVariable)fParameterMap.get(key);
185     }
186
187     /* (non-Javadoc)
188      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.IConstraintVariableFactory#makeRawBindingVariable(org.eclipse.jdt.core.dom.ITypeBinding)
189      */

190     public RawBindingVariable makeRawBindingVariable(ITypeBinding binding) {
191         String JavaDoc key = binding.getKey();
192         if (! fRawBindingMap.containsKey(key)){
193             fRawBindingMap.put(key, new RawBindingVariable(binding));
194             if (REPORT) nrCreated++;
195         } else {
196             if (REPORT) nrRetrieved++;
197         }
198         if (REPORT) dumpConstraintStats();
199         return (RawBindingVariable)fRawBindingMap.get(key);
200     }
201
202     /* (non-Javadoc)
203      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.IConstraintVariableFactory#makeReturnTypeVariable(org.eclipse.jdt.core.dom.ReturnStatement)
204      */

205     public ReturnTypeVariable makeReturnTypeVariable(ReturnStatement returnStatement) {
206         return makeReturnTypeVariable(ReturnTypeVariable.getMethod(returnStatement).resolveBinding());
207     }
208
209     /* (non-Javadoc)
210      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.IConstraintVariableFactory#makeReturnTypeVariable(org.eclipse.jdt.core.dom.IMethodBinding)
211      */

212     public ReturnTypeVariable makeReturnTypeVariable(IMethodBinding methodBinding) {
213         String JavaDoc key= methodBinding.getKey();
214         if (!fReturnVariableMap.containsKey(key)){
215             fReturnVariableMap.put(key, new ReturnTypeVariable(methodBinding));
216             if (REPORT) nrCreated++;
217         } else {
218             if (REPORT) nrRetrieved++;
219         }
220         if (REPORT) dumpConstraintStats();
221         return (ReturnTypeVariable)fReturnVariableMap.get(key);
222     }
223
224     /* (non-Javadoc)
225      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.IConstraintVariableFactory#makeTypeVariable(org.eclipse.jdt.core.dom.Type)
226      */

227     public TypeVariable makeTypeVariable(Type type) {
228         ICompilationUnit cu= ASTCreator.getCu(type);
229         Assert.isNotNull(cu);
230         CompilationUnitRange range= new CompilationUnitRange(cu, type);
231         if (! fTypeVariableMap.containsKey(range)){
232             fTypeVariableMap.put(range, new TypeVariable(type));
233             if (REPORT) nrCreated++;
234         } else {
235             if (REPORT) nrRetrieved++;
236         }
237         if (REPORT) dumpConstraintStats();
238         return (TypeVariable) fTypeVariableMap.get(range);
239     }
240
241     /* (non-Javadoc)
242      * @see org.eclipse.jdt.internal.corext.refactoring.typeconstraints.IConstraintVariableFactory#makeTypeVariable(org.eclipse.jdt.core.dom.ITypeBinding, java.lang.String, org.eclipse.jdt.internal.corext.refactoring.typeconstraints.CompilationUnitRange)
243      */

244     public TypeVariable makeTypeVariable(ITypeBinding binding, String JavaDoc source, CompilationUnitRange range) {
245         if (! fTypeVariableMap.containsKey(range)){
246             fTypeVariableMap.put(range, new TypeVariable(binding, source, range));
247             if (REPORT) nrCreated++;
248         } else {
249             if (REPORT) nrRetrieved++;
250         }
251         if (REPORT) dumpConstraintStats();
252         return (TypeVariable) fTypeVariableMap.get(range);
253     }
254
255     protected void dumpConstraintStats() {
256         System.out.println("created: " + nrCreated + ", retrieved: " + nrRetrieved); //$NON-NLS-1$ //$NON-NLS-2$
257
}
258
259 }
260
Popular Tags