| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2004, 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 | package org.eclipse.jdt.core.dom; |
| 15 | |
| 16 | /** |
| 17 | * Abstract base class for property descriptors of AST nodes. |
| 18 | * There are three kinds of properties: |
| 19 | * <ul> |
| 20 | * <li>simple properties ({@link SimplePropertyDescriptor}) |
| 21 | * - properties where the value is a primitive (int, boolean) |
| 22 | * or simple (String, InfixExprsssion.Operator) type other than an |
| 23 | * AST node; for example, the identifier of a {@link SimpleName}</li> |
| 24 | * <li>child properties ({@link ChildPropertyDescriptor}) |
| 25 | * - properties whose value is another AST node; |
| 26 | * for example, the name of a {@link MethodDeclaration}</li> |
| 27 | * <li>child list properties ({@link ChildListPropertyDescriptor}) |
| 28 | * - properties where the value is a list of AST nodes; |
| 29 | * for example, the statements of a {@link Block}</li> |
| 30 | * </ul> |
| 31 | * |
| 32 | * @since 3.0 |
| 33 | * @noextend This class is not intended to be subclassed by clients. |
| 34 | */ |
| 35 | @SuppressWarnings("rawtypes") |
| 36 | public abstract class StructuralPropertyDescriptor { |
| 37 | |
| 38 | /** |
| 39 | * Property id. |
| 40 | */ |
| 41 | private final String propertyId; |
| 42 | |
| 43 | /** |
| 44 | * The concrete AST node type that owns this property. |
| 45 | */ |
| 46 | private final Class nodeClass; |
| 47 | |
| 48 | /** |
| 49 | * Creates a new property descriptor for the given node type |
| 50 | * with the given property id. |
| 51 | * Note that this constructor is declared package-private so that |
| 52 | * property descriptors can only be created by the AST |
| 53 | * implementation. |
| 54 | * |
| 55 | * @param nodeClass concrete AST node type that owns this property |
| 56 | * @param propertyId the property id |
| 57 | */ |
| 58 | StructuralPropertyDescriptor(Class nodeClass, String propertyId) { |
| 59 | if (nodeClass == null || propertyId == null) { |
| 60 | throw new IllegalArgumentException(); |
| 61 | } |
| 62 | this.propertyId = propertyId; |
| 63 | this.nodeClass = nodeClass; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the id of this property. |
| 68 | * |
| 69 | * @return the property id |
| 70 | */ |
| 71 | public final String getId() { |
| 72 | return this.propertyId; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the AST node type that owns this property. |
| 77 | * <p> |
| 78 | * For example, for all properties of the node type |
| 79 | * TypeDeclaration, this method returns <code>TypeDeclaration.class</code>. |
| 80 | * </p> |
| 81 | * |
| 82 | * @return the node type that owns this property |
| 83 | */ |
| 84 | public final Class getNodeClass() { |
| 85 | return this.nodeClass; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns whether this property is a simple property |
| 90 | * (instance of {@link SimplePropertyDescriptor}. |
| 91 | * |
| 92 | * @return <code>true</code> if this is a simple property, and |
| 93 | * <code>false</code> otherwise |
| 94 | */ |
| 95 | public final boolean isSimpleProperty(){ |
| 96 | return (this instanceof SimplePropertyDescriptor); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns whether this property is a child property |
| 101 | * (instance of {@link ChildPropertyDescriptor}. |
| 102 | * |
| 103 | * @return <code>true</code> if this is a child property, and |
| 104 | * <code>false</code> otherwise |
| 105 | */ |
| 106 | public final boolean isChildProperty() { |
| 107 | return (this instanceof ChildPropertyDescriptor); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns whether this property is a child list property |
| 112 | * (instance of {@link ChildListPropertyDescriptor}. |
| 113 | * |
| 114 | * @return <code>true</code> if this is a child list property, and |
| 115 | * <code>false</code> otherwise |
| 116 | */ |
| 117 | public final boolean isChildListProperty() { |
| 118 | return (this instanceof ChildListPropertyDescriptor); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns a string suitable for debug purposes. |
| 123 | * @return {@inheritDoc} |
| 124 | */ |
| 125 | @Override |
| 126 | public String toString() { |
| 127 | StringBuilder b = new StringBuilder(); |
| 128 | if (isChildListProperty()) { |
| 129 | b.append("ChildList"); //$NON-NLS-1$ |
| 130 | } |
| 131 | if (isChildProperty()) { |
| 132 | b.append("Child"); //$NON-NLS-1$ |
| 133 | } |
| 134 | if (isSimpleProperty()) { |
| 135 | b.append("Simple"); //$NON-NLS-1$ |
| 136 | } |
| 137 | b.append("Property["); //$NON-NLS-1$ |
| 138 | if (this.nodeClass != null) { |
| 139 | b.append(this.nodeClass.getName()); |
| 140 | } |
| 141 | b.append(","); //$NON-NLS-1$ |
| 142 | if (this.propertyId != null) { |
| 143 | b.append(this.propertyId); |
| 144 | } |
| 145 | b.append("]"); //$NON-NLS-1$ |
| 146 | return b.toString(); |
| 147 | } |
| 148 | } |
| 149 |
Members