/*******************************************************************************
* 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
*******************************************************************************/
package net.sourceforge.phpdt.internal.corext.util;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
+import java.util.StringTokenizer;
import net.sourceforge.phpdt.core.Flags;
import net.sourceforge.phpdt.core.ICompilationUnit;
import net.sourceforge.phpdt.core.IField;
import net.sourceforge.phpdt.core.IJavaElement;
-import net.sourceforge.phpdt.core.IJavaProject;
import net.sourceforge.phpdt.core.IMember;
import net.sourceforge.phpdt.core.IMethod;
import net.sourceforge.phpdt.core.IPackageFragment;
import net.sourceforge.phpdt.core.compiler.CharOperation;
import net.sourceforge.phpeclipse.phpeditor.EditorUtility;
-import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.SubProgressMonitor;
* 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);
// 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
// }
return false;
}
-
+
// private static IType findType(IPackageFragmentRoot root, String fullyQualifiedName) throws JavaModelException{
// IJavaElement[] children= root.getChildren();
// for (int i= 0; i < children.length; i++) {
// 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++) {
// }
// return null;
// }
-
+
// private static IType findType(ICompilationUnit cu, String fullyQualifiedName) throws JavaModelException{
// IType[] types= cu.getAllTypes();
// for (int i= 0; i < types.length; i++) {
// }
// 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);
// }
// 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++) {
}
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) {
}
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) {
}
return null;
}
-
+
/**
* Returns the qualified type name of the given type using '.' as separators.
* This is a replace for IType.getTypeQualifiedName()
public static String getTypeQualifiedName(IType type) {
return type.getTypeQualifiedName('.');
}
-
+
private static void getTypeQualifiedName(IType type, StringBuffer buf) {
IType outerType= type.getDeclaringType();
if (outerType != null) {
buf.append('.');
}
buf.append(type.getElementName());
- }
+ }
/**
* Returns the fully qualified name of the given type using '.' as separators.
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)
*/
return type.getPackageFragment().getElementName();
}
}
-
-
+
+
/**
* Concatenates two names. Uses a dot for separation.
* Both strings can be empty or <code>null</code>.
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>.
buf.append('.');
}
buf.append(name2);
- }
+ }
return buf.toString();
- }
-
+ }
+
/**
* Evaluates if a member (possible from another package) is visible from
* elements in a package.
*/
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.
}
/**
- * 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);
*/
}
return null;
}
-
+
/**
* Finds a method in a type.
* This searches for a method with the same name and signature. Parameter types are only
}
return null;
}
-
+
/**
* Finds a method declararion in a type's hierarchy. The search is top down, so this
// }
// 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.
// }
// }
// return null;
-// }
-
+// }
+
/**
* Tests if a method equals to the given signature.
* Parameter types are only compared by the simple name, no resolving for
}
return false;
}
-
+
/**
* Checks whether the given type has a valid main method or not.
*/
}
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.
* @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 {
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]);
return Signature.toString(refTypeSig.substring(arrayCount));
}
}
-
+
/**
* Returns if a CU can be edited.
*/
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++) {
// 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
return (IMember)cu.getOriginal(member);
return member;
}
-
+
/*
* XXX workaround for bug 18568
* http://bugs.eclipse.org/bugs/show_bug.cgi?id=18568
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;
- }
+ }
}
/**
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.
/**
* 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);
}
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.
if (!unit.isWorkingCopy())
return true;
}
- return false;
+ return false;
}
// public static IType[] getAllSuperTypes(IType type, IProgressMonitor pm) throws JavaModelException {
// 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$
// 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++) {
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) {
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;
+ */
+ public static String[] getProblemArgumentsFromMarker(String argumentsString){
+ if (argumentsString == null) return null;
+ int index = argumentsString.indexOf(':');
+ if(index == -1)
+ return null;
+
+ int length = argumentsString.length();
+ int numberOfArg;
+ try{
+ numberOfArg = Integer.parseInt(argumentsString.substring(0 , index));
+ } catch (NumberFormatException e) {
+ 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();
+ if(argument.equals(EMPTY_ARGUMENT))
+ argument = ""; //$NON-NLS-1$
+ args[count++] = argument;
+ }
+
+ if(count != numberOfArg)
+ return null;
+
+ System.arraycopy(args, 0, args = new String[count], 0, count);
+ return args;
+ }
}