1 11 package org.eclipse.jdt.internal.core.util; 12 13 import java.io.*; 14 import java.net.URI ; 15 import java.util.*; 16 import java.util.zip.ZipEntry ; 17 import java.util.zip.ZipFile ; 18 19 import org.eclipse.core.filesystem.EFS; 20 import org.eclipse.core.filesystem.IFileStore; 21 import org.eclipse.core.resources.*; 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.core.runtime.content.IContentType; 24 import org.eclipse.core.runtime.preferences.IScopeContext; 25 import org.eclipse.core.runtime.preferences.InstanceScope; 26 import org.eclipse.jdt.core.*; 27 import org.eclipse.jdt.core.compiler.CharOperation; 28 import org.eclipse.jdt.core.dom.ASTNode; 29 import org.eclipse.jdt.core.dom.ArrayType; 30 import org.eclipse.jdt.core.dom.ParameterizedType; 31 import org.eclipse.jdt.core.dom.PrimitiveType; 32 import org.eclipse.jdt.core.dom.QualifiedType; 33 import org.eclipse.jdt.core.dom.SimpleType; 34 import org.eclipse.jdt.core.dom.Type; 35 import org.eclipse.jdt.core.dom.WildcardType; 36 import org.eclipse.jdt.core.util.IClassFileAttribute; 37 import org.eclipse.jdt.core.util.IClassFileReader; 38 import org.eclipse.jdt.core.util.ICodeAttribute; 39 import org.eclipse.jdt.core.util.IFieldInfo; 40 import org.eclipse.jdt.core.util.IMethodInfo; 41 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; 42 import org.eclipse.jdt.internal.compiler.ast.Argument; 43 import org.eclipse.jdt.internal.compiler.ast.TypeReference; 44 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; 45 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException; 46 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper; 47 import org.eclipse.jdt.internal.compiler.util.SuffixConstants; 48 import org.eclipse.jdt.internal.core.JavaElement; 49 import org.eclipse.jdt.internal.core.JavaModelManager; 50 import org.eclipse.jdt.internal.core.PackageFragmentRoot; 51 import org.eclipse.jface.text.BadLocationException; 52 import org.eclipse.text.edits.MalformedTreeException; 53 import org.eclipse.text.edits.TextEdit; 54 55 58 public class Util { 59 60 public interface Comparable { 61 65 int compareTo(Comparable c); 66 } 67 68 public interface Comparer { 69 73 int compare(Object a, Object b); 74 } 75 private static final String ARGUMENTS_DELIMITER = "#"; 77 private static final String EMPTY_ARGUMENT = " "; 79 private static char[][] JAVA_LIKE_EXTENSIONS; 80 public static boolean ENABLE_JAVA_LIKE_EXTENSIONS = true; 81 82 private static final char[] BOOLEAN = "boolean".toCharArray(); private static final char[] BYTE = "byte".toCharArray(); private static final char[] CHAR = "char".toCharArray(); private static final char[] DOUBLE = "double".toCharArray(); private static final char[] FLOAT = "float".toCharArray(); private static final char[] INT = "int".toCharArray(); private static final char[] LONG = "long".toCharArray(); private static final char[] SHORT = "short".toCharArray(); private static final char[] VOID = "void".toCharArray(); private static final char[] INIT = "<init>".toCharArray(); 93 private Util() { 94 } 96 97 129 public static final String [] arrayConcat(String [] first, String second) { 130 if (second == null) 131 return first; 132 if (first == null) 133 return new String [] {second}; 134 135 int length = first.length; 136 if (first.length == 0) { 137 return new String [] {second}; 138 } 139 140 String [] result = new String [length + 1]; 141 System.arraycopy(first, 0, result, 0, length); 142 result[length] = second; 143 return result; 144 } 145 146 152 private static int checkTypeSignature(String sig, int start, int end, boolean allowVoid) { 153 if (start >= end) return -1; 154 int i = start; 155 char c = sig.charAt(i++); 156 int nestingDepth = 0; 157 while (c == '[') { 158 ++nestingDepth; 159 if (i >= end) return -1; 160 c = sig.charAt(i++); 161 } 162 switch (c) { 163 case 'B': 164 case 'C': 165 case 'D': 166 case 'F': 167 case 'I': 168 case 'J': 169 case 'S': 170 case 'Z': 171 break; 172 case 'V': 173 if (!allowVoid) return -1; 174 if (nestingDepth != 0) return -1; 176 break; 177 case 'L': 178 int semicolon = sig.indexOf(';', i); 179 if (semicolon <= i || semicolon >= end) return -1; 181 i = semicolon + 1; 182 break; 183 default: 184 return -1; 185 } 186 return i; 187 } 188 189 192 public static int combineHashCodes(int hashCode1, int hashCode2) { 193 return hashCode1 * 17 + hashCode2; 194 } 195 196 202 public static int compare(byte[] a, byte[] b) { 203 if (a == b) 204 return 0; 205 if (a == null) 206 return -1; 207 if (b == null) 208 return 1; 209 int len = Math.min(a.length, b.length); 210 for (int i = 0; i < len; ++i) { 211 int diff = a[i] - b[i]; 212 if (diff != 0) 213 return diff; 214 } 215 if (a.length > len) 216 return 1; 217 if (b.length > len) 218 return -1; 219 return 0; 220 } 221 232 public static int compare(char[] str1, char[] str2) { 233 int len1= str1.length; 234 int len2= str2.length; 235 int n= Math.min(len1, len2); 236 int i= 0; 237 while (n-- != 0) { 238 char c1= str1[i]; 239 char c2= str2[i++]; 240 if (c1 != c2) { 241 return c1 - c2; 242 } 243 } 244 return len1 - len2; 245 } 246 247 251 public static String concat(String s1, char c, String s2) { 252 if (s1 == null) s1 = "null"; if (s2 == null) s2 = "null"; int l1 = s1.length(); 255 int l2 = s2.length(); 256 char[] buf = new char[l1 + 1 + l2]; 257 s1.getChars(0, l1, buf, 0); 258 buf[l1] = c; 259 s2.getChars(0, l2, buf, l1 + 1); 260 return new String (buf); 261 } 262 263 273 public static String concat(String s1, String s2) { 274 if (s1 == null) s1 = "null"; if (s2 == null) s2 = "null"; int l1 = s1.length(); 277 int l2 = s2.length(); 278 char[] buf = new char[l1 + l2]; 279 s1.getChars(0, l1, buf, 0); 280 s2.getChars(0, l2, buf, l1); 281 return new String (buf); 282 } 283 284 307 public static final String concatWith(String [] array, char separator) { 308 StringBuffer buffer = new StringBuffer (); 309 for (int i = 0, length = array.length; i < length; i++) { 310 buffer.append(array[i]); 311 if (i < length - 1) 312 buffer.append(separator); 313 } 314 return buffer.toString(); 315 } 316 317 351 public static final String concatWith( 352 String [] array, 353 String name, 354 char separator) { 355 356 if (array == null || array.length == 0) return name; 357 if (name == null || name.length() == 0) return concatWith(array, separator); 358 StringBuffer buffer = new StringBuffer (); 359 for (int i = 0, length = array.length; i < length; i++) { 360 buffer.append(array[i]); 361 buffer.append(separator); 362 } 363 buffer.append(name); 364 return buffer.toString(); 365 366 } 367 368 372 public static String concat(String s1, String s2, String s3) { 373 if (s1 == null) s1 = "null"; if (s2 == null) s2 = "null"; if (s3 == null) s3 = "null"; int l1 = s1.length(); 377 int l2 = s2.length(); 378 int l3 = s3.length(); 379 char[] buf = new char[l1 + l2 + l3]; 380 s1.getChars(0, l1, buf, 0); 381 s2.getChars(0, l2, buf, l1); 382 s3.getChars(0, l3, buf, l1 + l2); 383 return new String (buf); 384 } 385 386 389 public static String convertTypeSignature(char[] sig, int start, int length) { 390 return new String (sig, start, length).replace('/', '.'); 391 } 392 393 397 public static String defaultJavaExtension() { 398 return SuffixConstants.SUFFIX_STRING_java; 399 } 400 401 410 public final static String editedString(String original, TextEdit edit) { 411 if (edit == null) { 412 return original; 413 } 414 SimpleDocument document = new SimpleDocument(original); 415 try { 416 edit.apply(document, TextEdit.NONE); 417 return document.get(); 418 } catch (MalformedTreeException e) { 419 e.printStackTrace(); 420 } catch (BadLocationException e) { 421 e.printStackTrace(); 422 } 423 return original; 424 } 425 426 430 public final static boolean endsWithIgnoreCase(String str, String end) { 431 432 int strLength = str == null ? 0 : str.length(); 433 int endLength = end == null ? 0 : end.length(); 434 435 if(endLength > strLength) 437 return false; 438 439 for(int i = 1 ; i <= endLength; i++){ 442 if(ScannerHelper.toLowerCase(end.charAt(endLength - i)) != ScannerHelper.toLowerCase(str.charAt(strLength - i))) 443 return false; 444 } 445 446 return true; 447 } 448 449 454 public static boolean equalArrays(Object [] a, Object [] b, int len) { 455 if (a == b) return true; 456 if (a.length < len || b.length < len) return false; 457 for (int i = 0; i < len; ++i) { 458 if (a[i] == null) { 459 if (b[i] != null) return false; 460 } else { 461 if (!a[i].equals(b[i])) return false; 462 } 463 } 464 return true; 465 } 466 467 475 public static boolean equalArraysOrNull(int[] a, int[] b) { 476 if (a == b) 477 return true; 478 if (a == null || b == null) 479 return false; 480 int len = a.length; 481 if (len != b.length) 482 return false; 483 for (int i = 0; i < len; ++i) { 484 if (a[i] != b[i]) 485 return false; 486 } 487 return true; 488 } 489 490 498 public static boolean equalArraysOrNull(Object [] a, Object [] b) { 499 if (a == b) return true; 500 if (a == null || b == null) return false; 501 502 int len = a.length; 503 if (len != b.length) return false; 504 for (int i = len-1; i >= 0; i--) { 507 if (a[i] == null) { 508 if (b[i] != null) return false; 509 } else { 510 if (!a[i].equals(b[i])) return false; 511 } 512 } 513 return true; 514 } 515 516 526 public static boolean equalArraysOrNullSortFirst(Comparable [] a, Comparable [] b) { 527 if (a == b) return true; 528 if (a == null || b == null) return false; 529 int len = a.length; 530 if (len != b.length) return false; 531 if (len >= 2) { a = sortCopy(a); 533 b = sortCopy(b); 534 } 535 for (int i = 0; i < len; ++i) { 536 if (!a[i].equals(b[i])) return false; 537 } 538 return true; 539 } 540 541 551 public static boolean equalArraysOrNullSortFirst(String [] a, String [] b) { 552 if (a == b) return true; 553 if (a == null || b == null) return false; 554 int len = a.length; 555 if (len != b.length) return false; 556 if (len >= 2) { a = sortCopy(a); 558 b = sortCopy(b); 559 } 560 for (int i = 0; i < len; ++i) { 561 if (!a[i].equals(b[i])) return false; 562 } 563 return true; 564 } 565 566 573 public static boolean equalOrNull(Object a, Object b) { 574 if (a == b) { 575 return true; 576 } 577 if (a == null || b == null) { 578 return false; 579 } 580 return a.equals(b); 581 } 582 583 588 public static boolean equalsIgnoreJavaLikeExtension(String fileName, String string) { 589 int fileNameLength = fileName.length(); 590 int stringLength = string.length(); 591 if (fileNameLength < stringLength) return false; 592 for (int i = 0; i < stringLength; i ++) { 593 if (fileName.charAt(i) != string.charAt(i)) { 594 return false; 595 } 596 } 597 char[][] javaLikeExtensions = getJavaLikeExtensions(); 598 suffixes: for (int i = 0, length = javaLikeExtensions.length; i < length; i++) { 599 char[] suffix = javaLikeExtensions[i]; 600 int extensionStart = stringLength+1; 601 if (extensionStart + suffix.length != fileNameLength) continue; 602 if (fileName.charAt(stringLength) != '.') continue; 603 for (int j = extensionStart; j < fileNameLength; j++) { 604 if (fileName.charAt(j) != suffix[j-extensionStart]) 605 continue suffixes; 606 } 607 return true; 608 } 609 return false; 610 } 611 612 616 public static String extractLastName(String qualifiedName) { 617 int i = qualifiedName.lastIndexOf('.'); 618 if (i == -1) return qualifiedName; 619 return qualifiedName.substring(i+1); 620 } 621 622 625 public static String [] extractParameterTypes(char[] sig) { 626 int count = getParameterCount(sig); 627 String [] result = new String [count]; 628 if (count == 0) 629 return result; 630 int i = CharOperation.indexOf('(', sig) + 1; 631 count = 0; 632 int len = sig.length; 633 int start = i; 634 for (;;) { 635 if (i == len) 636 break; 637 char c = sig[i]; 638 if (c == ')') 639 break; 640 if (c == '[') { 641 ++i; 642 } else 643 if (c == 'L') { 644 i = CharOperation.indexOf(';', sig, i + 1) + 1; 645 Assert.isTrue(i != 0); 646 result[count++] = convertTypeSignature(sig, start, i - start); 647 start = i; 648 } else { 649 ++i; 650 result[count++] = convertTypeSignature(sig, start, i - start); 651 start = i; 652 } 653 } 654 return result; 655 } 656 657 660 public static String extractReturnType(String sig) { 661 int i = sig.lastIndexOf(')'); 662 Assert.isTrue(i != -1); 663 return sig.substring(i+1); 664 } 665 private static IFile findFirstClassFile(IFolder folder) { 666 try { 667 IResource[] members = folder.members(); 668 for (int i = 0, max = members.length; i < max; i++) { 669 IResource member = members[i]; 670 if (member.getType() == IResource.FOLDER) { 671 return findFirstClassFile((IFolder)member); 672 } else if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(member.getName())) { 673 return (IFile) member; 674 } 675 } 676 } catch (CoreException e) { 677 } 679 return null; 680 } 681 682 688 public static String findLineSeparator(char[] text) { 689 int length = text.length; 691 if (length > 0) { 692 char nextChar = text[0]; 693 for (int i = 0; i < length; i++) { 694 char currentChar = nextChar; 695 nextChar = i < length-1 ? text[i+1] : ' '; 696 switch (currentChar) { 697 case '\n': return "\n"; case '\r': return nextChar == '\n' ? "\r\n" : "\r"; } 700 } 701 } 702 return null; 704 } 705 706 public static IClassFileAttribute getAttribute(IClassFileReader classFileReader, char[] attributeName) { 707 IClassFileAttribute[] attributes = classFileReader.getAttributes(); 708 for (int i = 0, max = attributes.length; i < max; i++) { 709 if (CharOperation.equals(attributes[i].getAttributeName(), attributeName)) { 710 return attributes[i]; 711 } 712 } 713 return null; 714 } 715 716 public static IClassFileAttribute getAttribute(ICodeAttribute codeAttribute, char[] attributeName) { 717 IClassFileAttribute[] attributes = codeAttribute.getAttributes(); 718 for (int i = 0, max = attributes.length; i < max; i++) { 719 if (CharOperation.equals(attributes[i].getAttributeName(), attributeName)) { 720 return attributes[i]; 721 } 722 } 723 return null; 724 } 725 726 public static IClassFileAttribute getAttribute(IFieldInfo fieldInfo, char[] attributeName) { 727 IClassFileAttribute[] attributes = fieldInfo.getAttributes(); 728 for (int i = 0, max = attributes.length; i < max; i++) { 729 if (CharOperation.equals(attributes[i].getAttributeName(), attributeName)) { 730 return attributes[i]; 731 } 732 } 733 return null; 734 } 735 736 public static IClassFileAttribute getAttribute(IMethodInfo methodInfo, char[] attributeName) { 737 IClassFileAttribute[] attributes = methodInfo.getAttributes(); 738 for (int i = 0, max = attributes.length; i < max; i++) { 739 if (CharOperation.equals(attributes[i].getAttributeName(), attributeName)) { 740 return attributes[i]; 741 } 742 } 743 return null; 744 } 745 746 749 public static char[][] getJavaLikeExtensions() { 750 if (JAVA_LIKE_EXTENSIONS == null) { 751 if (!ENABLE_JAVA_LIKE_EXTENSIONS) 753 JAVA_LIKE_EXTENSIONS = new char[][] {SuffixConstants.EXTENSION_java.toCharArray()}; 754 else { 755 IContentType javaContentType = Platform.getContentTypeManager().getContentType(JavaCore.JAVA_SOURCE_CONTENT_TYPE); 756 HashSet fileExtensions = new HashSet(); 757 IContentType[] contentTypes = Platform.getContentTypeManager().getAllContentTypes(); 759 for (int i = 0, length = contentTypes.length; i < length; i++) { 760 if (contentTypes[i].isKindOf(javaContentType)) { String [] fileExtension = contentTypes[i].getFileSpecs(IContentType.FILE_EXTENSION_SPEC); 762 for (int j = 0, length2 = fileExtension.length; j < length2; j++) { 763 fileExtensions.add(fileExtension[j]); 764 } 765 } 766 } 767 int length = fileExtensions.size(); 768 char[][] extensions = new char[length][]; 770 extensions[0] = SuffixConstants.EXTENSION_java.toCharArray(); int index = 1; 772 Iterator iterator = fileExtensions.iterator(); 773 while (iterator.hasNext()) { 774 String fileExtension = (String ) iterator.next(); 775 if (SuffixConstants.EXTENSION_java.equals(fileExtension)) 776 continue; 777 extensions[index++] = fileExtension.toCharArray(); 778 } 779 JAVA_LIKE_EXTENSIONS = extensions; 780 } 781 } 782 return JAVA_LIKE_EXTENSIONS; 783 } 784 793 public static long getJdkLevel(Object targetLibrary) { 794 try { 795 ClassFileReader reader = null; 796 if (targetLibrary instanceof IFolder) { 797 IFile classFile = findFirstClassFile((IFolder) targetLibrary); if (classFile != null) 799 reader = Util.newClassFileReader(classFile); 800 } else { 801 ZipFile jar = null; 803 try { 804 IPath path = null; 805 if (targetLibrary instanceof IResource) { 806 path = ((IResource)targetLibrary).getFullPath(); 807 } else if (targetLibrary instanceof File ){ 808 File f = (File ) targetLibrary; 809 if (!f.isDirectory()) { 810 path = new Path(((File )targetLibrary).getPath()); 811 } 812 } 813 if (path != null) { 814 jar = JavaModelManager.getJavaModelManager().getZipFile(path); 815 for (Enumeration e= jar.entries(); e.hasMoreElements();) { 816 ZipEntry member= (ZipEntry ) e.nextElement(); 817 String entryName= member.getName(); 818 if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entryName)) { 819 reader = ClassFileReader.read(jar, entryName); 820 break; 821 } 822 } 823 } 824 } catch (CoreException e) { 825 } finally { 827 JavaModelManager.getJavaModelManager().closeZipFile(jar); 828 } 829 } 830 if (reader != null) { 831 return reader.getVersion(); 832 } 833 } catch (CoreException e) { 834 } catch(ClassFormatException e) { 836 } catch(IOException e) { 838 } 840 return 0; 841 } 842 843 848 public static String getNameWithoutJavaLikeExtension(String fileName) { 849 int index = indexOfJavaLikeExtension(fileName); 850 if (index == -1) 851 return fileName; 852 return fileName.substring(0, index); 853 } 854 855 861 public static String getLineSeparator(String text, IJavaProject project) { 862 String lineSeparator = null; 863 864 if (text != null && text.length() != 0) { 866 lineSeparator = findLineSeparator(text.toCharArray()); 867 if (lineSeparator != null) 868 return lineSeparator; 869 } 870 871 IScopeContext[] scopeContext; 873 if (project != null) { 874 scopeContext= new IScopeContext[] { new ProjectScope(project.getProject()) }; 875 lineSeparator= Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, null, scopeContext); 876 if (lineSeparator != null) 877 return lineSeparator; 878 } 879 880 scopeContext= new IScopeContext[] { new InstanceScope() }; 882 lineSeparator = Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, null, scopeContext); 883 if (lineSeparator != null) 884 return lineSeparator; 885 886 return org.eclipse.jdt.internal.compiler.util.Util.LINE_SEPARATOR; 888 } 889 890 896 private static String getLineSeparator(char[] text, char[] buffer) { 897 String lineSeparator = findLineSeparator(buffer); 899 if (lineSeparator == null) { 900 lineSeparator = findLineSeparator(text); 902 if (lineSeparator == null) { 903 return getLineSeparator((String ) null, (IJavaProject) null); 905 } 906 } 907 return lineSeparator; 908 } 909 910 913 public static int getParameterCount(char[] sig) { 914 int i = CharOperation.indexOf('(', sig) + 1; 915 Assert.isTrue(i != 0); 916 int count = 0; 917 int len = sig.length; 918 for (;;) { 919 if (i == len) 920 break; 921 char c = sig[i]; 922 if (c == ')') 923 break; 924 if (c == '[') { 925 ++i; 926 } else 927 if (c == 'L') { 928 ++count; 929 i = CharOperation.indexOf(';', sig, i + 1) + 1; 930 Assert.isTrue(i != 0); 931 } else { 932 ++count; 933 ++i; 934 } 935 } 936 return count; 937 } 938 939 942 public static String getProblemArgumentsForMarker(String [] arguments){ 943 StringBuffer args = new StringBuffer (10); 944 945 args.append(arguments.length); 946 args.append(':'); 947 948 949 for (int j = 0; j < arguments.length; j++) { 950 if(j != 0) 951 args.append(ARGUMENTS_DELIMITER); 952 953 if(arguments[j].length() == 0) { 954 args.append(EMPTY_ARGUMENT); 955 } else { 956 args.append(arguments[j]); 957 } 958 } 959 960 return args.toString(); 961 } 962 963 966 public static String [] getProblemArgumentsFromMarker(String argumentsString){ 967 if (argumentsString == null) return null; 968 int index = argumentsString.indexOf(':'); 969 if(index == -1) 970 return null; 971 972 int length = argumentsString.length(); 973 int numberOfArg; 974 try{ 975 numberOfArg = Integer.parseInt(argumentsString.substring(0 , index)); 976 } catch (NumberFormatException e) { 977 return null; 978 } 979 argumentsString = argumentsString.substring(index + 1, length); 980 981 String [] args = new String [length]; 982 int count = 0; 983 984 StringTokenizer tokenizer = new StringTokenizer(argumentsString, ARGUMENTS_DELIMITER); 985 while(tokenizer.hasMoreTokens()) { 986 String argument = tokenizer.nextToken(); 987 if(argument.equals(EMPTY_ARGUMENT)) 988 argument = ""; args[count++] = argument; 990 } 991 992 if(count != numberOfArg) 993 return null; 994 995 System.arraycopy(args, 0, args = new String [count], 0, count); 996 return args; 997 } 998 999 1002 public static byte[] getResourceContentsAsByteArray(IFile file) throws JavaModelException { 1003 InputStream stream= null; 1004 try { 1005 stream = file.getContents(true); 1006 } catch (CoreException e) { 1007 throw new JavaModelException(e); 1008 } 1009 try { 1010 return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsByteArray(stream, -1); 1011 } catch (IOException e) { 1012 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION); 1013 } finally { 1014 try { 1015 stream.close(); 1016 } catch (IOException e) { 1017 } 1019 } 1020 } 1021 1022 1025 public static char[] getResourceContentsAsCharArray(IFile file) throws JavaModelException { 1026 String encoding; 1028 try { 1029 encoding = file.getCharset(); 1030 } catch(CoreException ce) { 1031 encoding = null; 1033 } 1034 return getResourceContentsAsCharArray(file, encoding); 1035 } 1036 1037 public static char[] getResourceContentsAsCharArray(IFile file, String encoding) throws JavaModelException { 1038 IPath location = file.getLocation(); 1041 long length; 1042 if (location == null) { 1043 try { 1045 length = EFS.getStore(file.getLocationURI()).fetchInfo().getLength(); 1046 } catch (CoreException e) { 1047 throw new JavaModelException(e); 1048 } 1049 } else { 1050 length = location.toFile().length(); 1052 } 1053 1054 InputStream stream= null; 1056 try { 1057 stream = file.getContents(true); 1058 } catch (CoreException e) { 1059 throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST); 1060 } 1061 try { 1062 return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, (int) length, encoding); 1063 } catch (IOException e) { 1064 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION); 1065 } finally { 1066 try { 1067 stream.close(); 1068 } catch (IOException e) { 1069 } 1071 } 1072 } 1073 1074 1077 public static String getSignature(Type type) { 1078 StringBuffer buffer = new StringBuffer (); 1079 getFullyQualifiedName(type, buffer); 1080 return Signature.createTypeSignature(buffer.toString(), false); 1081 } 1082 1083 1086 public static String getSourceAttachmentProperty(IPath path) throws JavaModelException { 1087 Map rootPathToAttachments = JavaModelManager.getJavaModelManager().rootPathToAttachments; 1088 String property = (String ) rootPathToAttachments.get(path); 1089 if (property == null) { 1090 try { 1091 property = ResourcesPlugin.getWorkspace().getRoot().getPersistentProperty(getSourceAttachmentPropertyName(path)); 1092 if (property == null) { 1093 rootPathToAttachments.put(path, PackageFragmentRoot.NO_SOURCE_ATTACHMENT); 1094 return null; 1095 } 1096 rootPathToAttachments.put(path, property); 1097 return property; 1098 } catch (CoreException e) { 1099 throw new JavaModelException(e); 1100 } 1101 } else if (property.equals(PackageFragmentRoot.NO_SOURCE_ATTACHMENT)) { 1102 return null; 1103 } else 1104 return property; 1105 } 1106 1107 private static QualifiedName getSourceAttachmentPropertyName(IPath path) { 1108 return new QualifiedName(JavaCore.PLUGIN_ID, "sourceattachment: " + path.toOSString()); } 1110 1111 public static void setSourceAttachmentProperty(IPath path, String property) { 1112 JavaModelManager.getJavaModelManager().rootPathToAttachments.put(path, property); 1113 try { 1114 ResourcesPlugin.getWorkspace().getRoot().setPersistentProperty(getSourceAttachmentPropertyName(path), property); 1115 } catch (CoreException e) { 1116 e.printStackTrace(); 1117 } 1118 } 1119 1120 1126 public static String getDeclaringTypeSignature(String key) { 1127 KeyToSignature keyToSignature = new KeyToSignature(key, KeyToSignature.DECLARING_TYPE); 1128 keyToSignature.parse(); 1129 return keyToSignature.signature.toString(); 1130 } 1131 1132 1135 private static void getFullyQualifiedName(Type type, StringBuffer buffer) { 1136 switch (type.getNodeType()) { 1137 case ASTNode.ARRAY_TYPE: 1138 ArrayType arrayType = (ArrayType) type; 1139 getFullyQualifiedName(arrayType.getElementType(), buffer); 1140 for (int i = 0, length = arrayType.getDimensions(); i < length; i++) { 1141 buffer.append('['); 1142 buffer.append(']'); 1143 } 1144 break; 1145 case ASTNode.PARAMETERIZED_TYPE: 1146 ParameterizedType parameterizedType = (ParameterizedType) type; 1147 getFullyQualifiedName(parameterizedType.getType(), buffer); 1148 buffer.append('<'); 1149 Iterator iterator = parameterizedType.typeArguments().iterator(); 1150 boolean isFirst = true; 1151 while (iterator.hasNext()) { 1152 if (!isFirst) 1153 buffer.append(','); 1154 else 1155 isFirst = false; 1156 Type typeArgument = (Type) iterator.next(); 1157 getFullyQualifiedName(typeArgument, buffer); 1158 } 1159 buffer.append('>'); 1160 break; 1161 case ASTNode.PRIMITIVE_TYPE: 1162 buffer.append(((PrimitiveType) type).getPrimitiveTypeCode().toString()); 1163 break; 1164 case ASTNode.QUALIFIED_TYPE: 1165 buffer.append(((QualifiedType) type).getName().getFullyQualifiedName()); 1166 break; 1167 case ASTNode.SIMPLE_TYPE: 1168 buffer.append(((SimpleType) type).getName().getFullyQualifiedName()); 1169 break; 1170 case ASTNode.WILDCARD_TYPE: 1171 buffer.append('?'); 1172 WildcardType wildcardType = (WildcardType) type; 1173 Type bound = wildcardType.getBound(); 1174 if (bound == null) return; 1175 if (wildcardType.isUpperBound()) { 1176 buffer.append(" extends "); } else { 1178 buffer.append(" super "); } 1180 getFullyQualifiedName(bound, buffer); 1181 break; 1182 } 1183 } 1184 1185 1188 public static String [] getTrimmedSimpleNames(String name) { 1189 String [] result = Signature.getSimpleNames(name); 1190 for (int i = 0, length = result.length; i < length; i++) { 1191 result[i] = result[i].trim(); 1192 } 1193 return result; 1194 } 1195 1196 1199 public static int indexOfEnclosingPath(IPath checkedPath, IPath[] paths, int pathCount) { 1200 1201 int bestMatch = -1, bestLength = -1; 1202 for (int i = 0; i < pathCount; i++){ 1203 if (paths[i].equals(checkedPath)) continue; 1204 if (paths[i].isPrefixOf(checkedPath)) { 1205 int currentLength = paths[i].segmentCount(); 1206 if (currentLength > bestLength) { 1207 bestLength = currentLength; 1208 bestMatch = i; 1209 } 1210 } 1211 } 1212 return bestMatch; 1213 } 1214 1215 1220 public static int indexOfJavaLikeExtension(String fileName) { 1221 int fileNameLength = fileName.length(); 1222 char[][] javaLikeExtensions = getJavaLikeExtensions(); 1223 extensions: for (int i = 0, length = javaLikeExtensions.length; i < length; i++) { 1224 char[] extension = javaLikeExtensions[i]; 1225 int extensionLength = extension.length; 1226 int extensionStart = fileNameLength - extensionLength; 1227 int dotIndex = extensionStart - 1; 1228 if (dotIndex < 0) continue; 1229 if (fileName.charAt(dotIndex) != '.') continue; 1230 for (int j = 0; j < extensionLength; j++) { 1231 if (fileName.charAt(extensionStart + j) != extension[j]) 1232 continue extensions; 1233 } 1234 return dotIndex; 1235 } 1236 return -1; 1237 } 1238 1239 1242 public static int indexOfMatchingPath(IPath checkedPath, IPath[] paths, int pathCount) { 1243 1244 for (int i = 0; i < pathCount; i++){ 1245 if (paths[i].equals(checkedPath)) return i; 1246 } 1247 return -1; 1248 } 1249 1250 1253 public static int indexOfNestedPath(IPath checkedPath, IPath[] paths, int pathCount) { 1254 1255 for (int i = 0; i < pathCount; i++){ 1256 if (checkedPath.equals(paths[i])) continue; 1257 if (checkedPath.isPrefixOf(paths[i])) return i; 1258 } 1259 return -1; 1260 } 1261 1265 protected static boolean isAttributeSupported(int attribute) { 1266 return (EFS.getLocalFileSystem().attributes() & attribute) != 0; 1267 } 1268 1269 1275 public static boolean isReadOnly(IResource resource) { 1276 if (isReadOnlySupported()) { 1277 ResourceAttributes resourceAttributes = resource.getResourceAttributes(); 1278 if (resourceAttributes == null) return false; return resourceAttributes.isReadOnly(); 1280 } 1281 return false; 1282 } 1283 1284 1288 public static boolean isReadOnlySupported() { 1289 return isAttributeSupported(EFS.ATTRIBUTE_READ_ONLY); 1290 } 1291 1292 1296 public static final boolean isExcluded(IJavaElement element) { 1297 int elementType = element.getElementType(); 1298 switch (elementType) { 1299 case IJavaElement.JAVA_MODEL: 1300 case IJavaElement.JAVA_PROJECT: 1301 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 1302 return false; 1303 1304 case IJavaElement.PACKAGE_FRAGMENT: 1305 PackageFragmentRoot root = (PackageFragmentRoot)element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 1306 IResource resource = element.getResource(); 1307 return resource != null && isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars()); 1308 1309 case IJavaElement.COMPILATION_UNIT: 1310 root = (PackageFragmentRoot)element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 1311 resource = element.getResource(); 1312 if (resource == null) 1313 return false; 1314 if (isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars())) 1315 return true; 1316 return isExcluded(element.getParent()); 1317 1318 default: 1319 IJavaElement cu = element.getAncestor(IJavaElement.COMPILATION_UNIT); 1320 return cu != null && isExcluded(cu); 1321 } 1322 } 1323 1330 public final static boolean isExcluded(IPath resourcePath, char[][] inclusionPatterns, char[][] exclusionPatterns, boolean isFolderPath) { 1331 if (inclusionPatterns == null && exclusionPatterns == null) return false; 1332 return org.eclipse.jdt.internal.compiler.util.Util.isExcluded(resourcePath.toString().toCharArray(), inclusionPatterns, exclusionPatterns, isFolderPath); 1333 } 1334 1335 1340 public final static boolean isExcluded(IResource resource, char[][] inclusionPatterns, char[][] exclusionPatterns) { 1341 IPath path = resource.getFullPath(); 1342 int resourceType = resource.getType(); 1344 return isExcluded(path, inclusionPatterns, exclusionPatterns, resourceType == IResource.FOLDER || resourceType == IResource.PROJECT); 1345 } 1346 1347 1348 1364 public static boolean isValidClassFileName(String name, String sourceLevel, String complianceLevel) { 1365 return JavaConventions.validateClassFileName(name, sourceLevel, complianceLevel).getSeverity() != IStatus.ERROR; 1366 } 1367 1368 1369 1385 public static boolean isValidCompilationUnitName(String name, String sourceLevel, String complianceLevel) { 1386 return JavaConventions.validateCompilationUnitName(name, sourceLevel, complianceLevel).getSeverity() != IStatus.ERROR; 1387 } 1388 1389 1396 public static boolean isValidFolderNameForPackage(String folderName, String sourceLevel, String complianceLevel) { 1397 return JavaConventions.validateIdentifier(folderName, sourceLevel, complianceLevel).getSeverity() != IStatus.ERROR; 1398 } 1399 1400 1404 public static boolean isValidMethodSignature(String sig) { 1405 int len = sig.length(); 1406 if (len == 0) return false; 1407 int i = 0; 1408 char c = sig.charAt(i++); 1409 if (c != '(') return false; 1410 if (i >= len) return false; 1411 while (sig.charAt(i) != ')') { 1412 i = checkTypeSignature(sig, i, len, false); 1414 if (i == -1) return false; 1415 if (i >= len) return false; 1416 } 1417 ++i; 1418 i = checkTypeSignature(sig, i, len, true); 1419 return i == len; 1420 } 1421 1422 1426 public static boolean isValidTypeSignature(String sig, boolean allowVoid) { 1427 int len = sig.length(); 1428 return checkTypeSignature(sig, 0, len, allowVoid) == len; 1429 } 1430 1431 1435 public static String localTypeName(String binaryTypeName, int lastDollar, int end) { 1436 if (lastDollar > 0 && binaryTypeName.charAt(lastDollar-1) == '$') 1437 return binaryTypeName; 1440 int nameStart = lastDollar+1; 1441 while (nameStart < end && Character.isDigit(binaryTypeName.charAt(nameStart))) 1442 nameStart++; 1443 return binaryTypeName.substring(nameStart, end); 1444 } 1445 1446 1449 public static void log(Throwable e, String message) { 1450 Throwable nestedException; 1451 if (e instanceof JavaModelException 1452 && (nestedException = ((JavaModelException)e).getException()) != null) { 1453 e = nestedException; 1454 } 1455 IStatus status= new Status( 1456 IStatus.ERROR, 1457 JavaCore.PLUGIN_ID, 1458 IStatus.ERROR, 1459 message, 1460 e); 1461 JavaCore.getPlugin().getLog().log(status); 1462 } 1463 1464 public static ClassFileReader newClassFileReader(IResource resource) throws CoreException, ClassFormatException, IOException { 1465 InputStream in = null; 1466 try { 1467 in = ((IFile) resource).getContents(true); 1468 return ClassFileReader.read(in, resource.getFullPath().toString()); 1469 } finally { 1470 if (in != null) 1471 in.close(); 1472 } 1473 } 1474 1475 1479 public static char[] normalizeCRs(char[] text, char[] buffer) { 1480 CharArrayBuffer result = new CharArrayBuffer(); 1481 int lineStart = 0; 1482 int length = text.length; 1483 if (length == 0) return text; 1484 String lineSeparator = getLineSeparator(text, buffer); 1485 char nextChar = text[0]; 1486 for (int i = 0; i < length; i++) { 1487 char currentChar = nextChar; 1488 nextChar = i < length-1 ? text[i+1] : ' '; 1489 switch (currentChar) { 1490 case '\n': 1491 int lineLength = i-lineStart; 1492 char[] line = new char[lineLength]; 1493 System.arraycopy(text, lineStart, line, 0, lineLength); 1494 result.append(line); 1495 result.append(lineSeparator); 1496 lineStart = i+1; 1497 break; 1498 case '\r': 1499 lineLength = i-lineStart; 1500 if (lineLength >= 0) { 1501 line = new char[lineLength]; 1502 System.arraycopy(text, lineStart, line, 0, lineLength); 1503 result.append(line); 1504 result.append(lineSeparator); 1505 if (nextChar == '\n') { 1506 nextChar = ' '; 1507 lineStart = i+2; 1508 } else { 1509 lineStart = i+1; 1513 } 1514 } else { 1515 lineStart = i+1; 1518 } 1519 break; 1520 } 1521 } 1522 char[] lastLine; 1523 if (lineStart > 0) { 1524 int lastLineLength = length-lineStart; 1525 if (lastLineLength > 0) { 1526 lastLine = new char[lastLineLength]; 1527 System.arraycopy(text, lineStart, lastLine, 0, lastLineLength); 1528 result.append(lastLine); 1529 } 1530 return result.getContents(); 1531 } 1532 return text; 1533 } 1534 1535 1539 public static String normalizeCRs(String text, String buffer) { 1540 return new String (normalizeCRs(text.toCharArray(), buffer.toCharArray())); 1541 } 1542 1543 1550 public static String packageName(IPath pkgPath, String sourceLevel, String complianceLevel) { 1551 StringBuffer pkgName = new StringBuffer (IPackageFragment.DEFAULT_PACKAGE_NAME); 1552 for (int j = 0, max = pkgPath.segmentCount(); j < max; j++) { 1553 String segment = pkgPath.segment(j); 1554 if (!isValidFolderNameForPackage(segment, sourceLevel, complianceLevel)) { 1555 return null; 1556 } 1557 pkgName.append(segment); 1558 if (j < pkgPath.segmentCount() - 1) { 1559 pkgName.append("." ); } 1561 } 1562 return pkgName.toString(); 1563 } 1564 1565 1568 public static int prefixLength(char[] s1, char[] s2) { 1569 int len= 0; 1570 int max= Math.min(s1.length, s2.length); 1571 for (int i= 0; i < max && s1[i] == s2[i]; ++i) 1572 ++len; 1573 return len; 1574 } 1575 1578 public static int prefixLength(String s1, String s2) { 1579 int len= 0; 1580 int max= Math.min(s1.length(), s2.length()); 1581 for (int i= 0; i < max && s1.charAt(i) == s2.charAt(i); ++i) 1582 ++len; 1583 return len; 1584 } 1585 private static void quickSort(char[][] list, int left, int right) { 1586 int original_left= left; 1587 int original_right= right; 1588 char[] mid= list[left + (right - left) / 2]; 1589 do { 1590 while (compare(list[left], mid) < 0) { 1591 left++; 1592 } 1593 while (compare(mid, list[right]) < 0) { 1594 right--; 1595 } 1596 if (left <= right) { 1597 char[] tmp= list[left]; 1598 list[left]= list[right]; 1599 list[right]= tmp; 1600 left++; 1601 right--; 1602 } 1603 } while (left <= right); 1604 if (original_left < right) { 1605 quickSort(list, original_left, right); 1606 } 1607 if (left < original_right) { 1608 quickSort(list, left, original_right); 1609 } 1610 } 1611 1612 1615 private static void quickSort(Comparable [] sortedCollection, int left, int right) { 1616 int original_left = left; 1617 int original_right = right; 1618 Comparable mid = sortedCollection[ left + (right - left) / 2]; 1619 do { 1620 while (sortedCollection[left].compareTo(mid) < 0) { 1621 left++; 1622 } 1623 while (mid.compareTo(sortedCollection[right]) < 0) { 1624 right--; 1625 } 1626 if (left <= right) { 1627 Comparable tmp = sortedCollection[left]; 1628 sortedCollection[left] = sortedCollection[right]; 1629 sortedCollection[right] = tmp; 1630 left++; 1631 right--; 1632 } 1633 } while (left <= right); 1634 if (original_left < right) { 1635 quickSort(sortedCollection, original_left, right); 1636 } 1637 if (left < original_right) { 1638 quickSort(sortedCollection, left, original_right); 1639 } 1640 } 1641 private static void quickSort(int[] list, int left, int right) { 1642 int original_left= left; 1643 int original_right= right; 1644 int mid= list[left + (right - left) / 2]; 1645 do { 1646 while (list[left] < mid) { 1647 left++; 1648 } 1649 while (mid < list[right]) { 1650 right--; 1651 } 1652 if (left <= right) { 1653 int tmp= list[left]; 1654 list[left]= list[right]; 1655 list[right]= tmp; 1656 left++; 1657 right--; 1658 } 1659 } while (left <= right); 1660 if (original_left < right) { 1661 quickSort(list, original_left, right); 1662 } 1663 if (left < original_right) { 1664 quickSort(list, left, original_right); 1665 } 1666 } 1667 1668 1671 private static void quickSort(Object [] sortedCollection, int left, int right, Comparer comparer) { 1672 int original_left = left; 1673 int original_right = right; 1674 Object mid = sortedCollection[ left + (right - left) / 2]; 1675 do { 1676 while (comparer.compare(sortedCollection[left], mid) < 0) { 1677 left++; 1678 } 1679 while (comparer.compare(mid, sortedCollection[right]) < 0) { 1680 right--; 1681 } 1682 if (left <= right) { 1683 Object tmp = sortedCollection[left]; 1684 sortedCollection[left] = sortedCollection[right]; 1685 sortedCollection[right] = tmp; 1686 left++; 1687 right--; 1688 } 1689 } while (left <= right); 1690 if (original_left < right) { 1691 quickSort(sortedCollection, original_left, right, comparer); 1692 } 1693 if (left < original_right) { 1694 quickSort(sortedCollection, left, original_right, comparer); 1695 } 1696 } 1697 1698 1701 private static void quickSort(String [] sortedCollection, int left, int right) { 1702 int original_left = left; 1703 int original_right = right; 1704 String mid = sortedCollection[ left + (right - left) / 2]; 1705 do { 1706 while (sortedCollection[left].compareTo(mid) < 0) { 1707 left++; 1708 } 1709 while (mid.compareTo(sortedCollection[right]) < 0) { 1710 right--; 1711 } 1712 if (left <= right) { 1713 String tmp = sortedCollection[left]; 1714 sortedCollection[left] = sortedCollection[right]; 1715 sortedCollection[right] = tmp; 1716 left++; 1717 right--; 1718 } 1719 } while (left <= right); 1720 if (original_left < right) { 1721 quickSort(sortedCollection, original_left, right); 1722 } 1723 if (left < original_right) { 1724 quickSort(sortedCollection, left, original_right); 1725 } 1726 } 1727 1728 1732 public static String relativePath(IPath fullPath, int skipSegmentCount) { 1733 boolean hasTrailingSeparator = fullPath.hasTrailingSeparator(); 1734 String [] segments = fullPath.segments(); 1735 1736 int length = 0; 1738 int max = segments.length; 1739 if (max > skipSegmentCount) { 1740 for (int i1 = skipSegmentCount; i1 < max; i1++) { 1741 length += segments[i1].length(); 1742 } 1743 length += max - skipSegmentCount - 1; 1745 } 1746 if (hasTrailingSeparator) 1747 length++; 1748 1749 char[] result = new char[length]; 1750 int offset = 0; 1751 int len = segments.length - 1; 1752 if (len >= skipSegmentCount) { 1753 for (int i = skipSegmentCount; i < len; i++) { 1755 int size = segments[i].length(); 1756 segments[i].getChars(0, size, result, offset); 1757 offset += size; 1758 result[offset++] = '/'; 1759 } 1760 int size = segments[len].length(); 1762 segments[len].getChars(0, size, result, offset); 1763 offset += size; 1764 } 1765 if (hasTrailingSeparator) 1766 result[offset++] = '/'; 1767 return new String (result); 1768 } 1769 1770 1773 public static void resetJavaLikeExtensions() { 1774 JAVA_LIKE_EXTENSIONS = null; 1775 } 1776 1777 1801 public static final String [] splitOn( 1802 char divider, 1803 String string, 1804 int start, 1805 int end) { 1806 int length = string == null ? 0 : string.length(); 1807 if (length == 0 || start > end) 1808 return CharOperation.NO_STRINGS; 1809 1810 int wordCount = 1; 1811 for (int i = start; i < end; i++) 1812 if (string.charAt(i) == divider) 1813 wordCount++; 1814 String [] split = new String [wordCount]; 1815 int last = start, currentWord = 0; 1816 for (int i = start; i < end; i++) { 1817 if (string.charAt(i) == divider) { 1818 split[currentWord++] = string.substring(last, i); 1819 last = i + 1; 1820 } 1821 } 1822 split[currentWord] = string.substring(last, end); 1823 return split; 1824 } 1825 1833 public static void setReadOnly(IResource resource, boolean readOnly) { 1834 if (isReadOnlySupported()) { 1835 ResourceAttributes resourceAttributes = resource.getResourceAttributes(); 1836 if (resourceAttributes == null) return; resourceAttributes.setReadOnly(readOnly); 1838 try { 1839 resource.setResourceAttributes(resourceAttributes); 1840 } catch (CoreException e) { 1841 } 1843 } 1844 } 1845 public static void sort(char[][] list) { 1846 if (list.length > 1) 1847 quickSort(list, 0, list.length - 1); 1848 } 1849 1850 1853 public static void sort(Comparable [] objects) { 1854 if (objects.length > 1) 1855 quickSort(objects, 0, objects.length - 1); 1856 } 1857 public static void sort(int[] list) { 1858 if (list.length > 1) 1859 quickSort(list, 0, list.length - 1); 1860 } 1861 1862 1866 public static void sort(Object [] objects, Comparer comparer) { 1867 if (objects.length > 1) 1868 quickSort(objects, 0, objects.length - 1, comparer); 1869 } 1870 1871 1874 public static void sort(String [] strings) { 1875 if (strings.length > 1) 1876 quickSort(strings, 0, strings.length - 1); 1877 } 1878 1879 1883 public static Comparable [] sortCopy(Comparable [] objects) { 1884 int len = objects.length; 1885 Comparable [] copy = new Comparable [len]; 1886 System.arraycopy(objects, 0, copy, 0, len); 1887 sort(copy); 1888 return copy; 1889 } 1890 1891 1896 public static IJavaElement[] sortCopy(IJavaElement[] elements) { 1897 int len = elements.length; 1898 IJavaElement[] copy = new IJavaElement[len]; 1899 System.arraycopy(elements, 0, copy, 0, len); 1900 sort(copy, new Comparer() { 1901 public int compare(Object a, Object b) { 1902 return ((JavaElement) a).toStringWithAncestors().compareTo(((JavaElement) b).toStringWithAncestors()); 1903 } 1904 }); 1905 return copy; 1906 } 1907 1908 1912 public static Object [] sortCopy(Object [] objects, Comparer comparer) { 1913 int len = objects.length; 1914 Object [] copy = new Object [len]; 1915 System.arraycopy(objects, 0, copy, 0, len); 1916 sort(copy, comparer); 1917 return copy; 1918 } 1919 1920 1924 public static String [] sortCopy(String [] objects) { 1925 int len = objects.length; 1926 String [] copy = new String [len]; 1927 System.arraycopy(objects, 0, copy, 0, len); 1928 sort(copy); 1929 return copy; 1930 } 1931 1932 1937 public static boolean startsWithIgnoreCase(String [] compoundName, String [] prefix, boolean partialMatch) { 1938 int prefixLength = prefix.length; 1939 int nameLength = compoundName.length; 1940 if (prefixLength > nameLength) return false; 1941 for (int i = 0; i < prefixLength - 1; i++) { 1942 if (!compoundName[i].equalsIgnoreCase(prefix[i])) 1943 return false; 1944 } 1945 return (partialMatch || prefixLength == nameLength) && compoundName[prefixLength-1].toLowerCase().startsWith(prefix[prefixLength-1].toLowerCase()); 1946 } 1947 1948 1951 public static boolean matchesWithIgnoreCase(String [] compoundName, String pattern) { 1952 if (pattern.equals("*")) return true; int nameLength = compoundName.length; 1954 if (pattern.length() == 0) return nameLength == 0; 1955 if (nameLength == 0) return false; 1956 int length = nameLength-1; 1957 for (int i=0; i<nameLength; i++) { 1958 length += compoundName[i].length(); 1959 } 1960 char[] compoundChars = new char[length]; 1961 int pos = 0; 1962 for (int i=0; i<nameLength; i++) { 1963 if (pos > 0) compoundChars[pos++] = '.'; 1964 char[] array = compoundName[i].toCharArray(); 1965 int size = array.length; 1966 System.arraycopy(array, 0, compoundChars, pos, size); 1967 pos += size; 1968 } 1969 return CharOperation.match(pattern.toCharArray(), compoundChars, false); 1970 } 1971 1972 1975 public static char[][] toCharArrays(String [] a) { 1976 int len = a.length; 1977 if (len == 0) return CharOperation.NO_CHAR_CHAR; 1978 char[][] result = new char[len][]; 1979 for (int i = 0; i < len; ++i) { 1980 result[i] = a[i].toCharArray(); 1981 } 1982 return result; 1983 } 1984 1985 1988 public static char[][] toCompoundChars(String s) { 1989 int len = s.length(); 1990 if (len == 0) { 1991 return CharOperation.NO_CHAR_CHAR; 1992 } 1993 int segCount = 1; 1994 for (int off = s.indexOf('.'); off != -1; off = s.indexOf('.', off + 1)) { 1995 ++segCount; 1996 } 1997 char[][] segs = new char[segCount][]; 1998 int start = 0; 1999 for (int i = 0; i < segCount; ++i) { 2000 int dot = s.indexOf('.', start); 2001 int end = (dot == -1 ? s.length() : dot); 2002 segs[i] = new char[end - start]; 2003 s.getChars(start, end, segs[i], 0); 2004 start = end + 1; 2005 } 2006 return segs; 2007 } 2008 2013 public static File toLocalFile(URI uri, IProgressMonitor monitor) throws CoreException { 2014 IFileStore fileStore = EFS.getStore(uri); 2015 File localFile = fileStore.toLocalFile(EFS.NONE, monitor); 2016 if (localFile ==null) 2017 localFile= fileStore.toLocalFile(EFS.CACHE, monitor); 2019 return localFile; 2020 } 2021 2024 public static String toString(char[][] c) { 2025 StringBuffer sb = new StringBuffer (); 2026 for (int i = 0, max = c.length; i < max; ++i) { 2027 if (i != 0) sb.append('.'); 2028 sb.append(c[i]); 2029 } 2030 return sb.toString(); 2031 } 2032 2033 2036 public static String toString(char[][] c, char[] d) { 2037 if (c == null) return new String (d); 2038 StringBuffer sb = new StringBuffer (); 2039 for (int i = 0, max = c.length; i < max; ++i) { 2040 sb.append(c[i]); 2041 sb.append('.'); 2042 } 2043 sb.append(d); 2044 return sb.toString(); 2045 } 2046 2047 2050 public static String [] toStrings(char[][] a) { 2051 int len = a.length; 2052 String [] result = new String [len]; 2053 for (int i = 0; i < len; ++i) { 2054 result[i] = new String (a[i]); 2055 } 2056 return result; 2057 } 2058 private static void appendArrayTypeSignature(char[] string, int start, StringBuffer buffer, boolean compact) { 2059 int length = string.length; 2060 if (start >= length - 1) { 2062 throw new IllegalArgumentException (); 2063 } 2064 char c = string[start]; 2065 if (c != Signature.C_ARRAY) { 2066 throw new IllegalArgumentException (); 2067 } 2068 2069 int index = start; 2070 c = string[++index]; 2071 while(c == Signature.C_ARRAY) { 2072 if (index >= length - 1) { 2074 throw new IllegalArgumentException (); 2075 } 2076 c = string[++index]; 2077 } 2078 2079 appendTypeSignature(string, index, buffer, compact); 2080 2081 for(int i = 0, dims = index - start; i < dims; i++) { 2082 buffer.append('[').append(']'); 2083 } 2084 } 2085 private static void appendClassTypeSignature(char[] string, int start, StringBuffer buffer, boolean compact) { 2086 char c = string[start]; 2087 if (c != Signature.C_RESOLVED) { 2088 return; 2089 } 2090 int p = start + 1; 2091 int checkpoint = buffer.length(); 2092 while (true) { 2093 c = string[p]; 2094 switch(c) { 2095 case Signature.C_SEMICOLON : 2096 return; 2098 case Signature.C_DOT : 2099 case '/' : 2100 if (compact) { 2102 buffer.setLength(checkpoint); 2103 } else { 2104 buffer.append('.'); 2105 } 2106 break; 2107 case Signature.C_DOLLAR : 2108 2114 buffer.append('.'); 2115 break; 2116 default : 2117 buffer.append(c); 2118 } 2119 p++; 2120 } 2121 } 2122 static void appendTypeSignature(char[] string, int start, StringBuffer buffer, boolean compact) { 2123 char c = string[start]; 2124 switch (c) { 2125 case Signature.C_ARRAY : 2126 appendArrayTypeSignature(string, start, buffer, compact); 2127 break; 2128 case Signature.C_RESOLVED : 2129 appendClassTypeSignature(string, start, buffer, compact); 2130 break; 2131 case Signature.C_TYPE_VARIABLE : 2132 int e = Util.scanTypeVariableSignature(string, start); 2133 buffer.append(string, start + 1, e - start - 1); 2134 break; 2135 case Signature.C_BOOLEAN : 2136 buffer.append(BOOLEAN); 2137 break; 2138 case Signature.C_BYTE : 2139 buffer.append(BYTE); 2140 break; 2141 case Signature.C_CHAR : 2142 buffer.append(CHAR); 2143 break; 2144 case Signature.C_DOUBLE : 2145 buffer.append(DOUBLE); 2146 break; 2147 case Signature.C_FLOAT : 2148 buffer.append(FLOAT); 2149 break; 2150 case Signature.C_INT : 2151 buffer.append(INT); 2152 break; 2153 case Signature.C_LONG : 2154 buffer.append(LONG); 2155 break; 2156 case Signature.C_SHORT : 2157 buffer.append(SHORT); 2158 break; 2159 case Signature.C_VOID : 2160 buffer.append(VOID); 2161 break; 2162 } 2163 } 2164 public static String toString(char[] declaringClass, char[] methodName, char[] methodSignature, boolean includeReturnType, boolean compact) { 2165 final boolean isConstructor = CharOperation.equals(methodName, INIT); 2166 int firstParen = CharOperation.indexOf(Signature.C_PARAM_START, methodSignature); 2167 if (firstParen == -1) { 2168 return ""; } 2170 2171 StringBuffer buffer = new StringBuffer (methodSignature.length + 10); 2172 2173 if (declaringClass.length > 0) { 2176 char[] declaringClassSignature = null; 2177 if (declaringClass[0] == Signature.C_ARRAY) { 2178 CharOperation.replace(declaringClass, '/', '.'); 2179 declaringClassSignature = Signature.toCharArray(declaringClass); 2180 } else { 2181 CharOperation.replace(declaringClass, '/', '.'); 2182 declaringClassSignature = declaringClass; 2183 } 2184 int lastIndexOfSlash = CharOperation.lastIndexOf('.', declaringClassSignature); 2185 if (compact && lastIndexOfSlash != -1) { 2186 buffer.append(declaringClassSignature, lastIndexOfSlash + 1, declaringClassSignature.length - lastIndexOfSlash - 1); 2187 } else { 2188 buffer.append(declaringClassSignature); 2189 } 2190 } 2191 2192 if (!isConstructor) { 2194 buffer.append('.'); 2195 if (methodName != null) { 2196 buffer.append(methodName); 2197 } 2198 } 2199 2200 buffer.append('('); 2202 char[][] pts = Signature.getParameterTypes(methodSignature); 2203 for (int i = 0, max = pts.length; i < max; i++) { 2204 appendTypeSignature(pts[i], 0 , buffer, compact); 2205 if (i != pts.length - 1) { 2206 buffer.append(','); 2207 buffer.append(' '); 2208 } 2209 } 2210 buffer.append(')'); 2211 2212 if (!isConstructor) { 2213 buffer.append(" : "); if (includeReturnType) { 2216 char[] rts = Signature.getReturnType(methodSignature); 2217 appendTypeSignature(rts, 0 , buffer, compact); 2218 } 2219 } 2220 return String.valueOf(buffer); 2221 } 2222 2226 public static String [] typeParameterSignatures(AbstractMethodDeclaration method) { 2227 Argument[] args = method.arguments; 2228 if (args != null) { 2229 int length = args.length; 2230 String [] signatures = new String [length]; 2231 for (int i = 0; i < args.length; i++) { 2232 Argument arg = args[i]; 2233 signatures[i] = typeSignature(arg.type); 2234 } 2235 return signatures; 2236 } 2237 return CharOperation.NO_STRINGS; 2238 } 2239 2240 2244 public static String typeSignature(TypeReference type) { 2245 char[][] compoundName = type.getParameterizedTypeName(); 2246 char[] typeName =CharOperation.concatWith(compoundName, '.'); 2247 String signature = Signature.createTypeSignature(typeName, false); 2248 return signature; 2249 } 2250 2251 2254 public static void validateMethodSignature(String sig) { 2255 Assert.isTrue(isValidMethodSignature(sig)); 2256 } 2257 2258 2261 public static void validateTypeSignature(String sig, boolean allowVoid) { 2262 Assert.isTrue(isValidTypeSignature(sig, allowVoid)); 2263 } 2264 public static void verbose(String log) { 2265 verbose(log, System.out); 2266 } 2267 public static synchronized void verbose(String log, PrintStream printStream) { 2268 int start = 0; 2269 do { 2270 int end = log.indexOf('\n', start); 2271 printStream.print(Thread.currentThread()); 2272 printStream.print(" "); printStream.print(log.substring(start, end == -1 ? log.length() : end+1)); 2274 start = end+1; 2275 } while (start != 0); 2276 printStream.println(); 2277 } 2278 2279 2283 public final static boolean isJavaLikeFileName(String name) { 2284 if (name == null) return false; 2285 return indexOfJavaLikeExtension(name) != -1; 2286 } 2287 2288 2292 public final static boolean isJavaLikeFileName(char[] fileName) { 2293 if (fileName == null) return false; 2294 int fileNameLength = fileName.length; 2295 char[][] javaLikeExtensions = getJavaLikeExtensions(); 2296 extensions: for (int i = 0, length = javaLikeExtensions.length; i < length; i++) { 2297 char[] extension = javaLikeExtensions[i]; 2298 int extensionLength = extension.length; 2299 int extensionStart = fileNameLength - extensionLength; 2300 if (extensionStart-1 < 0) continue; 2301 if (fileName[extensionStart-1] != '.') continue; 2302 for (int j = 0; j < extensionLength; j++) { 2303 if (fileName[extensionStart + j] != extension[j]) 2304 continue extensions; 2305 } 2306 return true; 2307 } 2308 return false; 2309 } 2310 2311 2327 public static int scanTypeSignature(char[] string, int start) { 2328 if (start >= string.length) { 2330 throw new IllegalArgumentException (); 2331 } 2332 char c = string[start]; 2333 switch (c) { 2334 case Signature.C_ARRAY : 2335 return scanArrayTypeSignature(string, start); 2336 case Signature.C_RESOLVED : 2337 case Signature.C_UNRESOLVED : 2338 return scanClassTypeSignature(string, start); 2339 case Signature.C_TYPE_VARIABLE : 2340 return scanTypeVariableSignature(string, start); 2341 case Signature.C_BOOLEAN : 2342 case Signature.C_BYTE : 2343 case Signature.C_CHAR : 2344 case Signature.C_DOUBLE : 2345 case Signature.C_FLOAT : 2346 case Signature.C_INT : 2347 case Signature.C_LONG : 2348 case Signature.C_SHORT : 2349 case Signature.C_VOID : 2350 return scanBaseTypeSignature(string, start); 2351 case Signature.C_CAPTURE : 2352 return scanCaptureTypeSignature(string, start); 2353 case Signature.C_EXTENDS: 2354 case Signature.C_SUPER: 2355 case Signature.C_STAR: 2356 return scanTypeBoundSignature(string, start); 2357 default : 2358 throw new IllegalArgumentException (); 2359 } 2360 } 2361 2362 2379 public static int scanBaseTypeSignature(char[] string, int start) { 2380 if (start >= string.length) { 2382 throw new IllegalArgumentException (); 2383 } 2384 char c = string[start]; 2385 if ("BCDFIJSVZ".indexOf(c) >= 0) { return start; 2387 } else { 2388 throw new IllegalArgumentException (); 2389 } 2390 } 2391 2392 2405 public static int scanArrayTypeSignature(char[] string, int start) { 2406 int length = string.length; 2407 if (start >= length - 1) { 2409 throw new IllegalArgumentException (); 2410 } 2411 char c = string[start]; 2412 if (c != Signature.C_ARRAY) { 2413 throw new IllegalArgumentException (); 2414 } 2415 2416 c = string[++start]; 2417 while(c == Signature.C_ARRAY) { 2418 if (start >= length - 1) { 2420 throw new IllegalArgumentException (); 2421 } 2422 c = string[++start]; 2423 } 2424 return scanTypeSignature(string, start); 2425 } 2426 2427 2440 public static int scanCaptureTypeSignature(char[] string, int start) { 2441 if (start >= string.length - 1) { 2443 throw new IllegalArgumentException (); 2444 } 2445 char c = string[start]; 2446 if (c != Signature.C_CAPTURE) { 2447 throw new IllegalArgumentException (); 2448 } 2449 return scanTypeBoundSignature(string, start + 1); 2450 } 2451 2452 2465 public static int scanTypeVariableSignature(char[] string, int start) { 2466 if (start >= string.length - 2) { 2468 throw new IllegalArgumentException (); 2469 } 2470 char c = string[start]; 2472 if (c != Signature.C_TYPE_VARIABLE) { 2473 throw new IllegalArgumentException (); 2474 } 2475 int id = scanIdentifier(string, start + 1); 2476 c = string[id + 1]; 2477 if (c == Signature.C_SEMICOLON) { 2478 return id + 1; 2479 } else { 2480 throw new IllegalArgumentException (); 2481 } 2482 } 2483 2484 2494 public static int scanIdentifier(char[] string, int start) { 2495 if (start >= string.length) { 2497 throw new IllegalArgumentException (); 2498 } 2499 int p = start; 2500 while (true) { 2501 char c = string[p]; 2502 if (c == '<' || c == '>' || c == ':' || c == ';' || c == '.' || c == '/') { 2503 return p - 1; 2504 } 2505 p++; 2506 if (p == string.length) { 2507 return p - 1; 2508 } 2509 } 2510 } 2511 2512 2530 public static int scanClassTypeSignature(char[] string, int start) { 2531 if (start >= string.length - 2) { 2533 throw new IllegalArgumentException (); 2534 } 2535 char c = string[start]; 2537 if (c != Signature.C_RESOLVED && c != Signature.C_UNRESOLVED) { 2538 return -1; 2539 } 2540 int p = start + 1; 2541 while (true) { 2542 if (p >= string.length) { 2543 throw new IllegalArgumentException (); 2544 } 2545 c = string[p]; 2546 if (c == Signature.C_SEMICOLON) { 2547 return p; 2549 } else if (c == Signature.C_GENERIC_START) { 2550 int e = scanTypeArgumentSignatures(string, p); 2551 p = e; 2552 } else if (c == Signature.C_DOT || c == '/') { 2553 int id = scanIdentifier(string, p + 1); 2554 p = id; 2555 } 2556 p++; 2557 } 2558 } 2559 2560 2574 public static int scanTypeBoundSignature(char[] string, int start) { 2575 if (start >= string.length) { 2577 throw new IllegalArgumentException (); 2578 } 2579 char c = string[start]; 2580 switch (c) { 2581 case Signature.C_STAR : 2582 return start; 2583 case Signature.C_SUPER : 2584 case Signature.C_EXTENDS : 2585 if (start >= string.length - 2) { 2587 throw new IllegalArgumentException (); 2588 } 2589 break; 2590 default : 2591 throw new IllegalArgumentException (); 2593 2594 } 2595 c = string[++start]; 2596 switch (c) { 2597 case Signature.C_CAPTURE : 2598 return scanCaptureTypeSignature(string, start); 2599 case Signature.C_SUPER : 2600 case Signature.C_EXTENDS : 2601 return scanTypeBoundSignature(string, start); 2602 case Signature.C_RESOLVED : 2603 case Signature.C_UNRESOLVED : 2604 return scanClassTypeSignature(string, start); 2605 case Signature.C_TYPE_VARIABLE : 2606 return scanTypeVariableSignature(string, start); 2607 case Signature.C_ARRAY : 2608 return scanArrayTypeSignature(string, start); 2609 case Signature.C_STAR: 2610 return start; 2611 default: 2612 throw new IllegalArgumentException (); 2613 } 2614 } 2615 2616 2633 public static int scanTypeArgumentSignatures(char[] string, int start) { 2634 if (start >= string.length - 1) { 2636 throw new IllegalArgumentException (); 2637 } 2638 char c = string[start]; 2639 if (c != Signature.C_GENERIC_START) { 2640 throw new IllegalArgumentException (); 2641 } 2642 int p = start + 1; 2643 while (true) { 2644 if (p >= string.length) { 2645 throw new IllegalArgumentException (); 2646 } 2647 c = string[p]; 2648 if (c == Signature.C_GENERIC_END) { 2649 return p; 2650 } 2651 int e = scanTypeArgumentSignature(string, p); 2652 p = e + 1; 2653 } 2654 } 2655 2656 2674 public static int scanTypeArgumentSignature(char[] string, int start) { 2675 if (start >= string.length) { 2677 throw new IllegalArgumentException (); 2678 } 2679 char c = string[start]; 2680 switch (c) { 2681 case Signature.C_STAR : 2682 return start; 2683 case Signature.C_EXTENDS : 2684 case Signature.C_SUPER : 2685 return scanTypeBoundSignature(string, start); 2686 default : 2687 return scanTypeSignature(string, start); 2688 } 2689 } 2690 2691 2717 public final static char[][][] getAllTypeArguments(char[][] typeSignatures) { 2718 if (typeSignatures == null) return null; 2719 int length = typeSignatures.length; 2720 char[][][] typeArguments = new char[length][][]; 2721 for (int i=0; i<length; i++){ 2722 typeArguments[i] = Signature.getTypeArguments(typeSignatures[i]); 2723 } 2724 return typeArguments; 2725 } 2726 2741 public final static char[][] splitTypeLevelsSignature(String typeSignature) { 2742 char[] source = Signature.removeCapture(typeSignature.toCharArray()); 2744 CharOperation.replace(source, '$', '.'); 2745 2746 char[][] signatures = new char[10][]; 2748 int signaturesCount = 0; 2749 int typeArgsCount = 0; 2751 int paramOpening = 0; 2752 2753 for (int idx=0, ln = source.length; idx < ln; idx++) { 2755 switch (source[idx]) { 2756 case '>': 2757 paramOpening--; 2758 if (paramOpening == 0) { 2759 if (signaturesCount == signatures.length) { 2760 System.arraycopy(signatures, 0, signatures = new char[signaturesCount+10][], 0, signaturesCount); 2761 } 2762 typeArgsCount = 0; 2763 } 2764 break; 2765 case '<': 2766 paramOpening++; 2767 if (paramOpening == 1) { 2768 typeArgsCount = 1; 2769 } 2770 break; 2771 case '*': 2772 case ';': 2773 if (paramOpening == 1) typeArgsCount++; 2774 break; 2775 case '.': 2776 if (paramOpening == 0) { 2777 if (signaturesCount == signatures.length) { 2778 System.arraycopy(signatures, 0, signatures = new char[signaturesCount+10][], 0, signaturesCount); 2779 } 2780 signatures[signaturesCount] = new char[idx+1]; 2781 System.arraycopy(source, 0, signatures[signaturesCount], 0, idx); 2782 signatures[signaturesCount][idx] = Signature.C_SEMICOLON; 2783 signaturesCount++; 2784 } 2785 break; 2786 case '/': 2787 source[idx] = '.'; 2788 break; 2789 } 2790 } 2791 2792 char[][] typeSignatures = new char[signaturesCount+1][]; 2794 typeSignatures[0] = source; 2795 for (int i=1, j=signaturesCount-1; i<=signaturesCount; i++, j--){ 2796 typeSignatures[i] = signatures[j]; 2797 } 2798 return typeSignatures; 2799 } 2800 2801 2804 public static String toAnchor(char[] methodSignature, String methodName, boolean isVarArgs) { 2805 try { 2806 return new String (toAnchor(methodSignature, methodName.toCharArray(), isVarArgs)); 2807 } catch(IllegalArgumentException e) { 2808 return null; 2809 } 2810 } 2811 private static char[] toAnchor(char[] methodSignature, char[] methodName, boolean isVargArgs) { 2812 int firstParen = CharOperation.indexOf(Signature.C_PARAM_START, methodSignature); 2813 if (firstParen == -1) { 2814 throw new IllegalArgumentException (); 2815 } 2816 2817 StringBuffer buffer = new StringBuffer (methodSignature.length + 10); 2818 2819 if (methodName != null) { 2821 buffer.append(methodName); 2822 } 2823 2824 buffer.append('('); 2826 char[][] pts = Signature.getParameterTypes(methodSignature); 2827 for (int i = 0, max = pts.length; i < max; i++) { 2828 if (i == max - 1) { 2829 appendTypeSignatureForAnchor(pts[i], 0 , buffer, isVargArgs); 2830 } else { 2831 appendTypeSignatureForAnchor(pts[i], 0 , buffer, false); 2832 } 2833 if (i != pts.length - 1) { 2834 buffer.append(','); 2835 buffer.append(' '); 2836 } 2837 } 2838 buffer.append(')'); 2839 char[] result = new char[buffer.length()]; 2840 buffer.getChars(0, buffer.length(), result, 0); 2841 return result; 2842 } 2843 2844 private static int appendTypeSignatureForAnchor(char[] string, int start, StringBuffer buffer, boolean isVarArgs) { 2845 if (start >= string.length) { 2847 throw new IllegalArgumentException (); 2848 } 2849 char c = string[start]; 2850 if (isVarArgs) { 2851 switch (c) { 2852 case Signature.C_ARRAY : 2853 return appendArrayTypeSignatureForAnchor(string, start, buffer, true); 2854 case Signature.C_RESOLVED : 2855 case Signature.C_TYPE_VARIABLE : 2856 case Signature.C_BOOLEAN : 2857 case Signature.C_BYTE : 2858 case Signature.C_CHAR : 2859 case Signature.C_DOUBLE : 2860 case Signature.C_FLOAT : 2861 case Signature.C_INT : 2862 case Signature.C_LONG : 2863 case Signature.C_SHORT : 2864 case Signature.C_VOID : 2865 case Signature.C_STAR: 2866 case Signature.C_EXTENDS: 2867 case Signature.C_SUPER: 2868 case Signature.C_CAPTURE: 2869 default: 2870 throw new IllegalArgumentException (); } 2872 } else { 2873 switch (c) { 2874 case Signature.C_ARRAY : 2875 return appendArrayTypeSignatureForAnchor(string, start, buffer, false); 2876 case Signature.C_RESOLVED : 2877 return appendClassTypeSignatureForAnchor(string, start, buffer); 2878 case Signature.C_TYPE_VARIABLE : 2879 int e = Util.scanTypeVariableSignature(string, start); 2880 buffer.append(string, start + 1, e - start - 1); 2881 return e; 2882 case Signature.C_BOOLEAN : 2883 buffer.append(BOOLEAN); 2884 return start; 2885 case Signature.C_BYTE : 2886 buffer.append(BYTE); 2887 return start; 2888 case Signature.C_CHAR : 2889 buffer.append(CHAR); 2890 return start; 2891 case Signature.C_DOUBLE : 2892 buffer.append(DOUBLE); 2893 return start; 2894 case Signature.C_FLOAT : 2895 buffer.append(FLOAT); 2896 return start; 2897 case Signature.C_INT : 2898 buffer.append(INT); 2899 return start; 2900 case Signature.C_LONG : 2901 buffer.append(LONG); 2902 return start; 2903 case Signature.C_SHORT : 2904 buffer.append(SHORT); 2905 return start; 2906 case Signature.C_VOID : 2907 buffer.append(VOID); 2908 return start; 2909 case Signature.C_CAPTURE : 2910 return appendCaptureTypeSignatureForAnchor(string, start, buffer); 2911 case Signature.C_STAR: 2912 case Signature.C_EXTENDS: 2913 case Signature.C_SUPER: 2914 return appendTypeArgumentSignatureForAnchor(string, start, buffer); 2915 default : 2916 throw new IllegalArgumentException (); 2917 } 2918 } 2919 } 2920 private static int appendTypeArgumentSignatureForAnchor(char[] string, int start, StringBuffer buffer) { 2921 if (start >= string.length) { 2923 throw new IllegalArgumentException (); 2924 } 2925 char c = string[start]; 2926 switch(c) { 2927 case Signature.C_STAR : 2928 return start; 2929 case Signature.C_EXTENDS : 2930 return appendTypeSignatureForAnchor(string, start + 1, buffer, false); 2931 case Signature.C_SUPER : 2932 return appendTypeSignatureForAnchor(string, start + 1, buffer, false); 2933 default : 2934 return appendTypeSignatureForAnchor(string, start, buffer, false); 2935 } 2936 } 2937 private static int appendCaptureTypeSignatureForAnchor(char[] string, int start, StringBuffer buffer) { 2938 if (start >= string.length - 1) { 2940 throw new IllegalArgumentException (); 2941 } 2942 char c = string[start]; 2943 if (c != Signature.C_CAPTURE) { 2944 throw new IllegalArgumentException (); 2945 } 2946 return appendTypeArgumentSignatureForAnchor(string, start + 1, buffer); 2947 } 2948 private static int appendArrayTypeSignatureForAnchor(char[] string, int start, StringBuffer buffer, boolean isVarArgs) { 2949 int length = string.length; 2950 if (start >= length - 1) { 2952 throw new IllegalArgumentException (); 2953 } 2954 char c = string[start]; 2955 if (c != Signature.C_ARRAY) { 2956 throw new IllegalArgumentException (); 2957 } 2958 2959 int index = start; 2960 c = string[++index]; 2961 while(c == Signature.C_ARRAY) { 2962 if (index >= length - 1) { 2964 throw new IllegalArgumentException (); 2965 } 2966 c = string[++index]; 2967 } 2968 2969 int e = appendTypeSignatureForAnchor(string, index, buffer, false); 2970 2971 for(int i = 1, dims = index - start; i < dims; i++) { 2972 buffer.append('[').append(']'); 2973 } 2974 2975 if (isVarArgs) { 2976 buffer.append('.').append('.').append('.'); 2977 } else { 2978 buffer.append('[').append(']'); 2979 } 2980 return e; 2981 } 2982 private static int appendClassTypeSignatureForAnchor(char[] string, int start, StringBuffer buffer) { 2983 if (start >= string.length - 2) { 2985 throw new IllegalArgumentException (); 2986 } 2987 char c = string[start]; 2989 if (c != Signature.C_RESOLVED && c != Signature.C_UNRESOLVED) { 2990 throw new IllegalArgumentException (); 2991 } 2992 int p = start + 1; 2993 while (true) { 2994 if (p >= string.length) { 2995 throw new IllegalArgumentException (); 2996 } 2997 c = string[p]; 2998 switch(c) { 2999 case Signature.C_SEMICOLON : 3000 return p; 3002 case Signature.C_GENERIC_START : 3003 int e = scanGenericEnd(string, p + 1); 3004 p = e; 3006 break; 3007 case Signature.C_DOT : 3008 buffer.append('.'); 3009 break; 3010 case '/' : 3011 buffer.append('/'); 3012 break; 3013 case Signature.C_DOLLAR : 3014 3021 buffer.append('.'); 3022 break; 3023 default : 3024 buffer.append(c); 3025 } 3026 p++; 3027 } 3028 } 3029 private static int scanGenericEnd(char[] string, int start) { 3030 if (string[start] == Signature.C_GENERIC_END) { 3031 return start; 3032 } 3033 int length = string.length; 3034 int balance = 1; 3035 start++; 3036 while (start <= length) { 3037 switch(string[start]) { 3038 case Signature.C_GENERIC_END : 3039 balance--; 3040 if (balance == 0) { 3041 return start; 3042 } 3043 break; 3044 case Signature.C_GENERIC_START : 3045 balance++; 3046 break; 3047 } 3048 start++; 3049 } 3050 return start; 3051 } 3052} 3053 | Popular Tags |