1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

2004-08-13 Chris Wiebe

Extra functionality in type cache to support new class wizard
	* browser/org/eclipse/cdt/core/browser/AllTypesCache.java
	* browser/org/eclipse/cdt/core/browser/IQualifiedTypeName.java
	* browser/org/eclipse/cdt/core/browser/QualifiedTypeName.java
	* browser/org/eclipse/cdt/core/browser/ITypeInfo.java
	* browser/org/eclipse/cdt/core/browser/TypeInfo.java
	* browser/org/eclipse/cdt/internal/core/browser/cache/ITypeCache.java
	* browser/org/eclipse/cdt/internal/core/browser/cache/TypeCache.java
This commit is contained in:
Chris Wiebe 2004-08-13 21:41:59 +00:00
parent 3cd2cd8098
commit 3ead56997c
9 changed files with 192 additions and 28 deletions

View file

@ -1,3 +1,14 @@
2004-08-13 Chris Wiebe
Extra functionality in type cache to support new class wizard
* browser/org/eclipse/cdt/core/browser/AllTypesCache.java
* browser/org/eclipse/cdt/core/browser/IQualifiedTypeName.java
* browser/org/eclipse/cdt/core/browser/QualifiedTypeName.java
* browser/org/eclipse/cdt/core/browser/ITypeInfo.java
* browser/org/eclipse/cdt/core/browser/TypeInfo.java
* browser/org/eclipse/cdt/internal/core/browser/cache/ITypeCache.java
* browser/org/eclipse/cdt/internal/core/browser/cache/TypeCache.java
2004-08-13 Chris Wiebe
Add findSourceRoot() method needed for class wizard

View file

@ -313,6 +313,33 @@ public class AllTypesCache {
public static ITypeInfo getTypeForElement(ICElement elem) {
return TypeUtil.getTypeForElement(elem);
}
/** Returns first type in the cache which matches the given
* type and name. If no type is found, <code>null</code>
* is returned.
*
* @param project the enclosing project
* @param type the ICElement type
* @param qualifiedName the qualified type name to match
* @return the matching type
*/
public static ITypeInfo getType(IProject project, int type, IQualifiedTypeName qualifiedName) {
ITypeCache cache = TypeCacheManager.getInstance().getCache(project);
return cache.getType(type, qualifiedName);
}
/**
* Returns all types matching name in the given project.
*
* @param project the enclosing project
* @param qualifiedName The qualified type name
* @param ignoreCase <code>true</code> if case-insensitive
* @return Array of types
*/
public static ITypeInfo[] getTypes(IProject project, IQualifiedTypeName qualifiedName, boolean ignoreCase) {
ITypeCache cache = TypeCacheManager.getInstance().getCache(project);
return cache.getTypes(qualifiedName, ignoreCase);
}
/**
* Creates and returns a type hierarchy for this type containing

View file

@ -40,4 +40,9 @@ public interface IQualifiedTypeName extends Comparable {
public boolean isLowLevel();
public boolean validate();
public boolean equals(IQualifiedTypeName typeName);
public boolean equalsIgnoreCase(IQualifiedTypeName typeName);
public int compareTo(IQualifiedTypeName typeName);
public int compareToIgnoreCase(IQualifiedTypeName typeName);
}

View file

@ -72,6 +72,11 @@ public interface ITypeInfo extends Comparable {
*/
public ITypeInfo getEnclosingType();
/** Gets the enclosing namespace for this type.
* @return the enclosing namespace, or <code>null</code> if none exists.
*/
public ITypeInfo getEnclosingNamespace();
/** Gets the first enclosing type which matches one of the given kinds.
* @param kinds Array containing CElement types: C_NAMESPACE, C_CLASS, C_STRUCT
* @return the enclosing type, or <code>null</code> if not found.

View file

@ -246,8 +246,35 @@ public class QualifiedTypeName implements IQualifiedTypeName {
if (!(obj instanceof IQualifiedTypeName)) {
throw new ClassCastException();
}
IQualifiedTypeName typeName = (IQualifiedTypeName) obj;
return getFullyQualifiedName().compareTo(typeName.getFullyQualifiedName());
return compareTo((IQualifiedTypeName)obj);
}
public int compareTo(IQualifiedTypeName typeName) {
if (typeName == this)
return 0;
if (typeName == null)
return 1;
String[] segments = typeName.segments();
int len = Math.min(fSegments.length, segments.length);
int result = 0;
for (int i = 0; result == 0 && i < len; ++i) {
result = fSegments[i].compareTo(segments[i]);
}
return result;
}
public int compareToIgnoreCase(IQualifiedTypeName typeName) {
if (typeName == this)
return 0;
if (typeName == null)
return 1;
String[] segments = typeName.segments();
int len = Math.min(fSegments.length, segments.length);
int result = 0;
for (int i = 0; result == 0 && i < len; ++i) {
result = fSegments[i].compareToIgnoreCase(segments[i]);
}
return result;
}
public boolean equals(Object obj) {
@ -255,23 +282,40 @@ public class QualifiedTypeName implements IQualifiedTypeName {
return true;
}
if (!(obj instanceof IQualifiedTypeName)) {
return false;
throw new ClassCastException();
}
IQualifiedTypeName typeName = (IQualifiedTypeName) obj;
return matchSegments(fSegments, typeName.segments());
return equals((IQualifiedTypeName)obj);
}
public boolean equals(IQualifiedTypeName typeName) {
if (typeName == this)
return true;
if (typeName == null)
return false;
String[] segments = typeName.segments();
int len = segments.length;
if (fSegments.length != len)
return false;
for (int i = 0; i < len; ++i) {
if (!fSegments[i].equals(segments[i]))
return false;
}
return true;
}
private static boolean matchSegments(String[] a, String[] b) {
if (a == null && b == null)
public boolean equalsIgnoreCase(IQualifiedTypeName typeName) {
if (typeName == this)
return true;
if (a == null || b == null)
return false;
if (a.length != b.length)
return false;
for (int i = 0; i < a.length; ++i) {
if (!a[i].equals(b[i]))
return false;
}
return true;
if (typeName == null)
return false;
String[] segments = typeName.segments();
int len = segments.length;
if (fSegments.length != len)
return false;
for (int i = 0; i < len; ++i) {
if (!fSegments[i].equalsIgnoreCase(segments[i]))
return false;
}
return true;
}
}

View file

@ -149,6 +149,13 @@ public class TypeInfo implements ITypeInfo
return getEnclosingType(KNOWN_TYPES);
}
public ITypeInfo getEnclosingNamespace() {
if (fTypeCache != null) {
return fTypeCache.getEnclosingNamespace(this);
}
return null;
}
public ITypeInfo getRootNamespace(boolean includeGlobalNamespace) {
if (fTypeCache != null) {
return fTypeCache.getRootNamespace(this, true);

View file

@ -85,6 +85,7 @@ public class TypeUtil {
return (ICElement[])typeList.toArray(new ICElement[typeList.size()]);
}
// TODO move method to CModelUtil
public static IQualifiedTypeName getFullyQualifiedName(ICElement type) {
String name = type.getElementName();
IQualifiedTypeName qualifiedName = new QualifiedTypeName(name);

View file

@ -105,7 +105,7 @@ public interface ITypeCache extends ISchedulingRule {
* @param qualifiedName the qualified type name to match
* @return Array of types
*/
public ITypeInfo[] getTypes(IQualifiedTypeName qualifiedName);
public ITypeInfo[] getTypes(IQualifiedTypeName qualifiedName, boolean ignoreCase);
/** Returns first type in the cache which matches the given
* type and name. If no type is found, <code>null</code>
@ -126,6 +126,13 @@ public interface ITypeCache extends ISchedulingRule {
*/
public ITypeInfo getEnclosingType(ITypeInfo info, int[] kinds);
/** Returns the enclosing namespace for the given type, or
* <code>null</code> if none exists.
*
* @param type the ICElement type
* @return the namespace
*/
public ITypeInfo getEnclosingNamespace(ITypeInfo info);
/** Gets the root namespace of which encloses the given type.
*

View file

@ -218,8 +218,7 @@ public class TypeCache implements ITypeCache {
if (!(obj instanceof IQualifiedTypeName)) {
return false;
}
IQualifiedTypeName typeName = (IQualifiedTypeName) obj;
return (typeName instanceof GlobalNamespace);
return equals((IQualifiedTypeName)obj);
}
public int compareTo(Object obj) {
@ -229,9 +228,36 @@ public class TypeCache implements ITypeCache {
if (!(obj instanceof IQualifiedTypeName)) {
throw new ClassCastException();
}
IQualifiedTypeName typeName = (IQualifiedTypeName) obj;
return getFullyQualifiedName().compareTo(typeName.getFullyQualifiedName());
return compareTo((IQualifiedTypeName)obj);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.browser.IQualifiedTypeName#equals(org.eclipse.cdt.core.browser.IQualifiedTypeName)
*/
public boolean equals(IQualifiedTypeName typeName) {
return (typeName instanceof GlobalNamespace);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.browser.IQualifiedTypeName#equalsIgnoreCase(org.eclipse.cdt.core.browser.IQualifiedTypeName)
*/
public boolean equalsIgnoreCase(IQualifiedTypeName typeName) {
return (typeName instanceof GlobalNamespace);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.browser.IQualifiedTypeName#compareTo(org.eclipse.cdt.core.browser.IQualifiedTypeName)
*/
public int compareTo(IQualifiedTypeName typeName) {
return getFullyQualifiedName().compareTo(typeName.getFullyQualifiedName());
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.browser.IQualifiedTypeName#compareToIgnoreCase(org.eclipse.cdt.core.browser.IQualifiedTypeName)
*/
public int compareToIgnoreCase(IQualifiedTypeName typeName) {
return getFullyQualifiedName().compareToIgnoreCase(typeName.getFullyQualifiedName());
}
}
private static class HashKey {
@ -454,17 +480,28 @@ public class TypeCache implements ITypeCache {
return (ITypeInfo[]) results.toArray(new ITypeInfo[results.size()]);
}
public synchronized ITypeInfo[] getTypes(IQualifiedTypeName qualifiedName) {
public synchronized ITypeInfo[] getTypes(IQualifiedTypeName qualifiedName, boolean ignoreCase) {
Collection results = new ArrayList();
for (int i = 0; i < ITypeInfo.KNOWN_TYPES.length; ++i) {
ITypeInfo info = (ITypeInfo) fTypeKeyMap.get(new HashKey(qualifiedName, ITypeInfo.KNOWN_TYPES[i]));
if (!ignoreCase) {
for (int i = 0; i < ITypeInfo.KNOWN_TYPES.length; ++i) {
ITypeInfo info = (ITypeInfo) fTypeKeyMap.get(new HashKey(qualifiedName, ITypeInfo.KNOWN_TYPES[i]));
if (info != null) {
results.add(info);
}
}
ITypeInfo info = (ITypeInfo) fTypeKeyMap.get(new HashKey(qualifiedName, 0));
if (info != null) {
results.add(info);
}
}
ITypeInfo info = (ITypeInfo) fTypeKeyMap.get(new HashKey(qualifiedName, 0));
if (info != null) {
results.add(info);
} else {
// TODO this should probably use a more efficient search algorithm
for (Iterator mapIter = fTypeKeyMap.entrySet().iterator(); mapIter.hasNext(); ) {
Map.Entry entry = (Map.Entry) mapIter.next();
ITypeInfo info = (ITypeInfo) entry.getValue();
if (info.getQualifiedTypeName().compareToIgnoreCase(qualifiedName) == 0) {
results.add(info);
}
}
}
return (ITypeInfo[]) results.toArray(new ITypeInfo[results.size()]);
}
@ -492,6 +529,26 @@ public class TypeCache implements ITypeCache {
return null;
}
public synchronized ITypeInfo getEnclosingNamespace(ITypeInfo info) {
IQualifiedTypeName enclosingName = info.getQualifiedTypeName().getEnclosingTypeName();
if (enclosingName != null) {
// look for namespace
ITypeInfo enclosingType = (ITypeInfo) fTypeKeyMap.get(new HashKey(enclosingName, ICElement.C_NAMESPACE));
if (enclosingType != null) {
return enclosingType;
}
// try class, struct, then undefined
final int[] kinds = {ICElement.C_CLASS, ICElement.C_STRUCT, 0};
for (int i = 0; enclosingType == null && i < kinds.length; ++i) {
enclosingType = (ITypeInfo) fTypeKeyMap.get(new HashKey(enclosingName, kinds[i]));
}
if (enclosingType != null) {
return getEnclosingNamespace(enclosingType);
}
}
return null;
}
public synchronized ITypeInfo getRootNamespace(ITypeInfo info, boolean includeGlobalNamespace) {
IQualifiedTypeName qualifiedName = info.getQualifiedTypeName();
if (qualifiedName.isGlobal()) {