0001: /*******************************************************************************
0002: * Copyright (c) 2000, 2006 IBM Corporation and others.
0003: * All rights reserved. This program and the accompanying materials
0004: * are made available under the terms of the Eclipse Public License v1.0
0005: * which accompanies this distribution, and is available at
0006: * http://www.eclipse.org/legal/epl-v10.html
0007: *
0008: * Contributors:
0009: * IBM Corporation - initial API and implementation
0010: *******************************************************************************/package org.eclipse.jdt.internal.core;
0011:
0012: import java.io.InputStream;
0013: import java.util.ArrayList;
0014: import java.util.HashMap;
0015:
0016: import org.eclipse.core.runtime.Assert;
0017: import org.eclipse.core.runtime.IProgressMonitor;
0018: import org.eclipse.jdt.core.*;
0019: import org.eclipse.jdt.core.compiler.*;
0020: import org.eclipse.jdt.core.search.SearchEngine;
0021: import org.eclipse.jdt.internal.codeassist.CompletionEngine;
0022: import org.eclipse.jdt.internal.codeassist.ISelectionRequestor;
0023: import org.eclipse.jdt.internal.codeassist.SelectionEngine;
0024: import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
0025: import org.eclipse.jdt.internal.compiler.env.ISourceType;
0026: import org.eclipse.jdt.internal.compiler.lookup.Binding;
0027: import org.eclipse.jdt.internal.core.hierarchy.TypeHierarchy;
0028: import org.eclipse.jdt.internal.core.util.MementoTokenizer;
0029: import org.eclipse.jdt.internal.core.util.Messages;
0030:
0031: /**
0032: * Handle for a source type. Info object is a SourceTypeElementInfo.
0033: *
0034: * Note: Parent is either an IClassFile, an ICompilationUnit or an IType.
0035: *
0036: * @see IType
0037: */
0038:
0039: public class SourceType extends NamedMember implements IType {
0040:
0041: protected SourceType(JavaElement parent, String name) {
0042: super (parent, name);
0043: }
0044:
0045: protected void closing(Object info) throws JavaModelException {
0046: super .closing(info);
0047: SourceTypeElementInfo elementInfo = (SourceTypeElementInfo) info;
0048: ITypeParameter[] typeParameters = elementInfo.typeParameters;
0049: for (int i = 0, length = typeParameters.length; i < length; i++) {
0050: ((TypeParameter) typeParameters[i]).close();
0051: }
0052: }
0053:
0054: /**
0055: * @see IType
0056: * @deprecated
0057: */
0058: public void codeComplete(char[] snippet, int insertion,
0059: int position, char[][] localVariableTypeNames,
0060: char[][] localVariableNames, int[] localVariableModifiers,
0061: boolean isStatic, ICompletionRequestor requestor)
0062: throws JavaModelException {
0063: codeComplete(snippet, insertion, position,
0064: localVariableTypeNames, localVariableNames,
0065: localVariableModifiers, isStatic, requestor,
0066: DefaultWorkingCopyOwner.PRIMARY);
0067: }
0068:
0069: /**
0070: * @see IType
0071: * @deprecated
0072: */
0073: public void codeComplete(char[] snippet, int insertion,
0074: int position, char[][] localVariableTypeNames,
0075: char[][] localVariableNames, int[] localVariableModifiers,
0076: boolean isStatic, ICompletionRequestor requestor,
0077: WorkingCopyOwner owner) throws JavaModelException {
0078: if (requestor == null) {
0079: throw new IllegalArgumentException(
0080: "Completion requestor cannot be null"); //$NON-NLS-1$
0081: }
0082: codeComplete(
0083: snippet,
0084: insertion,
0085: position,
0086: localVariableTypeNames,
0087: localVariableNames,
0088: localVariableModifiers,
0089: isStatic,
0090: new org.eclipse.jdt.internal.codeassist.CompletionRequestorWrapper(
0091: requestor), owner);
0092:
0093: }
0094:
0095: /**
0096: * @see IType
0097: */
0098: public void codeComplete(char[] snippet, int insertion,
0099: int position, char[][] localVariableTypeNames,
0100: char[][] localVariableNames, int[] localVariableModifiers,
0101: boolean isStatic, CompletionRequestor requestor)
0102: throws JavaModelException {
0103: codeComplete(snippet, insertion, position,
0104: localVariableTypeNames, localVariableNames,
0105: localVariableModifiers, isStatic, requestor,
0106: DefaultWorkingCopyOwner.PRIMARY);
0107: }
0108:
0109: /**
0110: * @see IType
0111: */
0112: public void codeComplete(char[] snippet, int insertion,
0113: int position, char[][] localVariableTypeNames,
0114: char[][] localVariableNames, int[] localVariableModifiers,
0115: boolean isStatic, CompletionRequestor requestor,
0116: WorkingCopyOwner owner) throws JavaModelException {
0117: if (requestor == null) {
0118: throw new IllegalArgumentException(
0119: "Completion requestor cannot be null"); //$NON-NLS-1$
0120: }
0121:
0122: JavaProject project = (JavaProject) getJavaProject();
0123: SearchableEnvironment environment = project
0124: .newSearchableNameEnvironment(owner);
0125: CompletionEngine engine = new CompletionEngine(environment,
0126: requestor, project.getOptions(true), project);
0127:
0128: String source = getCompilationUnit().getSource();
0129: if (source != null && insertion > -1
0130: && insertion < source.length()) {
0131:
0132: char[] prefix = CharOperation.concat(source.substring(0,
0133: insertion).toCharArray(), new char[] { '{' });
0134: char[] suffix = CharOperation.concat(new char[] { '}' },
0135: source.substring(insertion).toCharArray());
0136: char[] fakeSource = CharOperation.concat(prefix, snippet,
0137: suffix);
0138:
0139: BasicCompilationUnit cu = new BasicCompilationUnit(
0140: fakeSource, null, getElementName(), getParent());
0141:
0142: engine
0143: .complete(cu, prefix.length + position,
0144: prefix.length);
0145: } else {
0146: engine.complete(this , snippet, position,
0147: localVariableTypeNames, localVariableNames,
0148: localVariableModifiers, isStatic);
0149: }
0150: if (NameLookup.VERBOSE) {
0151: System.out
0152: .println(Thread.currentThread()
0153: + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
0154: System.out
0155: .println(Thread.currentThread()
0156: + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
0157: }
0158: }
0159:
0160: /**
0161: * @see IType
0162: */
0163: public IField createField(String contents, IJavaElement sibling,
0164: boolean force, IProgressMonitor monitor)
0165: throws JavaModelException {
0166: CreateFieldOperation op = new CreateFieldOperation(this ,
0167: contents, force);
0168: if (sibling != null) {
0169: op.createBefore(sibling);
0170: }
0171: op.runOperation(monitor);
0172: return (IField) op.getResultElements()[0];
0173: }
0174:
0175: /**
0176: * @see IType
0177: */
0178: public IInitializer createInitializer(String contents,
0179: IJavaElement sibling, IProgressMonitor monitor)
0180: throws JavaModelException {
0181: CreateInitializerOperation op = new CreateInitializerOperation(
0182: this , contents);
0183: if (sibling != null) {
0184: op.createBefore(sibling);
0185: }
0186: op.runOperation(monitor);
0187: return (IInitializer) op.getResultElements()[0];
0188: }
0189:
0190: /**
0191: * @see IType
0192: */
0193: public IMethod createMethod(String contents, IJavaElement sibling,
0194: boolean force, IProgressMonitor monitor)
0195: throws JavaModelException {
0196: CreateMethodOperation op = new CreateMethodOperation(this ,
0197: contents, force);
0198: if (sibling != null) {
0199: op.createBefore(sibling);
0200: }
0201: op.runOperation(monitor);
0202: return (IMethod) op.getResultElements()[0];
0203: }
0204:
0205: /**
0206: * @see IType
0207: */
0208: public IType createType(String contents, IJavaElement sibling,
0209: boolean force, IProgressMonitor monitor)
0210: throws JavaModelException {
0211: CreateTypeOperation op = new CreateTypeOperation(this ,
0212: contents, force);
0213: if (sibling != null) {
0214: op.createBefore(sibling);
0215: }
0216: op.runOperation(monitor);
0217: return (IType) op.getResultElements()[0];
0218: }
0219:
0220: public boolean equals(Object o) {
0221: if (!(o instanceof SourceType))
0222: return false;
0223: return super .equals(o);
0224: }
0225:
0226: /*
0227: * @see IType
0228: */
0229: public IMethod[] findMethods(IMethod method) {
0230: try {
0231: return findMethods(method, getMethods());
0232: } catch (JavaModelException e) {
0233: // if type doesn't exist, no matching method can exist
0234: return null;
0235: }
0236: }
0237:
0238: public IJavaElement[] getChildrenForCategory(String category)
0239: throws JavaModelException {
0240: IJavaElement[] children = getChildren();
0241: int length = children.length;
0242: if (length == 0)
0243: return NO_ELEMENTS;
0244: SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
0245: HashMap categories = info.getCategories();
0246: if (categories == null)
0247: return NO_ELEMENTS;
0248: IJavaElement[] result = new IJavaElement[length];
0249: int index = 0;
0250: for (int i = 0; i < length; i++) {
0251: IJavaElement child = children[i];
0252: String[] elementCategories = (String[]) categories
0253: .get(child);
0254: if (elementCategories != null)
0255: for (int j = 0, length2 = elementCategories.length; j < length2; j++) {
0256: if (elementCategories[j].equals(category))
0257: result[index++] = child;
0258: }
0259: }
0260: if (index == 0)
0261: return NO_ELEMENTS;
0262: if (index < length)
0263: System.arraycopy(result, 0,
0264: result = new IJavaElement[index], 0, index);
0265: return result;
0266: }
0267:
0268: /**
0269: * @see IMember
0270: */
0271: public IType getDeclaringType() {
0272: IJavaElement parentElement = getParent();
0273: while (parentElement != null) {
0274: if (parentElement.getElementType() == IJavaElement.TYPE) {
0275: return (IType) parentElement;
0276: } else if (parentElement instanceof IMember) {
0277: parentElement = parentElement.getParent();
0278: } else {
0279: return null;
0280: }
0281: }
0282: return null;
0283: }
0284:
0285: /**
0286: * @see IJavaElement
0287: */
0288: public int getElementType() {
0289: return TYPE;
0290: }
0291:
0292: /**
0293: * @see IType#getField
0294: */
0295: public IField getField(String fieldName) {
0296: return new SourceField(this , fieldName);
0297: }
0298:
0299: /**
0300: * @see IType
0301: */
0302: public IField[] getFields() throws JavaModelException {
0303: ArrayList list = getChildrenOfType(FIELD);
0304: IField[] array = new IField[list.size()];
0305: list.toArray(array);
0306: return array;
0307: }
0308:
0309: /**
0310: * @see IType#getFullyQualifiedName()
0311: */
0312: public String getFullyQualifiedName() {
0313: return this .getFullyQualifiedName('$');
0314: }
0315:
0316: /**
0317: * @see IType#getFullyQualifiedName(char)
0318: */
0319: public String getFullyQualifiedName(char enclosingTypeSeparator) {
0320: try {
0321: return getFullyQualifiedName(enclosingTypeSeparator, false/*don't show parameters*/);
0322: } catch (JavaModelException e) {
0323: // exception thrown only when showing parameters
0324: return null;
0325: }
0326: }
0327:
0328: /*
0329: * @see IType#getFullyQualifiedParameterizedName()
0330: */
0331: public String getFullyQualifiedParameterizedName()
0332: throws JavaModelException {
0333: return getFullyQualifiedName('.', true/*show parameters*/);
0334: }
0335:
0336: /*
0337: * @see JavaElement
0338: */
0339: public IJavaElement getHandleFromMemento(String token,
0340: MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
0341: switch (token.charAt(0)) {
0342: case JEM_COUNT:
0343: return getHandleUpdatingCountFromMemento(memento,
0344: workingCopyOwner);
0345: case JEM_FIELD:
0346: if (!memento.hasMoreTokens())
0347: return this ;
0348: String fieldName = memento.nextToken();
0349: JavaElement field = (JavaElement) getField(fieldName);
0350: return field
0351: .getHandleFromMemento(memento, workingCopyOwner);
0352: case JEM_INITIALIZER:
0353: if (!memento.hasMoreTokens())
0354: return this ;
0355: String count = memento.nextToken();
0356: JavaElement initializer = (JavaElement) getInitializer(Integer
0357: .parseInt(count));
0358: return initializer.getHandleFromMemento(memento,
0359: workingCopyOwner);
0360: case JEM_METHOD:
0361: if (!memento.hasMoreTokens())
0362: return this ;
0363: String selector = memento.nextToken();
0364: ArrayList params = new ArrayList();
0365: nextParam: while (memento.hasMoreTokens()) {
0366: token = memento.nextToken();
0367: switch (token.charAt(0)) {
0368: case JEM_TYPE:
0369: case JEM_TYPE_PARAMETER:
0370: break nextParam;
0371: case JEM_METHOD:
0372: if (!memento.hasMoreTokens())
0373: return this ;
0374: String param = memento.nextToken();
0375: StringBuffer buffer = new StringBuffer();
0376: while (param.length() == 1
0377: && Signature.C_ARRAY == param.charAt(0)) { // backward compatible with 3.0 mementos
0378: buffer.append(Signature.C_ARRAY);
0379: if (!memento.hasMoreTokens())
0380: return this ;
0381: param = memento.nextToken();
0382: }
0383: params.add(buffer.toString() + param);
0384: break;
0385: default:
0386: break nextParam;
0387: }
0388: }
0389: String[] parameters = new String[params.size()];
0390: params.toArray(parameters);
0391: JavaElement method = (JavaElement) getMethod(selector,
0392: parameters);
0393: switch (token.charAt(0)) {
0394: case JEM_TYPE:
0395: case JEM_TYPE_PARAMETER:
0396: case JEM_LOCALVARIABLE:
0397: return method.getHandleFromMemento(token, memento,
0398: workingCopyOwner);
0399: default:
0400: return method;
0401: }
0402: case JEM_TYPE:
0403: String typeName;
0404: if (memento.hasMoreTokens()) {
0405: typeName = memento.nextToken();
0406: char firstChar = typeName.charAt(0);
0407: if (firstChar == JEM_FIELD
0408: || firstChar == JEM_INITIALIZER
0409: || firstChar == JEM_METHOD
0410: || firstChar == JEM_TYPE
0411: || firstChar == JEM_COUNT) {
0412: token = typeName;
0413: typeName = ""; //$NON-NLS-1$
0414: } else {
0415: token = null;
0416: }
0417: } else {
0418: typeName = ""; //$NON-NLS-1$
0419: token = null;
0420: }
0421: JavaElement type = (JavaElement) getType(typeName);
0422: if (token == null) {
0423: return type.getHandleFromMemento(memento,
0424: workingCopyOwner);
0425: } else {
0426: return type.getHandleFromMemento(token, memento,
0427: workingCopyOwner);
0428: }
0429: case JEM_TYPE_PARAMETER:
0430: if (!memento.hasMoreTokens())
0431: return this ;
0432: String typeParameterName = memento.nextToken();
0433: JavaElement typeParameter = new TypeParameter(this ,
0434: typeParameterName);
0435: return typeParameter.getHandleFromMemento(memento,
0436: workingCopyOwner);
0437:
0438: }
0439: return null;
0440: }
0441:
0442: /**
0443: * @see IType
0444: */
0445: public IInitializer getInitializer(int count) {
0446: return new Initializer(this , count);
0447: }
0448:
0449: /**
0450: * @see IType
0451: */
0452: public IInitializer[] getInitializers() throws JavaModelException {
0453: ArrayList list = getChildrenOfType(INITIALIZER);
0454: IInitializer[] array = new IInitializer[list.size()];
0455: list.toArray(array);
0456: return array;
0457: }
0458:
0459: /* (non-Javadoc)
0460: * @see org.eclipse.jdt.core.IType#getKey()
0461: */
0462: public String getKey() {
0463: try {
0464: return getKey(this , false/*don't open*/);
0465: } catch (JavaModelException e) {
0466: // happen only if force open is true
0467: return null;
0468: }
0469: }
0470:
0471: /**
0472: * @see IType#getMethod
0473: */
0474: public IMethod getMethod(String selector,
0475: String[] parameterTypeSignatures) {
0476: return new SourceMethod(this , selector, parameterTypeSignatures);
0477: }
0478:
0479: /**
0480: * @see IType
0481: */
0482: public IMethod[] getMethods() throws JavaModelException {
0483: ArrayList list = getChildrenOfType(METHOD);
0484: IMethod[] array = new IMethod[list.size()];
0485: list.toArray(array);
0486: return array;
0487: }
0488:
0489: /**
0490: * @see IType
0491: */
0492: public IPackageFragment getPackageFragment() {
0493: IJavaElement parentElement = this .parent;
0494: while (parentElement != null) {
0495: if (parentElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
0496: return (IPackageFragment) parentElement;
0497: } else {
0498: parentElement = parentElement.getParent();
0499: }
0500: }
0501: Assert.isTrue(false); // should not happen
0502: return null;
0503: }
0504:
0505: /*
0506: * @see JavaElement#getPrimaryElement(boolean)
0507: */
0508: public IJavaElement getPrimaryElement(boolean checkOwner) {
0509: if (checkOwner) {
0510: CompilationUnit cu = (CompilationUnit) getAncestor(COMPILATION_UNIT);
0511: if (cu.isPrimary())
0512: return this ;
0513: }
0514: IJavaElement primaryParent = this .parent
0515: .getPrimaryElement(false);
0516: switch (primaryParent.getElementType()) {
0517: case IJavaElement.COMPILATION_UNIT:
0518: return ((ICompilationUnit) primaryParent)
0519: .getType(this .name);
0520: case IJavaElement.TYPE:
0521: return ((IType) primaryParent).getType(this .name);
0522: case IJavaElement.FIELD:
0523: case IJavaElement.INITIALIZER:
0524: case IJavaElement.METHOD:
0525: return ((IMember) primaryParent).getType(this .name,
0526: this .occurrenceCount);
0527: }
0528: return this ;
0529: }
0530:
0531: /**
0532: * @see IType
0533: */
0534: public String getSuperclassName() throws JavaModelException {
0535: SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
0536: char[] super className = info.getSuperclassName();
0537: if (super className == null) {
0538: return null;
0539: }
0540: return new String(super className);
0541: }
0542:
0543: /**
0544: * @see IType#getSuperclassTypeSignature()
0545: * @since 3.0
0546: */
0547: public String getSuperclassTypeSignature()
0548: throws JavaModelException {
0549: SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
0550: char[] super className = info.getSuperclassName();
0551: if (super className == null) {
0552: return null;
0553: }
0554: return new String(Signature.createTypeSignature(super className,
0555: false));
0556: }
0557:
0558: /**
0559: * @see IType
0560: */
0561: public String[] getSuperInterfaceNames() throws JavaModelException {
0562: SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
0563: char[][] names = info.getInterfaceNames();
0564: return CharOperation.toStrings(names);
0565: }
0566:
0567: /**
0568: * @see IType#getSuperInterfaceTypeSignatures()
0569: * @since 3.0
0570: */
0571: public String[] getSuperInterfaceTypeSignatures()
0572: throws JavaModelException {
0573: SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
0574: char[][] names = info.getInterfaceNames();
0575: if (names == null) {
0576: return CharOperation.NO_STRINGS;
0577: }
0578: String[] strings = new String[names.length];
0579: for (int i = 0; i < names.length; i++) {
0580: strings[i] = new String(Signature.createTypeSignature(
0581: names[i], false));
0582: }
0583: return strings;
0584: }
0585:
0586: public ITypeParameter[] getTypeParameters()
0587: throws JavaModelException {
0588: SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
0589: return info.typeParameters;
0590: }
0591:
0592: /**
0593: * @see IType#getTypeParameterSignatures()
0594: * @since 3.0
0595: */
0596: public String[] getTypeParameterSignatures()
0597: throws JavaModelException {
0598: ITypeParameter[] typeParameters = getTypeParameters();
0599: int length = typeParameters.length;
0600: String[] typeParameterSignatures = new String[length];
0601: for (int i = 0; i < length; i++) {
0602: TypeParameter typeParameter = (TypeParameter) typeParameters[i];
0603: TypeParameterElementInfo info = (TypeParameterElementInfo) typeParameter
0604: .getElementInfo();
0605: char[][] bounds = info.bounds;
0606: if (bounds == null) {
0607: typeParameterSignatures[i] = Signature
0608: .createTypeParameterSignature(typeParameter
0609: .getElementName(),
0610: CharOperation.NO_STRINGS);
0611: } else {
0612: int boundsLength = bounds.length;
0613: char[][] boundSignatures = new char[boundsLength][];
0614: for (int j = 0; j < boundsLength; j++) {
0615: boundSignatures[j] = Signature
0616: .createCharArrayTypeSignature(bounds[j],
0617: false);
0618: }
0619: typeParameterSignatures[i] = new String(Signature
0620: .createTypeParameterSignature(typeParameter
0621: .getElementName().toCharArray(),
0622: boundSignatures));
0623: }
0624: }
0625: return typeParameterSignatures;
0626: }
0627:
0628: /**
0629: * @see IType
0630: */
0631: public IType getType(String typeName) {
0632: return new SourceType(this , typeName);
0633: }
0634:
0635: public ITypeParameter getTypeParameter(String typeParameterName) {
0636: return new TypeParameter(this , typeParameterName);
0637: }
0638:
0639: /**
0640: * @see IType#getTypeQualifiedName()
0641: */
0642: public String getTypeQualifiedName() {
0643: return this .getTypeQualifiedName('$');
0644: }
0645:
0646: /**
0647: * @see IType#getTypeQualifiedName(char)
0648: */
0649: public String getTypeQualifiedName(char enclosingTypeSeparator) {
0650: try {
0651: return getTypeQualifiedName(enclosingTypeSeparator, false/*don't show parameters*/);
0652: } catch (JavaModelException e) {
0653: // exception thrown only when showing parameters
0654: return null;
0655: }
0656: }
0657:
0658: /**
0659: * @see IType
0660: */
0661: public IType[] getTypes() throws JavaModelException {
0662: ArrayList list = getChildrenOfType(TYPE);
0663: IType[] array = new IType[list.size()];
0664: list.toArray(array);
0665: return array;
0666: }
0667:
0668: /**
0669: * @see IType#isAnonymous()
0670: */
0671: public boolean isAnonymous() {
0672: return this .name.length() == 0;
0673: }
0674:
0675: /**
0676: * @see IType
0677: */
0678: public boolean isClass() throws JavaModelException {
0679: SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
0680: return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.CLASS_DECL;
0681: }
0682:
0683: /**
0684: * @see IType#isEnum()
0685: * @since 3.0
0686: */
0687: public boolean isEnum() throws JavaModelException {
0688: SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
0689: return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.ENUM_DECL;
0690: }
0691:
0692: /**
0693: * @see IType
0694: */
0695: public boolean isInterface() throws JavaModelException {
0696: SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
0697: switch (TypeDeclaration.kind(info.getModifiers())) {
0698: case TypeDeclaration.INTERFACE_DECL:
0699: case TypeDeclaration.ANNOTATION_TYPE_DECL: // annotation is interface too
0700: return true;
0701: }
0702: return false;
0703: }
0704:
0705: /**
0706: * @see IType#isAnnotation()
0707: * @since 3.0
0708: */
0709: public boolean isAnnotation() throws JavaModelException {
0710: SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo();
0711: return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.ANNOTATION_TYPE_DECL;
0712: }
0713:
0714: /**
0715: * @see IType#isLocal()
0716: */
0717: public boolean isLocal() {
0718: switch (this .parent.getElementType()) {
0719: case IJavaElement.METHOD:
0720: case IJavaElement.INITIALIZER:
0721: case IJavaElement.FIELD:
0722: return true;
0723: default:
0724: return false;
0725: }
0726: }
0727:
0728: /**
0729: * @see IType#isMember()
0730: */
0731: public boolean isMember() {
0732: return getDeclaringType() != null;
0733: }
0734:
0735: /* (non-Javadoc)
0736: * @see org.eclipse.jdt.core.IType#isResolved()
0737: */
0738: public boolean isResolved() {
0739: return false;
0740: }
0741:
0742: /**
0743: * @see IType
0744: */
0745: public ITypeHierarchy loadTypeHierachy(InputStream input,
0746: IProgressMonitor monitor) throws JavaModelException {
0747: return loadTypeHierachy(input, DefaultWorkingCopyOwner.PRIMARY,
0748: monitor);
0749: }
0750:
0751: /**
0752: * NOTE: This method is not part of the API has it is not clear clients would easily use it: they would need to
0753: * first make sure all working copies for the given owner exist before calling it. This is especially har at startup
0754: * time.
0755: * In case clients want this API, here is how it should be specified:
0756: * <p>
0757: * Loads a previously saved ITypeHierarchy from an input stream. A type hierarchy can
0758: * be stored using ITypeHierachy#store(OutputStream). A compilation unit of a
0759: * loaded type has the given owner if such a working copy exists, otherwise the type's
0760: * compilation unit is a primary compilation unit.
0761: *
0762: * Only hierarchies originally created by the following methods can be loaded:
0763: * <ul>
0764: * <li>IType#newSupertypeHierarchy(IProgressMonitor)</li>
0765: * <li>IType#newSupertypeHierarchy(WorkingCopyOwner, IProgressMonitor)</li>
0766: * <li>IType#newTypeHierarchy(IJavaProject, IProgressMonitor)</li>
0767: * <li>IType#newTypeHierarchy(IJavaProject, WorkingCopyOwner, IProgressMonitor)</li>
0768: * <li>IType#newTypeHierarchy(IProgressMonitor)</li>
0769: * <li>IType#newTypeHierarchy(WorkingCopyOwner, IProgressMonitor)</li>
0770: * </u>
0771: *
0772: * @param input stream where hierarchy will be read
0773: * @param monitor the given progress monitor
0774: * @return the stored hierarchy
0775: * @exception JavaModelException if the hierarchy could not be restored, reasons include:
0776: * - type is not the focus of the hierarchy or
0777: * - unable to read the input stream (wrong format, IOException during reading, ...)
0778: * @see ITypeHierarchy#store(java.io.OutputStream, IProgressMonitor)
0779: * @since 3.0
0780: */
0781: public ITypeHierarchy loadTypeHierachy(InputStream input,
0782: WorkingCopyOwner owner, IProgressMonitor monitor)
0783: throws JavaModelException {
0784: // TODO monitor should be passed to TypeHierarchy.load(...)
0785: return TypeHierarchy.load(this , input, owner);
0786: }
0787:
0788: /**
0789: * @see IType
0790: */
0791: public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor)
0792: throws JavaModelException {
0793: return this .newSupertypeHierarchy(
0794: DefaultWorkingCopyOwner.PRIMARY, monitor);
0795: }
0796:
0797: /*
0798: * @see IType#newSupertypeHierarchy(ICompilationUnit[], IProgressMonitor)
0799: */
0800: public ITypeHierarchy newSupertypeHierarchy(
0801: ICompilationUnit[] workingCopies, IProgressMonitor monitor)
0802: throws JavaModelException {
0803:
0804: CreateTypeHierarchyOperation op = new CreateTypeHierarchyOperation(
0805: this , workingCopies, SearchEngine
0806: .createWorkspaceScope(), false);
0807: op.runOperation(monitor);
0808: return op.getResult();
0809: }
0810:
0811: /**
0812: * @param workingCopies the working copies that take precedence over their original compilation units
0813: * @param monitor the given progress monitor
0814: * @return a type hierarchy for this type containing this type and all of its supertypes
0815: * @exception JavaModelException if this element does not exist or if an
0816: * exception occurs while accessing its corresponding resource.
0817: *
0818: * @see IType#newSupertypeHierarchy(IWorkingCopy[], IProgressMonitor)
0819: * @deprecated
0820: */
0821: public ITypeHierarchy newSupertypeHierarchy(
0822: IWorkingCopy[] workingCopies, IProgressMonitor monitor)
0823: throws JavaModelException {
0824:
0825: ICompilationUnit[] copies;
0826: if (workingCopies == null) {
0827: copies = null;
0828: } else {
0829: int length = workingCopies.length;
0830: System.arraycopy(workingCopies, 0,
0831: copies = new ICompilationUnit[length], 0, length);
0832: }
0833: return newSupertypeHierarchy(copies, monitor);
0834: }
0835:
0836: /**
0837: * @see IType#newSupertypeHierarchy(WorkingCopyOwner, IProgressMonitor)
0838: */
0839: public ITypeHierarchy newSupertypeHierarchy(WorkingCopyOwner owner,
0840: IProgressMonitor monitor) throws JavaModelException {
0841:
0842: ICompilationUnit[] workingCopies = JavaModelManager
0843: .getJavaModelManager()
0844: .getWorkingCopies(owner, true/*add primary working copies*/);
0845: CreateTypeHierarchyOperation op = new CreateTypeHierarchyOperation(
0846: this , workingCopies, SearchEngine
0847: .createWorkspaceScope(), false);
0848: op.runOperation(monitor);
0849: return op.getResult();
0850: }
0851:
0852: /**
0853: * @see IType
0854: */
0855: public ITypeHierarchy newTypeHierarchy(IJavaProject project,
0856: IProgressMonitor monitor) throws JavaModelException {
0857: return newTypeHierarchy(project,
0858: DefaultWorkingCopyOwner.PRIMARY, monitor);
0859: }
0860:
0861: /**
0862: * @see IType#newTypeHierarchy(IJavaProject, WorkingCopyOwner, IProgressMonitor)
0863: */
0864: public ITypeHierarchy newTypeHierarchy(IJavaProject project,
0865: WorkingCopyOwner owner, IProgressMonitor monitor)
0866: throws JavaModelException {
0867: if (project == null) {
0868: throw new IllegalArgumentException(
0869: Messages.hierarchy_nullProject);
0870: }
0871: ICompilationUnit[] workingCopies = JavaModelManager
0872: .getJavaModelManager()
0873: .getWorkingCopies(owner, true/*add primary working copies*/);
0874: ICompilationUnit[] projectWCs = null;
0875: if (workingCopies != null) {
0876: int length = workingCopies.length;
0877: projectWCs = new ICompilationUnit[length];
0878: int index = 0;
0879: for (int i = 0; i < length; i++) {
0880: ICompilationUnit wc = workingCopies[i];
0881: if (project.equals(wc.getJavaProject())) {
0882: projectWCs[index++] = wc;
0883: }
0884: }
0885: if (index != length) {
0886: System.arraycopy(projectWCs, 0,
0887: projectWCs = new ICompilationUnit[index], 0,
0888: index);
0889: }
0890: }
0891: CreateTypeHierarchyOperation op = new CreateTypeHierarchyOperation(
0892: this , projectWCs, project, true);
0893: op.runOperation(monitor);
0894: return op.getResult();
0895: }
0896:
0897: /**
0898: * @see IType
0899: */
0900: public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor)
0901: throws JavaModelException {
0902: CreateTypeHierarchyOperation op = new CreateTypeHierarchyOperation(
0903: this , null, SearchEngine.createWorkspaceScope(), true);
0904: op.runOperation(monitor);
0905: return op.getResult();
0906: }
0907:
0908: /*
0909: * @see IType#newTypeHierarchy(ICompilationUnit[], IProgressMonitor)
0910: */
0911: public ITypeHierarchy newTypeHierarchy(
0912: ICompilationUnit[] workingCopies, IProgressMonitor monitor)
0913: throws JavaModelException {
0914:
0915: CreateTypeHierarchyOperation op = new CreateTypeHierarchyOperation(
0916: this , workingCopies, SearchEngine
0917: .createWorkspaceScope(), true);
0918: op.runOperation(monitor);
0919: return op.getResult();
0920: }
0921:
0922: /**
0923: * @see IType#newTypeHierarchy(IWorkingCopy[], IProgressMonitor)
0924: * @deprecated
0925: */
0926: public ITypeHierarchy newTypeHierarchy(
0927: IWorkingCopy[] workingCopies, IProgressMonitor monitor)
0928: throws JavaModelException {
0929:
0930: ICompilationUnit[] copies;
0931: if (workingCopies == null) {
0932: copies = null;
0933: } else {
0934: int length = workingCopies.length;
0935: System.arraycopy(workingCopies, 0,
0936: copies = new ICompilationUnit[length], 0, length);
0937: }
0938: return newTypeHierarchy(copies, monitor);
0939: }
0940:
0941: /**
0942: * @see IType#newTypeHierarchy(WorkingCopyOwner, IProgressMonitor)
0943: */
0944: public ITypeHierarchy newTypeHierarchy(WorkingCopyOwner owner,
0945: IProgressMonitor monitor) throws JavaModelException {
0946:
0947: ICompilationUnit[] workingCopies = JavaModelManager
0948: .getJavaModelManager()
0949: .getWorkingCopies(owner, true/*add primary working copies*/);
0950: CreateTypeHierarchyOperation op = new CreateTypeHierarchyOperation(
0951: this , workingCopies, SearchEngine
0952: .createWorkspaceScope(), true);
0953: op.runOperation(monitor);
0954: return op.getResult();
0955: }
0956:
0957: public JavaElement resolved(Binding binding) {
0958: SourceRefElement resolvedHandle = new ResolvedSourceType(
0959: this .parent, this .name, new String(binding
0960: .computeUniqueKey()));
0961: resolvedHandle.occurrenceCount = this .occurrenceCount;
0962: return resolvedHandle;
0963: }
0964:
0965: /**
0966: * @see IType#resolveType(String)
0967: */
0968: public String[][] resolveType(String typeName)
0969: throws JavaModelException {
0970: return resolveType(typeName, DefaultWorkingCopyOwner.PRIMARY);
0971: }
0972:
0973: /**
0974: * @see IType#resolveType(String, WorkingCopyOwner)
0975: */
0976: public String[][] resolveType(String typeName,
0977: WorkingCopyOwner owner) throws JavaModelException {
0978: ISourceType info = (ISourceType) getElementInfo();
0979: JavaProject project = (JavaProject) getJavaProject();
0980: SearchableEnvironment environment = project
0981: .newSearchableNameEnvironment(owner);
0982:
0983: class TypeResolveRequestor implements ISelectionRequestor {
0984: String[][] answers = null;
0985:
0986: public void acceptType(char[] packageName, char[] tName,
0987: int modifiers, boolean isDeclaration,
0988: char[] uniqueKey, int start, int end) {
0989: String[] answer = new String[] {
0990: new String(packageName), new String(tName) };
0991: if (this .answers == null) {
0992: this .answers = new String[][] { answer };
0993: } else {
0994: // grow
0995: int length = this .answers.length;
0996: System.arraycopy(this .answers, 0,
0997: this .answers = new String[length + 1][], 0,
0998: length);
0999: this .answers[length] = answer;
1000: }
1001: }
1002:
1003: public void acceptError(CategorizedProblem error) {
1004: // ignore
1005: }
1006:
1007: public void acceptField(char[] declaringTypePackageName,
1008: char[] declaringTypeName, char[] fieldName,
1009: boolean isDeclaration, char[] uniqueKey, int start,
1010: int end) {
1011: // ignore
1012: }
1013:
1014: public void acceptMethod(char[] declaringTypePackageName,
1015: char[] declaringTypeName,
1016: String enclosingDeclaringTypeSignature,
1017: char[] selector, char[][] parameterPackageNames,
1018: char[][] parameterTypeNames,
1019: String[] parameterSignatures,
1020: char[][] typeParameterNames,
1021: char[][][] typeParameterBoundNames,
1022: boolean isConstructor, boolean isDeclaration,
1023: char[] uniqueKey, int start, int end) {
1024: // ignore
1025: }
1026:
1027: public void acceptPackage(char[] packageName) {
1028: // ignore
1029: }
1030:
1031: public void acceptTypeParameter(
1032: char[] declaringTypePackageName,
1033: char[] declaringTypeName, char[] typeParameterName,
1034: boolean isDeclaration, int start, int end) {
1035: // ignore
1036: }
1037:
1038: public void acceptMethodTypeParameter(
1039: char[] declaringTypePackageName,
1040: char[] declaringTypeName, char[] selector,
1041: int selectorStart, int selcetorEnd,
1042: char[] typeParameterName, boolean isDeclaration,
1043: int start, int end) {
1044: // ignore
1045: }
1046:
1047: }
1048: TypeResolveRequestor requestor = new TypeResolveRequestor();
1049: SelectionEngine engine = new SelectionEngine(environment,
1050: requestor, project.getOptions(true));
1051:
1052: IType[] topLevelTypes = getCompilationUnit().getTypes();
1053: int length = topLevelTypes.length;
1054: SourceTypeElementInfo[] topLevelInfos = new SourceTypeElementInfo[length];
1055: for (int i = 0; i < length; i++) {
1056: topLevelInfos[i] = (SourceTypeElementInfo) ((SourceType) topLevelTypes[i])
1057: .getElementInfo();
1058: }
1059:
1060: engine.selectType(info, typeName.toCharArray(), topLevelInfos,
1061: false);
1062: if (NameLookup.VERBOSE) {
1063: System.out
1064: .println(Thread.currentThread()
1065: + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
1066: System.out
1067: .println(Thread.currentThread()
1068: + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
1069: }
1070: return requestor.answers;
1071: }
1072:
1073: /**
1074: * @private Debugging purposes
1075: */
1076: protected void toStringInfo(int tab, StringBuffer buffer,
1077: Object info, boolean showResolvedInfo) {
1078: buffer.append(tabString(tab));
1079: if (info == null) {
1080: String elementName = getElementName();
1081: if (elementName.length() == 0) {
1082: buffer.append("<anonymous #"); //$NON-NLS-1$
1083: buffer.append(this .occurrenceCount);
1084: buffer.append(">"); //$NON-NLS-1$
1085: } else {
1086: toStringName(buffer);
1087: }
1088: buffer.append(" (not open)"); //$NON-NLS-1$
1089: } else if (info == NO_INFO) {
1090: String elementName = getElementName();
1091: if (elementName.length() == 0) {
1092: buffer.append("<anonymous #"); //$NON-NLS-1$
1093: buffer.append(this .occurrenceCount);
1094: buffer.append(">"); //$NON-NLS-1$
1095: } else {
1096: toStringName(buffer);
1097: }
1098: } else {
1099: try {
1100: if (this .isEnum()) {
1101: buffer.append("enum "); //$NON-NLS-1$
1102: } else if (this .isAnnotation()) {
1103: buffer.append("@interface "); //$NON-NLS-1$
1104: } else if (this .isInterface()) {
1105: buffer.append("interface "); //$NON-NLS-1$
1106: } else {
1107: buffer.append("class "); //$NON-NLS-1$
1108: }
1109: String elementName = getElementName();
1110: if (elementName.length() == 0) {
1111: buffer.append("<anonymous #"); //$NON-NLS-1$
1112: buffer.append(this .occurrenceCount);
1113: buffer.append(">"); //$NON-NLS-1$
1114: } else {
1115: toStringName(buffer);
1116: }
1117: } catch (JavaModelException e) {
1118: buffer
1119: .append("<JavaModelException in toString of " + getElementName()); //$NON-NLS-1$
1120: }
1121: }
1122: }
1123: }
|