| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2013 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 | import java.util.ArrayList; |
| 18 | import java.util.List; |
| 19 | |
| 20 | /** |
| 21 | * Simple or qualified "this" AST node type. |
| 22 | * |
| 23 | * <pre> |
| 24 | * ThisExpression: |
| 25 | * [ ClassName <b>.</b> ] <b>this</b> |
| 26 | * </pre> |
| 27 | * <p> |
| 28 | * See <code>FieldAccess</code> for guidelines on handling other expressions |
| 29 | * that resemble qualified names. |
| 30 | * </p> |
| 31 | * |
| 32 | * @see FieldAccess |
| 33 | * @since 2.0 |
| 34 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 35 | */ |
| 36 | @SuppressWarnings("rawtypes") |
| 37 | public class ThisExpression extends Expression { |
| 38 | |
| 39 | /** |
| 40 | * The "qualifier" structural property of this node type (child type: {@link Name}). |
| 41 | * @since 3.0 |
| 42 | */ |
| 43 | public static final ChildPropertyDescriptor QUALIFIER_PROPERTY = |
| 44 | new ChildPropertyDescriptor(ThisExpression.class, "qualifier", Name.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 45 | |
| 46 | /** |
| 47 | * A list of property descriptors (element type: |
| 48 | * {@link StructuralPropertyDescriptor}), |
| 49 | * or null if uninitialized. |
| 50 | */ |
| 51 | private static final List PROPERTY_DESCRIPTORS; |
| 52 | |
| 53 | static { |
| 54 | List propertyList = new ArrayList(2); |
| 55 | createPropertyList(ThisExpression.class, propertyList); |
| 56 | addProperty(QUALIFIER_PROPERTY, propertyList); |
| 57 | PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns a list of structural property descriptors for this node type. |
| 62 | * Clients must not modify the result. |
| 63 | * |
| 64 | * @param apiLevel the API level; one of the |
| 65 | * <code>AST.JLS*</code> constants |
| 66 | * @return a list of property descriptors (element type: |
| 67 | * {@link StructuralPropertyDescriptor}) |
| 68 | * @since 3.0 |
| 69 | */ |
| 70 | public static List propertyDescriptors(int apiLevel) { |
| 71 | return PROPERTY_DESCRIPTORS; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * The optional qualifier; <code>null</code> for none; defaults to none. |
| 76 | */ |
| 77 | private Name optionalQualifier = null; |
| 78 | |
| 79 | /** |
| 80 | * Creates a new AST node for a "this" expression owned by the |
| 81 | * given AST. By default, there is no qualifier. |
| 82 | * |
| 83 | * @param ast the AST that is to own this node |
| 84 | */ |
| 85 | ThisExpression(AST ast) { |
| 86 | super(ast); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 91 | return propertyDescriptors(apiLevel); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 96 | if (property == QUALIFIER_PROPERTY) { |
| 97 | if (get) { |
| 98 | return getQualifier(); |
| 99 | } else { |
| 100 | setQualifier((Name) child); |
| 101 | return null; |
| 102 | } |
| 103 | } |
| 104 | // allow default implementation to flag the error |
| 105 | return super.internalGetSetChildProperty(property, get, child); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | final int getNodeType0() { |
| 110 | return THIS_EXPRESSION; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | ASTNode clone0(AST target) { |
| 115 | ThisExpression result = new ThisExpression(target); |
| 116 | result.setSourceRange(getStartPosition(), getLength()); |
| 117 | result.setQualifier((Name) ASTNode.copySubtree(target, getQualifier())); |
| 118 | return result; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 123 | // dispatch to correct overloaded match method |
| 124 | return matcher.match(this, other); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | void accept0(ASTVisitor visitor) { |
| 129 | boolean visitChildren = visitor.visit(this); |
| 130 | if (visitChildren) { |
| 131 | acceptChild(visitor, getQualifier()); |
| 132 | } |
| 133 | visitor.endVisit(this); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns the qualifier of this "this" expression, or |
| 138 | * <code>null</code> if there is none. |
| 139 | * |
| 140 | * @return the qualifier name node, or <code>null</code> if there is none |
| 141 | */ |
| 142 | public Name getQualifier() { |
| 143 | return this.optionalQualifier; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Sets or clears the qualifier of this "this" expression. |
| 148 | * |
| 149 | * @param name the qualifier name node, or <code>null</code> if |
| 150 | * there is none |
| 151 | * @exception IllegalArgumentException if: |
| 152 | * <ul> |
| 153 | * <li>the node belongs to a different AST</li> |
| 154 | * <li>the node already has a parent</li> |
| 155 | * </ul> |
| 156 | */ |
| 157 | public void setQualifier(Name name) { |
| 158 | ASTNode oldChild = this.optionalQualifier; |
| 159 | preReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
| 160 | this.optionalQualifier = name; |
| 161 | postReplaceChild(oldChild, name, QUALIFIER_PROPERTY); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | int memSize() { |
| 166 | // treat Operator as free |
| 167 | return BASE_NODE_SIZE + 1 * 4; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | int treeSize() { |
| 172 | return |
| 173 | memSize() |
| 174 | + (this.optionalQualifier == null ? 0 : getQualifier().treeSize()); |
| 175 | } |
| 176 | } |
| 177 |
Members