| 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 &
|