1 11 package org.eclipse.jdt.internal.core; 12 13 import org.eclipse.jdt.core.*; 14 import org.eclipse.jdt.core.compiler.CharOperation; 15 import org.eclipse.jdt.internal.compiler.lookup.Binding; 16 import org.eclipse.jdt.internal.core.util.Util; 17 18 21 22 public class SourceMethod extends NamedMember implements IMethod { 23 24 29 protected String [] parameterTypes; 30 31 protected SourceMethod(JavaElement parent, String name, String [] parameterTypes) { 32 super(parent, name); 33 if (parameterTypes == null) { 36 this.parameterTypes= CharOperation.NO_STRINGS; 37 } else { 38 this.parameterTypes= parameterTypes; 39 } 40 } 41 protected void closing(Object info) throws JavaModelException { 42 super.closing(info); 43 SourceMethodElementInfo elementInfo = (SourceMethodElementInfo) info; 44 ITypeParameter[] typeParameters = elementInfo.typeParameters; 45 for (int i = 0, length = typeParameters.length; i < length; i++) { 46 ((TypeParameter) typeParameters[i]).close(); 47 } 48 } 49 public boolean equals(Object o) { 50 if (!(o instanceof SourceMethod)) return false; 51 return super.equals(o) && Util.equalArraysOrNull(this.parameterTypes, ((SourceMethod)o).parameterTypes); 52 } 53 56 public int getElementType() { 57 return METHOD; 58 } 59 62 public String [] getExceptionTypes() throws JavaModelException { 63 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo(); 64 char[][] exs= info.getExceptionTypeNames(); 65 return CompilationUnitStructureRequestor.convertTypeNamesToSigs(exs); 66 } 67 70 protected void getHandleMemento(StringBuffer buff) { 71 ((JavaElement) getParent()).getHandleMemento(buff); 72 char delimiter = getHandleMementoDelimiter(); 73 buff.append(delimiter); 74 escapeMementoName(buff, getElementName()); 75 for (int i = 0; i < this.parameterTypes.length; i++) { 76 buff.append(delimiter); 77 escapeMementoName(buff, this.parameterTypes[i]); 78 } 79 if (this.occurrenceCount > 1) { 80 buff.append(JEM_COUNT); 81 buff.append(this.occurrenceCount); 82 } 83 } 84 87 protected char getHandleMementoDelimiter() { 88 return JavaElement.JEM_METHOD; 89 } 90 93 public String getKey() { 94 try { 95 return getKey(this, false); 96 } catch (JavaModelException e) { 97 return null; 99 } 100 } 101 104 public int getNumberOfParameters() { 105 return this.parameterTypes == null ? 0 : this.parameterTypes.length; 106 } 107 110 public String [] getParameterNames() throws JavaModelException { 111 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo(); 112 char[][] names= info.getArgumentNames(); 113 return CharOperation.toStrings(names); 114 } 115 118 public String [] getParameterTypes() { 119 return this.parameterTypes; 120 } 121 122 public ITypeParameter getTypeParameter(String typeParameterName) { 123 return new TypeParameter(this, typeParameterName); 124 } 125 126 public ITypeParameter[] getTypeParameters() throws JavaModelException { 127 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo(); 128 return info.typeParameters; 129 } 130 131 136 public String [] getTypeParameterSignatures() throws JavaModelException { 137 ITypeParameter[] typeParameters = getTypeParameters(); 138 int length = typeParameters.length; 139 String [] typeParameterSignatures = new String [length]; 140 for (int i = 0; i < length; i++) { 141 TypeParameter typeParameter = (TypeParameter) typeParameters[i]; 142 TypeParameterElementInfo info = (TypeParameterElementInfo) typeParameter.getElementInfo(); 143 char[][] bounds = info.bounds; 144 if (bounds == null) { 145 typeParameterSignatures[i] = Signature.createTypeParameterSignature(typeParameter.getElementName(), CharOperation.NO_STRINGS); 146 } else { 147 int boundsLength = bounds.length; 148 char[][] boundSignatures = new char[boundsLength][]; 149 for (int j = 0; j < boundsLength; j++) { 150 boundSignatures[j] = Signature.createCharArrayTypeSignature(bounds[j], false); 151 } 152 typeParameterSignatures[i] = new String (Signature.createTypeParameterSignature(typeParameter.getElementName().toCharArray(), boundSignatures)); 153 } 154 } 155 return typeParameterSignatures; 156 } 157 158 161 public IJavaElement getPrimaryElement(boolean checkOwner) { 162 if (checkOwner) { 163 CompilationUnit cu = (CompilationUnit)getAncestor(COMPILATION_UNIT); 164 if (cu.isPrimary()) return this; 165 } 166 IJavaElement primaryParent = this.parent.getPrimaryElement(false); 167 return ((IType)primaryParent).getMethod(this.name, this.parameterTypes); 168 } 169 public String [] getRawParameterNames() throws JavaModelException { 170 return getParameterNames(); 171 } 172 175 public String getReturnType() throws JavaModelException { 176 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo(); 177 return Signature.createTypeSignature(info.getReturnTypeName(), false); 178 } 179 182 public String getSignature() throws JavaModelException { 183 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo(); 184 return Signature.createMethodSignature(this.parameterTypes, Signature.createTypeSignature(info.getReturnTypeName(), false)); 185 } 186 189 public int hashCode() { 190 int hash = super.hashCode(); 191 for (int i = 0, length = this.parameterTypes.length; i < length; i++) { 192 hash = Util.combineHashCodes(hash, this.parameterTypes[i].hashCode()); 193 } 194 return hash; 195 } 196 199 public boolean isConstructor() throws JavaModelException { 200 if (!this.getElementName().equals(this.parent.getElementName())) { 201 return false; 203 } 204 SourceMethodElementInfo info = (SourceMethodElementInfo) getElementInfo(); 205 return info.isConstructor(); 206 } 207 210 public boolean isMainMethod() throws JavaModelException { 211 return this.isMainMethod(this); 212 } 213 216 public boolean isResolved() { 217 return false; 218 } 219 222 public boolean isSimilar(IMethod method) { 223 return 224 areSimilarMethods( 225 this.getElementName(), this.getParameterTypes(), 226 method.getElementName(), method.getParameterTypes(), 227 null); 228 } 229 230 232 public String readableName() { 233 234 StringBuffer buffer = new StringBuffer (super.readableName()); 235 buffer.append('('); 236 int length; 237 if (this.parameterTypes != null && (length = this.parameterTypes.length) > 0) { 238 for (int i = 0; i < length; i++) { 239 buffer.append(Signature.toString(this.parameterTypes[i])); 240 if (i < length - 1) { 241 buffer.append(", "); } 243 } 244 } 245 buffer.append(')'); 246 return buffer.toString(); 247 } 248 public JavaElement resolved(Binding binding) { 249 SourceRefElement resolvedHandle = new ResolvedSourceMethod(this.parent, this.name, this.parameterTypes, new String (binding.computeUniqueKey())); 250 resolvedHandle.occurrenceCount = this.occurrenceCount; 251 return resolvedHandle; 252 } 253 256 protected void toStringInfo(int tab, StringBuffer buffer, Object info, boolean showResolvedInfo) { 257 buffer.append(tabString(tab)); 258 if (info == null) { 259 toStringName(buffer); 260 buffer.append(" (not open)"); } else if (info == NO_INFO) { 262 toStringName(buffer); 263 } else { 264 SourceMethodElementInfo methodInfo = (SourceMethodElementInfo) info; 265 int flags = methodInfo.getModifiers(); 266 if (Flags.isStatic(flags)) { 267 buffer.append("static "); } 269 if (!methodInfo.isConstructor()) { 270 buffer.append(methodInfo.getReturnTypeName()); 271 buffer.append(' '); 272 } 273 toStringName(buffer, flags); 274 } 275 } 276 protected void toStringName(StringBuffer buffer) { 277 toStringName(buffer, 0); 278 } 279 protected void toStringName(StringBuffer buffer, int flags) { 280 buffer.append(getElementName()); 281 buffer.append('('); 282 String [] parameters = getParameterTypes(); 283 int length; 284 if (parameters != null && (length = parameters.length) > 0) { 285 boolean isVarargs = Flags.isVarargs(flags); 286 for (int i = 0; i < length; i++) { 287 try { 288 if (i < length - 1) { 289 buffer.append(Signature.toString(parameters[i])); 290 buffer.append(", "); } else if (isVarargs) { 292 String parameter = parameters[i].substring(1); 294 buffer.append(Signature.toString(parameter)); 295 buffer.append(" ..."); } else { 297 buffer.append(Signature.toString(parameters[i])); 298 } 299 } catch (IllegalArgumentException e) { 300 buffer.append("*** invalid signature: "); buffer.append(parameters[i]); 303 } 304 } 305 } 306 buffer.append(')'); 307 if (this.occurrenceCount > 1) { 308 buffer.append("#"); buffer.append(this.occurrenceCount); 310 } 311 } 312 } 313 | Popular Tags |