1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 16 import org.eclipse.jdt.core.*; 17 import org.eclipse.jdt.core.compiler.CharOperation; 18 import org.eclipse.jdt.core.compiler.IScanner; 19 import org.eclipse.jdt.core.compiler.ITerminalSymbols; 20 import org.eclipse.jdt.core.compiler.InvalidInputException; 21 import org.eclipse.jdt.internal.compiler.impl.Constant; 22 import org.eclipse.jdt.internal.compiler.lookup.TypeIds; 23 import org.eclipse.jdt.internal.core.util.MementoTokenizer; 24 25 28 29 public abstract class Member extends SourceRefElement implements IMember { 30 31 protected Member(JavaElement parent) { 32 super(parent); 33 } 34 protected static boolean areSimilarMethods( 35 String name1, String [] params1, 36 String name2, String [] params2, 37 String [] simpleNames1) { 38 39 if (name1.equals(name2)) { 40 int params1Length = params1.length; 41 if (params1Length == params2.length) { 42 for (int i = 0; i < params1Length; i++) { 43 String simpleName1 = 44 simpleNames1 == null ? 45 Signature.getSimpleName(Signature.toString(Signature.getTypeErasure(params1[i]))) : 46 simpleNames1[i]; 47 String simpleName2 = Signature.getSimpleName(Signature.toString(Signature.getTypeErasure(params2[i]))); 48 if (!simpleName1.equals(simpleName2)) { 49 return false; 50 } 51 } 52 return true; 53 } 54 } 55 return false; 56 } 57 61 protected static Object convertConstant(Constant constant) { 62 if (constant == null) 63 return null; 64 if (constant == Constant.NotAConstant) { 65 return null; 66 } 67 switch (constant.typeID()) { 68 case TypeIds.T_boolean : 69 return constant.booleanValue() ? Boolean.TRUE : Boolean.FALSE; 70 case TypeIds.T_byte : 71 return new Byte (constant.byteValue()); 72 case TypeIds.T_char : 73 return new Character (constant.charValue()); 74 case TypeIds.T_double : 75 return new Double (constant.doubleValue()); 76 case TypeIds.T_float : 77 return new Float (constant.floatValue()); 78 case TypeIds.T_int : 79 return new Integer (constant.intValue()); 80 case TypeIds.T_long : 81 return new Long (constant.longValue()); 82 case TypeIds.T_short : 83 return new Short (constant.shortValue()); 84 case TypeIds.T_JavaLangString : 85 return constant.stringValue(); 86 default : 87 return null; 88 } 89 } 90 93 public static IMethod[] findMethods(IMethod method, IMethod[] methods) { 94 String elementName = method.getElementName(); 95 String [] parameters = method.getParameterTypes(); 96 int paramLength = parameters.length; 97 String [] simpleNames = new String [paramLength]; 98 for (int i = 0; i < paramLength; i++) { 99 String erasure = Signature.getTypeErasure(parameters[i]); 100 simpleNames[i] = Signature.getSimpleName(Signature.toString(erasure)); 101 } 102 ArrayList list = new ArrayList (); 103 for (int i = 0, length = methods.length; i < length; i++) { 104 IMethod existingMethod = methods[i]; 105 if (areSimilarMethods( 106 elementName, 107 parameters, 108 existingMethod.getElementName(), 109 existingMethod.getParameterTypes(), 110 simpleNames)) { 111 list.add(existingMethod); 112 } 113 } 114 int size = list.size(); 115 if (size == 0) { 116 return null; 117 } else { 118 IMethod[] result = new IMethod[size]; 119 list.toArray(result); 120 return result; 121 } 122 } 123 public String [] getCategories() throws JavaModelException { 124 IType type = (IType) getAncestor(IJavaElement.TYPE); 125 if (type == null) return CharOperation.NO_STRINGS; 126 if (type.isBinary()) { 127 return CharOperation.NO_STRINGS; 128 } else { 129 SourceTypeElementInfo info = (SourceTypeElementInfo) ((SourceType) type).getElementInfo(); 130 HashMap map = info.getCategories(); 131 if (map == null) return CharOperation.NO_STRINGS; 132 String [] categories = (String []) map.get(this); 133 if (categories == null) return CharOperation.NO_STRINGS; 134 return categories; 135 } 136 } 137 140 public IClassFile getClassFile() { 141 IJavaElement element = getParent(); 142 while (element instanceof IMember) { 143 element= element.getParent(); 144 } 145 if (element instanceof IClassFile) { 146 return (IClassFile) element; 147 } 148 return null; 149 } 150 153 public IType getDeclaringType() { 154 JavaElement parentElement = (JavaElement)getParent(); 155 if (parentElement.getElementType() == TYPE) { 156 return (IType) parentElement; 157 } 158 return null; 159 } 160 163 public int getFlags() throws JavaModelException { 164 MemberElementInfo info = (MemberElementInfo) getElementInfo(); 165 return info.getModifiers(); 166 } 167 170 public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) { 171 switch (token.charAt(0)) { 172 case JEM_COUNT: 173 return getHandleUpdatingCountFromMemento(memento, workingCopyOwner); 174 case JEM_TYPE: 175 String typeName; 176 if (memento.hasMoreTokens()) { 177 typeName = memento.nextToken(); 178 char firstChar = typeName.charAt(0); 179 if (firstChar == JEM_FIELD || firstChar == JEM_INITIALIZER || firstChar == JEM_METHOD || firstChar == JEM_TYPE || firstChar == JEM_COUNT) { 180 token = typeName; 181 typeName = ""; } else { 183 token = null; 184 } 185 } else { 186 typeName = ""; token = null; 188 } 189 JavaElement type = (JavaElement)getType(typeName, 1); 190 if (token == null) { 191 return type.getHandleFromMemento(memento, workingCopyOwner); 192 } else { 193 return type.getHandleFromMemento(token, memento, workingCopyOwner); 194 } 195 case JEM_LOCALVARIABLE: 196 if (!memento.hasMoreTokens()) return this; 197 String varName = memento.nextToken(); 198 if (!memento.hasMoreTokens()) return this; 199 memento.nextToken(); if (!memento.hasMoreTokens()) return this; 201 int declarationStart = Integer.parseInt(memento.nextToken()); 202 if (!memento.hasMoreTokens()) return this; 203 memento.nextToken(); if (!memento.hasMoreTokens()) return this; 205 int declarationEnd = Integer.parseInt(memento.nextToken()); 206 if (!memento.hasMoreTokens()) return this; 207 memento.nextToken(); if (!memento.hasMoreTokens()) return this; 209 int nameStart = Integer.parseInt(memento.nextToken()); 210 if (!memento.hasMoreTokens()) return this; 211 memento.nextToken(); if (!memento.hasMoreTokens()) return this; 213 int nameEnd = Integer.parseInt(memento.nextToken()); 214 if (!memento.hasMoreTokens()) return this; 215 memento.nextToken(); if (!memento.hasMoreTokens()) return this; 217 String typeSignature = memento.nextToken(); 218 return new LocalVariable(this, varName, declarationStart, declarationEnd, nameStart, nameEnd, typeSignature); 219 case JEM_TYPE_PARAMETER: 220 if (!memento.hasMoreTokens()) return this; 221 String typeParameterName = memento.nextToken(); 222 JavaElement typeParameter = new TypeParameter(this, typeParameterName); 223 return typeParameter.getHandleFromMemento(memento, workingCopyOwner); 224 } 225 return null; 226 } 227 230 protected char getHandleMementoDelimiter() { 231 return JavaElement.JEM_TYPE; 232 } 233 238 public Member getOuterMostLocalContext() { 239 IJavaElement current = this; 240 Member lastLocalContext = null; 241 parentLoop: while (true) { 242 switch (current.getElementType()) { 243 case CLASS_FILE: 244 case COMPILATION_UNIT: 245 break parentLoop; case TYPE: 247 break; 249 case INITIALIZER: 250 case FIELD: 251 case METHOD: 252 lastLocalContext = (Member) current; 254 break; 255 } 256 current = current.getParent(); 257 } 258 return lastLocalContext; 259 } 260 public ISourceRange getJavadocRange() throws JavaModelException { 261 ISourceRange range= this.getSourceRange(); 262 if (range == null) return null; 263 IBuffer buf= null; 264 if (this.isBinary()) { 265 buf = this.getClassFile().getBuffer(); 266 } else { 267 ICompilationUnit compilationUnit = this.getCompilationUnit(); 268 if (!compilationUnit.isConsistent()) { 269 return null; 270 } 271 buf = compilationUnit.getBuffer(); 272 } 273 final int start= range.getOffset(); 274 final int length= range.getLength(); 275 if (length > 0 && buf.getChar(start) == '/') { 276 IScanner scanner= ToolFactory.createScanner(true, false, false, false); 277 scanner.setSource(buf.getText(start, length).toCharArray()); 278 try { 279 int docOffset= -1; 280 int docEnd= -1; 281 282 int terminal= scanner.getNextToken(); 283 loop: while (true) { 284 switch(terminal) { 285 case ITerminalSymbols.TokenNameCOMMENT_JAVADOC : 286 docOffset= scanner.getCurrentTokenStartPosition(); 287 docEnd= scanner.getCurrentTokenEndPosition() + 1; 288 terminal= scanner.getNextToken(); 289 break; 290 case ITerminalSymbols.TokenNameCOMMENT_LINE : 291 case ITerminalSymbols.TokenNameCOMMENT_BLOCK : 292 terminal= scanner.getNextToken(); 293 continue loop; 294 default : 295 break loop; 296 } 297 } 298 if (docOffset != -1) { 299 return new SourceRange(docOffset + start, docEnd - docOffset + 1); 300 } 301 } catch (InvalidInputException ex) { 302 } 304 } 305 return null; 306 } 307 310 public ISourceRange getNameRange() throws JavaModelException { 311 MemberElementInfo info= (MemberElementInfo)getElementInfo(); 312 return new SourceRange(info.getNameSourceStart(), info.getNameSourceEnd() - info.getNameSourceStart() + 1); 313 } 314 317 public IType getType(String typeName, int count) { 318 if (isBinary()) { 319 throw new IllegalArgumentException ("Not a source member " + toStringWithAncestors()); } else { 321 SourceType type = new SourceType(this, typeName); 322 type.occurrenceCount = count; 323 return type; 324 } 325 } 326 329 public ITypeRoot getTypeRoot() { 330 IJavaElement element = getParent(); 331 while (element instanceof IMember) { 332 element= element.getParent(); 333 } 334 return (ITypeRoot) element; 335 } 336 339 public boolean isBinary() { 340 return false; 341 } 342 protected boolean isMainMethod(IMethod method) throws JavaModelException { 343 if ("main".equals(method.getElementName()) && Signature.SIG_VOID.equals(method.getReturnType())) { int flags= method.getFlags(); 345 if (Flags.isStatic(flags) && Flags.isPublic(flags)) { 346 String [] paramTypes= method.getParameterTypes(); 347 if (paramTypes.length == 1) { 348 String typeSignature= Signature.toString(paramTypes[0]); 349 return "String[]".equals(Signature.getSimpleName(typeSignature)); } 351 } 352 } 353 return false; 354 } 355 358 public boolean isReadOnly() { 359 return getClassFile() != null; 360 } 361 363 public String readableName() { 364 365 IJavaElement declaringType = getDeclaringType(); 366 if (declaringType != null) { 367 String declaringName = ((JavaElement) getDeclaringType()).readableName(); 368 StringBuffer buffer = new StringBuffer (declaringName); 369 buffer.append('.'); 370 buffer.append(this.getElementName()); 371 return buffer.toString(); 372 } else { 373 return super.readableName(); 374 } 375 } 376 379 protected void updateNameRange(int nameStart, int nameEnd) { 380 try { 381 MemberElementInfo info = (MemberElementInfo) getElementInfo(); 382 info.setNameSourceStart(nameStart); 383 info.setNameSourceEnd(nameEnd); 384 } catch (JavaModelException npe) { 385 return; 386 } 387 } 388 } 389 | Popular Tags |