1 11 package org.eclipse.jdt.internal.core; 12 13 import org.eclipse.jdt.core.BindingKey; 14 import org.eclipse.jdt.core.ICompilationUnit; 15 import org.eclipse.jdt.core.IField; 16 import org.eclipse.jdt.core.IJavaElement; 17 import org.eclipse.jdt.core.IMember; 18 import org.eclipse.jdt.core.IMethod; 19 import org.eclipse.jdt.core.IPackageFragment; 20 import org.eclipse.jdt.core.IType; 21 import org.eclipse.jdt.core.ITypeParameter; 22 import org.eclipse.jdt.core.JavaModelException; 23 import org.eclipse.jdt.core.Signature; 24 import org.eclipse.jdt.core.compiler.CharOperation; 25 26 public abstract class NamedMember extends Member { 27 28 32 protected String name; 33 34 public NamedMember(JavaElement parent, String name) { 35 super(parent); 36 this.name = name; 37 } 38 39 private void appendTypeParameters(StringBuffer buffer) throws JavaModelException { 40 ITypeParameter[] typeParameters = getTypeParameters(); 41 int length = typeParameters.length; 42 if (length == 0) return; 43 buffer.append('<'); 44 for (int i = 0; i < length; i++) { 45 ITypeParameter typeParameter = typeParameters[i]; 46 buffer.append(typeParameter.getElementName()); 47 String [] bounds = typeParameter.getBounds(); 48 int boundsLength = bounds.length; 49 if (boundsLength > 0) { 50 buffer.append(" extends "); for (int j = 0; j < boundsLength; j++) { 52 buffer.append(bounds[j]); 53 if (j < boundsLength-1) 54 buffer.append(" & "); } 56 } 57 if (i < length-1) 58 buffer.append(", "); } 60 buffer.append('>'); 61 } 62 63 public String getElementName() { 64 return this.name; 65 } 66 67 protected String getKey(IField field, boolean forceOpen) throws JavaModelException { 68 StringBuffer key = new StringBuffer (); 69 70 String declaringKey = getKey((IType) field.getParent(), forceOpen); 72 key.append(declaringKey); 73 74 key.append('.'); 76 key.append(field.getElementName()); 77 78 return key.toString(); 79 } 80 81 protected String getKey(IMethod method, boolean forceOpen) throws JavaModelException { 82 StringBuffer key = new StringBuffer (); 83 84 String declaringKey = getKey((IType) method.getParent(), forceOpen); 86 key.append(declaringKey); 87 88 key.append('.'); 90 String selector = method.getElementName(); 91 key.append(selector); 92 93 if (forceOpen) { 95 ITypeParameter[] typeParameters = method.getTypeParameters(); 96 int length = typeParameters.length; 97 if (length > 0) { 98 key.append('<'); 99 for (int i = 0; i < length; i++) { 100 ITypeParameter typeParameter = typeParameters[i]; 101 String [] bounds = typeParameter.getBounds(); 102 int boundsLength = bounds.length; 103 char[][] boundSignatures = new char[boundsLength][]; 104 for (int j = 0; j < boundsLength; j++) { 105 boundSignatures[j] = Signature.createCharArrayTypeSignature(bounds[j].toCharArray(), method.isBinary()); 106 CharOperation.replace(boundSignatures[j], '.', '/'); 107 } 108 char[] sig = Signature.createTypeParameterSignature(typeParameter.getElementName().toCharArray(), boundSignatures); 109 key.append(sig); 110 } 111 key.append('>'); 112 } 113 } 114 115 key.append('('); 117 String [] parameters = method.getParameterTypes(); 118 for (int i = 0, length = parameters.length; i < length; i++) 119 key.append(parameters[i].replace('.', '/')); 120 key.append(')'); 121 122 if (forceOpen) 124 key.append(method.getReturnType().replace('.', '/')); 125 else 126 key.append('V'); 127 128 return key.toString(); 129 } 130 131 protected String getKey(IType type, boolean forceOpen) throws JavaModelException { 132 StringBuffer key = new StringBuffer (); 133 key.append('L'); 134 String packageName = type.getPackageFragment().getElementName(); 135 key.append(packageName.replace('.', '/')); 136 if (packageName.length() > 0) 137 key.append('/'); 138 String typeQualifiedName = type.getTypeQualifiedName('$'); 139 ICompilationUnit cu = (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT); 140 if (cu != null) { 141 String cuName = cu.getElementName(); 142 String mainTypeName = cuName.substring(0, cuName.lastIndexOf('.')); 143 int end = typeQualifiedName.indexOf('$'); 144 if (end == -1) 145 end = typeQualifiedName.length(); 146 String topLevelTypeName = typeQualifiedName.substring(0, end); 147 if (!mainTypeName.equals(topLevelTypeName)) { 148 key.append(mainTypeName); 149 key.append('~'); 150 } 151 } 152 key.append(typeQualifiedName); 153 key.append(';'); 154 return key.toString(); 155 } 156 157 protected String getFullyQualifiedParameterizedName(String fullyQualifiedName, String uniqueKey) throws JavaModelException { 158 String [] typeArguments = new BindingKey(uniqueKey).getTypeArguments(); 159 int length = typeArguments.length; 160 if (length == 0) return fullyQualifiedName; 161 StringBuffer buffer = new StringBuffer (); 162 buffer.append(fullyQualifiedName); 163 buffer.append('<'); 164 for (int i = 0; i < length; i++) { 165 String typeArgument = typeArguments[i]; 166 buffer.append(Signature.toString(typeArgument)); 167 if (i < length-1) 168 buffer.append(','); 169 } 170 buffer.append('>'); 171 return buffer.toString(); 172 } 173 174 protected IPackageFragment getPackageFragment() { 175 return null; 176 } 177 178 public String getFullyQualifiedName(char enclosingTypeSeparator, boolean showParameters) throws JavaModelException { 179 String packageName = getPackageFragment().getElementName(); 180 if (packageName.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) { 181 return getTypeQualifiedName(enclosingTypeSeparator, showParameters); 182 } 183 return packageName + '.' + getTypeQualifiedName(enclosingTypeSeparator, showParameters); 184 } 185 186 public String getTypeQualifiedName(char enclosingTypeSeparator, boolean showParameters) throws JavaModelException { 187 NamedMember declaringType; 188 switch (this.parent.getElementType()) { 189 case IJavaElement.COMPILATION_UNIT: 190 if (showParameters) { 191 StringBuffer buffer = new StringBuffer (this.name); 192 appendTypeParameters(buffer); 193 return buffer.toString(); 194 } 195 return this.name; 196 case IJavaElement.CLASS_FILE: 197 String classFileName = this.parent.getElementName(); 198 String typeName; 199 if (classFileName.indexOf('$') == -1) { 200 typeName = this.name; 202 } else { 203 typeName = classFileName.substring(0, classFileName.lastIndexOf('.')); } 206 if (showParameters) { 207 StringBuffer buffer = new StringBuffer (typeName); 208 appendTypeParameters(buffer); 209 return buffer.toString(); 210 } 211 return typeName; 212 case IJavaElement.TYPE: 213 declaringType = (NamedMember) this.parent; 214 break; 215 case IJavaElement.FIELD: 216 case IJavaElement.INITIALIZER: 217 case IJavaElement.METHOD: 218 declaringType = (NamedMember) ((IMember) this.parent).getDeclaringType(); 219 break; 220 default: 221 return null; 222 } 223 StringBuffer buffer = new StringBuffer (declaringType.getTypeQualifiedName(enclosingTypeSeparator, showParameters)); 224 buffer.append(enclosingTypeSeparator); 225 String simpleName = this.name.length() == 0 ? Integer.toString(this.occurrenceCount) : this.name; 226 buffer.append(simpleName); 227 if (showParameters) { 228 appendTypeParameters(buffer); 229 } 230 return buffer.toString(); 231 } 232 233 protected ITypeParameter[] getTypeParameters() throws JavaModelException { 234 return null; 235 } 236 } 237 | Popular Tags |