KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > util > Util


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.util;
12
13 import java.io.*;
14 import java.net.URI JavaDoc;
15 import java.util.*;
16 import java.util.zip.ZipEntry JavaDoc;
17 import java.util.zip.ZipFile JavaDoc;
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 /**
56  * Provides convenient utility methods to other types in this package.
57  */

58 public class Util {
59
60     public interface Comparable {
61         /**
62          * Returns 0 if this and c are equal, >0 if this is greater than c,
63          * or <0 if this is less than c.
64          */

65         int compareTo(Comparable JavaDoc c);
66     }
67
68     public interface Comparer {
69         /**
70          * Returns 0 if a and b are equal, >0 if a is greater than b,
71          * or <0 if a is less than b.
72          */

73         int compare(Object JavaDoc a, Object JavaDoc b);
74     }
75     private static final String JavaDoc ARGUMENTS_DELIMITER = "#"; //$NON-NLS-1$
76

77     private static final String JavaDoc EMPTY_ARGUMENT = " "; //$NON-NLS-1$
78

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(); //$NON-NLS-1$
83
private static final char[] BYTE = "byte".toCharArray(); //$NON-NLS-1$
84
private static final char[] CHAR = "char".toCharArray(); //$NON-NLS-1$
85
private static final char[] DOUBLE = "double".toCharArray(); //$NON-NLS-1$
86
private static final char[] FLOAT = "float".toCharArray(); //$NON-NLS-1$
87
private static final char[] INT = "int".toCharArray(); //$NON-NLS-1$
88
private static final char[] LONG = "long".toCharArray(); //$NON-NLS-1$
89
private static final char[] SHORT = "short".toCharArray(); //$NON-NLS-1$
90
private static final char[] VOID = "void".toCharArray(); //$NON-NLS-1$
91
private static final char[] INIT = "<init>".toCharArray(); //$NON-NLS-1$
92

93     private Util() {
94         // cannot be instantiated
95
}
96     
97     /**
98      * Returns a new array adding the second array at the end of first array.
99      * It answers null if the first and second are null.
100      * If the first array is null or if it is empty, then a new array is created with second.
101      * If the second array is null, then the first array is returned.
102      * <br>
103      * <br>
104      * For example:
105      * <ol>
106      * <li><pre>
107      * first = null
108      * second = "a"
109      * => result = {"a"}
110      * </pre>
111      * <li><pre>
112      * first = {"a"}
113      * second = null
114      * => result = {"a"}
115      * </pre>
116      * </li>
117      * <li><pre>
118      * first = {"a"}
119      * second = {"b"}
120      * => result = {"a", "b"}
121      * </pre>
122      * </li>
123      * </ol>
124      *
125      * @param first the first array to concatenate
126      * @param second the array to add at the end of the first array
127      * @return a new array adding the second array at the end of first array, or null if the two arrays are null.
128      */

129     public static final String JavaDoc[] arrayConcat(String JavaDoc[] first, String JavaDoc second) {
130         if (second == null)
131             return first;
132         if (first == null)
133             return new String JavaDoc[] {second};
134
135         int length = first.length;
136         if (first.length == 0) {
137             return new String JavaDoc[] {second};
138         }
139         
140         String JavaDoc[] result = new String JavaDoc[length + 1];
141         System.arraycopy(first, 0, result, 0, length);
142         result[length] = second;
143         return result;
144     }
145
146     /**
147      * Checks the type signature in String sig,
148      * starting at start and ending before end (end is not included).
149      * Returns the index of the character immediately after the signature if valid,
150      * or -1 if not valid.
151      */

152     private static int checkTypeSignature(String JavaDoc 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                 // array of void is not allowed
175
if (nestingDepth != 0) return -1;
176                 break;
177             case 'L':
178                 int semicolon = sig.indexOf(';', i);
179                 // Must have at least one character between L and ;
180
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     /**
190      * Combines two hash codes to make a new one.
191      */

192     public static int combineHashCodes(int hashCode1, int hashCode2) {
193         return hashCode1 * 17 + hashCode2;
194     }
195     
196     /**
197      * Compares two byte arrays.
198      * Returns <0 if a byte in a is less than the corresponding byte in b, or if a is shorter, or if a is null.
199      * Returns >0 if a byte in a is greater than the corresponding byte in b, or if a is longer, or if b is null.
200      * Returns 0 if they are equal or both null.
201      */

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     /**
222      * Compares two strings lexicographically.
223      * The comparison is based on the Unicode value of each character in
224      * the strings.
225      *
226      * @return the value <code>0</code> if the str1 is equal to str2;
227      * a value less than <code>0</code> if str1
228      * is lexicographically less than str2;
229      * and a value greater than <code>0</code> if str1 is
230      * lexicographically greater than str2.
231      */

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     /**
248      * Concatenate two strings with a char in between.
249      * @see #concat(String, String)
250      */

251     public static String JavaDoc concat(String JavaDoc s1, char c, String JavaDoc s2) {
252         if (s1 == null) s1 = "null"; //$NON-NLS-1$
253
if (s2 == null) s2 = "null"; //$NON-NLS-1$
254
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 JavaDoc(buf);
261     }
262     
263     /**
264      * Concatenate two strings.
265      * Much faster than using +, which:
266      * - creates a StringBuffer,
267      * - which is synchronized,
268      * - of default size, so the resulting char array is
269      * often larger than needed.
270      * This implementation creates an extra char array, since the
271      * String constructor copies its argument, but there's no way around this.
272      */

273     public static String JavaDoc concat(String JavaDoc s1, String JavaDoc s2) {
274         if (s1 == null) s1 = "null"; //$NON-NLS-1$
275
if (s2 == null) s2 = "null"; //$NON-NLS-1$
276
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 JavaDoc(buf);
282     }
283
284     /**
285      * Returns the concatenation of the given array parts using the given separator between each part.
286      * <br>
287      * <br>
288      * For example:<br>
289      * <ol>
290      * <li><pre>
291      * array = {"a", "b"}
292      * separator = '.'
293      * => result = "a.b"
294      * </pre>
295      * </li>
296      * <li><pre>
297      * array = {}
298      * separator = '.'
299      * => result = ""
300      * </pre></li>
301      * </ol>
302      *
303      * @param array the given array
304      * @param separator the given separator
305      * @return the concatenation of the given array parts using the given separator between each part
306      */

307     public static final String JavaDoc concatWith(String JavaDoc[] array, char separator) {
308         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
318      * Returns the concatenation of the given array parts using the given separator between each
319      * part and appending the given name at the end.
320      * <br>
321      * <br>
322      * For example:<br>
323      * <ol>
324      * <li><pre>
325      * name = "c"
326      * array = { "a", "b" }
327      * separator = '.'
328      * => result = "a.b.c"
329      * </pre>
330      * </li>
331      * <li><pre>
332      * name = null
333      * array = { "a", "b" }
334      * separator = '.'
335      * => result = "a.b"
336      * </pre></li>
337      * <li><pre>
338      * name = " c"
339      * array = null
340      * separator = '.'
341      * => result = "c"
342      * </pre></li>
343      * </ol>
344      *
345      * @param array the given array
346      * @param name the given name
347      * @param separator the given separator
348      * @return the concatenation of the given array parts using the given separator between each
349      * part and appending the given name at the end
350      */

351     public static final String JavaDoc concatWith(
352         String JavaDoc[] array,
353         String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
369      * Concatenate three strings.
370      * @see #concat(String, String)
371      */

372     public static String JavaDoc concat(String JavaDoc s1, String JavaDoc s2, String JavaDoc s3) {
373         if (s1 == null) s1 = "null"; //$NON-NLS-1$
374
if (s2 == null) s2 = "null"; //$NON-NLS-1$
375
if (s3 == null) s3 = "null"; //$NON-NLS-1$
376
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 JavaDoc(buf);
384     }
385         
386     /**
387      * Converts a type signature from the IBinaryType representation to the DC representation.
388      */

389     public static String JavaDoc convertTypeSignature(char[] sig, int start, int length) {
390         return new String JavaDoc(sig, start, length).replace('/', '.');
391     }
392     
393     /*
394      * Returns the default java extension (".java").
395      * To be used when the extension is not known.
396      */

397     public static String JavaDoc defaultJavaExtension() {
398         return SuffixConstants.SUFFIX_STRING_java;
399     }
400
401     /**
402      * Apply the given edit on the given string and return the updated string.
403      * Return the given string if anything wrong happen while applying the edit.
404      *
405      * @param original the given string
406      * @param edit the given edit
407      *
408      * @return the updated string
409      */

410     public final static String JavaDoc editedString(String JavaDoc 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     /**
427      * Returns true iff str.toLowerCase().endsWith(end.toLowerCase())
428      * implementation is not creating extra strings.
429      */

430     public final static boolean endsWithIgnoreCase(String JavaDoc str, String JavaDoc end) {
431         
432         int strLength = str == null ? 0 : str.length();
433         int endLength = end == null ? 0 : end.length();
434         
435         // return false if the string is smaller than the end.
436
if(endLength > strLength)
437             return false;
438             
439         // return false if any character of the end are
440
// not the same in lower case.
441
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     /**
450      * Compares two arrays using equals() on the elements.
451      * Neither can be null. Only the first len elements are compared.
452      * Return false if either array is shorter than len.
453      */

454     public static boolean equalArrays(Object JavaDoc[] a, Object JavaDoc[] 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     /**
468      * Compares two arrays using equals() on the elements.
469      * Either or both arrays may be null.
470      * Returns true if both are null.
471      * Returns false if only one is null.
472      * If both are arrays, returns true iff they have the same length and
473      * all elements are equal.
474      */

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     /**
491      * Compares two arrays using equals() on the elements.
492      * Either or both arrays may be null.
493      * Returns true if both are null.
494      * Returns false if only one is null.
495      * If both are arrays, returns true iff they have the same length and
496      * all elements compare true with equals.
497      */

498     public static boolean equalArraysOrNull(Object JavaDoc[] a, Object JavaDoc[] 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         // walk array from end to beginning as this optimizes package name cases
505
// where the first part is always the same (e.g. org.eclipse.jdt)
506
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     /**
517      * Compares two arrays using equals() on the elements.
518      * The arrays are first sorted.
519      * Either or both arrays may be null.
520      * Returns true if both are null.
521      * Returns false if only one is null.
522      * If both are arrays, returns true iff they have the same length and
523      * iff, after sorting both arrays, all elements compare true with equals.
524      * The original arrays are left untouched.
525      */

526     public static boolean equalArraysOrNullSortFirst(Comparable JavaDoc[] a, Comparable JavaDoc[] 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) { // only need to sort if more than two items
532
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     /**
542      * Compares two String arrays using equals() on the elements.
543      * The arrays are first sorted.
544      * Either or both arrays may be null.
545      * Returns true if both are null.
546      * Returns false if only one is null.
547      * If both are arrays, returns true iff they have the same length and
548      * iff, after sorting both arrays, all elements compare true with equals.
549      * The original arrays are left untouched.
550      */

551     public static boolean equalArraysOrNullSortFirst(String JavaDoc[] a, String JavaDoc[] 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) { // only need to sort if more than two items
557
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     /**
567      * Compares two objects using equals().
568      * Either or both array may be null.
569      * Returns true if both are null.
570      * Returns false if only one is null.
571      * Otherwise, return the result of comparing with equals().
572      */

573     public static boolean equalOrNull(Object JavaDoc a, Object JavaDoc 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     /*
584      * Returns whether the given file name equals to the given string ignoring the java like extension
585      * of the file name.
586      * Returns false if it is not a java like file name.
587      */

588     public static boolean equalsIgnoreJavaLikeExtension(String JavaDoc fileName, String JavaDoc 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     /**
613      * Given a qualified name, extract the last component.
614      * If the input is not qualified, the same string is answered.
615      */

616     public static String JavaDoc extractLastName(String JavaDoc qualifiedName) {
617         int i = qualifiedName.lastIndexOf('.');
618         if (i == -1) return qualifiedName;
619         return qualifiedName.substring(i+1);
620     }
621     
622     /**
623      * Extracts the parameter types from a method signature.
624      */

625     public static String JavaDoc[] extractParameterTypes(char[] sig) {
626         int count = getParameterCount(sig);
627         String JavaDoc[] result = new String JavaDoc[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     /**
658      * Extracts the return type from a method signature.
659      */

660     public static String JavaDoc extractReturnType(String JavaDoc 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             // ignore
678
}
679         return null;
680     }
681     
682     /**
683      * Finds the first line separator used by the given text.
684      *
685      * @return </code>"\n"</code> or </code>"\r"</code> or </code>"\r\n"</code>,
686      * or <code>null</code> if none found
687      */

688     public static String JavaDoc findLineSeparator(char[] text) {
689         // find the first line separator
690
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"; //$NON-NLS-1$
698
case '\r': return nextChar == '\n' ? "\r\n" : "\r"; //$NON-NLS-1$ //$NON-NLS-2$
699
}
700             }
701         }
702         // not found
703
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     /**
747      * Returns the registered Java like extensions.
748      */

749     public static char[][] getJavaLikeExtensions() {
750         if (JAVA_LIKE_EXTENSIONS == null) {
751             // TODO (jerome) reenable once JDT UI supports other file extensions (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=71460)
752
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                 // content types derived from java content type should be included (https://bugs.eclipse.org/bugs/show_bug.cgi?id=121715)
758
IContentType[] contentTypes = Platform.getContentTypeManager().getAllContentTypes();
759                 for (int i = 0, length = contentTypes.length; i < length; i++) {
760                     if (contentTypes[i].isKindOf(javaContentType)) { // note that javaContentType.isKindOf(javaContentType) == true
761
String JavaDoc[] 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                 // note that file extensions contains "java" as it is defined in JDT Core's plugin.xml
769
char[][] extensions = new char[length][];
770                 extensions[0] = SuffixConstants.EXTENSION_java.toCharArray(); // ensure that "java" is first
771
int index = 1;
772                 Iterator iterator = fileExtensions.iterator();
773                 while (iterator.hasNext()) {
774                     String JavaDoc fileExtension = (String JavaDoc) iterator.next();
775                     if (SuffixConstants.EXTENSION_java.equals(fileExtension))
776                         continue;
777            &