001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.core.dom;
011:
012: /**
013: * Abstract base class for all AST nodes that represent names.
014: * There are exactly two kinds of name: simple ones
015: * (<code>SimpleName</code>) and qualified ones (<code>QualifiedName</code>).
016: * <p>
017: * <pre>
018: * Name:
019: * SimpleName
020: * QualifiedName
021: * </pre>
022: * </p>
023: *
024: * @since 2.0
025: */
026: public abstract class Name extends Expression implements IDocElement {
027:
028: /**
029: * Approximate base size of an expression node instance in bytes,
030: * including object header and instance fields.
031: */
032: static final int BASE_NAME_NODE_SIZE = BASE_NODE_SIZE + 1 * 4;
033:
034: /**
035: * This index represents the position inside a qualified name.
036: */
037: int index;
038:
039: /**
040: * Creates a new AST node for a name owned by the given AST.
041: * <p>
042: * N.B. This constructor is package-private.
043: * </p>
044: *
045: * @param ast the AST that is to own this node
046: */
047: Name(AST ast) {
048: super (ast);
049: }
050:
051: /**
052: * Returns whether this name is a simple name
053: * (<code>SimpleName</code>).
054: *
055: * @return <code>true</code> if this is a simple name, and
056: * <code>false</code> otherwise
057: */
058: public final boolean isSimpleName() {
059: return (this instanceof SimpleName);
060: }
061:
062: /**
063: * Returns whether this name is a qualified name
064: * (<code>QualifiedName</code>).
065: *
066: * @return <code>true</code> if this is a qualified name, and
067: * <code>false</code> otherwise
068: */
069: public final boolean isQualifiedName() {
070: return (this instanceof QualifiedName);
071: }
072:
073: /**
074: * Resolves and returns the binding for the entity referred to by this name.
075: * <p>
076: * Note that bindings are generally unavailable unless requested when the
077: * AST is being built.
078: * </p>
079: *
080: * @return the binding, or <code>null</code> if the binding cannot be
081: * resolved
082: */
083: public final IBinding resolveBinding() {
084: return this .ast.getBindingResolver().resolveName(this );
085: }
086:
087: /**
088: * Returns the standard dot-separated representation of this name.
089: * If the name is a simple name, the result is the name's identifier.
090: * If the name is a qualified name, the result is the name of the qualifier
091: * (as computed by this method) followed by "." followed by the name's
092: * identifier.
093: *
094: * @return the fully qualified name
095: * @since 3.0
096: */
097: public final String getFullyQualifiedName() {
098: if (isSimpleName()) {
099: // avoid creating garbage for common case
100: return ((SimpleName) this ).getIdentifier();
101: } else {
102: StringBuffer buffer = new StringBuffer(50);
103: appendName(buffer);
104: return new String(buffer);
105: }
106: }
107:
108: /**
109: * Appends the standard representation of this name to the given string
110: * buffer.
111: *
112: * @param buffer the buffer
113: * @since 3.0
114: */
115: abstract void appendName(StringBuffer buffer);
116: }
|