| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2019 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.jdt.core.dom; |
| 16 | |
| 17 | /** |
| 18 | * Abstract base class for all AST nodes that represent names. |
| 19 | * There are exactly two kinds of name: simple ones |
| 20 | * (<code>SimpleName</code>) and qualified ones (<code>QualifiedName</code>). |
| 21 | * <pre> |
| 22 | * Name: |
| 23 | * SimpleName |
| 24 | * QualifiedName |
| 25 | * </pre> |
| 26 | * |
| 27 | * @since 2.0 |
| 28 | * @noextend This class is not intended to be subclassed by clients. |
| 29 | */ |
| 30 | public abstract class Name extends Expression implements IDocElement { |
| 31 | |
| 32 | /** |
| 33 | * Approximate base size of an expression node instance in bytes, |
| 34 | * including object header and instance fields. |
| 35 | */ |
| 36 | static final int BASE_NAME_NODE_SIZE = BASE_NODE_SIZE + 1 * 4; |
| 37 | |
| 38 | /** |
| 39 | * This index represents the position inside a qualified name. |
| 40 | */ |
| 41 | int index; |
| 42 | |
| 43 | /** |
| 44 | * Creates a new AST node for a name owned by the given AST. |
| 45 | * <p> |
| 46 | * N.B. This constructor is package-private. |
| 47 | * </p> |
| 48 | * |
| 49 | * @param ast the AST that is to own this node |
| 50 | */ |
| 51 | Name(AST ast) { |
| 52 | super(ast); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns whether this name is a simple name |
| 57 | * (<code>SimpleName</code>). |
| 58 | * |
| 59 | * @return <code>true</code> if this is a simple name, and |
| 60 | * <code>false</code> otherwise |
| 61 | */ |
| 62 | public final boolean isSimpleName() { |
| 63 | return (this instanceof SimpleName); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns whether this name is a qualified name |
| 68 | * (<code>QualifiedName</code>). |
| 69 | * |
| 70 | * @return <code>true</code> if this is a qualified name, and |
| 71 | * <code>false</code> otherwise |
| 72 | */ |
| 73 | public final boolean isQualifiedName() { |
| 74 | return (this instanceof QualifiedName); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Resolves and returns the binding for the entity referred to by this name. |
| 79 | * <p> |
| 80 | * Note that bindings are generally unavailable unless requested when the |
| 81 | * AST is being built. |
| 82 | * </p> |
| 83 | * |
| 84 | * @return the binding, or <code>null</code> if the binding cannot be |
| 85 | * resolved |
| 86 | */ |
| 87 | public final IBinding resolveBinding() { |
| 88 | return this.ast.getBindingResolver().resolveName(this); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the standard dot-separated representation of this name. |
| 93 | * If the name is a simple name, the result is the name's identifier. |
| 94 | * If the name is a qualified name, the result is the name of the qualifier |
| 95 | * (as computed by this method) followed by "." followed by the name's |
| 96 | * identifier. |
| 97 | * |
| 98 | * @return the fully qualified name |
| 99 | * @since 3.0 |
| 100 | */ |
| 101 | public final String getFullyQualifiedName() { |
| 102 | if (isSimpleName()) { |
| 103 | // avoid creating garbage for common case |
| 104 | return ((SimpleName) this).getIdentifier(); |
| 105 | } else { |
| 106 | StringBuffer buffer = new StringBuffer(50); |
| 107 | appendName(buffer); |
| 108 | return new String(buffer); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Appends the standard representation of this name to the given string |
| 114 | * buffer. |
| 115 | * |
| 116 | * @param buffer the buffer |
| 117 | * @since 3.0 |
| 118 | */ |
| 119 | abstract void appendName(StringBuffer buffer); |
| 120 | } |
| 121 |
Members