1 11 package org.eclipse.jdt.internal.corext.template.java; 12 13 import java.util.Arrays ; 14 15 import org.eclipse.jdt.core.IType; 16 import org.eclipse.jdt.core.JavaModelException; 17 import org.eclipse.jdt.core.Signature; 18 19 25 public final class SignatureUtil { 26 27 40 private static final String NULL_TYPE_SIGNATURE= "Tnull;"; private static final char[] NULL_TYPE_SIGNATURE_ARRAY= NULL_TYPE_SIGNATURE.toCharArray(); 42 45 private static final String OBJECT_SIGNATURE= "Ljava.lang.Object;"; private static final char[] OBJECT_SIGNATURE_ARRAY= OBJECT_SIGNATURE.toCharArray(); 47 48 private SignatureUtil() { 49 } 51 52 61 public static boolean isJavaLangObject(String signature) { 62 return OBJECT_SIGNATURE.equals(signature); 63 } 64 65 74 public static String getUpperBound(String signature) { 75 return String.valueOf(getUpperBound(signature.toCharArray())); 76 } 77 78 87 public static char[] getUpperBound(char[] signature) { 88 if (signature.length < 1) 89 return signature; 90 91 if (signature[0] == Signature.C_STAR) 92 return OBJECT_SIGNATURE_ARRAY; 93 94 int superIndex= indexOf(signature, Signature.C_SUPER); 95 if (superIndex == 0) 96 return OBJECT_SIGNATURE_ARRAY; 97 98 if (superIndex != -1) { 99 char afterSuper= signature[superIndex + 1]; 100 if (afterSuper == Signature.C_STAR) { 101 char[] type= new char[signature.length - 1]; 102 System.arraycopy(signature, 0, type, 0, superIndex); 103 type[superIndex]= Signature.C_STAR; 104 System.arraycopy(signature, superIndex + 2, type, superIndex + 1, signature.length - superIndex - 2); 105 return getUpperBound(type); 106 } 107 108 if (afterSuper == Signature.C_EXTENDS) { 109 int typeEnd= typeEnd(signature, superIndex + 1); 110 char[] type= new char[signature.length - (typeEnd - superIndex - 1)]; 111 System.arraycopy(signature, 0, type, 0, superIndex); 112 type[superIndex]= Signature.C_STAR; 113 System.arraycopy(signature, typeEnd, type, superIndex + 1, signature.length - typeEnd); 114 return getUpperBound(type); 115 } 116 117 } 118 119 if (signature[0] == Signature.C_EXTENDS) { 120 char[] type= new char[signature.length - 1]; 121 System.arraycopy(signature, 1, type, 0, signature.length - 1); 122 return type; 123 } 124 125 return signature; 126 } 127 128 137 public static String getLowerBound(String signature) { 138 return String.valueOf(getLowerBound(signature.toCharArray())); 139 } 140 141 150 public static char[] getLowerBound(char[] signature) { 151 if (signature.length < 1) 152 return signature; 153 154 if (signature.length == 1 && signature[0] == Signature.C_STAR) 155 return signature; 156 157 int superIndex= indexOf(signature, Signature.C_EXTENDS); 158 if (superIndex == 0) 159 return NULL_TYPE_SIGNATURE_ARRAY; 160 161 if (superIndex != -1) { 162 char afterSuper= signature[superIndex + 1]; 163 if (afterSuper == Signature.C_STAR || afterSuper == Signature.C_EXTENDS) 164 return NULL_TYPE_SIGNATURE_ARRAY; 166 } 167 168 char[][] typeArguments= Signature.getTypeArguments(signature); 169 for (int i= 0; i < typeArguments.length; i++) 170 if (Arrays.equals(typeArguments[i], NULL_TYPE_SIGNATURE_ARRAY)) 171 return NULL_TYPE_SIGNATURE_ARRAY; 172 173 if (signature[0] == Signature.C_SUPER) { 174 char[] type= new char[signature.length - 1]; 175 System.arraycopy(signature, 1, type, 0, signature.length - 1); 176 return type; 177 } 178 179 return signature; 180 } 181 182 private static int indexOf(char[] signature, char ch) { 183 for (int i= 0; i < signature.length; i++) { 184 if (signature[i] == ch) 185 return i; 186 } 187 return -1; 188 } 189 190 197 public static String stripSignatureToFQN(String signature) throws IllegalArgumentException { 198 signature= Signature.getTypeErasure(signature); 199 signature= Signature.getElementType(signature); 200 return Signature.toString(signature); 201 } 202 203 213 public static String qualifySignature(final String signature, final IType context) { 214 if (context == null) 215 return signature; 216 217 String qualifier= Signature.getSignatureQualifier(signature); 218 if (qualifier.length() > 0) 219 return signature; 220 221 String elementType= Signature.getElementType(signature); 222 String erasure= Signature.getTypeErasure(elementType); 223 String simpleName= Signature.getSignatureSimpleName(erasure); 224 String genericSimpleName= Signature.getSignatureSimpleName(elementType); 225 226 int dim= Signature.getArrayCount(signature); 227 228 try { 229 String [][] strings= context.resolveType(simpleName); 230 if (strings != null && strings.length > 0) 231 qualifier= strings[0][0]; 232 } catch (JavaModelException e) { 233 } 235 236 if (qualifier.length() == 0) 237 return signature; 238 239 String qualifiedType= Signature.toQualifiedName(new String [] {qualifier, genericSimpleName}); 240 String qualifiedSignature= Signature.createTypeSignature(qualifiedType, true); 241 String newSignature= Signature.createArraySignature(qualifiedSignature, dim); 242 243 return newSignature; 244 } 245 246 261 public static char[] unboundedSignature(char[] signature) { 262 if (signature == null || signature.length < 2) 263 return signature; 264 265 final boolean BUG_83600= true; 266 if (BUG_83600) { 269 signature= fix83600(signature); 270 } 271 272 StringBuffer res= new StringBuffer ("("); char[][] parameters= Signature.getParameterTypes(signature); 274 for (int i= 0; i < parameters.length; i++) { 275 char[] param= parameters[i]; 276 res.append(getLowerBound(param)); 277 } 278 res.append(')'); 279 res.append(getUpperBound(Signature.getReturnType(signature))); 280 return res.toString().toCharArray(); 281 } 282 283 291 public static char[] fix83600(char[] signature) { 292 if (signature == null || signature.length < 2) 293 return signature; 294 295 return Signature.removeCapture(signature); 296 } 297 298 private static int typeEnd(char[] signature, int pos) { 299 int depth= 0; 300 while (pos < signature.length) { 301 switch (signature[pos]) { 302 case Signature.C_GENERIC_START: 303 depth++; 304 break; 305 case Signature.C_GENERIC_END: 306 depth--; 307 break; 308 case Signature.C_SEMICOLON: 309 if (depth == 0) 310 return pos + 1; 311 break; 312 } 313 pos++; 314 } 315 return pos + 1; 316 } 317 } 318 | Popular Tags |