replaced a lot of deprecated code; if someone runs into a commit conflict afterwards...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / util / JavaModelUtil.java
index a098975..d3b4949 100644 (file)
@@ -1,10 +1,10 @@
 /*******************************************************************************
  * Copyright (c) 2000, 2003 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials 
+ * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Common Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/cpl-v10.html
- * 
+ *
  * Contributors:
  *     IBM Corporation - initial API and implementation
  *******************************************************************************/
@@ -36,13 +36,13 @@ import org.eclipse.core.runtime.IPath;
  * Utility methods for the Java Model.
  */
 public class JavaModelUtil {
-       
-       /** 
+
+       /**
         * Finds a type by its qualified type name (dot separated).
         * @param jproject The java project to search in
         * @param str The fully qualified name (type name with enclosing type names and package (all separated by dots))
         * @return The type found, or null if not existing
-        */     
+        */
 //     public static IType findType(IJavaProject jproject, String fullyQualifiedName) throws JavaModelException {
 //             //workaround for bug 22883
 //             IType type= jproject.findType(fullyQualifiedName);
@@ -54,10 +54,10 @@ public class JavaModelUtil {
 //                     type= findType(root, fullyQualifiedName);
 //                     if (type != null && type.exists())
 //                             return type;
-//             }       
+//             }
 //             return null;
 //     }
-       
+
        /**
         * Returns <code>true</code> if the given package fragment root is
         * referenced. This means it is own by a different project but is referenced
@@ -73,7 +73,7 @@ public class JavaModelUtil {
 //             }
                return false;
        }
-       
+
 //     private static IType findType(IPackageFragmentRoot root, String fullyQualifiedName) throws JavaModelException{
 //             IJavaElement[] children= root.getChildren();
 //             for (int i= 0; i < children.length; i++) {
@@ -86,10 +86,10 @@ public class JavaModelUtil {
 //                             if (type != null && type.exists())
 //                                     return type;
 //                     }
-//             }               
+//             }
 //             return null;
 //     }
-       
+
 //     private static IType findType(IPackageFragment pack, String fullyQualifiedName) throws JavaModelException{
 //             ICompilationUnit[] cus= pack.getCompilationUnits();
 //             for (int i= 0; i < cus.length; i++) {
@@ -101,7 +101,7 @@ public class JavaModelUtil {
 //             }
 //             return null;
 //     }
-       
+
 //     private static IType findType(ICompilationUnit cu, String fullyQualifiedName) throws JavaModelException{
 //             IType[] types= cu.getAllTypes();
 //             for (int i= 0; i < types.length; i++) {
@@ -111,15 +111,15 @@ public class JavaModelUtil {
 //             }
 //             return null;
 //     }
-       
-       /** 
+
+       /**
         * Finds a type by package and type name.
         * @param jproject the java project to search in
         * @param pack The package name
         * @param typeQualifiedName the type qualified name (type name with enclosing type names (separated by dots))
         * @return the type found, or null if not existing
         * @deprecated Use IJavaProject.findType(String, String) instead
-        */     
+        */
 //     public static IType findType(IJavaProject jproject, String pack, String typeQualifiedName) throws JavaModelException {
 //             return jproject.findType(pack, typeQualifiedName);
 //     }
@@ -142,18 +142,18 @@ public class JavaModelUtil {
 //                     if (!(result instanceof IPackageFragment)) {
 //                             result= null;
 //                     }
-//                     
+//
 //             }
 //             return result;
-//     }       
-       
-       /** 
+//     }
+
+       /**
         * Finds a type in a compilation unit. Typical usage is to find the corresponding
         * type in a working copy.
         * @param cu the compilation unit to search in
         * @param typeQualifiedName the type qualified name (type name with enclosing type names (separated by dots))
         * @return the type found, or null if not existing
-        */             
+        */
        public static IType findTypeInCompilationUnit(ICompilationUnit cu, String typeQualifiedName) throws JavaModelException {
                IType[] types= cu.getAllTypes();
                for (int i= 0; i < types.length; i++) {
@@ -164,14 +164,14 @@ public class JavaModelUtil {
                }
                return null;
        }
-               
-       /** 
+
+       /**
         * Finds a a member in a compilation unit. Typical usage is to find the corresponding
         * member in a working copy.
         * @param cu the compilation unit (eg. working copy) to search in
         * @param member the member (eg. from the original)
         * @return the member found, or null if not existing
-        */             
+        */
        public static IMember findMemberInCompilationUnit(ICompilationUnit cu, IMember member) throws JavaModelException {
                IJavaElement[] elements= cu.findElements(member);
                if (elements != null && elements.length > 0) {
@@ -179,17 +179,17 @@ public class JavaModelUtil {
                }
                return null;
        }
-       
-       
-       /** 
+
+
+       /**
         * Returns the element of the given compilation unit which is "equal" to the
         * given element. Note that the given element usually has a parent different
         * from the given compilation unit.
-        * 
+        *
         * @param cu the cu to search in
         * @param element the element to look for
         * @return an element of the given cu "equal" to the given element
-        */             
+        */
        public static IJavaElement findInCompilationUnit(ICompilationUnit cu, IJavaElement element) throws JavaModelException {
                IJavaElement[] elements= cu.findElements(element);
                if (elements != null && elements.length > 0) {
@@ -197,7 +197,7 @@ public class JavaModelUtil {
                }
                return null;
        }
-       
+
        /**
         * Returns the qualified type name of the given type using '.' as separators.
         * This is a replace for IType.getTypeQualifiedName()
@@ -207,7 +207,7 @@ public class JavaModelUtil {
        public static String getTypeQualifiedName(IType type) {
                return type.getTypeQualifiedName('.');
        }
-       
+
        private static void getTypeQualifiedName(IType type, StringBuffer buf) {
                IType outerType= type.getDeclaringType();
                if (outerType != null) {
@@ -215,7 +215,7 @@ public class JavaModelUtil {
                        buf.append('.');
                }
                buf.append(type.getElementName());
-       }       
+       }
 
        /**
         * Returns the fully qualified name of the given type using '.' as separators.
@@ -226,7 +226,7 @@ public class JavaModelUtil {
        public static String getFullyQualifiedName(IType type) {
                return type.getFullyQualifiedName('.');
        }
-       
+
        /**
         * Returns the fully qualified name of a type's container. (package name or enclosing type name)
         */
@@ -238,8 +238,8 @@ public class JavaModelUtil {
                        return type.getPackageFragment().getElementName();
                }
        }
-       
-       
+
+
        /**
         * Concatenates two names. Uses a dot for separation.
         * Both strings can be empty or <code>null</code>.
@@ -254,10 +254,10 @@ public class JavaModelUtil {
                                buf.append('.');
                        }
                        buf.append(name2);
-               }               
+               }
                return buf.toString();
        }
-       
+
        /**
         * Concatenates two names. Uses a dot for separation.
         * Both strings can be empty or <code>null</code>.
@@ -272,10 +272,10 @@ public class JavaModelUtil {
                                buf.append('.');
                        }
                        buf.append(name2);
-               }               
+               }
                return buf.toString();
-       }       
-       
+       }
+
        /**
         * Evaluates if a member (possible from another package) is visible from
         * elements in a package.
@@ -284,17 +284,17 @@ public class JavaModelUtil {
         */
        public static boolean isVisible(IMember member, IPackageFragment pack) throws JavaModelException {
                int otherflags= member.getFlags();
-               
+
                if (Flags.isPublic(otherflags)) {
                        return true;
                } else if (Flags.isPrivate(otherflags)) {
                        return false;
-               }               
-               
+               }
+
                IPackageFragment otherpack= (IPackageFragment) findParentOfKind(member, IJavaElement.PACKAGE_FRAGMENT);
                return (pack != null && pack.equals(otherpack));
        }
-               
+
        /**
         * Returns the package fragment root of <code>IJavaElement</code>. If the given
         * element is already a package fragment root, the element itself is returned.
@@ -304,7 +304,7 @@ public class JavaModelUtil {
        }
 
        /**
-        * Returns the parent of the supplied java element that conforms to the given 
+        * Returns the parent of the supplied java element that conforms to the given
         * parent type or <code>null</code>, if such a parent doesn't exit.
         * @deprecated Use element.getParent().getAncestor(kind);
         */
@@ -314,7 +314,7 @@ public class JavaModelUtil {
                }
                return null;
        }
-       
+
        /**
         * Finds a method in a type.
         * This searches for a method with the same name and signature. Parameter types are only
@@ -348,7 +348,7 @@ public class JavaModelUtil {
                }
                return null;
        }
-       
+
 
        /**
         * Finds a method declararion in a type's hierarchy. The search is top down, so this
@@ -377,7 +377,7 @@ public class JavaModelUtil {
 //             }
 //             return null;
 //     }
-       
+
        /**
         * Finds a method implementation in a type's classhierarchy. The search is bottom-up, so this
         * returns the nearest overridden method. Does not find methods in interfaces or abstract methods.
@@ -402,8 +402,8 @@ public class JavaModelUtil {
 //                     }
 //             }
 //             return null;
-//     }       
-       
+//     }
+
        /**
         * Tests if a method equals to the given signature.
         * Parameter types are only compared by the simple name, no resolving for
@@ -432,7 +432,7 @@ public class JavaModelUtil {
                }
                return false;
        }
-       
+
        /**
         * Checks whether the given type has a valid main method or not.
         */
@@ -445,22 +445,22 @@ public class JavaModelUtil {
                }
                return false;
        }
-       
+
        /**
         * Checks if the field is boolean.
         */
        public static boolean isBoolean(IField field) throws JavaModelException{
                return field.getTypeSignature().equals(Signature.SIG_BOOLEAN);
        }
-       
+
        /**
         * Returns true if the element is on the build path of the given project
         * @deprecated Use jproject.isOnClasspath(element);
-        */     
+        */
 //     public static boolean isOnBuildPath(IJavaProject jproject, IJavaElement element) throws JavaModelException {
 //             return jproject.isOnClasspath(element);
 //     }
-       
+
        /**
         * Tests if the given element is on the class path of its containing project. Handles the case
         * that the containing project isn't a Java project.
@@ -477,7 +477,7 @@ public class JavaModelUtil {
         * @param refTypeSig the type name in signature notation (for example 'QVector')
         *                   this can also be an array type, but dimensions will be ignored.
         * @param declaringType the context for resolving (type where the reference was made in)
-        * @return returns the fully qualified type name or build-in-type name. 
+        * @return returns the fully qualified type name or build-in-type name.
         *                      if a unresoved type couldn't be resolved null is returned
         */
        public static String getResolvedTypeName(String refTypeSig, IType declaringType) throws JavaModelException {
@@ -488,8 +488,8 @@ public class JavaModelUtil {
                        if (semi == -1) {
                                throw new IllegalArgumentException();
                        }
-                       String name= refTypeSig.substring(arrayCount + 1, semi);                                
-                       
+                       String name= refTypeSig.substring(arrayCount + 1, semi);
+
 //                     String[][] resolvedNames= declaringType.resolveType(name);
 //                     if (resolvedNames != null && resolvedNames.length > 0) {
 //                             return JavaModelUtil.concatenateName(resolvedNames[0][0], resolvedNames[0][1]);
@@ -499,7 +499,7 @@ public class JavaModelUtil {
                        return Signature.toString(refTypeSig.substring(arrayCount));
                }
        }
-       
+
        /**
         * Returns if a CU can be edited.
         */
@@ -508,12 +508,12 @@ public class JavaModelUtil {
                        cu= (ICompilationUnit) cu.getOriginalElement();
                }
                IResource resource= cu.getResource();
-               return (resource.exists() && !resource.isReadOnly());
+               return (resource.exists() && !resource.getResourceAttributes().isReadOnly());
        }
 
        /**
         * Finds a qualified import for a type name.
-        */     
+        */
 //     public static IImportDeclaration findImport(ICompilationUnit cu, String simpleName) throws JavaModelException {
 //             IImportDeclaration[] existing= cu.getImports();
 //             for (int i= 0; i < existing.length; i++) {
@@ -524,10 +524,10 @@ public class JavaModelUtil {
 //                                     return existing[i];
 //                             }
 //                     }
-//             }       
+//             }
 //             return null;
 //     }
-       
+
        /**
         * Returns the original if the given member. If the member is already
         * an original the input is returned. The returned member must not exist
@@ -540,7 +540,7 @@ public class JavaModelUtil {
                        return (IMember)cu.getOriginal(member);
                return member;
        }
-       
+
        /*
         * XXX workaround for bug 18568
         * http://bugs.eclipse.org/bugs/show_bug.cgi?id=18568
@@ -551,21 +551,21 @@ public class JavaModelUtil {
                        ICompilationUnit cu= method.getCompilationUnit();
                        if (cu == null || ! cu.isWorkingCopy())
                                return method;
-                       //use the workaround only if needed     
+                       //use the workaround only if needed
                        if (! method.getElementName().equals(method.getDeclaringType().getElementName()))
                                return (IMethod)cu.getOriginal(method);
-                       
+
                        IType originalType = (IType)toOriginal(method.getDeclaringType());
                        IMethod[] methods = originalType.findMethods(method);
                        boolean isConstructor = method.isConstructor();
                        for (int i=0; i < methods.length; i++) {
-                         if (methods[i].isConstructor() == isConstructor) 
+                         if (methods[i].isConstructor() == isConstructor)
                                return methods[i];
                        }
                        return null;
                } catch(JavaModelException e){
                        return null;
-               }       
+               }
        }
 
        /**
@@ -576,8 +576,8 @@ public class JavaModelUtil {
                if (cu != null && cu.isWorkingCopy())
                        return (ICompilationUnit) cu.getOriginal(cu);
                return cu;
-       }       
-       
+       }
+
        /**
         * Returns the working copy of the given member. If the member is already in a
         * working copy or the member does not exist in the working copy the input is returned.
@@ -600,7 +600,7 @@ public class JavaModelUtil {
        /**
         * Returns the working copy CU of the given CU. If the CU is already a
         * working copy or the CU has no working copy the input CU is returned.
-        */     
+        */
        public static ICompilationUnit toWorkingCopy(ICompilationUnit cu) {
                if (!cu.isWorkingCopy()) {
                        ICompilationUnit workingCopy= EditorUtility.getWorkingCopy(cu);
@@ -610,10 +610,10 @@ public class JavaModelUtil {
                }
                return cu;
        }
-       
+
        /*
         * http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
-        * 
+        *
         * Reconciling happens in a separate thread. This can cause a situation where the
         * Java element gets disposed after an exists test has been done. So we should not
         * log not present exceptions when they happen in working copies.
@@ -633,7 +633,7 @@ public class JavaModelUtil {
                        if (!unit.isWorkingCopy())
                                return true;
                }
-               return false;           
+               return false;
        }
 
 //     public static IType[] getAllSuperTypes(IType type, IProgressMonitor pm) throws JavaModelException {
@@ -641,7 +641,7 @@ public class JavaModelUtil {
 //             try{
 //                     pm.beginTask("", 3); //$NON-NLS-1$
 //                     ITypeHierarchy hierarchy= type.newSupertypeHierarchy(new SubProgressMonitor(pm, 1));
-//                     
+//
 //                     IProgressMonitor subPm= new SubProgressMonitor(pm, 2);
 //                     List typeList= Arrays.asList(hierarchy.getAllSupertypes(type));
 //                     subPm.beginTask("", typeList.size()); //$NON-NLS-1$
@@ -656,10 +656,10 @@ public class JavaModelUtil {
 //                     return (IType[]) types.toArray(new IType[types.size()]);
 //             } finally {
 //                     pm.done();
-//             }       
+//             }
 //     }
-       
-       
+
+
        public static boolean isExcludedPath(IPath resourcePath, IPath[] exclusionPatterns) {
                char[] path = resourcePath.toString().toCharArray();
                for (int i = 0, length = exclusionPatterns.length; i < length; i++) {
@@ -668,14 +668,14 @@ public class JavaModelUtil {
                                return true;
                        }
                }
-               return false;   
+               return false;
        }
 
 
        /*
         * Returns whether the given resource path matches one of the exclusion
         * patterns.
-        * 
+        *
         * @see IClasspathEntry#getExclusionPatterns
         */
        public final static boolean isExcluded(IPath resourcePath, char[][] exclusionPatterns) {
@@ -685,12 +685,12 @@ public class JavaModelUtil {
                        if (CharOperation.pathMatch(exclusionPatterns[i], path, true, '/'))
                                return true;
                return false;
-       }       
-       
+       }
+
 
        private static final String ARGUMENTS_DELIMITER = "#"; //$NON-NLS-1$
        private static final String EMPTY_ARGUMENT = "   "; //$NON-NLS-1$
-       
+
        /**
         * Copied from net.sourceforge.phpdt.internal.core.ProjectPrefUtil;
         */
@@ -699,7 +699,7 @@ public class JavaModelUtil {
                int index = argumentsString.indexOf(':');
                if(index == -1)
                        return null;
-               
+
                int length = argumentsString.length();
                int numberOfArg;
                try{
@@ -708,10 +708,10 @@ public class JavaModelUtil {
                        return null;
                }
                argumentsString = argumentsString.substring(index + 1, length);
-               
+
                String[] args = new String[length];
                int count = 0;
-               
+
                StringTokenizer tokenizer = new StringTokenizer(argumentsString, ARGUMENTS_DELIMITER);
                while(tokenizer.hasMoreTokens()) {
                        String argument = tokenizer.nextToken();
@@ -719,10 +719,10 @@ public class JavaModelUtil {
                                argument = "";  //$NON-NLS-1$
                        args[count++] = argument;
                }
-               
+
                if(count != numberOfArg)
                        return null;
-               
+
                System.arraycopy(args, 0, args = new String[count], 0, count);
                return args;
        }