1 11 12 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints; 13 14 import java.util.HashMap ; 15 import java.util.Hashtable ; 16 import java.util.Iterator ; 17 import java.util.Map ; 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 37 public class ConstraintVariableFactory implements IConstraintVariableFactory { 38 39 private Map fBindingMap= new HashMap (); 40 41 private Map fExpressionMap= new Hashtable (); 42 private Map fLiteralMap= new HashMap (); 43 private Map fTypeVariableMap= new HashMap (); 44 private Map fDeclaringTypeVariableMap= new HashMap (); 45 private Map fParameterMap= new HashMap (); 46 private Map fRawBindingMap= new HashMap (); 47 private Map fReturnVariableMap= new HashMap (); 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 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 nodeType= new Integer (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 ExpressionVariable ev; 87 Object 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 private IBinding getKey(IBinding binding) { 112 if (fBindingMap.containsKey(binding)){ 113 return (IBinding)fBindingMap.get(binding); 114 } else { 115 for (Iterator 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 130 public DeclaringTypeVariable makeDeclaringTypeVariable(ITypeBinding memberTypeBinding) { 131 String 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 144 public DeclaringTypeVariable makeDeclaringTypeVariable(IVariableBinding fieldBinding) { 145 String 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 159 public DeclaringTypeVariable makeDeclaringTypeVariable(IMethodBinding methodBinding) { 160 String 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 174 public ParameterTypeVariable makeParameterTypeVariable(IMethodBinding methodBinding, 175 int parameterIndex) { 176 String 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 190 public RawBindingVariable makeRawBindingVariable(ITypeBinding binding) { 191 String 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 205 public ReturnTypeVariable makeReturnTypeVariable(ReturnStatement returnStatement) { 206 return makeReturnTypeVariable(ReturnTypeVariable.getMethod(returnStatement).resolveBinding()); 207 } 208 209 212 public ReturnTypeVariable makeReturnTypeVariable(IMethodBinding methodBinding) { 213 String 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 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 244 public TypeVariable makeTypeVariable(ITypeBinding binding, String 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); } 258 259 } 260 | Popular Tags |